2继续NTS库学习(读取shapefile)
引用库如下:
读取shapefile代码如下:
namespace IfoxDemo
{public class Class1{[CommandMethod("xx")]public static void nts二次学习(){Document doc = Application.DocumentManager.MdiActiveDocument;var ed = doc.Editor;string shpPath = @"C:\Users\Administrator\Desktop\1.shp";var shpPath2 = @"C:\Users\Administrator\Desktop\2.shp";foreach (var feature in NetTopologySuite.IO.Esri.Shapefile.ReadAllFeatures(shpPath2)){foreach (var attrName in feature.Attributes.GetNames()){ed.WriteMessage($"\n字段名为:\"{attrName,10}\" \n 字段内容为: \"{feature.Attributes[attrName]}\"\n");}ed.WriteMessage($" 形状和xy为\"{feature.Geometry}\"\n");break;}ed.WriteMessage("下一个:\n");foreach (var feature in NetTopologySuite.IO.Esri.Shapefile.ReadAllFeatures(shpPath)){foreach (var attrName in feature.Attributes.GetNames()){ed.WriteMessage($"字段名为:\"{attrName,10}\" \n 字段内容为: \"{feature.Attributes[attrName]}\n");}ed.WriteMessage($" SHAPE: 形状和xy为\"{feature.Geometry}\"\n");break;}}}
}
读取结果如下:
读取dbf文件如下:
var ed = Env.Editor;var shpPath = @"C:\Users\Administrator\Desktop\新建文件夹 (2)\zd.shp";var dbfPath = @"C:\Users\Administrator\Desktop\新建文件夹 (2)\zd.dbf";using var dbf = new DbfReader(dbfPath);int id = 0;foreach (var record in dbf){ed.WriteMessage($"\n第{id}条记录: \n");foreach (var fieldName in record.GetNames()){ed.WriteMessage($"\n字段名为:{fieldName,10} ,字段记录内容为{record[fieldName]}。\n");}id++;}
读取shp文件的几何信息:
var ed = Env.Editor;var shpPath = @"C:\Users\Administrator\Desktop\新建文件夹 (2)\zd.shp";var dbfPath = @"C:\Users\Administrator\Desktop\新建文件夹 (2)\zd.dbf";using var dbf = new DbfReader(dbfPath);int id = 0;foreach (NetTopologySuite.Geometries.Geometry geometry in Shapefile.ReadAllGeometries(shpPath)){id++;ed.WriteMessage($"\n第{id}个geometry:"+geometry.ToString());ed.WriteMessage("\ngeometry的类型为:"+geometry.GetType().ToString()+"\n");// 根据几何类型提取坐标点List<Point3d> points = new List<Point3d>();if (geometry is Point point){// 如果是点类型points.Add(new Point3d(point.X, point.Y, point.Z)); // 假设 Z 坐标为 0}else if (geometry is LineString lineString){// 如果是线类型foreach (Coordinate coord in lineString.Coordinates){points.Add(new Point3d(coord.X, coord.Y, coord.Z)); // 假设 Z 坐标为 0}}else if (geometry is Polygon polygon){// 如果是面类型foreach (Coordinate coord in polygon.ExteriorRing.Coordinates){points.Add(new Point3d(coord.X, coord.Y, coord.Z)); // 假设 Z 坐标为 0}}// 其他几何类型(如 MultiPoint、MultiLineString、MultiPolygon 等)可以类似处理else if (geometry is MultiPolygon multiPolygon){// 遍历 MultiPolygon 中的每个 Polygonforeach (Polygon polygon1 in multiPolygon.Geometries){// 提取外环的坐标点foreach (Coordinate coord in polygon1.ExteriorRing.Coordinates){points.Add(new Point3d(coord.X, coord.Y, coord.Z)); // 假设 Z 坐标为 0}// 提取内环的坐标点foreach (LineString interiorRing in polygon1.InteriorRings){foreach (Coordinate coord in interiorRing.Coordinates){points.Add(new Point3d(coord.X, coord.Y, coord.Z)); // 假设 Z 坐标为 0}}}}int j = 0;foreach(var pt in points){j++;ed.WriteMessage($"\n第{j}个坐标\n");ed.WriteMessage($"x:{pt.X},y:{pt.X}\n"); }}
double.NaN
是 C# 中表示 非数字(Not a Number) 的特殊值。它是 double
类型的一个常量,用于表示无效或未定义的数值结果。以下是关于 double.NaN
的详细说明:
1. 什么是 double.NaN
?
-
定义:
NaN
是 IEEE 754 浮点数标准中定义的一个特殊值,表示 非数字。 -
特点:
-
它不是任何具体的数值。
-
它用于表示无效的数学运算结果(如
0 / 0
或Math.Sqrt(-1)
)。 -
它与任何值(包括它自己)的比较结果都是
false
。
-