I need to import and treat a Shapefile using the above mentioned coordinate system into a Java application that uses WGS84 coordinate system.
More specifically, I read the shapefile and extract polygons, and store each polygon to an object in my database. Later, I'll have to compare spatial points with those polygons (checking if point is in or out its polygon). The fact that the points and the polygons are in different CRSes seems to be the reason why I reach 0 hits.
While I think it is irrelevant, here is how I read the shapefile with GeoTools:
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();
}
The following code is more relevant: 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));
How to convert polygons from ED_1950... to WGS84? Or, how to compare a WGS84 point with a ED_1950 polygon?