gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:00:44 26 4
gpt4 key购买 nike

我有一个从 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 型转换为字符串再转换回整数无论如何都行不通。

对于负抽取值,我怎样才能解决一个点是否在多边形中的问题?请帮忙。

最佳答案

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

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

关于java - 查找点是否在多边形内 - JAVA jts/awt/geotools,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54235693/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com