1

I am trying to use GeoTools in order to load a shapefile into java and then check whether a point is located within one of the polygons in the shape file The problem is that i am not able to load the shapefile and therefore to continue forward. Here is my code so far:

public static void main(String[] args){
    // create sample coordinate
    double lon = -105.0;
    double lat = 40.0;
    GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(PrecisionModel.maximumPreciseValue),8307);
    Geometry point  =  geometryFactory.createPoint(new Coordinate(lon,lat));
    //

    String path = System.getProperty("user.dir") + "/continent_shp/continent_shp.shp";
    File file = new File(path);

    try {
        Map<String, Serializable> connectParameters = new HashMap<String, Serializable>();

        // load shapefile ---- does not work !!!!!!!!
        connectParameters.put("url", file.toURI().toURL());
        connectParameters.put("create spatial index", true);
        DataStore dataStore = DataStoreFinder.getDataStore(connectParameters);
        // 

        FeatureSource featureSource = dataStore.getFeatureSource("POLYGON");
        FeatureCollection collection = (FeatureCollection) featureSource.getFeatures();
        FeatureIterator iterator = collection.features();

        while (iterator.hasNext()) {
            Feature feature = iterator.next();
            Geometry sourceGeometry = feature.getDefaultGeometry();
            boolean isContained = sourceGeometry.contains(point);
            System.out.println(isContained);
        }
    } 
    catch (MalformedURLException e) {e.printStackTrace();} 
    catch (IOException e) {e.printStackTrace();}
}

The problem is that the dataStore variable is null after I try to load the shapefile.

Here are my imports:

    import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;

import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.geotools.feature.Feature;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.PrecisionModel;

Can anyone shed some light on this issue? Any help would be appreciated. Thank you.

2 Answers2

0

The most likely problem is that you don't have a Shapefile Datastore implementation available on your path. Try the following method to check what stores are available:

public Map<String, DataStoreFactorySpi> fetchAvailableDataStores() {

      Iterator<DataStoreFactorySpi> it = DataStoreFinder.getAllDataStores();
      while (it.hasNext()) {
        DataStoreFactorySpi fac = it.next();
        System.out.println(fac.getDisplayName());
      }
  }

Another thing that can go wrong is the File to URL conversion, especially if there are spaces in the filename or path. Try using DataUtilities.fileToURL(file) instead.

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

This worked for me:

    // load shapefile ---- does not work !!!!!!!!
    connectParameters.put("url", file.toURI().toURL());
    connectParameters.put("create spatial index", Boolean.TRUE);
    ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
    ShapefileDataStore store = (ShapefileDataStore) dataStoreFactory.createNewDataStore(connectParameters);
    // 
hemisphire
  • 1,205
  • 9
  • 19