I use the java libraries geotools and vividsolutions to extract xyz data from a shapefile (Vienna from this example).
This works perfectly fine for x and y data, but all coordinate values for z are set to "NaN" (not a number), same as for regular 2D Shapefiles. I found other questions relating to Z=0, but i think this one is quite different since its about different tools, and the source file already does contain Z-Values (as can be seen in the example).
Using the code below, i can successfully extract x and y values for coordinates, but coordinate.z always yields "NaN". Since i know the shapefile in question contains z-Values, the problem must be in geotools/vividsolutions (or my use of those libraries). So, my question is, does anybody know how to read the Z coordinate value using geotools & vivdsolutions?
For reference, see below for the (condensed) code i used to exctract coordinates.
Getting the Geometries from Shapefile:
public List<Geometry> getShapefileGeometries(File shpFile) throws MalformedURLException, IOException {
List<Geometry> shpGeometries = null;
Map<String, Object> mapConnectionTypeInfo = new HashMap<>();
mapConnectionTypeInfo.put("url", shpFile.toURI().toURL());
DataStore myDataStore = DataStoreFinder.getDataStore(mapConnectionTypeInfo);
String typeName = myDataStore.getTypeNames()[0];
FeatureSource<SimpleFeatureType, SimpleFeature> dataSource = myDataStore.getFeatureSource(typeName);
FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollection = dataSource.getFeatures();
Object[] featureObjects = featureCollection.toArray();
if (featureObjects != null) {
shpGeometries = new ArrayList<>();
for (Object featureObject : featureObjects) {
SimpleFeature feature = (SimpleFeature) featureObject;
Object defaultGeometry = feature.getDefaultGeometry();
if (defaultGeometry != null && defaultGeometry instanceof Geometry) {
shpGeometries.add((Geometry) defaultGeometry);
}
}
}
return shpGeometries;
}
Searching Geometries for coordinates with Z-Values:
public void checkGeometriesForZ(List<Geometry> shpGeometries) {
for (Geometry currentGeometry : shpGeometries) {
for (int i = 0; i < currentGeometry.getNumGeometries(); i++) {
Geometry g = currentGeometry.getGeometryN(i);
Coordinate[] coordinates = null;
if (g instanceof Polygon) coordinates = ((Polygon) g).getCoordinates();
if (g instanceof LineString) coordinates = ((LineString) g).getCoordinates();
if (g instanceof Point) coordinates = ((Point) g).getCoordinates();
if (coordinates != null) {
for (Coordinate currentCoordinate : coordinates) {
System.out.println(String.format("X: %f, Y: %f, Z: %f", currentCoordinate.x, currentCoordinate.y, currentCoordinate.z));
}
}
}
}
}
Testing (using the vienna shapefile from the example):
@Test
public void searchSHPWithZ() throws IOException {
File vienna = new File("shapefiles/105082_bkm.shp");
CoordinateExtraction mySearch = new CoordinateExtraction();
List<Geometry> currentGeometries = mySearch.getShapefileGeometries(vienna);
mySearch.checkGeometriesForZ(currentGeometries);
}
Typical output:
"X: 2963,808248, Y: 341051,336212, Z: NaN"