From 93264081cc9cbdf829d885d898cb06f9af2ad910 Mon Sep 17 00:00:00 2001
From: zhongrj <646384940@qq.com>
Date: Mon, 10 Feb 2025 18:14:17 +0800
Subject: [PATCH] 增加空间分析接口,增加geojson文件读取导入

---
 src/main/java/org/springblade/modules/yw/service/impl/GeomInfoServiceImpl.java |   58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 56 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/springblade/modules/yw/service/impl/GeomInfoServiceImpl.java b/src/main/java/org/springblade/modules/yw/service/impl/GeomInfoServiceImpl.java
index f3a28c9..497881c 100644
--- a/src/main/java/org/springblade/modules/yw/service/impl/GeomInfoServiceImpl.java
+++ b/src/main/java/org/springblade/modules/yw/service/impl/GeomInfoServiceImpl.java
@@ -1,9 +1,17 @@
 package org.springblade.modules.yw.service.impl;
 
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.apache.logging.log4j.util.Strings;
+import org.geotools.feature.FeatureCollection;
+import org.geotools.feature.FeatureIterator;
+import org.geotools.geojson.feature.FeatureJSON;
+import org.geotools.geojson.geom.GeometryJSON;
+import org.locationtech.jts.geom.Geometry;
+import org.opengis.feature.simple.SimpleFeature;
 import org.springblade.common.utils.FileUtil;
 import org.springblade.common.utils.ShapeFileUtil;
 import org.springblade.core.tool.api.R;
@@ -17,7 +25,8 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
-import java.io.File;
+
+import java.io.*;
 import java.util.*;
 
 /**
@@ -80,7 +89,6 @@
 	/**
 	 * 导出 空间信息shp zip 包
 	 * @param ids
-	 * @param response
 	 * @return
 	 */
 	@Override
@@ -167,4 +175,50 @@
 		}
 		return null;
 	}
+
+	/**
+	 * 导入 geojson 文件解析空间信息
+	 * @param multipartFile
+	 * @return
+	 */
+	@Override
+	public R importGeoJson(MultipartFile multipartFile) {
+		// 转 file
+		File file = FileUtil.toFile(multipartFile);
+
+		FeatureJSON featureJSON = new FeatureJSON();
+		try {
+			FeatureCollection featureCollection = featureJSON.readFeatureCollection(file);
+			FeatureIterator iterator = featureCollection.features();
+			// 遍历feature转为json对象
+			while (iterator.hasNext()) {
+				SimpleFeature feature = (SimpleFeature) iterator.next();
+				StringWriter writer = new StringWriter();
+				featureJSON.writeFeature(feature, writer);
+				String temp = writer.toString();
+				JSONObject json = JSONObject.parseObject(temp);
+				Object value = feature.getDefaultGeometryProperty().getValue();
+				System.out.println("value = " + value);
+				try {
+					GeomInfoEntity geomInfoEntity = new GeomInfoEntity();
+					// 空间坐标
+					geomInfoEntity.setGeom("'" + value + "'");
+					// 空间类型
+					if (json.getJSONObject("geometry").get("type") != null) {
+						geomInfoEntity.setType(json.getJSONObject("geometry").getString("type"));
+					}
+					// 保存
+					baseMapper.saveGeoInfo(geomInfoEntity);
+				} catch (NumberFormatException e) {
+					log.error("NumberFormatException: {}", e);
+				} catch (Exception e) {
+					log.error("Exception: {}", e);
+				}
+			}
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		// 返回
+		return R.data(200,"","操作成功!");
+	}
 }

--
Gitblit v1.9.3