C# GIS库 NetTopologySuite shp 转 geojson

郏景澄
2023-12-01

将ShapeFile 读取为 FeatureCollection:

public bool ShpToFeatureCollection1(string filename, out FeatureCollection collection)
        {
            collection = null;
            try
            {
                var featureCollection = new Collection<DotSpatial.NetTopologySuite.Features.IFeature>();

                if (!File.Exists(filename + ".dbf"))
                {                    
                    return false;
                }
                var streamreader = new ShapefileStreamProviderRegistry(filename);
                var dataReader = new ShapefileDataReader(streamreader, new GeometryFactory(new PrecisionModel()));
             
                while (dataReader.Read())
                {                    
                    var feature = new DotSpatial.NetTopologySuite.Features.Feature { Geometry = dataReader.Geometry };
                    int length = dataReader.DbaseHeader.NumFields;
                    string[] keys = new string[length];
                    for (int i = 0; i < length; i++)
                        keys[i] = dataReader.DbaseHeader.Fields[i].Name;

                    feature.Attributes = new AttributesTable();
                    for (int i = 0; i < length; i++)
                    {
                        object val = dataReader.GetValue(i + 1);
                        feature.Attributes.AddAttribute(keys[i], val);
                    }

                    featureCollection.Add(feature);
                }
                dataReader.Close();
                dataReader.Dispose();

                if (featureCollection.Count == 0)
                {                    
                    return false;
                }

                collection = new FeatureCollection(featureCollection);                     
            }
            catch(Exception ex)
            {                
                return false;
            }

            return true;
        }

FeatureCollection 转为 GeoJson:

private bool ConvertFeatureCollection(FeatureCollection value, out StringBuilder stringBuilder)
        {            
            FeatureCollectionConverter target = new FeatureCollectionConverter();
            stringBuilder = new StringBuilder();
            JsonTextWriter writer = new JsonTextWriter(new StringWriter(stringBuilder));        
            JsonSerializer serializer = DotSpatial.NetTopologySuite.IO.GeoJsonSerializer.Create(new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore },
                GeometryFactory.Default);
            target.WriteJson(writer, value, serializer);                        
            writer.Flush();
            writer.Close();          

            if (stringBuilder.Length == 0)
            {                
                return false;
            }
            return true;
        }

Geojson 转为 FeatureCollection:

public bool ReadGeojson(string geojson, string fileName)
        {
            bool bResult = true;
            try
            {
                do
                {                    
                    DotSpatial.NetTopologySuite.IO.GeoJsonReader reader = new DotSpatial.NetTopologySuite.IO.GeoJsonReader();
                    FeatureCollection result = reader.Read<FeatureCollection>(geojson);                  

                    if (result == null)
                    {
                        bResult = false;                        
                        break;
                    }

                    ShapefileWriter.WriteFeatureCollection(fileName, result);                    
                }
                while (false);
            }
            catch(Exception ex)
            {
                bResult = false;                
            }
            return bResult;
        }

  

转载于:https://www.cnblogs.com/lili9696189/p/11315358.html

 类似资料: