3

I'm working with a shapefile having about 85.000 features. I need to select some of these using a filter and then showing these on a map. Now... If I draw the entire shapefile's features the creation of the map, the zooming and moving around is fast but if I select, even only 100, features from the SimpleFeatureSource, these steps are really slow. Here is how I select, for example, the first 1000 features and then draw:

The execution stuks at Layer layer = new FeatureLayer (features, style);

and at JMapFrame.showMap(map);

public static void main(String[] args) {
    File file = JFileDataStoreChooser.showOpenFile("shp", null);
     if (file == null) return;
 try{

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

        FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();

        String[] selection = new String[1000];

        for(int i=0; i<selection.length; i++){

            selection[i] = i+"";

        }

        ArrayList<Filter> match = new ArrayList<Filter>();

        for (String name : selection) {
            Filter aMatch = ff.equal(ff.property("ID"), ff.literal(name), false);
            match.add(aMatch);
        }
        Filter filter = ff.or(match);

        SimpleFeatureCollection features = featureSource.getFeatures(filter);



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


        Style style = SLD.createSimpleStyle(features.getSchema());


        Layer layer = new FeatureLayer(features, style);

        map.addLayer(layer);

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

        }catch (Exception e)
        {
            e.printStackTrace();
}

Are there some faster way to do this? ......

2
  • without knowing more about your data it's hard to give a good answer. But if you are only searching on ID the use a FeatureID filter will be quicker. Shapefiles are unindexed and will always be slow to filter. Consider a DB instead.
    – Ian Turton
    Commented Oct 21, 2013 at 8:25
  • In my project I'm using mySQL to store other infos. can I import the shapefile's features into it and, when I need to draw, make some queries to the DB for the features of interest? Can you help me on this? Thank you
    – Felix
    Commented Oct 21, 2013 at 9:03

1 Answer 1

1

There are two ways to speed up drawing a layer in GeoTools, first speed up reading the file and second only read it once.

In the first case you should consider moving your data to a database and accessing that through the relevant datastore - otherwise the code will be identical.

In the second case you can read all of the features into memory and wrap them in an SpatialIndexFeatureCollection.

SpatialIndexFeatureCollection indexed = new SpatialIndexFeatureCollection((SimpleFeatureType) source.getFeatures());

Which will speed up spatial searches.

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.