gpt4 book ai didi

c# - 使用 NetTopologySuite 编写二维形状文件

转载 作者:行者123 更新时间:2023-12-05 01:15:04 24 4
gpt4 key购买 nike

我创建了一个可以正确写入形状文件的例程,但我遇到了一个问题:形状是 3d,但我需要编写一个 2d 形状文件。
我该怎么做?

谢谢

最佳答案

网上没有人给出答案,我花了 2 天终于找到了这个解决方案:

using NetTopologySuite.Features;
using NetTopologySuite.Geometries;
using NetTopologySuite.IO;
using ProjNet;
using System;
using System.Collections.Generic;
using System.IO;

public static void TestCreate2Dshape(string shapeFileName)
{
int srid = 4326;
var sequenceFactory = new NetTopologySuite.Geometries.Implementation.DotSpatialAffineCoordinateSequenceFactory(Ordinates.XY);
var geomFactory = new GeometryFactory(new PrecisionModel(PrecisionModels.Floating), srid, sequenceFactory);

// use shapefilewriter to force geometryType a u want ex=point
var tmpFile = System.IO.Path.GetTempFileName();
ShapefileWriter shpWriter = new ShapefileWriter(geomFactory, tmpFile,ShapeGeometryType.Point);

// use the dataWriter to write the shape file with geometry and data
ShapefileDataWriter dataWriter = new ShapefileDataWriter(shapeFileName, geomFactory);

// create an entity and add to the features
List<Feature> features = new List<Feature>();
AttributesTable att = new AttributesTable();
att.Add("ID", 1);
var geomZ = geomFactory.CreatePoint(new Coordinate(0,0)) ;

// force geomz to geom xy using the shpWriter
var geom = shpWriter.Factory.CreateGeometry(geomZ);

// add to the features
features.Add(new Feature(geom, att));

// header
DbaseFileHeader outDbaseHeader = new DbaseFileHeader();
outDbaseHeader.AddColumn("ID", 'N', 10, 0);
outDbaseHeader = ShapefileDataWriter.GetHeader(outDbaseHeader.Fields, Math.Max(features.Count, 1));
dataWriter.Header = outDbaseHeader;

// write the shapefile
dataWriter.Write(features);

// Create the projection file if necessary, choose value matching srid
using (StreamWriter streamWriter = new StreamWriter(shapeFileName+".prj"))
{
streamWriter.Write(ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84.WKT);
}

// delete the temporary writer
shpWriter.Close();
System.IO.File.Delete(tmpFile + ".shp");
System.IO.File.Delete(tmpFile + ".shx");
}

关于c# - 使用 NetTopologySuite 编写二维形状文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57012279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com