Yearly Archives: 2020

GIS

GIS库JTS例子

JTS库依赖

1
2
3
4
5
<dependency>
    <groupId>com.vividsolutions</groupId>
    <artifactId>jts</artifactId>
    <version>1.13</version>
</dependency>

判断点是否在多边形内

1
2
3
4
5
6
7
8
9
GeometryFactory factory = new GeometryFactory();
// 定义多边形
LinearRing shell = factory.createLinearRing(cs);
Polygon polygon = factory.createPolygon(shell);
// Polygon polygon = factory.createPolygon(shell, holes);// holes 是 LinearRing 的数组
// 定义点
Coordinate coordinate = new Coordinate(lon, lat);
// 判断是否在多边形内
boolean contains = SimplePointInAreaLocator.containsPointInPolygon(coordinate, polygon);

抽稀算法(道格拉斯-普克算法 Douglas–Peucker algorithm)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
double lon1 = 116.556408D;
double lat1 = 34.012041D;
Coordinate[] coordinates = new Coordinate[]{
  new Coordinate(lon1, lat1),
  ...
};
CoordinateSequence coordinateSequence = new CoordinateArraySequence(coordinates);
//数据精度
PrecisionModel precisionModel = new PrecisionModel(PrecisionModel.FLOATING_SINGLE);
GeometryFactory factory = new GeometryFactory(precisionModel, 4326);
LineString lineString = new LineString(coordinateSequence, factory);
 
DouglasPeuckerSimplifier simplifier = new DouglasPeuckerSimplifier(lineString);
// 抽稀释容差,比如抽稀距离20m转换成=20/110km=20/110000m
simplifier.setDistanceTolerance(0.0001D);
Geometry geometry = simplifier.getResultGeometry();
// 抽稀后的结果
Coordinate[] result = geometry.getCoordinates();