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

使用NetTopologySuite读写gpkg文件

直接上代码:

优势是什么?纯C#开发,不存在兼容和字符问题。

using NetTopologySuite;
using NetTopologySuite.Features;
using NetTopologySuite.Geometries;
using CdIts.NetTopologySuite.IO;
using CdIts.NetTopologySuite.IO.GeoPackage.FeatureReader;
using CdIts.NetTopologySuite.IO.GeoPackage.FeatureWriter;
using CdIts.NetTopologySuite.IO.GeoPackage.Features;
using CdIts.NetTopologySuite.IO.GeoPackage;namespace WinFormsApp1
{public partial class Form1 : Form{// Define the path for the GeoPackage filestring path = @"D:\temp\test测试.gpkg";public Form1(){InitializeComponent();}private void btnWriter_Click(object sender, EventArgs e){// Ensure the directory existsDirectory.CreateDirectory(Path.GetDirectoryName(path));var writer = new GeoPackageFeatureWriter(path);// Define the spatial reference system (SRID 4326 for WGS84)var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(4543);var point1 = geometryFactory.CreatePoint(new Coordinate(10, 20));var point2 = geometryFactory.CreatePoint(new Coordinate(52, 80));var point3 = geometryFactory.CreatePoint(new Coordinate(150, 60));Feature feature1 = new Feature();feature1.Attributes = new AttributesTable();feature1.Attributes.Add("id", "1");feature1.Geometry = point1;Feature feature2 = new Feature();feature2.Attributes = new AttributesTable();feature2.Attributes.Add("id", "2");feature2.Geometry = point2;Feature feature3 = new Feature();feature3.Attributes = new AttributesTable();feature3.Attributes.Add("id", "3");feature3.Geometry = point3;Feature[] pointFeatures = { feature1, feature2, feature3 };writer.AddLayer(pointFeatures, "testPoint");Coordinate coordinate1 = new Coordinate(10, 20);Coordinate coordinate2 = new Coordinate(52, 80);Coordinate coordinate3 = new Coordinate(150, 60);Coordinate[] coordinates1 = { coordinate1, coordinate2 };var line1 = geometryFactory.CreateLineString(coordinates1);Feature featureLine1 = new Feature();featureLine1.Attributes = new AttributesTable();featureLine1.Attributes.Add("id", "1");featureLine1.Geometry = line1;Coordinate[] coordinates2 = { coordinate2, coordinate3 };var line2 = geometryFactory.CreateLineString(coordinates2);Feature featureLine2 = new Feature();featureLine2.Attributes = new AttributesTable();featureLine2.Attributes.Add("id", "2");featureLine2.Geometry = line2;Coordinate[] coordinates3 = { coordinate1, coordinate3 };var line3 = geometryFactory.CreateLineString(coordinates3);Feature featureLine3 = new Feature();featureLine3.Attributes = new AttributesTable();featureLine3.Attributes.Add("id", "3");featureLine3.Geometry = line3;Feature[] lineFeatures = { featureLine1, featureLine2, featureLine3 };writer.AddLayer(lineFeatures, "testLine");/Coordinate[] coordinates = { coordinate1, coordinate2, coordinate3, coordinate1 };var polygon = geometryFactory.CreatePolygon(coordinates);Feature polygonFeature = new Feature();polygonFeature.Attributes = new AttributesTable();polygonFeature.Attributes.Add("id", "1");polygonFeature.Geometry = polygon;Feature[] polygonFeatures = { polygonFeature };writer.AddLayer(polygonFeatures, "testPolygon");writer.Close();}private void btnReader_Click(object sender, EventArgs e){var reader = new GeoPackageFeatureReader(path);IList<GeoPackageFeatureInfo> featureInfoes = reader.GetFeatureInfos();List<string> tableNames = new List<string>();foreach(var info in featureInfoes){if(tableNames.Contains(info.TableName) == false){tableNames.Add(info.TableName);}}foreach (var name in tableNames){Feature[] features = reader.ReadFeatures(name);foreach (var feature in features){Geometry geo = feature.Geometry;MessageBox.Show($"{geo.Centroid.X},{geo.Centroid.Y}");}}}}
}

http://www.lryc.cn/news/459060.html

相关文章:

  • 什么是http?列出常见方法并解他们?
  • 通过修改注册表来提高导出图像的分辨率(PPT尝试)
  • OpenCV 环境配置
  • 使用通义千问模拟ChatGPT-o1进行思考,并以类似于ChatGPT-o1的形式输出
  • 维生素对于生活的重要性
  • STM32学习--4-1 OLED显示屏
  • 原生 App 上架 Mac App Store 过程总结
  • [实时计算flink]双流JOIN语句
  • metahuman如何导入UE5
  • Python知识点:基于Python技术,如何使用TensorFlow进行自动驾驶模型训练
  • Django的请求与响应
  • [java]Iterable<Integer> 和Iterator<Integer>的区别和用法
  • JavaScript进行数据可视化:D3.js入门
  • 字符串拼接方法性能对比和分析
  • [Halcon矩阵] 通过手眼标定矩阵计算相机旋转角度
  • 推荐几本编程入门书目
  • 每天一个数据分析题(五百零五)- 提升方法
  • 华为云ECS部署DR模式的LVS
  • 如何在 Jupyter Notebook 执行和学习 SQL 语句(上)
  • 数据结构-5.7.二叉树的层次遍历
  • RISC-V知识点目录
  • C++11 新特性 学习笔记
  • Go 语言中的格式化占位符
  • QD1-P5 HTML 段落标签(p)换行标签(br)
  • Django的模板语法
  • 【在Linux世界中追寻伟大的One Piece】信号捕捉|阻塞信号
  • 信息系统运维管理方案,运维建设文档,运维平台建设方案,软件硬件中间件运维方案,信息安全管理(原件word,PPT,excel)
  • 多元统计实验报告内容
  • 使用机器学习边缘设备的快速目标检测
  • Anthropic的CEO达里奥·阿莫迪(Dario Amodei)文章传达他对AI未来的乐观展望