创建集合图形
// 几何创建工厂(也可不使用工厂模式直接创建几何图形)
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)。