3

I am working with the geotools library. My goal is to input a coordinate and in return get the Feature's info that contains it.

Geotools Quickstart tutorial's map does exactly what I want with the button I circled in red below. However, I couldn't find the method used.

I've been googling, reading documentation and debugging the code to no avail. This method seems to be the way to go: FeatureCollection myCollection = mySimpleFeatureSource.getFeatures(Query query);

or

FeatureCollection myCollection = mySimpleFeatureSource.getFeatures(Filter filter);

but I haven't figured it out or found out how to query with coordinates yet. If anyone cares to lend a hand, I'd very much appreciate it!

Thanks in advance!

enter image description here

The example I'm running is the following:


package org.geotools.tutorial.quickstart;

import java.io.File;

import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.data.JFileDataStoreChooser;

/**
 * Prompts the user for a shapefile and displays the contents on the screen in a map frame.
 * <p>
 * This is the GeoTools Quickstart application used in documentationa and tutorials. *
 */
public class Quickstart {

    /**
     * GeoTools Quickstart demo application. Prompts the user for a shapefile and displays its
     * contents on the screen in a map frame
     */
    public static void main(String[] args) throws Exception {
        // display a data store file chooser dialog for shapefiles
        File file = JFileDataStoreChooser.showOpenFile("shp", null);
        if (file == null) {
            return;
        }

        FileDataStore store = FileDataStoreFinder.getDataStore(file);
        SimpleFeatureSource featureSource = store.getFeatureSource();

        // Create a map content and add our shapefile to it
        MapContent map = new MapContent();
        map.setTitle("Quickstart");

        Style style = SLD.createSimpleStyle(featureSource.getSchema());
        Layer layer = new FeatureLayer(featureSource, style);
        map.addLayer(layer);

        // Now display the map
        JMapFrame.showMap(map);
    }

}

jjgl
  • 387
  • 1
  • 5
  • 10

1 Answers1

1

Ideally after completing the QuickStart you should work through the other tutorials and so come to the Query Tutorial. This introduces you to how to create a simple Filter and the more complex Query (which adds to a filter with sort order, subsetting of attributes etc).

So to create a simple filter the easiest way is to use the Common Query Language (CQL) static toFilter method:

Filter f = CQL.toFilter("contains(the_geom,Point("+p.x+" "+p.y+"))"); 

where your features have a geometry attribute the_geom and p is a point.

BTW the little tool you are looking for is an InfoTool and the actual Filter is constructed in FeatureLayerHelper.

Ian Turton
  • 10,018
  • 1
  • 28
  • 47