1

I've followed the geotools selection lab tutorial. It works perfectly. Now I would like to add another layer, using another shapefile. So I used JFileDataStoreChooser to select another shapefile and added that layer to the existing map. Both layers are now shown on the map frame.

But when I click on a feature, only one type of feature is selected. For example, I have loaded a nodes layer and a lines layer. When I click on a node, it's selected but when I click on a line it's not selected.

I've seen that SimpleFeatureIterator uses featureSource. What should be the approach I follow in this kind of a scenario?

2
  • please add the code that is called when you click on a feature
    – Ian Turton
    Commented Apr 8, 2016 at 10:45
  • Can you share any tutorial or code to load multiple shape files in the form of layers?
    – Just Fun
    Commented Jan 9, 2017 at 1:41

1 Answer 1

1

I've found a sample answer. Just find the available layers in the map and apply a filter for each layer separately. Following is the code I used.

    MapContent con = mapPane.getMapContent();
    con.layers();
    for (Layer l: con.layers()) {
        FeatureType schema = l.getFeatureSource().getSchema();
        String geometryPropertyName = schema.getGeometryDescriptor().getLocalName(); // "THE_GEOM"
        System.out.println("Layer : " + geometryPropertyName);

        Point screenPos = ev.getPoint();
    Rectangle screenRect = new Rectangle(screenPos.x-2, screenPos.y-2, 5, 5);
        AffineTransform screenToWorld = mapPane.getScreenToWorldTransform();
    Rectangle2D worldRect = screenToWorld.createTransformedShape(screenRect).getBounds2D();
        ReferencedEnvelope bbox = new ReferencedEnvelope(
            worldRect,
            mapPane.getMapContent().getCoordinateReferenceSystem());
         Filter filternew = ff.intersects(ff.property(geometryAttributeName), ff.literal(bbox));
        try {
            FeatureCollection self = l.getFeatureSource().getFeatures(filternew);
            FeatureIterator iter = self.features();
            System.out.println("New : " + iter.hasNext());
        } catch (IOException ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /*
     * Construct a 5x5 pixel rectangle centred on the mouse click position
     */
    Point screenPos = ev.getPoint();
    Rectangle screenRect = new Rectangle(screenPos.x-2, screenPos.y-2, 5, 5);

    /*
     * Transform the screen rectangle into bounding box in the coordinate
     * reference system of our map context. Note: we are using a naive method
     * here but GeoTools also offers other, more accurate methods.
     */
    AffineTransform screenToWorld = mapPane.getScreenToWorldTransform();
    Rectangle2D worldRect = screenToWorld.createTransformedShape(screenRect).getBounds2D();
    ReferencedEnvelope bbox = new ReferencedEnvelope(
            worldRect,
            mapPane.getMapContent().getCoordinateReferenceSystem());

    /*
     * Create a Filter to select features that intersect with
     * the bounding box
     */
    Filter filter = ff.intersects(ff.property(geometryAttributeName), ff.literal(bbox));

    /*
     * Use the filter to identify the selected features
     */
    try {
        SimpleFeatureCollection selectedFeatures =
                featureSource.getFeatures(filter);

        SimpleFeatureIterator iter = selectedFeatures.features();
        System.out.println("1 Has next ? " + iter.hasNext());


        SimpleFeatureCollection selectedFeatures2 =
                featureSource2.getFeatures(filter);

        SimpleFeatureIterator iter2 = selectedFeatures.features();
        System.out.println("2 Has next ? " + iter2.hasNext());




        Set<FeatureId> IDs = new HashSet<FeatureId>();
        try {
            while (iter.hasNext()) {
                SimpleFeature feature = iter.next();
                IDs.add(feature.getIdentifier());

                System.out.println("   " + feature.getIdentifier());
            }

        } finally {
            iter.close();
        }

        if (IDs.isEmpty()) {
            System.out.println("   no feature selected");
        }

        displaySelectedFeatures(IDs);

    } catch (Exception ex) {
        System.out.println(ex.getMessage());
        ex.printStackTrace();
        return;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.