Ejemplo n.º 1
0
        /// <summary>
        /// 判断数据是否是有效数据
        /// </summary>
        /// <returns></returns>
        public bool IsValidDataSource()
        {
            _ogrLayer.ResetReading();
            int featureCount = _ogrLayer.GetFeatureCount(1);

            if (featureCount <= 0)
            {
                return(false);
            }
            Feature pFeature = _ogrLayer.GetNextFeature();

            if (pFeature == null)
            {
                return(false);
            }
            Geometry pGeom = pFeature.GetGeometryRef();

            if (pGeom == null)
            {
                return(false);
            }
            if (pGeom.GetGeometryType() == wkbGeometryType.wkbNone || pGeom.GetGeometryType() == wkbGeometryType.wkbUnknown)
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns geometry Object IDs whose bounding box intersects 'bbox'
        /// </summary>
        /// <param name="bbox"></param>
        /// <returns></returns>
        public Collection <uint> GetObjectIDsInView(SharpMap.Geometries.BoundingBox bbox)
        {
            _OgrLayer.SetSpatialFilterRect(bbox.Min.X, bbox.Min.Y, bbox.Max.X, bbox.Max.Y);
            OSGeo.OGR.Feature _OgrFeature = null;
            _OgrLayer.ResetReading();

            Collection <uint> _ObjectIDs = new Collection <uint>();

            while ((_OgrFeature = _OgrLayer.GetNextFeature()) != null)
            {
                _ObjectIDs.Add((uint)_OgrFeature.GetFID());
                _OgrFeature.Dispose();
            }
            return(_ObjectIDs);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns geometry Object IDs whose bounding box intersects 'bbox'
        /// </summary>
        /// <param name="bbox"></param>
        /// <returns></returns>
        public Collection <uint> GetObjectIDsInView(IEnvelope bbox)
        {
            _OgrLayer.SetSpatialFilterRect(bbox.MinX, bbox.MinY, bbox.MaxX, bbox.MaxY);
            OSGeo.OGR.Feature _OgrFeature = null;
            _OgrLayer.ResetReading();

            Collection <uint> _ObjectIDs = new Collection <uint>();

            while ((_OgrFeature = _OgrLayer.GetNextFeature()) != null)
            {
                _ObjectIDs.Add((uint)_OgrFeature.GetFID());
                _OgrFeature.Dispose();
            }
            return(_ObjectIDs);
        }
Ejemplo n.º 4
0
        public static string ToGeoJson(string shpPath)
        {
            OSGeo.OGR.Ogr.RegisterAll();
            Driver drv = Ogr.GetDriverByName("ESRI Shapefile");

            using (var ds = drv.Open(shpPath, 0))
            {
                OSGeo.OGR.Layer layer = ds.GetLayerByIndex(0);

                OSGeo.OGR.Feature f;
                layer.ResetReading();

                System.Text.StringBuilder sb = new System.Text.StringBuilder();

                while ((f = layer.GetNextFeature()) != null)
                {
                    var geom = f.GetGeometryRef();
                    if (geom != null)
                    {
                        var geometryJson = geom.ExportToJson(null);
                        sb.AppendLine(geometryJson);
                    }
                }

                return(sb.ToString());
            }
        }
Ejemplo n.º 5
0
        public static void ConvertSHPtoGJSON(string shapefilePath, out StringBuilder gJSON)
        {
            string shpPath = shapefilePath;

            Ogr.RegisterAll();
            OSGeo.OGR.Driver drv = Ogr.GetDriverByName("ESRI Shapefile");

            var ds = drv.Open(shpPath, 0);

            /*
             * Driver geoJSONDriver = Ogr.GetDriverByName("GeoJSON");
             *
             * string geojsonfilepath = @"c:\temp\us_counties_test.json";
             *
             * if (System.IO.File.Exists(geojsonfilepath))
             *  System.IO.File.Delete(geojsonfilepath);
             *
             * geoJSONDriver.CreateDataSource(@"c:\temp\us_counties_test.json", null);
             */

            OSGeo.OGR.Layer layer = ds.GetLayerByIndex(0);

            OSGeo.OGR.Feature f;
            layer.ResetReading();

            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            var options = new[]
            {
                "-f", "GeoJSON",
                "-lco", "RFC7946=YES"
            };


            while ((f = layer.GetNextFeature()) != null)
            {
                var geom = f.GetGeometryRef();
                if (geom != null)
                {
                    var geometryJson = geom.ExportToJson(options);
                    sb.AppendLine(geometryJson);
                }
            }

            gJSON = sb;
        }