首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法使用GeoTools加载shapefile

无法使用GeoTools加载shapefile
EN

Stack Overflow用户
提问于 2017-04-27 15:40:53
回答 2查看 245关注 0票数 1

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

代码语言:javascript
运行
复制
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变量是空的。

以下是我的导入内容:

代码语言:javascript
运行
复制
    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;

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

EN

回答 2

Stack Overflow用户

发布于 2017-04-28 16:34:43

最有可能的问题是您的路径上没有可用的Shapefile Datastore实现。尝试以下方法来检查哪些商店可用:

代码语言:javascript
运行
复制
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)

票数 0
EN

Stack Overflow用户

发布于 2017-05-12 02:10:15

这对我很有效:

代码语言:javascript
运行
复制
    // 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);
    // 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43651504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档