介绍而不与实际需求相结合,都是食之无味---某人说。今天介绍一下geotools生成泰森多边形。
在geotools中是内置了泰森多边形的算法的,主要依赖于 VoronoiDiagramBuilder 类 利用如下的方式就完成了泰森多边形的计算:
VoronoiDiagramBuilder voronoiDiagramBuilder = new VoronoiDiagramBuilder();
List<Coordinate> coords = new ArrayList<Coordinate>();
//这是读取了包含elevation字段的点数据
SimpleFeatureIterator simpleFeatureIterator = featureCollection.features();
while(simpleFeatureIterator.hasNext()) {
SimpleFeature simpleFeauture = simpleFeatureIterator.next();
Point point = (Point)simpleFeauture.getDefaultGeometry();
double elevation = Double.parseDouble(simpleFeauture.getProperty("elevation").getValue().toString());
Coordinate coord = new Coordinate(point.getX(),point.getY(),elevation);
coords.add(coord);
}
simpleFeatureIterator.close();
//输入坐标点
voronoiDiagramBuilder.setSites(coords);
//设置泰森多边形的边界(一个com.vividsolutions.jts.geom.Polygon)
voronoiDiagramBuilder.setClipEnvelope(boundary.getEnvelopeInternal());
//输出成果
Geometry geom = voronoiDiagramBuilder.getDiagram(gf);
用到的jar的maven如下:
<properties>
<geotools.version>19.4</geotools.version>
</properties>
<dependencies>
<!-- Geotools -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-main</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geotiff</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-process-raster</artifactId>
<version>${geotools.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.geotools/gt-image -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-image</artifactId>
<version>${geotools.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
<!-- 代码库 -->
<repositories>
<repository>
<id>maven-ali</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
<repository>
<id>osgeo</id>
<name>OSGeo Release Repository</name>
<url>https://repo.osgeo.org/repository/release/</url>
<snapshots><enabled>false</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</repository>
</repositories>
</project>
因为,一直都很忙,博文内容上也觉得越写越生硬,缺少一些有趣的部分,可能等之后有时间会变化下博文的写作形式,或者创作内容。