gpt4 book ai didi

java - 为什么这段代码如何使用 geotools 读取 shapefile 会抛出此异常?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:55:41 25 4
gpt4 key购买 nike

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

这是示例代码:

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

}

异常(exception):

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)

最佳答案

退出前必须执行dataStore.dispose();

关于java - 为什么这段代码如何使用 geotools 读取 shapefile 会抛出此异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19882341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com