使用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}");}}}}
}