使用 geoTools 读取 Java (.dbf) 中的 ESRI Shapefile
Read ESRI Shapefile in Java (.dbf) using geoTools
我试图从 java 中的 esri shapefile 中获取属性 table,但我只能使用 Geotools 库获取 header table?
这是我的源代码:
File dbfFile = new File("C:/Users/ilyasse2.0/Desktop/shapefiles/marocShp/mar_admbndp_admALL_unhcr_itos_20201203.dbf");
FileInputStream fis = new FileInputStream(dbfFile);
DbaseFileReader dbfReader = new DbaseFileReader(fis.getChannel(), false, Charset.forName("ISO-8859-1"));
DbaseFileHeader dbfHeader = dbfReader.getHeader();
System.out.println(dbfHeader.getRecordLength());
List<String> names = new Vector<String>();
int n = dbfHeader.getNumFields();
for (int i = 0; i < n; i++) names.add(dbfHeader.getFieldName(i));
System.out.println(names);
您需要使用 ShapefileDataStore
来读取 Shapefile,然后您可以遍历特征并提取属性。注意:该文件应以 .shp
结尾,GeoTools 将找到其他必要的文件。
List attributes = new ArrayList<>();
FileDataStore store = FileDataStoreFinder.getDataStore(file);
SimpleFeatureSource source = store.getFeatureSource();
FeatureType schema = source.getSchema();
try(SimpleFeatureCollection features = source.getFeatures();SimpleFeatureIterator iterator = featureCollection.features()) {
while (iterator.hasNext()) {
// copy the contents of each feature and transform the geometry
SimpleFeature feature = iterator.next();
attributes.add(feature.getAttributes());
}
}
我试图从 java 中的 esri shapefile 中获取属性 table,但我只能使用 Geotools 库获取 header table? 这是我的源代码:
File dbfFile = new File("C:/Users/ilyasse2.0/Desktop/shapefiles/marocShp/mar_admbndp_admALL_unhcr_itos_20201203.dbf");
FileInputStream fis = new FileInputStream(dbfFile);
DbaseFileReader dbfReader = new DbaseFileReader(fis.getChannel(), false, Charset.forName("ISO-8859-1"));
DbaseFileHeader dbfHeader = dbfReader.getHeader();
System.out.println(dbfHeader.getRecordLength());
List<String> names = new Vector<String>();
int n = dbfHeader.getNumFields();
for (int i = 0; i < n; i++) names.add(dbfHeader.getFieldName(i));
System.out.println(names);
您需要使用 ShapefileDataStore
来读取 Shapefile,然后您可以遍历特征并提取属性。注意:该文件应以 .shp
结尾,GeoTools 将找到其他必要的文件。
List attributes = new ArrayList<>();
FileDataStore store = FileDataStoreFinder.getDataStore(file);
SimpleFeatureSource source = store.getFeatureSource();
FeatureType schema = source.getSchema();
try(SimpleFeatureCollection features = source.getFeatures();SimpleFeatureIterator iterator = featureCollection.features()) {
while (iterator.hasNext()) {
// copy the contents of each feature and transform the geometry
SimpleFeature feature = iterator.next();
attributes.add(feature.getAttributes());
}
}