一.GeoTools介绍
GeoTools是一套操作和显示地图的开源Java 代码库,提供了一系列符合规范的操作地理空间数据的方法,GeoTools被许多项目使用,在地理信息领域有着广泛的应用。
二.背景
本文介绍的是如何使用GeoTools将Geometry对象转换成MultiPolygon,因为Geometry可以包含点线面多种几何图形,而在进行几何分析时,通常需要面要素,因此我们需要将Geometry中的面提取出来,转换成MultiPolygon,便于后续进行空间分析查询使用。
三.实现代码
1.常规转换
下面定义了一个geometryToMultiPolygon,调用这个方法即可实现Geometry对象转换成MultiPolygon。
public static MultiPolygon geometryToMultiPolygon(Geometry geometry){
if (geometry == null){
return null;
}
Integer srid = geometry.getSRID();
String geometryType = geometry.getGeometryType();
if (geometryType.equals("Polygon")){
return polygonToMultipolygon((Polygon) geometry);
}else if (geometryType.equals("MultiPolygon")){
return (MultiPolygon) geometry;
}else if (geometryType.equals("GeometryCollection")){
int numGeometries = geometry.getNumGeometries();
Geometry geometryChird = null;
String geoType = null;
Geometry geometryUnion = null;
for (int i = 0;i < numGeometries;i++){
geometryChird = geometry.getGeometryN(i);
geoType = geometryChird.getGeometryType();
if (geoType.equals("Polygon") || geoType.equals("MultiPolygon") ){
if (geometryUnion == null){
geometryUnion = geometryChird;
}else {
geometryUnion = geometryUnion.union(geometryChird);
}
}
}
if (geometryUnion != null){
geometryUnion.setSRID(srid);
if (geometryUnion.getGeometryType().equals("MultiPolygon")){
return (MultiPolygon) geometryUnion;
}else {
return polygonToMultipolygon((Polygon) geometryUnion);
}
}
}
return null;
}
2.设定最小面积转换
在前面的常规简单进行Geometry对象转换成MultiPolygon的情况下,我们还可以指定一个最小面积,是Geometry中达到最小面积的面和微小面实现分离提取。代码如下
public static List<MultiPolygon> geometryToMultiPolygon(Geometry geometry,double minArea){
List<MultiPolygon> list = new ArrayList<>();
String geometryType = geometry.getGeometryType();
if (geometryType.equals("Polygon")){
Polygon polygon = (Polygon) geometry;
MultiPolygon multiPolygon = polygonToMultipolygon(polygon);
if (multiPolygon.getArea() >= minArea){
list.add(multiPolygon);
list.add(null);
}else {
list.add(null);
list.add(multiPolygon);
}
}else if (geometryType.equals("MultiPolygon")){
MultiPolygon multiPolygon = (MultiPolygon) geometry;
if (multiPolygon.getArea() >= minArea){
list.add(multiPolygon);
list.add(null);
}else {
list.add(null);
list.add(multiPolygon);
}
}else if (geometryType.equals("GeometryCollection")){
int numGeometries = geometry.getNumGeometries();
Geometry geometryChird = null;
String geoType = null;
Geometry geometryUnionBig = null;
Geometry geometryUnionSmall = null;
for (int i = 0;i < numGeometries;i++){
geometryChird = geometry.getGeometryN(i);
geoType = geometryChird.getGeometryType();
if (geoType.equals("Polygon") || geoType.equals("MultiPolygon") ){
if (geometryChird.getArea() >= minArea){
if (geometryUnionBig == null){
geometryUnionBig = geometryChird;
}else {
geometryUnionBig = geometryUnionBig.union(geometryChird);
}
}else {
if (geometryUnionSmall == null){
geometryUnionSmall = geometryChird;
}else {
geometryUnionSmall = geometryUnionSmall.union(geometryChird);
}
}
}
}
if (geometryUnionBig != null){
if (geometryUnionBig.getGeometryType().equals("MultiPolygon")){
list.add((MultiPolygon) geometryUnionBig);
}else {
list.add(polygonToMultipolygon((Polygon) geometryUnionBig));
}
}else {
list.add(null);
}
if (geometryUnionSmall != null){
if (geometryUnionSmall.getGeometryType().equals("MultiPolygon")){
list.add((MultiPolygon) geometryUnionSmall);
}else {
list.add(polygonToMultipolygon((Polygon) geometryUnionSmall));
}
}else {
list.add(null);
}
}else {
list.add(null);
list.add(null);
}
return list;
}
————————————————
版权声明:本文为CSDN博主「会跑的小鹿」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_71232279/article/details/131022929