目录
一.GeoTools介绍
GeoTools是一套操作和显示地图的开源Java 代码库,提供了一系列符合规范的操作地理空间数据的方法,GeoTools被许多项目使用,在地理信息领域有着广泛的应用。
二.背景
在后端GIS开发中经常需要读取shp数据,本文介绍使用geotools读取shp的几何类型以及数据的方法。
三.实现代码
这里编写了一个读取shp几何类型的getShpGeomType()方法和读取shp字段属性的getShpFieldMap()方法。
/** * 获取shp几何类型 * * @param dataStore shp文件数据存储 * @return * @throws IOException */ public static String getShpGeomType(ShapefileDataStore dataStore) throws IOException { if (dataStore == null) { return "empty"; } String type = dataStore.getSchema().getGeometryDescriptor().getType().getName().getLocalPart(); if (StringUtil.isEmpty(type)) { type = "empty"; } return type; } /** * 获取shp字段map * * @param dataStore shp文件数据存储 * @return key--字段名,value--字段类型 * @throws IOException */ public static Map<String, String> getShpFieldMap(ShapefileDataStore dataStore) throws IOException { List<AttributeDescriptor> attributeDescriptors = dataStore.getSchema().getAttributeDescriptors(); Map<String, String> map = new HashMap<>(); for (AttributeDescriptor attributeDescriptor : attributeDescriptors) { String key = attributeDescriptor.getLocalName(); String value = attributeDescriptor.getType().getBinding().getName(); map.put(key, value); } return map; }