无法使用GeoTools加载shapefile

时间:2017-04-27 07:40:53

标签: java geotools

我正在尝试使用GeoTools将shapefile加载到java中,然后检查一个点是否位于shape文件中的一个多边形内 问题是我无法加载shapefile,因此无法继续前进。 到目前为止,这是我的代码:

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();}
}

问题是我尝试加载shapefile后dataStore变量为空。

以下是我的导入:

    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;

有人能解释一下这个问题吗? 任何帮助,将不胜感激。 谢谢。

2 个答案:

答案 0 :(得分:0)

最可能的问题是您的路径上没有可用的Shapefile数据存储区实现。请尝试以下方法检查可用的商店:

public Map<String, DataStoreFactorySpi> fetchAvailableDataStores() {

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

可能出错的另一件事是文件到URL转换,尤其是在文件名或路径中有空格的情况下。请尝试使用DataUtilities.fileToURL(file)

答案 1 :(得分:0)

这对我有用:

    // 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);
    //