Inheritance: ArcDeveloper.REST.Core.Model.GeoJSONResponse
Esempio n. 1
0
        /// <summary>
        /// Creates A GeoJSON feature from an IFeature
        /// </summary>
        /// <param name="feat">The feature.</param>
        /// <returns>A GeoJSON Feature</returns>
        public static GeoJSONFeature CreateFromIFeature(IFeature feat)
        {
            GeoJSONFeature jsonFeature = new GeoJSONFeature();

            jsonFeature.id       = feat.OID.ToString();
            jsonFeature.geometry = GeoJSONGeometry.CreateFromIGeometry(feat.Shape);
            StringBuilder sb = new StringBuilder();

            sb.Append("{");

            for (int i = 0; i < feat.Fields.FieldCount; i++)
            {
                ESRI.ArcGIS.Geodatabase.IField fld = feat.Fields.get_Field(i);
                switch (fld.Name.ToUpper())
                {
                case "SHAPE":
                    break;

                default:
                    string val = "null";
                    if (feat.get_Value(i) != null)
                    {
                        val = feat.get_Value(i).ToString();
                    }
                    if (sb.Length > 1)
                    {
                        sb.Append(",");
                    }
                    sb.Append(string.Format("\"{0}\":\"{1}\"", fld.Name, val));

                    break;
                }
            }

            jsonFeature.properties = sb.ToString();
            sb.Append("}");
            jsonFeature.properties = sb.ToString();
            return(jsonFeature);
        }
Esempio n. 2
0
        /// <summary>
        /// Create a geojson string from the geometry
        /// </summary>
        /// <param name="geometry"></param>
        /// <returns></returns>
        public static GeoJSONGeometry CreateFromIGeometry(IGeometry geometry)
        {
            GeoJSONGeometry jsonGeom = new GeoJSONGeometry();

            jsonGeom.Type = geometry.GeometryType.ToString();
            StringBuilder sb = new StringBuilder();

            if (geometry.GeometryType != esriGeometryType.esriGeometryPoint)
            {
                sb.Append("[");
            }
            //Need to work out how to easily rip the coords out of the IGeometry

            switch (geometry.GeometryType)
            {
            case esriGeometryType.esriGeometryPoint:
                IPoint pt = (IPoint)geometry;
                sb.Append(string.Format("[{0}, {1}]", Math.Round(pt.X, 5), Math.Round(pt.Y, 5)));
                jsonGeom.Type = "Point";
                break;

            case esriGeometryType.esriGeometryLine:
                IPolyline line = geometry as IPolyline;
                if (line == null)
                {
                    return(null);
                }

                line.Densify(-1, -1);   //make sure it's all straight line segments
                line.Weed(20);          //weed out some vertices
                line.SimplifyNetwork(); //make sure it is simple


                IPointCollection points = line as IPointCollection;
                for (int i = 0; i < points.PointCount; i++)
                {
                    IPoint point = points.get_Point(i);
                    if (sb.Length > 1)
                    {
                        sb.Append(",");
                    }
                    sb.Append(string.Format("[{0}, {1}]", Math.Round(point.X, 4), Math.Round(point.Y, 4)));
                }

                jsonGeom.Type = "LineString";
                break;

            case esriGeometryType.esriGeometryPolygon:
                IPolygon4 poly = geometry as IPolygon4;
                if (poly == null)
                {
                    return(null);
                }

                poly.Densify(-1, -1);          //make sure it is all straight line segments
                poly.Weed(20);                 //weed out some vertices
                poly.SimplifyPreserveFromTo(); //make sure it's simple

                //We aren't gonna deal with interior rings right now (ie - no holes in polygons)
                IGeometryBag  multiRing         = poly.ExteriorRingBag;
                IEnumGeometry exteriorRingsEnum = multiRing as IEnumGeometry;
                exteriorRingsEnum.Reset();
                IRing currentExteriorRing = exteriorRingsEnum.Next() as IRing;
                while (currentExteriorRing != null)
                {
                    if (!currentExteriorRing.IsClosed)
                    {
                        currentExteriorRing.Close();
                    }

                    IPointCollection multiRingPoints = currentExteriorRing as IPointCollection;
                    for (int pointIdx = 0; pointIdx < multiRingPoints.PointCount; pointIdx++)
                    {
                        IPoint multiRingPoint = multiRingPoints.get_Point(pointIdx);
                        //coords.Add(new GisSharpBlog.NetTopologySuite.Geometries.Coordinate(Math.Round(multiRingPoint.X, 5), Math.Round(multiRingPoint.Y, 5)));
                        if (sb.Length > 1)
                        {
                            sb.Append(",");
                        }
                        sb.Append(string.Format("[{0}, {1}]", Math.Round(multiRingPoint.X, 4), Math.Round(multiRingPoint.Y, 4)));
                    }

                    currentExteriorRing = exteriorRingsEnum.Next() as IRing;
                }
                jsonGeom.Type = "Polygon";
                break;
            }


            if (geometry.GeometryType != esriGeometryType.esriGeometryPoint)
            {
                sb.Append("]");
            }
            jsonGeom.coordinates = sb.ToString();
            return(jsonGeom);
        }
Esempio n. 3
0
        /// <summary>
        /// Create a geojson string from the geometry
        /// </summary>
        /// <param name="geometry"></param>
        /// <returns></returns>
        public static GeoJSONGeometry CreateFromIGeometry(IGeometry geometry)
        {
            GeoJSONGeometry jsonGeom = new GeoJSONGeometry();
            jsonGeom.Type = geometry.GeometryType.ToString();
            StringBuilder sb = new StringBuilder();
            if (geometry.GeometryType!=esriGeometryType.esriGeometryPoint)
            {
                sb.Append("[");
            }
            //Need to work out how to easily rip the coords out of the IGeometry

            switch(geometry.GeometryType)
            {
                case esriGeometryType.esriGeometryPoint:
                    IPoint pt = (IPoint)geometry;
                    sb.Append( string.Format("[{0}, {1}]", Math.Round(pt.X, 5), Math.Round(pt.Y, 5)));
                    jsonGeom.Type = "Point";
                    break;

                case esriGeometryType.esriGeometryLine:
                    IPolyline line = geometry as IPolyline;
                    if (line == null) return null;

                    line.Densify(-1, -1); //make sure it's all straight line segments
                    line.Weed(20);  //weed out some vertices
                    line.SimplifyNetwork();  //make sure it is simple

                    IPointCollection points = line as IPointCollection;
                    for (int i = 0; i < points.PointCount; i++)
                    {
                        IPoint point = points.get_Point(i);
                        if (sb.Length > 1)
                            sb.Append(",");
                        sb.Append(string.Format("[{0}, {1}]", Math.Round(point.X, 4), Math.Round(point.Y, 4)));
                    }

                    jsonGeom.Type = "LineString";
                    break;

                case esriGeometryType.esriGeometryPolygon:
                     IPolygon4 poly = geometry as IPolygon4;
                    if (poly == null) return null;

                    poly.Densify(-1, -1); //make sure it is all straight line segments
                    poly.Weed(20); //weed out some vertices
                    poly.SimplifyPreserveFromTo(); //make sure it's simple

                    //We aren't gonna deal with interior rings right now (ie - no holes in polygons)
                    IGeometryBag multiRing = poly.ExteriorRingBag;
                    IEnumGeometry exteriorRingsEnum = multiRing as IEnumGeometry;
                    exteriorRingsEnum.Reset();
                    IRing currentExteriorRing = exteriorRingsEnum.Next() as IRing;
                    while (currentExteriorRing != null)
                    {

                        if (!currentExteriorRing.IsClosed) currentExteriorRing.Close();

                        IPointCollection multiRingPoints = currentExteriorRing as IPointCollection;
                        for (int pointIdx = 0; pointIdx < multiRingPoints.PointCount; pointIdx++)
                        {
                            IPoint multiRingPoint = multiRingPoints.get_Point(pointIdx);
                            //coords.Add(new GisSharpBlog.NetTopologySuite.Geometries.Coordinate(Math.Round(multiRingPoint.X, 5), Math.Round(multiRingPoint.Y, 5)));
                            if (sb.Length > 1)
                                sb.Append(",");
                            sb.Append(string.Format("[{0}, {1}]", Math.Round(multiRingPoint.X, 4), Math.Round(multiRingPoint.Y, 4)));
                        }

                        currentExteriorRing = exteriorRingsEnum.Next() as IRing;
                    }
                    jsonGeom.Type = "Polygon";
                    break;

            }

            if (geometry.GeometryType != esriGeometryType.esriGeometryPoint)
            {
                sb.Append("]");
            }
            jsonGeom.coordinates = sb.ToString();
            return jsonGeom;
        }