日常GIS开发中难免会将一些shapefile文件数据读取存入数据库中,很多人第一想到的就是使用GeoTools操作和显示地图的开源Java 代码库。
生活捉弄人,因为项目需要对于从来没有做过GIS相关项目的我,突然接到了一个地图数据处理的需求,没办法只能临时抱佛脚各种百度/Google,完成需求后记录一个感觉今后还能用到的GeoTools读取shapefile文件的小工具。*小工具写的不好,无意路过的大佬勿喷~~~~*
import org.geotools.data.*;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.PropertyType;
import org.opengis.filter.Filter;
import java.io.File;
import java.nio.charset.Charset;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* create by: REID
* description: GeoTools工具类
* create time: 8:45 2020/4/17
*/
public class GeoToolsUtils {
/**
* 读取Shapefiles文件表内容和对应表数据
* @param SHPFile Shapefiles文件
* @return Map<(entity/datas), List(对应map数据)>
*/
public static Map<String, List> readSHP(File SHPFile) throws Exception {
// 一个数据存储实现,允许从Shapefiles读取和写入
ShapefileDataStore shpDataStore = null;
shpDataStore = new ShapefileDataStore(SHPFile.toURI().toURL());
shpDataStore.setCharset(Charset.forName("UTF-8"));
// 获取这个数据存储保存的类型名称数组
// 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 = new HashMap<>();
List<Map> entity = new ArrayList<>();
List<Map> 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;
}
/**
* 读取Shapefiles文件表内容不包含内容数据
* @param SHPFile Shapefiles文件
* @return List<(Map), List(对应map数据)>
*/
public static List readSHPNoData(File SHPFile) throws Exception {
// 一个数据存储实现,允许从Shapefiles读取和写入
ShapefileDataStore shpDataStore = null;
shpDataStore = new ShapefileDataStore(SHPFile.toURI().toURL());
shpDataStore.setCharset(Charset.forName("UTF-8"));
// 获取这个数据存储保存的类型名称数组
// 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;
List<Map> entity = 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
stop++;
} // end 最外层 while
iterator.close();
return entity;
}
}
相关依赖
<!-- 添加GeoTools依赖 -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-swing</artifactId>
<version>${geotools.version}</version>
</dependency>
如果相关依赖包无法下载,请将Maven配置文件修改为如下就ok了。
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*,!getui-nexus,!osgeo</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
或者直接在pom.xml文件中添加:
<repositories>
<!-- GeoServer -->
<repository>
<id>GeoSolutions</id>
<url>http://maven.geo-solutions.it/</url>
</repository>
<!-- GeoTools -->
<repository>
<id>osgeo</id>
<name>OSGeo Release Repository</name>
<url>https://repo.osgeo.org/repository/release/</url>
<snapshots><enabled>false</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</repository>
<repository>
<id>osgeo-snapshot</id>
<name>OSGeo Snapshot Repository</name>
<url>https://repo.osgeo.org/repository/snapshot/</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>false</enabled></releases>
</repository>
<!-- Activiti -->
<repository>
<id>alfresco</id>
<name>Activiti Releases</name>
<url>https://artifacts.alfresco.com/nexus/content/repositories/activiti-releases/</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>