繁体   English   中英

查找点是否在多边形内 - JAVA jts / awt / geotools

[英]Find if a point is inside a polygon - JAVA jts / awt / geotools

对于从shapefile中获取的多边形,我有一个随机数量的坐标。

-119.00072399999999 35.36158, -118.99903 35.361576, -118.999026 35.362579, -118.999023 35.363482, -118.999019 35.36432, -118.999408 35.364847999999995, -118.999406 35.365564, -118.999402 35.366516, -118.999398 35.367467999999995, -118.999394 35.368438, -118.999256 35.368438, -118.998232 35.368441

我现在必须检查一个点(33.63705, -112.17563)是否在这个多边形内。

我担心的是,我的坐标不适合int数据类型。

这是我尝试过的:

import java.awt.Polygon;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

public class ReadShapeFile {

    public static void main(String[] args) {

        File file = new File("D:\\shapefile201806\\tl_2018_06_bg.shp");

        try {
            Map<String, String> connect = new HashMap<String, String>();
            connect.put("url", file.toURI().toString());

            DataStore dataStore = DataStoreFinder.getDataStore(connect);
            String[] typeNames = dataStore.getTypeNames();
            String typeName = typeNames[0];

            System.out.println("Reading content : " + typeName);

            SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
            SimpleFeatureCollection collection = featureSource.getFeatures();
            SimpleFeatureIterator iterator = collection.features();

            try {
                while (iterator.hasNext()) {

                    SimpleFeature feature = iterator.next();
                    String featureString = feature.toString();

                    List<String> polygonList = new ArrayList<String>();

                    String polygonCoordinates = StringUtils.substringBetween(featureString, "(((", ")))");
                    System.out.println(polygonCoordinates);
                    polygonList = Arrays.asList(polygonCoordinates.split(","));

                    SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();

                    b.setName("MyFeatureType");
                    b.setCRS(DefaultGeographicCRS.WGS84);
                    b.add("location", Point.class);
                    final SimpleFeatureType TYPE = b.buildFeatureType();

                    SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
                    GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
                    SimpleFeature pointFeature = featureBuilder.buildFeature(null);
                    DefaultFeatureCollection featureCollection = new DefaultFeatureCollection("internal", TYPE);
                    featureCollection.add(pointFeature);

                    try {
                        Polygon polygon = new Polygon();
                        for (int i = 0; i < polygonList.size(); i++) {
                            String[] splitAxis = (polygonList.get(i).split("\\s+"));
                            polygon.addPoint(Integer.valueOf(splitAxis[0]), Integer.valueOf(splitAxis[1]));
                        }

                        boolean isInside = polygon.contains(33.63705,  -112.17563);
                        System.out.println(isInside);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            } finally {
                iterator.close();
            }

        } catch (Throwable e) {
        }

    }

}

我知道无论如何将double转换为string并返回整数都不会起作用。

如何在多边形中为负抽取值实现解决方案? 请帮忙。

使用SimpleFeature,您可以调用getDefaultGeometry并获取Geometry对象。 一旦你转换为Geometry,就应该有一个包含Point类的contains方法。

此外,您不想使用java.awt.Polygon类。 相反,你将使用org.locationtech.jts几何类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM