Exemple #1
0
        public void ProjectTest1()
        {
            var p1 = new Point (1, 1);
            var p2 = new Point (2, 1);

            var p3 = new Point (100, 0);

            var pp = p3.Project (p1, p2);

            Assert.AreEqual (pp.X, 100);
            Assert.AreEqual (pp.Y, 1);
        }
        /// <summary>
        ///     Handles the incoming rest requests
        /// </summary>
        /// <param name="boundVariables"> The bound variables. </param>
        /// <param name="operationInput"> The operation input. </param>
        /// <param name="outputFormat"> The output format. </param>
        /// <param name="requestProperties"> The request properties. </param>
        /// <param name="responseProperties"> The response properties. </param>
        /// <returns> </returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        public static byte[] Handler(NameValueCollection boundVariables, JsonObject operationInput,
                                     string outputFormat, string requestProperties,
                                     out string responseProperties)
        {
            responseProperties = null;
            var errors = new ErrorContainer(400);
            var wkid = 26912;
            const string featureClass = "SGID10.TRANSPORTATION.UDOTRoutes_LRS";

            //pull out all the variables
            var x = operationInput.GetNumberValue("x");
            var y = operationInput.GetNumberValue("y");
            var wkidInput = operationInput.GetNumberValue("wkid", nullable: true);
            var bufferInput = operationInput.GetNumberValue("buffer", nullable: true);
            var includeRamps = operationInput.GetNumberValue("includeRamps", nullable: true);

            ISpatialReference newSpatialRefefence = null;
            ISpatialReferenceFactory srFactory = new SpatialReferenceEnvironment();

            if (wkidInput > 0)
            {
                wkid = Convert.ToInt32(wkidInput);
            }

            if (bufferInput < 1 || bufferInput > 200)
            {
                bufferInput = 100;
            }

            //reproject to our data's spatial reference
            if (wkid != 26912)
            {
                var isProjected = true;
                try
                {
                    newSpatialRefefence = srFactory.CreateProjectedCoordinateSystem(wkid);
                }
                catch (ArgumentException)
                {
                    isProjected = false;
                }

                if (!isProjected)
                {
                    newSpatialRefefence = srFactory.CreateGeographicCoordinateSystem(wkid);
                }
            }

            var utm = srFactory.CreateProjectedCoordinateSystem(26912);

            IPoint point = new Point
                {
                    X = x,
                    Y = y,
                    SpatialReference = utm
                };

            //input is in different projection - reproject it
            if (wkid != 26912)
            {
                point = new Point
                    {
                        X = x,
                        Y = y,
                        SpatialReference = newSpatialRefefence
                    };

                point.Project(utm);
            }

            var bufferGeometry = CommandExecutor.ExecuteCommand(
                new BufferGeometryCommand(new GeometryContainer
                    {
                        Geometry = point
                    }, bufferInput));

            var sdeConnector = SdeConnectorFactory.Create(featureClass);

            if (sdeConnector == null)
            {
                errors.Add("{0} was not found in our database. ".With(featureClass) +
                           "A valid example would be SGID10.BOUNDARIES.Counties.");
            }

            if (errors.HasErrors)
            {
                return Json(errors);
            }

// ReSharper disable PossibleNullReferenceException because of returning errors if null
            var workspace = sdeConnector.Connect();
// ReSharper restore PossibleNullReferenceException

            var featureWorkspace = workspace as IFeatureWorkspace;

            if (featureWorkspace == null)
            {
                errors.Add("Error connecting to SDE.");
                return Json(errors);
            }

            var whereClause = "(LABEL LIKE '0%') AND RT_DIR <> 'B'"; //gets rid of ramps, collectors, and federal aid routes
            if (includeRamps < 1)
            {
                whereClause = whereClause.Insert(0, "LEN(LABEL) = 5 AND ");
            }

            var spatFilter = new SpatialFilterClass
                {
                    SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects,
                    Geometry = bufferGeometry,
                    WhereClause = whereClause
                };

            var lrsFeatureClass = featureWorkspace.OpenFeatureClass(featureClass.Trim());
            var fCursor = lrsFeatureClass.Search(spatFilter, true);
            var candidates = new TopAndEqualMilepostCandidates(new ClosestMilepostComparer());

            IFeature row;
            while ((row = fCursor.NextFeature()) != null)
            {
                var shape = row.ShapeCopy as IPolyline;
                if (shape == null)
                {
                    continue;
                }

                IPoint hitPoint = new Point();
                var hitDistance = -1d;
                var hitPartIndex = -1;
                var hitSegmentIndex = -1;
                var increasingSide = false;

                var hitTest = shape as IHitTest;
                if (hitTest == null)
                {
                    continue;
                }

                var isHit = hitTest.HitTest(point, bufferInput, esriGeometryHitPartType.esriGeometryPartBoundary, hitPoint,
                                ref hitDistance, ref hitPartIndex, ref hitSegmentIndex, ref increasingSide);

                if (!isHit)
                {
                    continue;
                }

                var milepost = hitPoint.M;

                var distance = GetDistanceBetween(point, hitPoint);

                var routeName = CommandExecutor.ExecuteCommand(new GetValueForFieldCommand("LABEL", row.Fields, row));

                candidates.Add(new ClosestMilepost(milepost, routeName.ToString(), distance, increasingSide));

                Marshal.ReleaseComObject(row);
                Marshal.ReleaseComObject(shape);
                Marshal.ReleaseComObject(hitPoint);
            }

            Marshal.ReleaseComObject(lrsFeatureClass);
            Marshal.ReleaseComObject(workspace);
            Marshal.ReleaseComObject(spatFilter);

            return Json(candidates);
        }