After successfully writing M values into a shapefile (.shp, only points for now), I now also want to read the generated file and print the full coordinate set (x, y, z and m) using geotools (23-SNAPSHOT).
I found two different ways to do it (see code below) but both only give me a normal Coordinate object that has an M attribute, which is always NaN. I know that writing the file was successful because QGIS can see the full coordinates (including M).
How do I read the file, while still getting the actual M values?
Code I've tested:
1.From official website:
File f = new File(path);
Map<String, Object> map = new HashMap<String, Object>();
try {
map.put("url", f.toURI().toURL());
DataStore dataStore = DataStoreFinder.getDataStore(map);
String typeName = dataStore.getTypeNames()[0];
FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(typeName);
Filter filter = Filter.INCLUDE; // ECQL.toFilter("BBOX(THE_GEOM, 10,20,30,40)")
FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures(filter);
FeatureIterator<SimpleFeature> features = collection.features();
while(features.hasNext()) {
SimpleFeature feature = features.next();
List attributes = feature.getAttributes();
Collection properties = feature.getProperties();
if(attributes.get(0) instanceof Point) {
Point p = (Point) attributes.get(0);
Coordinate c = p.getCoordinate(); //"c.getM()" returns "NaN"
CoordinateXYZM cxyzm = new CoordinateXYZM(c); //"cxyzm.getM()" returns "0.0"
}
}
} catch (Exception e) {
System.out.println("Exception: "+e);
}
2.From a question here on Stackexchange:
File f = new File(path);
try {
FileDataStore store = FileDataStoreFinder.getDataStore(f);
SimpleFeatureSource featureSource = store.getFeatureSource();
SimpleFeatureCollection featureCollection = featureSource.getFeatures();
SimpleFeatureIterator it = featureCollection.features();
while(it.hasNext()){
SimpleFeature feat = it.next();
Geometry g = (Geometry) feat.getDefaultGeometry(); //"org.locationtech.jts.geom.Geometry", NOT "org.opengis.geometry.Geometry"!
Coordinate c = g.getCoordinate(); //"c.getM()" returns "NaN"
CoordinateXYZM cxyzm = new CoordinateXYZM(c); //"cxyzm.getM()" returns "0.0"
}
} catch (IOException e) {
System.out.println("Exception: "+e);
}