对于来自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) {
}
}
}
我知道将双精度型转换为字符串然后再转换为整数仍然无法正常工作。
对于负抽取值,如何在多边形中找到点,该如何解决?请帮忙。
答案 0 :(得分:3)
使用SimpleFeature,可以调用getDefaultGeometry并获取Geometry对象。转换为Geometry后,应该有一个包含Point类的contains方法。
此外,您也不想使用java.awt.Polygon类。相反,您将使用org.locationtech.jts Geometry类。