欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 艺术 > 【NetTopologySuite类库】geojson和shp互转,和自定义对象互转

【NetTopologySuite类库】geojson和shp互转,和自定义对象互转

2025/4/19 5:38:12 来源:https://blog.csdn.net/liqian_ken/article/details/146081510  浏览:    关键词:【NetTopologySuite类库】geojson和shp互转,和自定义对象互转

geojson介绍

1. 示例

在visual studio中使用NuGet中安装了三个库(.net4.7.2环境):

  • NetTopologySuite 2.5
  • NetTopologySuite.IO.Esri.Shapefile 1.2
  • NetTopologySuite.IO.GeoJSON 4.0

1.1 shp数据转geojson

先创建一个shp文件作为例子:

using NetTopologySuite.Geometries;
using NetTopologySuite.IO;
using NetTopologySuite.IO.Esri;
using NetTopologySuite.Features;
using System.Collections.Generic;
using System.IO;//创建一个用于测试的shp文件
var r = new WKTReader();
var geo1 = r.Read("polygon((0 0,0 10,10 10,10 0,0 0))");
var geo2 = r.Read("polygon((10 0,10 10,20 10,20 0,10 0))");
Save("./Tmp/data.shp", geo1, geo2);//工具方法
public static void Save(string shpPath, params Geometry[] geos)
{Directory.CreateDirectory(Path.GetDirectoryName(shpPath));var features = geos.Select(x => new Feature(x, null)).ToList();Shapefile.WriteAllFeatures(features, shpPath);
}

再读取shp,将其转为geojson:

public void TestMethod1()
{//读取shp文件,转geojsonvar features = Shapefile.ReadAllFeatures("./Tmp/data.shp");var modifiedJson = new GeoJsonWriter().Write(features);File.WriteAllText("./Tmp/output.geojson", modifiedJson);
}

生成的geojson:

[{"type": "Feature","bbox": [0.0, 0.0, 10.0, 10.0],"geometry": {"type": "MultiPolygon","coordinates": [[[[0.0, 0.0],[10.0, 0.0],[10.0, 10.0],[0.0, 10.0],[0.0, 0.0]]]]},"properties": {"Id": 1}},{"type": "Feature","bbox": [10.0, 0.0, 20.0, 10.0],"geometry": {"type": "MultiPolygon","coordinates": [[[[10.0, 0.0],[20.0, 0.0],[20.0, 10.0],[10.0, 10.0],[10.0, 0.0]]]]},"properties": {"Id": 1}}
]

1.2 geojson转shp数据

public void TestMethod2()
{var json = File.ReadAllText("./Tmp/output.geojson");var reader = new GeoJsonReader();var features = reader.Read<Feature[]>(json);Shapefile.WriteAllFeatures(features, "./Tmp/data2.shp");
}

1.2 自定义对象转geojson

自定义一个类(里面有属性,有几何Geometry ):

 public class Item{public string Name { get; set; } = "Layer";public Geometry Geo { get; set; }public Dictionary<string, object> Attrs { get; set; }public Item(string name, Geometry geo, Dictionary<string, object> attrs){Name = name;Geo = geo;Attrs = attrs;}}

为该类创建对象数组,并将其转为geojson:

public void TestMethod3()
{var r = new WKTReader();var geo1 = r.Read("polygon((0 0,0 10,10 10,10 0,0 0))");var geo2 = r.Read("polygon((10 0,10 10,20 10,20 0,10 0))");var item1 = new Item("A", geo1, new Dictionary<string, object>(){{ "key", 1}, { "value", 2} });var item2 = new Item("A", geo1, new Dictionary<string, object>(){{ "key", 1}, { "value", 2}});var items = new Item[] {item1, item2 };var modifiedJson = new GeoJsonWriter().Write(items);File.WriteAllText("./Tmp/output2.geojson", modifiedJson);      
}

转成的geojson:

[{"Name": "A","Geo": {"type": "Polygon","coordinates": [[[0.0, 0.0],[10.0, 0.0],[10.0, 10.0],[0.0, 10.0],[0.0, 0.0]]]},"Attrs": {"key": 1,"value": 2}},{"Name": "A","Geo": {"type": "Polygon","coordinates": [[[0.0, 0.0],[10.0, 0.0],[10.0, 10.0],[0.0, 10.0],[0.0, 0.0]]]},"Attrs": {"key": 1,"value": 2}}
]

1.3 geojson转自定义对象

public void TestMethod4()
{var json = File.ReadAllText("./Tmp/output2.geojson");var reader = new GeoJsonReader();var obj = reader.Read<Item[]>(json);
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词