首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么这段代码--如何使用geotools读取一个shapefile --抛出这个异常?

为什么这段代码--如何使用geotools读取一个shapefile --抛出这个异常?
EN

Stack Overflow用户
提问于 2013-11-09 19:54:36
回答 1查看 4.5K关注 0票数 7

我正在使用geotools 10.1从一个shapefile中读取属性。我不明白为什么在打印所有特性属性之后抛出异常。

以下是示例代码:

代码语言:javascript
复制
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
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.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.opengis.feature.simple.SimpleFeature;

public class LayerBusinessTest {


public static void main(String[] args) throws IOException {

    File file = new File("../../setup/test/shp/sscc/SSCC2010_WGS84.shp");
    Map<String, Serializable> map = new HashMap<>();
    map.put( "url", file.toURI().toURL() );

    DataStore dataStore = DataStoreFinder.getDataStore( map );
    String typeName = dataStore.getTypeNames()[0];

    FeatureSource source = dataStore.getFeatureSource( typeName );

    FeatureCollection collection =  source.getFeatures();
    FeatureIterator<SimpleFeature> results = collection.features();
    try {
        while (results.hasNext()) {
            SimpleFeature feature = (SimpleFeature) results.next();
            String code = feature.getAttribute("Codigo_SSC").toString();
            System.out.println( code );
        }
    } finally {
        results.close();
    }

}

}

例外:

代码语言:javascript
复制
Exception in thread "main" java.lang.IllegalArgumentException: Expected requestor org.geotools.data.shapefile.dbf.DbaseFileReader@2ac9b619 to have locked the url but it does not hold the lock for the URL
    at org.geotools.data.shapefile.files.ShpFiles.unlockRead(ShpFiles.java:429)
    at org.geotools.data.shapefile.files.FileChannelDecorator.implCloseChannel(FileChannelDecorator.java:149)
    at java.nio.channels.spi.AbstractInterruptibleChannel.close(AbstractInterruptibleChannel.java:115)
    at org.geotools.data.shapefile.dbf.DbaseFileReader.close(DbaseFileReader.java:279)
    at org.geotools.data.shapefile.ShapefileFeatureReader.close(ShapefileFeatureReader.java:248)
    at org.geotools.data.store.ContentFeatureCollection$WrappingFeatureIterator.close(ContentFeatureCollection.java:154)
    at LayerBusinessTest.main(LayerBusinessTest.java:39)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-09 19:58:44

在退出前执行dataStore.dispose();是必要的。

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19882341

复制
相关文章

相似问题

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