当前位置: 首页 > article >正文

【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);
}

http://www.kler.cn/a/576346.html

相关文章:

  • Kotlin字符串操作在Android开发中的应用示例
  • Java网络爬虫工程
  • 深度学习(斋藤)学习笔记(五)-反向传播2
  • 【卫星语音通信】神经网络语音编解码算法:AudioDec
  • 常见Web应用源码泄露问题
  • 揭开Android View的神秘面纱:深入探索工作原理
  • Go语言集成DeepSeek API和GoFly框架文本编辑器实现流式输出和对话(GoFly快速开发框架)
  • vue的el-form-item循环检验rules
  • AWS DynamoDB深度解析:高并发场景下的NoSQL数据库设计与优化实践
  • 重学 Android 自定义 View 系列(十一):文字跑马灯剖析
  • GStreamer —— 2.2、Windows下Qt加载GStreamer库后运行 - “教程2:GStreamer 概念“(附:完整源码)
  • Swift系列02-Swift 数据类型系统与内存模型
  • 智慧风电赋能绿色能源的新篇章
  • IDEA Generate POJOs.groovy 踩坑小计 | 生成实体 |groovy报错
  • WPF在特定领域的应用:打造一款专业的图像编辑工具
  • Maven 使用指南:基础 + 进阶 + 高级用法
  • 【JavaWeb】Web基础概念
  • 嵌入式中Type-C 与快充相关知识详解
  • let、const和var的区别是什么?
  • Unity自定义区域UI滑动事件