解析shape到数据库中(推荐使用解析shape到数据库中)
第五章 Java代码解析shape压缩包上传-CSDN博客
1. 依赖
<repositories>
<repository>
<id>central</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/central</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
<repository>
<id>osgeo-releases</id>
<name>OSGeo Nexus 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-snapshots</id>
<name>OSGeo Nexus Snapshot Repository</name>
<url>https://repo.osgeo.org/repository/snapshot/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
<repository>
<id>geosolutions</id>
<name>geosolutions repository</name>
<url>https://maven.geo-solutions.it/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>23.0</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>23.0</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>23.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
2. 第一种解析shape
2.1 代码
public static Object read(String path) throws IOException {
/*
参数path就是shp文件的完整路径,如:E:\\蟠桃会资源清查\\调查图斑.shp
系统会自动检查同一个目录下有没有其他相关文件,有的话会一并读出,
相关文件的路径无须给出
.shp 存储地理形状和位置信息
.dbf 存储属性信息
.shx 索引文件
.prj 坐标系
.cpg 字符编码,如UTF-8
读取出来的结果类型为 List<Map<String, Object>>
*/
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
File file = getFile(path);
if (file == null) {
return list;
}
String charset = getCharSet(path);
FileDataStore store = FileDataStoreFinder.getDataStore(file);
((ShapefileDataStore)store).setCharset(Charset.forName(charset));
SimpleFeatureSource featureSource = store.getFeatureSource();
SimpleFeatureCollection collection = featureSource.getFeatures();
SimpleFeatureIterator features = collection.features();
while (features.hasNext()) {
Map<String, Object> item = new HashMap<String, Object>();
SimpleFeature f = features.next();
Collection<Property> p = f.getProperties();
Iterator<Property> it = p.iterator();
System.out.println("-----------------");
System.out.println(f.getType());
System.out.println("-----------------");
;
while (it.hasNext()) {
Property pro = it.next();
String field = pro.getName().toString();
field = field.equals("the_geom") ? "wkt" : field;
String value = pro.getValue().toString();
item.put(field, value);
}
list.add(item);
}
return list;
}
private static File getFile(String path){
File file = new File(path);
if (file == null) {
System.out.println("找不到路径:" + path);
}
return file;
}
/*
获取shapefile字符编码
如果存在.cpg文件,则从中读取,否则默认为UTF-8
*/
private static String getCharSet(String path){
String charset = "UTF-8";
int p = path.lastIndexOf(".");
String cpg = path.substring(0,p) + ".cpg";
File file = getFile(cpg);
if(file != null) {
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(cpg, "r");
charset = raf.readLine();
raf.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return charset;
}
2.2测试
@Test
public void readSha() throws IOException {
List<Map<String, Object>> list = (List<Map<String, Object>>) demo03.read("D:/Tomcat-9.0.37/webapps/hjxzxmdc/shape02/1area_yuanshi.shp");// ShapeFile全路径
//从shape中拿数据
for (Map<String, Object> listMap : list) {
String wktStr = (String) listMap.get("wkt");
System.out.println(wktStr);
}
System.out.println(list);
}
3. 第二种解析shape
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
String SHAPE_FILE = "D:\\3project\\2ak_tzhb\\2data\\shape02\\1area_yuanshi.shp"; // ShapeFile全路径
// 使用GeoTools读取ShapeFile文件
File shapeFile = new File(SHAPE_FILE);
ShapefileDataStore store = new ShapefileDataStore(shapeFile.toURI().toURL());
//设置编码
Charset charset = Charset.forName("UTF-8");
store.setCharset(charset);
SimpleFeatureSource sfSource = store.getFeatureSource();
SimpleFeatureIterator sfIter = sfSource.getFeatures().features();
// 从ShapeFile文件中遍历每一个Feature,然后将Feature转为GeoJSON字符串
while (sfIter.hasNext()) {
SimpleFeature feature = (SimpleFeature) sfIter.next();
// Feature转GeoJSON
FeatureJSON fjson = new FeatureJSON();
StringWriter writer = new StringWriter();
fjson.writeFeature(feature, writer);
String sjson = writer.toString();
System.out.println("sjson===== >>>> " + sjson);
}
System.out.println("数据导入完成,共耗时"+(System.currentTimeMillis() - start)+"ms");
}