使用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 file
        string path = @"D:\temp\test测试.gpkg";

        public Form1()
        {
            InitializeComponent();
        }

        private void btnWriter_Click(object sender, EventArgs e)
        {
            // Ensure the directory exists
            Directory.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}");
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值