我想通过使用NetTopologySuite.IO.Esri从wkt创建一个shapefile,但是得到如下错误:
Unhandled exception. System.InvalidCastException: Unable to cast object of type 'NetTopologySuite.Geometries.Polygon' to type 'NetTopologySuite.Geo
metries.MultiPolygon'.
at NetTopologySuite.IO.Esri.Shapefiles.Writers.ShapefileWriter`1.Write(IFeature feature)
at NetTopologySuite.IO.Esri.Shapefiles.Writers.ShapefileWriter.Write(IEnumerable`1 features)
at NetTopologySuite.IO.Esri.Shapefile.WriteAllFeatures(IEnumerable`1 features, String shpPath, Encoding encoding, String projection)
我尝试打开调试模式和跟踪代码,我发现在Shapefile.cs
第185行中,有一个参数的几何类型,如下所示:
var shapeType = features.FindNonEmptyGeometry().GetShapeType();
在我的情况下,它总是返回PolygonZM
--我尝试像NetTopologySuite create shp
那样搜索关键字,但没有回答NetTopologySuite.IO.Esri
;只有使用NetTopologySuite.IO.Shapefile
创建shp的方式,但是我在它的github页面上看到它永远不会被更新。
有人能给我一个点来解决这个问题吗?谢谢!!
(我不是以英语为母语的人,请原谅我的语法错误,谢谢!)
这是我的代码:
using System.Text;
using NetTopologySuite.Features;
using NetTopologySuite.IO;
using NetTopologySuite.IO.Esri;
var features = new List<Feature>();
var wktReader = new WKTReader();
// I tried wkt with EPSG:4326 and EPSG:3826, but no one work.
var geometry = wktReader.Read($"POLYGON((229884.458362927 2698919.1790506,229878.266318657 2698913.05244554,229872.637726088 2698914.0245398,229870.132848978 2698912.76299454,229868.585854503 2698911.60799823,229862.417875893 2698913.56799102,229859.466886255 2698913.2179918,229854.168905161 2698909.4280045,229832.918531065 2698900.75121021,229826.266555421 2698903.87487001,229815.242942057 2698911.30138916,229814.158096991 2698912.0322406,229802.736136414 2698917.5934203,229787.107090625 2698922.43900198,229770.41744862 2698926.49898628,229758.597689508 2698931.31876844,229754.622568952 2698933.63004125,229756.82934315 2698935.98290305,229758.154520219 2698937.39368305,229761.467578897 2698937.10864875,229772.427440716 2698935.38875568,229785.926793536 2698934.84915899,229801.796838123 2698933.66916467,229811.707603543 2698932.67086896,229818.576179578 2698931.9792722,229834.383317175 2698930.1646309,229840.615502711 2698929.44918301,229859.255237777 2698926.54949485,229875.612191821 2698922.46243583,229879.34506797 2698921.52971422,229884.458362927 2698919.1790506))");
// Also tried this but not work too.
// var geometry = wktReader.Read(wkt) as Polygon;
var attributes = new AttributesTable
{
{ "Date", new DateTime(2022, 1, 1) },
{ "Content", $"I am No. 1" }
};
var feature = new Feature(geometry, attributes);
features.Add(feature);
Shapefile.WriteAllFeatures(features, @"C:\Users\user\Downloads\test.shp");
发布于 2022-08-13 09:21:58
我最终解决了这个问题,将多边形写入器与原始多边形编写器分离,后者在begin中使用MultiPolygon
,并导致了我猜测的错误。现在,我有MultiPolygonWriter
(原始PolygonWriter )和PolygonWriter
(新),新的只会在Geometry
中创建Polygon
类型,其他多边形类型将由原始编写器处理。
如果有人需要我做的改变,这里是回购:https://github.com/anthea-wu/NetTopologySuite.IO.Esri希望它能对你有帮助
https://stackoverflow.com/questions/73333180
复制相似问题