必须的文件:
-
.shp— 图形格式,用于保存元素的几何实体。
-
.shx— 图形索引格式。几何体位置索引,记录每一个几何体在shp文件之中的位置,能够加快向前或向后搜索一个几何体的效率。
-
.dbf— 属性数据格式,以dBase IV的数据表格式存储每个几何形状的属性数据。
其他可选的文件:
-
.prj— 投帧式,用于保存地理坐标系统与投影信息,是一个存储well-known text投影描述符的文本文件。
-
.sbnand.sbx— 几何体的空间索引
-
.fbnand.fbx— 只读的Shapefiles的几何体的空间索引
-
.ainand.aih— 列表中活动字段的属性索引。
-
.ixs— 可读写Shapefile文件的地理编码索引
-
.mxs— 可读写Shapefile文件的地理编码索引(ODB格式)
-
.atx—.dbf文件的属性索引,其文件名格式为shapefile.columnname.atx(ArcGIS 8及之后的版本)
-
.shp.xml— 以XML格式保存元数据。
将shapefile文件放进一个文件夹
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>19.1</version>
</dependency>
方法奉上,该方法可指定要获取的字段
参数filePath为shapefile文件路径,任意文件均可
参数mapping为要查询的字段,key为字段别名,value为查询的字段
public static List<Map<String, Object>> getShpData(String filePath, Map<String, String> mapping) throws IOException {
File f = new File(filePath);
ShapefileDataStore shpDataStore = new ShapefileDataStore(f.toURI().toURL());
// shpDataStore.setCharset(StandardCharsets.UTF_8);
shpDataStore.setCharset(Charset.forName("GBK"));
String typeName = shpDataStore.getTypeNames()[0];
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = shpDataStore.getFeatureSource(typeName);
FeatureCollection<SimpleFeatureType, SimpleFeature> features = featureSource.getFeatures();
List<Map<String, Object>> data = new ArrayList<>();
try (FeatureIterator<SimpleFeature> itertor = features.features()) {
while (itertor.hasNext()) {
SimpleFeature feature = itertor.next();
Geometry geometry = (Geometry) feature.getDefaultGeometry();
String json = CoordinateConversion.geometryToGeojson(geometry);
Map<String, Object> o = new HashMap<>();
o.put("geom", GsonUtil.GsonToMaps(json));
for (Map.Entry<String, String> entry : mapping.entrySet()) {
String k = entry.getKey();
String v = entry.getValue();
o.put(k, feature.getAttribute(v));
}
data.add(o);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
shpDataStore.dispose();
}
return data;
}
但有的时候我们不知道都有哪些字段,我想获取所有字段,那就参考下面的方法
public static Map<String, List<Map<String, Object>>> readSHP(File SHPFile) throws Exception {
// 一个数据存储实现,允许从Shapefiles读取和写入
ShapefileDataStore shpDataStore = null;
System.out.println();
shpDataStore = new ShapefileDataStore(SHPFile.toURI().toURL());
shpDataStore.setCharset(Charset.forName("GBK"));
// 获取这个数据存储保存的类型名称数组
// getTypeNames:获取所有地理图层
String typeName = shpDataStore.getTypeNames()[0];
// 通过此接口可以引用单个shapefile、数据库表等。与数据存储进行比较和约束
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = null;
featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) shpDataStore.getFeatureSource(typeName);
// 一个用于处理FeatureCollection的实用工具类。提供一个获取FeatureCollection实例的机制
FeatureCollection<SimpleFeatureType, SimpleFeature> result = featureSource.getFeatures();
FeatureIterator<SimpleFeature> iterator = result.features();
// 迭代
int stop = 0;
Map<String, List<Map<String, Object>>> map = new HashMap<>();
List<Map<String, Object>> entity = new ArrayList<>();
List<Map<String, Object>> datas = new ArrayList<>();
while (iterator.hasNext()) {
SimpleFeature feature = iterator.next();
Collection<Property> p = feature.getProperties();
Iterator<Property> it = p.iterator();
// 构建实体
// 特征里面的属性再迭代,属性里面有字段
String name;
Map<String, Object> data = new HashMap<>();
while (it.hasNext()) {
Property pro = it.next();
name = pro.getName().toString();
if (stop == 0) {
Map<String, Object> et = new HashMap<>();
PropertyType propertyType = pro.getType();
Class cls = propertyType.getBinding();
String className = cls.getName();
String tName = className.substring(className.lastIndexOf(".") + 1);
Filter filter = propertyType.getRestrictions().isEmpty() ? null : propertyType.getRestrictions().get(0);
String typeLength = filter != null ? filter.toString() : "0";
Pattern pattern = Pattern.compile("[^0-9]");
Matcher matcher = pattern.matcher(typeLength);
String tLength = matcher.replaceAll("").trim();
et.put("name", name);
et.put("type", tName);
et.put("length", tLength);
entity.add(et);
}
data.put(name, pro.getValue().toString());
} // end 里层while
datas.add(data);
stop++;
} // end 最外层 while
map.put("entity", entity);
map.put("datas", datas);
iterator.close();
return map;
}