NetTopologySuite简单的学习

创建集合图形
 

// 几何创建工厂(也可不使用工厂模式直接创建几何图形)
var gf = new GeometryFactory();
var pg3 = gf.CreatePolygon(new[]
{
    new Coordinate(612, 612),
    new Coordinate(144, 355),
    new Coordinate(165, 188),
    new Coordinate(277, 328),
    new Coordinate(612, 612)
});
var pg4 = gf.CreatePolygon(new[]
{
    new Coordinate(412, 612),
    new Coordinate(555, 455),
    new Coordinate(655, 188),
    new Coordinate(577, 328),
    new Coordinate(412, 612)
});

创建了两个多边形  pg3  与pg4

求并集方法
var union = pg3.Union(pg4);
求交集方法
var intersection = pg3.Intersection(pg4);
求差集方法
var difference = pg3.Difference(pg4);
计算两个店的距离
using NetTopologySuite.Geometries;
using NetTopologySuite.Operation.Distance;

// 创建两个点
Point point1 = new Point(new Coordinate(1, 2));
Point point2 = new Point(new Coordinate(4, 6));

// 使用 DistanceOp 计算两点之间的距离
DistanceOp distanceOp = new DistanceOp(point1, point2);
double distance = distanceOp.Distance();

Console.WriteLine("Distance between point1 and point2: " + distance);
计算几何面积
using NetTopologySuite.Geometries;
using NetTopologySuite.Algorithm;

// 创建一个多边形
Coordinate[] coordinates = new Coordinate[]
{
    new Coordinate(0, 0),
    new Coordinate(0, 10),
    new Coordinate(10, 10),
    new Coordinate(10, 0),
    new Coordinate(0, 0)
};
LinearRing linearRing = new LinearRing(coordinates);
Polygon polygon = new Polygon(linearRing);

// 使用 JTS 中的 Area 类计算多边形的面积
Area area = new Area(polygon);
double polygonArea = area.GetArea();

Console.WriteLine("Area of the polygon: " + polygonArea);
凸包计算
using NetTopologySuite.Geometries;
using NetTopologySuite.Algorithm.ConvexHull;

// 创建一个包含多个点的集合
Coordinate[] coordinates = new Coordinate[]
{
    new Coordinate(0, 0),
    new Coordinate(5, 0),
    new Coordinate(3, 3),
    new Coordinate(0, 5),
    new Coordinate(2, 2),
    new Coordinate(1, 4)
};
GeometryFactory geometryFactory = new GeometryFactory();
MultiPoint points = geometryFactory.CreateMultiPointFromCoords(coordinates);

// 使用 NetTopologySuite 计算凸包
ConvexHull convexHull = new ConvexHull(points);
Geometry convexHullGeometry = convexHull.GetConvexHull();

Console.WriteLine("Convex hull geometry: " + convexHullGeometry);
判断是否相交
 
using NetTopologySuite.Geometries;
using NetTopologySuite.Operation.Overlay;

// 创建两个几何对象
Polygon polygon1 = new Polygon(new LinearRing(new Coordinate[] {
    new Coordinate(0, 0),
    new Coordinate(0, 5),
    new Coordinate(5, 5),
    new Coordinate(5, 0),
    new Coordinate(0, 0)
}));
Polygon polygon2 = new Polygon(new LinearRing(new Coordinate[] {
    new Coordinate(4, 4),
    new Coordinate(4, 6),
    new Coordinate(6, 6),
    new Coordinate(6, 4),
    new Coordinate(4, 4)
}));

// 使用 Intersection 方法计算两个几何对象的相交部分
Geometry intersection = polygon1.Intersection(polygon2);

// 检查相交结果是否为空
bool isIntersect = !intersection.IsEmpty;

Console.WriteLine("Are the two geometries intersecting? " + isIntersect);

判断是否覆盖
 

using NetTopologySuite.Geometries;

// 创建两个几何对象
Polygon polygon1 = new Polygon(new LinearRing(new Coordinate[] {
    new Coordinate(0, 0),
    new Coordinate(0, 5),
    new Coordinate(5, 5),
    new Coordinate(5, 0),
    new Coordinate(0, 0)
}));
Polygon polygon2 = new Polygon(new LinearRing(new Coordinate[] {
    new Coordinate(1, 1),
    new Coordinate(1, 2),
    new Coordinate(2, 2),
    new Coordinate(2, 1),
    new Coordinate(1, 1)
}));

// 使用 Covers 方法判断 polygon1 是否覆盖 polygon2
bool isCovered = polygon1.Covers(polygon2);

Console.WriteLine("Does polygon1 cover polygon2? " + isCovered);

缓冲区域分析
 

using NetTopologySuite.Geometries;

// 创建一个点对象
Point point = new Point(new Coordinate(1, 1));

// 创建一个缓冲区
double bufferDistance = 0.5; // 缓冲区距离为0.5个单位
Geometry bufferedGeometry = point.Buffer(bufferDistance);

// 打印缓冲区的几何类型和坐标点
Console.WriteLine("Buffered Geometry Type: " + bufferedGeometry.GeometryType);
Console.WriteLine("Buffered Geometry Coordinates: " + bufferedGeometry.Coordinates[0].X + ", " + bufferedGeometry.Coordinates[0].Y);
几何对象转换

可以对几何对象进行平移、旋转、缩放等变换操作

几何对象的属性获取

可以获取几何对象的长度、面积、中心点等属性信息。

几何对象的验证

可以验证几何对象是否有效,如检查多边形是否闭合、线段是否相交等。

核心对象

Coordinate: 表示一个二维坐标点,包括 X 和 Y 坐标值。
CoordinateSequence: 由一系列 Coordinate 对象组成的序列,可以表示线、多边形等几何对象的顶点。
CoordinateFilter: 用于对几何对象的坐标进行过滤或修改的接口。

Geometry: 表示一个几何对象的抽象基类。它有许多具体的子类,如 Point、LineString、Polygon 等,用于表示不同类型的几何对象。
GeometryFactory: 用于创建几何对象的工厂类,提供了创建不同类型几何对象的方法。
GeometryCollection: 表示多个几何对象的集合,可以包含各种类型的几何对象。
GeometryTransformer: 用于对几何对象进行转换的工具类,包括坐标系转换、平移、旋转、缩放等操作。

Envelope: 表示一个矩形边界框,可以用于表示几何对象的边界范围。

IntersectionMatrix: 描述几何对象之间的拓扑关系的矩阵,包含 9 个位置值(Dimensional Extended Nine-Intersection Model)。

PrecisionModel: 用于控制几何对象坐标的精度和舍入规则。

Point: 表示一个点的几何对象,由一个坐标构成。

LineString: 表示一条线段或曲线的几何对象,由一系列有序的坐标点构成。

LinearRing: 表示一个封闭的线环,通常用于定义多边形的外环或内环。

Polygon: 表示一个多边形的几何对象,由一个外环和零个或多个内环组成。

MultiPoint: 表示多个点的集合,每个点都是一个独立的几何对象。

MultiLineString: 表示多条线段或曲线的集合,每条线段都是一个独立的几何对象。

MultiPolygon: 表示多个多边形的集合,每个多边形都是一个独立的几何对象。

DistanceOp: 用于计算几何对象之间的距离和最近点的工具类。

IntersectionMatrix: 描述几何对象之间的拓扑关系的矩阵,包含 9 个位置值(Dimensional Extended Nine-Intersection Model)。
                        
 

  • 12
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
nettopologysuite是一个开源的地理信息系统 (GIS) 库,它提供了一些用于处理和分析地理空间数据的功能。如果要编译nettopologysuite为dll文件,可以按照以下步骤进行操作。 首先,打开nettopologysuite的源代码仓库,可以在GitHub上找到它的源代码。 接下来,在电脑上安装合适版本的Visual Studio集成开发环境 (IDE),例如Visual Studio 2019。 然后,使用Git或者直接下载nettopologysuite的源代码。 在Visual Studio中打开解决方案文件,这个文件通常是以.sln作为后缀名的。在解决方案资源管理器中,你可以看到项目的层次结构以及相关文件。 在解决方案资源管理器中,找到要编译为dll的项目(通常是一个类库项目),右键点击该项目并选择“生成”或者“生成解决方案”。 在编译过程中,Visual Studio将会执行编译和链接操作,生成dll文件。如果编译成功,你将在对应的输出文件夹中找到生成的dll文件。 请注意,编译过程中可能会出现错误或警告,这可能是由于环境配置或代码本身的问题。根据错误或警告信息,可能需要进行一些修改或调整。 编译成功后,你就可以在其他项目中引用这个生成的dll文件,从而使用nettopologysuite库提供的功能。 总之,通过使用Visual Studio编译nettopologysuite的源代码,你可以生成一个dll文件,该文件可以在其他项目中使用该库提供的地理空间数据处理和分析功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值