我需要将使用上述坐标系的 Shapefile 导入并处理到使用 WGS84 坐标系的 Java 应用程序中。
更具体地说,我读取 shapefile 并提取多边形,并将每个多边形存储到我的数据库中的一个对象中。稍后,我将不得不将空间点与这些多边形进行比较(检查点是否在其多边形之内或之外)。点和多边形位于不同的 CRS 中的事实似乎是我达到 0 次命中的原因。
虽然我认为这无关紧要,但这是我使用 GeoTools 读取 shapefile 的方式:
InputStream stream = new FileInputStream("path/to/file");
File tempDir = File.createTempFile("shapefile_temp", "");
if (tempDir.exists())
tempDir.delete();
tempDir.mkdir();
URL shpName = null;
Files.unzip(stream, tempDir);
for (File file : tempDir.listFiles())
if (file.getName()
.endsWith(".shp"))
{
shpName = file.toURI()
.toURL();
break;
}
if (shpName == null)
throw new RuntimeException("No SHP found");
Map<String, Object> map = new HashMap<String, Object>();
map.put("url", shpName);
DataStore dataStore = DataStoreFinder.getDataStore(map);
try
{
String typeName = dataStore.getTypeNames()[0];
FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(typeName);
FeatureIterator<SimpleFeature> iterator = source.getFeatures()
.features();
while (iterator.hasNext())
{
Feature feature = iterator.next();
System.out.println();
System.out.println((int) feature.getProperty("COD_PRO")
.getValue() + "\t" + feature.getProperty("NOME_PRO")
.getValue());
System.out.println(feature.getDefaultGeometryProperty()
.getValue()); //This thing will be stored in a CQEngine and compared against a given point
}
}
finally
{
tempDir.delete();
}
}
finally
{
System.gc();
}
下面的代码更相关:how to check if a point is inside the shape
final GeometryFactory geoFactory = new GeometryFactory();
com.vividsolutions.jts.geom.Point point = geoFactory.createPoint(new Coordinate(loc.getLongitude().doubleValue(), loc.getLatitude().doubleValue()));
loc.setContained(shape.getGeometry().covers(pointBing) || shape.getGeometry().contains(point));
如何将多边形从 ED_1950... 转换为 WGS84?或者,如何将 WGS84 点与 ED_1950 多边形进行比较?