【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文件,转geojson
var 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);
}