|
@@ -1,6 +1,10 @@
|
|
|
package com.onemap.spotoverlap.utils;
|
|
|
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
+import com.alibaba.fastjson2.TypeReference;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.google.gson.Gson;
|
|
|
+import com.google.gson.reflect.TypeToken;
|
|
|
import org.geotools.data.DataStore;
|
|
|
import org.geotools.data.DataStoreFinder;
|
|
|
import org.geotools.data.FeatureWriter;
|
|
@@ -23,10 +27,13 @@ import org.opengis.referencing.FactoryException;
|
|
|
import org.opengis.referencing.crs.CoordinateReferenceSystem;
|
|
|
import org.opengis.referencing.operation.MathTransform;
|
|
|
import org.opengis.referencing.operation.TransformException;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.io.Serializable;
|
|
|
+import java.lang.reflect.Type;
|
|
|
import java.nio.charset.Charset;
|
|
|
import java.sql.Connection;
|
|
|
import java.sql.SQLException;
|
|
@@ -36,7 +43,19 @@ import java.util.HashMap;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+@Component
|
|
|
public class ShpToPostGISWithReproject {
|
|
|
+ private static final Gson gson = new Gson();
|
|
|
+ public static String dbconnection;
|
|
|
+
|
|
|
+ @Value("${dbconnection}")
|
|
|
+ public void setDbconnection(String dbconnection) {
|
|
|
+ ShpToPostGISWithReproject.dbconnection = dbconnection;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static final Type mapType =
|
|
|
+ new TypeToken<Map<String, Object>>() {
|
|
|
+ }.getType();
|
|
|
|
|
|
/**
|
|
|
* 将Shapefile导入PostGIS并转换到指定坐标系
|
|
@@ -82,16 +101,22 @@ public class ShpToPostGISWithReproject {
|
|
|
SimpleFeatureType targetSchema = typeBuilder.buildFeatureType();
|
|
|
|
|
|
// 4. 准备PostGIS数据存储连接参数
|
|
|
- Map<String, Serializable> params = new HashMap<>();
|
|
|
- params.put(PostgisNGDataStoreFactory.DBTYPE.key, "postgis");
|
|
|
- params.put(PostgisNGDataStoreFactory.HOST.key, "127.0.0.1");
|
|
|
- params.put(PostgisNGDataStoreFactory.PORT.key, 5432);
|
|
|
- params.put(PostgisNGDataStoreFactory.DATABASE.key, "spot");
|
|
|
- params.put(PostgisNGDataStoreFactory.SCHEMA.key, "public");
|
|
|
- params.put(PostgisNGDataStoreFactory.USER.key, "postgres");
|
|
|
- params.put(PostgisNGDataStoreFactory.PASSWD.key, "postgres");
|
|
|
-
|
|
|
- DataStore postgisDataStore = DataStoreFinder.getDataStore(params);
|
|
|
+// Map<String, Serializable> params = new HashMap<>();
|
|
|
+// params.put(PostgisNGDataStoreFactory.DBTYPE.key, "postgis");
|
|
|
+// params.put(PostgisNGDataStoreFactory.HOST.key, "127.0.0.1");
|
|
|
+// params.put(PostgisNGDataStoreFactory.PORT.key, 5432);
|
|
|
+// params.put(PostgisNGDataStoreFactory.DATABASE.key, "spot");
|
|
|
+// params.put(PostgisNGDataStoreFactory.SCHEMA.key, "public");
|
|
|
+// params.put(PostgisNGDataStoreFactory.USER.key, "postgres");
|
|
|
+// params.put(PostgisNGDataStoreFactory.PASSWD.key, "postgres");
|
|
|
+ Map<String, Object> dbconn = new HashMap<>();
|
|
|
+ try {
|
|
|
+ dbconn = gson.fromJson(dbconnection, mapType);
|
|
|
+ dbconn.put("port", Integer.valueOf(String.valueOf(dbconn.get("port")).replaceAll(".0", "")));
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ DataStore postgisDataStore = DataStoreFinder.getDataStore(dbconn);
|
|
|
if (postgisDataStore == null) {
|
|
|
throw new IOException("无法连接到PostGIS数据库,请检查连接参数");
|
|
|
}
|
|
@@ -157,8 +182,8 @@ public class ShpToPostGISWithReproject {
|
|
|
// 将JSONObject转换为Map
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
Iterator<?> keys = (Iterator<?>) jsonObject.keySet();
|
|
|
- while(keys.hasNext()) {
|
|
|
- String key = (String)keys.next();
|
|
|
+ while (keys.hasNext()) {
|
|
|
+ String key = (String) keys.next();
|
|
|
map.put(key, jsonObject.get(key));
|
|
|
}
|
|
|
return map;
|
|
@@ -190,7 +215,6 @@ public class ShpToPostGISWithReproject {
|
|
|
// SimpleFeatureType schema = dataStore.getSchema(tableName);
|
|
|
// return schema.getGeometryDescriptor().getLocalName();
|
|
|
// }
|
|
|
-
|
|
|
public static void main(String[] args) {
|
|
|
try {
|
|
|
// 示例用法
|