From a9a2d0f1d8d271985869dd2b26ac29217191ab51 Mon Sep 17 00:00:00 2001
From: zhongrj <646384940@qq.com>
Date: Tue, 10 Dec 2024 11:51:58 +0800
Subject: [PATCH] 应急空间新增字典,查询修改
---
src/main/java/org/springblade/common/utils/ShapeFileUtil.java | 268 +++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 259 insertions(+), 9 deletions(-)
diff --git a/src/main/java/org/springblade/common/utils/ShapeFileUtil.java b/src/main/java/org/springblade/common/utils/ShapeFileUtil.java
index ae81ded..59c6450 100644
--- a/src/main/java/org/springblade/common/utils/ShapeFileUtil.java
+++ b/src/main/java/org/springblade/common/utils/ShapeFileUtil.java
@@ -1,25 +1,242 @@
package org.springblade.common.utils;
import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.map.MapUtil;
+import cn.hutool.core.text.CharSequenceUtil;
import com.alibaba.fastjson.JSONObject;
-import org.geotools.data.DataStore;
-import org.geotools.data.DataStoreFinder;
-import org.geotools.data.FeatureSource;
+import org.geotools.data.*;
+import org.geotools.data.shapefile.ShapefileDataStore;
+import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
+import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geojson.feature.FeatureJSON;
-import org.locationtech.jts.geom.Geometry;
-import org.locationtech.jts.geom.Point;
+import org.geotools.referencing.CRS;
+import org.locationtech.jts.geom.*;
import org.locationtech.jts.io.ParseException;
+import org.locationtech.jts.io.WKTReader;
+import org.opengis.feature.GeometryAttribute;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
+import org.opengis.referencing.FactoryException;
+import org.opengis.referencing.crs.CoordinateReferenceSystem;
+import org.springblade.core.log.exception.ServiceException;
+
import java.io.File;
import java.io.IOException;
+import java.io.Serializable;
import java.io.StringWriter;
+import java.nio.charset.StandardCharsets;
import java.util.*;
public class ShapeFileUtil {
+
+ /**
+ * 导出shp文件
+ *
+ * @param dataPropertiesList 属性列表{属性名:属性值}
+ * @param fileName 导出shp文件名
+ * @param geomType geometry类型
+ * @param saveFolder 导出位置
+ */
+ public static void exportShp(List<Map<String, Object>> dataPropertiesList,
+ String fileName,
+ String geomType,
+ String saveFolder) {
+ String path = saveFolder + "/" + fileName;
+ //创建保存shp文件夹
+ File dir = new File(path);
+ if (!dir.exists()) {
+ FileUtil.mkdir(dir);
+ }
+ //shp文件路径
+ String shpFileName = fileName + ".shp";
+ String fileUrl = path+ "/" + shpFileName;
+ File file = new File(fileUrl);
+
+ FeatureWriter<SimpleFeatureType, SimpleFeature> writer = null;
+ ShapefileDataStore ds = null;
+ try {
+ Map<String, Serializable> params = new HashMap<>();
+ params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
+ ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
+
+ //定义图形信息和属性信息
+ SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
+ //设置坐标系
+ CoordinateReferenceSystem crs84 = CRS.decode("EPSG:4326", true);
+ tb.setCRS(crs84);
+ //设置文件名
+ tb.setName(fileName);
+
+ //定义导出shp文件地块属性名称
+ String geomProperty = "the_geom";
+ String idProperty = "ID";
+ String nameProperty = "name";
+ String firmIdProperty = "firmId";
+ String firmNameProperty = "firmName";
+
+ //设置图形类型
+ if ("Polygon".equals(geomType)) {
+ tb.add(geomProperty, Polygon.class);
+ } else if ("MultiPolygon".equals(geomType)) {
+ tb.add(geomProperty, MultiPolygon.class);
+ } else if ("Point".equals(geomType)) {
+ tb.add(geomProperty, Point.class);
+ } else if ("MultiPoint".equals(geomType)) {
+ tb.add(geomProperty, MultiPoint.class);
+ } else if ("LineString".equals(geomType)) {
+ tb.add(geomProperty, LineString.class);
+ } else if ("MultiLineString".equals(geomType)) {
+ tb.add(geomProperty, MultiLineString.class);
+ } else {
+ throw new ServiceException("Geometry中没有该类型:" + geomType);
+ }
+ //设置对应属性类型
+ tb.add(idProperty, String.class);
+ tb.add(nameProperty, String.class);
+ tb.add(firmIdProperty, String.class);
+ tb.add(firmNameProperty, String.class);
+
+ //设置默认geometry
+ tb.setDefaultGeometry(geomProperty);
+ //创建
+ ds.createSchema(tb.buildFeatureType());
+ ds.setCharset(StandardCharsets.UTF_8);
+
+ //设置Writer
+ writer = ds.getFeatureWriter(ds.getTypeNames()[0],
+ Transaction.AUTO_COMMIT);
+ SimpleFeature feature;
+ for (Map<String, Object> map : dataPropertiesList) {
+ feature = writer.next();
+ //属性赋值 geometry要赋值wkt格式的
+ if (map.containsKey("geometry")) {
+ feature.setAttribute(geomProperty, new WKTReader().read((MapUtil.getStr(map, "geometry"))));
+ }
+ feature.setAttribute(idProperty, MapUtil.getStr(map, idProperty));
+ feature.setAttribute(nameProperty, MapUtil.getStr(map, nameProperty));
+ feature.setAttribute(firmIdProperty, MapUtil.getStr(map, firmIdProperty));
+ feature.setAttribute(firmNameProperty, MapUtil.getStr(map, firmNameProperty));
+// String description = MapUtil.getStr(map, "描述");
+// if (CharSequenceUtil.isNotBlank(description)) {
+// feature.setAttribute(descriptionProperty, description);
+// }
+ }
+ writer.write();
+ } catch (IOException | FactoryException | ParseException e) {
+ e.printStackTrace();
+ } finally {
+ //关闭相关流
+ try {
+ if (writer != null) {
+ writer.close();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ if (ds != null) {
+ ds.dispose();
+ }
+ }
+ }
+
+ /**
+ * 导出shp文件
+ *
+ * @param map 属性列表{属性名:属性值}
+ * @param fileName 导出shp文件名
+ * @param geomType geometry类型
+ * @param saveFolder 导出位置
+ */
+ public static void exportShp(Map<String, Object> map,
+ String fileName,
+ String geomType,
+ String saveFolder) {
+ String path = saveFolder + "/" + fileName;
+ //创建保存shp文件夹
+ File dir = new File(path);
+ if (!dir.exists()) {
+ FileUtil.mkdir(dir);
+ }
+ //shp文件路径
+ String shpFileName = fileName + ".shp";
+ String fileUrl = path+ "/" + shpFileName;
+ File file = new File(fileUrl);
+
+ FeatureWriter<SimpleFeatureType, SimpleFeature> writer = null;
+ ShapefileDataStore ds = null;
+ try {
+ Map<String, Serializable> params = new HashMap<>();
+ params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
+ ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
+
+ //定义图形信息和属性信息
+ SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
+ //设置坐标系
+ CoordinateReferenceSystem crs84 = CRS.decode("EPSG:4326", true);
+ tb.setCRS(crs84);
+ //设置文件名
+ tb.setName(fileName);
+
+ //定义导出shp文件地块属性名称
+ String geomProperty = "the_geom";
+ String idProperty = "ID";
+ String nameProperty = "name";
+
+ //设置图形类型
+ if ("Polygon".equals(geomType)) {
+ tb.add(geomProperty, Polygon.class);
+ } else if ("MultiPolygon".equals(geomType)) {
+ tb.add(geomProperty, MultiPolygon.class);
+ } else if ("Point".equals(geomType)) {
+ tb.add(geomProperty, Point.class);
+ } else if ("MultiPoint".equals(geomType)) {
+ tb.add(geomProperty, MultiPoint.class);
+ } else if ("LineString".equals(geomType)) {
+ tb.add(geomProperty, LineString.class);
+ } else if ("MultiLineString".equals(geomType)) {
+ tb.add(geomProperty, MultiLineString.class);
+ } else {
+ throw new ServiceException("Geometry中没有该类型:" + geomType);
+ }
+ //设置对应属性类型
+ tb.add(idProperty, String.class);
+ tb.add(nameProperty, String.class);
+
+ //设置默认geometry
+ tb.setDefaultGeometry(geomProperty);
+ //创建
+ ds.createSchema(tb.buildFeatureType());
+ ds.setCharset(StandardCharsets.UTF_8);
+
+ //设置Writer
+ writer = ds.getFeatureWriter(ds.getTypeNames()[0],
+ Transaction.AUTO_COMMIT);
+ SimpleFeature feature;
+ feature = writer.next();
+ //属性赋值 geometry要赋值wkt格式的
+ feature.setAttribute(geomProperty, new WKTReader().read((MapUtil.getStr(map, "geometry"))));
+ feature.setAttribute(idProperty, MapUtil.getStr(map, idProperty));
+ feature.setAttribute(nameProperty, MapUtil.getStr(map, nameProperty));
+ writer.write();
+ } catch (IOException | FactoryException | ParseException e) {
+ e.printStackTrace();
+ } finally {
+ //关闭相关流
+ try {
+ if (writer != null) {
+ writer.close();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ if (ds != null) {
+ ds.dispose();
+ }
+ }
+ }
/**
* 将文件解压
@@ -84,13 +301,27 @@
featureJSON.writeFeature(feature, writer);
String temp = writer.toString();
JSONObject json = JSONObject.parseObject(temp);
-
+ Object value = feature.getDefaultGeometryProperty().getValue();
try {
// 空间坐标
- shpDTO.put("geometry",json.getJSONObject("geometry").getString("coordinates"));
+ shpDTO.put("geometry",value);
// 空间类型
if (json.getJSONObject("geometry").get("type") != null) {
- shpDTO.put("geoType",json.getJSONObject("geometry").getString("type"));
+ shpDTO.put("type",json.getJSONObject("geometry").getString("type"));
+ }
+ // 名称
+ if (json.getJSONObject("properties").get("Name")!= null) {
+ shpDTO.put("name",json.getJSONObject("properties").getString("Name"));
+ }
+ if (json.getJSONObject("properties").get("TYPE")!= null) {
+ shpDTO.put("name",json.getJSONObject("properties").getString("TYPE"));
+ }
+ // 所属公司名称
+ if (json.getJSONObject("properties").get("name_1")!= null) {
+ shpDTO.put("firmName",json.getJSONObject("properties").getString("name_1"));
+ }
+ if (json.getJSONObject("properties").get("QYMC")!= null) {
+ shpDTO.put("firmName",json.getJSONObject("properties").getString("QYMC"));
}
dtoList.add(shpDTO);
} catch (NumberFormatException e) {
@@ -109,9 +340,28 @@
public static void main(String[] args) throws IOException, ParseException {
- File file = new File("F:\\test4jdata\\shp\\zrq.zip");
+// File file = new File("F:\\test4jdata\\shp\\import\\zrq.zip");
+ File file = new File("F:\\test4jdata\\shp\\zip\\test.zip");
List<Map<String, Object>> list = shpToGeoJson(file);
System.out.println("list = " + list);
+
+ // 导出shp 文件
+// List<Map<String,Object>> propertyList = new ArrayList<>();
+// for (int i = 0; i < 3; i++) {
+// Map<String,Object> map = new HashMap<>();
+// map.put("ID", i);
+// map.put("名称", "test" + i);
+// map.put("描述", "测试shp导出" + i);
+// map.put("geometry", "MULTILINESTRING ((114.0888763800001 22.549298400000055, 114.0897166200001 22.54931240800005, 114.09006708000004 22.549318250000056, 114.09104754000009 22.549328150000065))");
+// propertyList.add(map);
+// }
+// String exportPath = "F:\\test4jdata\\shp\\export\\";
+// exportShp(propertyList, "test", "MultiLineString",exportPath);
+// // 打包成 zip 包,然后导出
+// String zipPath = "F:\\test4jdata\\shp\\zip\\test.zip";
+// boolean folder = ZipUtil.zipFolder(exportPath, zipPath);
+ // 打包成功后导出
+
}
public static double[] getLongitudeLatitude(Geometry wktPoint) throws ParseException {
--
Gitblit v1.9.3