如何选择点集是一个只有你才能做出的设计决定,但是一旦你有了点集,你就可以使用 JTS ConcaveHull 类在点周围创建边界。
感谢
@Ian Turton
,我试过Ian Turton
的方法,用了一个算法进行分类,但是好像k-means计算不准确。
于是我用热力图进行过滤,终于得到了我想要的结果
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.geometry.jts.JTS;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.process.raster.PolygonExtractionProcess;
import org.geotools.process.vector.HeatmapProcess;
import org.geotools.referencing.CRS;
import org.junit.Test;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Point;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author Arjen10
* @date 2023/3/28 下午3:21
*/
public class UavTest {
@Test
public void testSimpleSurface() throws IOException, FactoryException {
Path path = Paths.get("/arjen_data/download/456.txt");
byte[] bytes = Files.readAllBytes(path);
String json = new String(bytes, StandardCharsets.UTF_8);
List<Map<String, Object>> maps = JSON.parseObject(json, new TypeReference<List<Map<String, Object>>>() {
});
List<Point> collect = maps.stream()
.map(m -> GeoToolsUtils.creatPoint(Double.valueOf(m.get("lon").toString()), Double.valueOf(m.get("lat").toString())))
.collect(Collectors.toList());
Geometry union = GeoToolsUtils.union(collect);
ReferencedEnvelope bounds = JTS.bounds(union, GeoToolsUtils.EPSG_4326);
SimpleFeatureCollection fc = GeoToolsUtils.geometries2FeatureCollection(collect, "test");
HeatmapProcess process = new HeatmapProcess();
GridCoverage2D cov = process.execute(
fc, // data
1, // radius
null, // weightAttr
1, // pixelsPerCell
bounds, // outputEnv
3840, // outputWidth
2160, // outputHeight
null // monitor)
);
CoordinateReferenceSystem crs = cov.getCoordinateReferenceSystem();
System.out.println(CRS.lookupEpsgCode(crs, true));
PolygonExtractionProcess process2 = new PolygonExtractionProcess();
SimpleFeatureCollection features = process2.execute(cov, 0, Boolean.TRUE, null, null, null, null);
List<Geometry> geometries = new ArrayList<>();
try (SimpleFeatureIterator it = features.features()) {
while (it.hasNext()) {
Geometry geometry = (Geometry) it.next().getDefaultGeometry();
geometries.add(geometry);
}
}
Geometry union1 = GeoToolsUtils.union(geometries);
String json1 = GeoToolsUtils.geometryToGeoJSON(JTS.smooth(union1, 0));
Files.write(Paths.get("/arjen_data/download/123456.geojson"), json1.getBytes(StandardCharsets.UTF_8));
}
}