'is there a published answer in this (Geotools MapStyle turtorial, select features)
Is there a published solution on Geotools userguide task? In the link: https://docs.geotools.org/stable/userguide/tutorial/map/style.html there is a "Things to try": In the Geometry and CRS tutorial we saw how to change the Coordinate Reference System of a MapContent. Try the following:
- Modify this application so that you can change the CRS in which the features are displayed.
- Display the bc_voting_areas shapefile and change the CRS to EPSG:4326
- Now try to use the selection tool. You will find that it no longer works !
I would like to find objects from different layers, who has different CRS, regardless of the map-window's CRS
This is my code that misses features in some layers (I can't understund why):
void selectFeatures(MapMouseEvent ev) {
Point screenPos = ev.getPoint();
Rectangle screenRect = new Rectangle(screenPos.x - 2, screenPos.y - 2, 5, 5);
AffineTransform screenToWorld = mapFrame.getMapPane().getScreenToWorldTransform();
Rectangle2D worldRect = screenToWorld.createTransformedShape(screenRect).getBounds2D();
ReferencedEnvelope bbox = new ReferencedEnvelope(worldRect,mapFrame.getMapContent().getCoordinateReferenceSystem());
try {
Iterator<Layer> it = map.layers().iterator();
IDs = new HashSet<>();
while (it.hasNext()) {
Layer l = it.next();
if (l.isSelected()) {
selectedFeatures = grabFeaturesInBoundingBox(bbox, l.getFeatureSource());
SimpleFeatureIterator iter = selectedFeatures.features();
while (iter.hasNext()) {
SimpleFeature feature = iter.next();
IDs.add(feature.getIdentifier());
System.out.println(" " + feature.getIdentifier());
}
iter.close();
}
}
if (IDs.isEmpty()) {
System.out.println(" no feature selected");
} else {
doSometing...
}
} catch (Exception ex) {
ex.printStackTrace();
}}
SimpleFeatureCollection grabFeaturesInBoundingBox(ReferencedEnvelope click, FeatureSource<?, ?> fs) throws Exception {
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
SimpleFeatureCollection sfc = null;
FeatureType schema = fs.getSchema();
String geometryPropertyName = schema.getGeometryDescriptor().getLocalName();
CoordinateReferenceSystem targetCRS = schema.getGeometryDescriptor().getCoordinateReferenceSystem();
ReferencedEnvelope bbox = click.transform(targetCRS, true);
Filter filter = ff.bbox(ff.property(geometryPropertyName), bbox);
sfc = featureSource.getFeatures(filter);
return sfc;}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
