如何在地理工具中将一组点转换为近似多边形

问题描述 投票:0回答:2

我有一系列的观点。我怎样才能将它们转换成类似于下面红框所示的多边形?

我想也许我们应该按指定的距离分组,通过分组创建多边形

geotools
2个回答
1
投票

如何选择点集是一个只有你才能做出的设计决定,但是一旦你有了点集,你就可以使用 JTS ConcaveHull 类在点周围创建边界。


0
投票

感谢

@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));
    }


}

结果

© www.soinside.com 2019 - 2024. All rights reserved.