geoJson 转 SimpleFeatureCollection 并实现裁剪功能
pom依赖
<properties>
<geotools.version>25.0</geotools.version>
</properties>
<dependencies>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geometry</artifactId>
<version>24.0</version>
</dependency>
<dependencies>
<repositories>
<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>
工具类:
package com.ruhr.sys.enshi.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.feature.collection.ClippedFeatureCollection;
import org.geotools.geojson.feature.FeatureJSON;
import org.geotools.geojson.geom.GeometryJSON;
import org.locationtech.jts.geom.Geometry;
import org.opengis.feature.simple.SimpleFeature;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
@Slf4j
public class GeoJsonUtils {
public static String clipGeoJsonStrByGeoJsonStr(String areaGeoJson,
String clipGeoJson) throws Exception {
SimpleFeatureCollection simpleFeatureCollection = geoJsonStr2FeatureCollection(areaGeoJson);
SimpleFeatureCollection clipFeatureCollection = clipFeatureCollectionByGeoJsonStr(simpleFeatureCollection, clipGeoJson);
return featureCollection2GeoJsonStr(clipFeatureCollection);
}
public static SimpleFeatureCollection clipFeatureCollectionByGeoJsonStr(SimpleFeatureCollection featureCollection,
String clipGeoJson) throws IOException {
GeometryJSON geometryJSON = new GeometryJSON();
Geometry geometry;
if (clipGeoJson.contains("MultiPolygon")) {
geometry = geometryJSON.readMultiPolygon(clipGeoJson);
} else if (clipGeoJson.contains("Polygon")) {
geometry = geometryJSON.readMultiPolygon(clipGeoJson);
} else {
log.error("当前geoJson不符合规范!");
return null;
}
return new ClippedFeatureCollection(featureCollection, geometry, true);
}
public static String featureCollection2GeoJsonStr(SimpleFeatureCollection featureCollection) throws IOException {
FeatureJSON featureJSON = new FeatureJSON(new GeometryJSON(8));
return featureJSON.toString(featureCollection);
}
public static SimpleFeatureCollection geoJsonStr2FeatureCollection(String geoJson) throws IOException {
DefaultFeatureCollection simpleFeatures = new DefaultFeatureCollection();
FeatureJSON featureJSON = new FeatureJSON();
SimpleFeature simpleFeature = featureJSON.readFeature(geoJson);
SimpleFeatureCollection featureCollection =
(SimpleFeatureCollection) featureJSON.readFeatureCollection(geoJson);
if (featureCollection == null || featureCollection.isEmpty()) {
simpleFeatures.add(simpleFeature);
return simpleFeatures;
}
return featureCollection;
}
}