我需要使用上面提到的坐标系将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();
}
以下代码更相关:如何检查点是否在形状内
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多边形进行比较?
答案 0 :(得分:0)
首先,您需要从丹麦转变为WGS84:
CoordinateReferenceSystem worldCRS = CRS.decode("EPSG:4326");
CoordinateReferenceSystem ed_1950 = CRS.decode("EPSG:23032");
boolean lenient = true; // allow for some error due to different datums
MathTransform transform = CRS.findMathTransform(ed_1950, worldCRS, lenient);
然后,对于添加到数据库的每个Geometry,您需要对其进行转换:
Geometry geometry = (Geometry) feature.getDefaultGeometry();
Geometry geometry2 = JTS.transform(geometry, transform);
(我实际上没有编译过这段代码,但它应该有效 - 请参阅此tutorial了解更多详情。)