Administrator il y a 2 ans
Parent
commit
faa0c7d6c0

+ 11 - 0
src/main/java/com/sysu/admin/controller/crop/CropLandService.java

@@ -15,6 +15,7 @@ import com.sysu.admin.support.system.config.ConfigContext;
 import com.sysu.admin.support.system.user.User;
 import com.sysu.admin.utils.file.FileUtil;
 import com.sysu.admin.utils.shape.GeoCastUtil;
+import com.sysu.admin.utils.shape.ShapeReader;
 import com.xiesx.fastboot.base.result.BaseResult;
 import com.xiesx.fastboot.core.jpa.JpaPlusRepository;
 import org.apache.commons.codec.binary.Base64;
@@ -26,10 +27,12 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import javax.transaction.Transactional;
+import java.io.File;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.nio.file.StandardOpenOption;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 
@@ -151,6 +154,14 @@ public class CropLandService extends BaseService<CropLand, Long> {
         return newFileName;
     }
 
+
+
+
+    @Transactional
+    public void exec(StringBuffer sql){
+            mEntityManager.createNativeQuery(sql.toString()).executeUpdate();
+    }
+
     @Override
     public JpaPlusRepository<CropLand, Long> r() {
         return mCropLandRepository;

+ 1 - 1
src/main/java/com/sysu/admin/utils/shape/CoordinateConversion.java

@@ -64,7 +64,7 @@ public class CoordinateConversion {
                 mercatorToLonLat(coordinate);
             }
             return geometry;
-        }, false);
+        }, false, null);
 
         postGisUtil.closeConnection();
         postGisUtil.dispose();

+ 42 - 55
src/main/java/com/sysu/admin/utils/shape/ShapeReader.java

@@ -4,7 +4,7 @@ import org.geotools.data.shapefile.ShapefileDataStore;
 import org.geotools.data.store.ContentFeatureCollection;
 import org.geotools.data.store.ContentFeatureSource;
 import org.geotools.feature.FeatureIterator;
-import org.locationtech.jts.geom.MultiLineString;
+import org.locationtech.jts.geom.Geometry;
 import org.opengis.feature.simple.SimpleFeature;
 
 import java.io.File;
@@ -19,49 +19,47 @@ import java.util.*;
  **/
 public class ShapeReader {
  
-    public ArrayList<Map> readShapeFile(String filePath) {
-        ArrayList<Map> modelList = new ArrayList<>();
-        File folder = new File(filePath);
-        if (!folder.isDirectory()) {
-            if (folder.toString().endsWith(".shp")) {
-                try {
-                    modelList = getShapeFile(folder);
-                    return modelList;
-                } catch (Exception e) {
-                    e.printStackTrace();
-                }
-            } else {
-                System.out.println("选择的文件后缀名不是.shp");
-            }
-        }else{
-	        File[] files = folder.listFiles();
-	        if (!(files.length > 0)) {
-	            System.out.println("目录文件为空");
-	            return modelList;
-	        }
-	
-	        for (File file : files) {
-	            if (!file.toString().endsWith(".shp")) {
-	                continue;
-	            }
-	            try {
-	                ArrayList<Map> models = getShapeFile(file);
-	                modelList.addAll(models);
-	            } catch (Exception e) {
-	                e.printStackTrace();
-	            }
-	        }
-        }
-        return modelList;
-    }
+//    public ArrayList<Map> readShapeFile(String filePath) {
+//        ArrayList<Map> modelList = new ArrayList<>();
+//        File folder = new File(filePath);
+//        if (!folder.isDirectory()) {
+//            if (folder.toString().endsWith(".shp")) {
+//                try {
+//                    modelList = getShapeFile(folder);
+//                    return modelList;
+//                } catch (Exception e) {
+//                    e.printStackTrace();
+//                }
+//            } else {
+//                System.out.println("选择的文件后缀名不是.shp");
+//            }
+//        }else{
+//	        File[] files = folder.listFiles();
+//	        if (!(files.length > 0)) {
+//	            System.out.println("目录文件为空");
+//	            return modelList;
+//	        }
+//
+//	        for (File file : files) {
+//	            if (!file.toString().endsWith(".shp")) {
+//	                continue;
+//	            }
+//	            try {
+//	                ArrayList<Map> models = getShapeFile(file);
+//	                modelList.addAll(models);
+//	            } catch (Exception e) {
+//	                e.printStackTrace();
+//	            }
+//	        }
+//        }
+//        return modelList;
+//    }
  
-    private ArrayList<Map> getShapeFile(File file) throws Exception {
+    public static List<String> getShapeFile(File file) throws Exception {
  
-        Map<String, Object> map = new HashMap<String, Object>();
-        map.put("url", file.toURI().toURL());
-        ArrayList<Map> models = new ArrayList<>();
-        ShapefileDataStore shpDataStore = null;
+        List<String> list = new ArrayList<>();
 
+        ShapefileDataStore shpDataStore = null;
         shpDataStore = new ShapefileDataStore(file.toURL());
         //设置编码
         Charset charset = Charset.forName("GBK");
@@ -73,22 +71,11 @@ public class ShapeReader {
         FeatureIterator<SimpleFeature> itertor = result.features();
         while (itertor.hasNext()) {
             SimpleFeature feature = itertor.next();
-            Map<String, String> model = new HashMap<>();
-            MultiLineString multiLineString = (MultiLineString) feature.getDefaultGeometry();
-            System.out.println(multiLineString.toText());
-            models.add(model);
+            list.add(GeoCastUtil.geomToWkt((Geometry)feature.getDefaultGeometry()));
         }
-        return models;
+        return list;
     }
 
-    public static void main(String[] args) {
-        //此处传值既可以是一个目录也可以是一个以.shp结尾的文件,如果是目录的话,会遍历读取所有的以.shp结尾的文件
-        String filePath = "E:\\zcjy\\一级河流hyd1_4m\\hyd1_4l.shp";
-        ShapeReader shapeReader = new ShapeReader();
-        List<Map> shapeModels =  shapeReader.readShapeFile(filePath);
-        for(Map shapeModel : shapeModels){
-            System.out.println(shapeModel.toString());
-        }
-    }
+
  
 }

+ 13 - 8
src/main/java/com/sysu/admin/utils/shape/postgis/PostGisUtil.java

@@ -7,15 +7,13 @@ import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 import com.sysu.admin.utils.shape.FileFormat;
 import com.sysu.admin.utils.shape.SrsCastListener;
 import com.sysu.admin.utils.shape.vo.Param;
 import com.sysu.admin.utils.shape.vo.ParamType;
+import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.geotools.data.*;
 import org.geotools.data.shapefile.ShapefileDataStore;
@@ -168,10 +166,10 @@ public class PostGisUtil {
     }
 
     public boolean insertShp(String typeName, File shpFile){
-        return insertShp(typeName, shpFile, null, true);
+        return insertShp(typeName, shpFile, null, true, null);
     }
 
-    public boolean insertShp(String typeName, File shpFile, SrsCastListener srsCastListener, boolean extAttr){
+    public boolean insertShp(String typeName, File shpFile, SrsCastListener srsCastListener, boolean extAttr, String[] notIgnore){
         Transaction t = new DefaultTransaction("handle");
         Connection tempConnection = null;
         Connection connection = null;
@@ -189,9 +187,16 @@ public class PostGisUtil {
             {
                 SimpleFeature sourceFeature = itertor.next();
                 SimpleFeature targetFeature = writer.next();
-                for(AttributeType attributeType : attributeTypes){
-                    targetFeature.setAttribute(attributeType.getName(), sourceFeature.getAttribute(attributeType.getName()));
+                if(ObjectUtils.isNotEmpty(notIgnore)){
+                    for(String name : notIgnore) {
+                        targetFeature.setAttribute(name, sourceFeature.getAttribute(name));
+                    }
+                }else{
+                    for(AttributeType attributeType : attributeTypes){
+                        targetFeature.setAttribute(attributeType.getName(), sourceFeature.getAttribute(attributeType.getName()));
+                    }
                 }
+
                 if(extAttr) {
                     targetFeature.setAttribute("objectid", sourceFeature.getAttribute("OBJECTID"));
                     targetFeature.setAttribute("shape_leng", sourceFeature.getAttribute("Shape_Leng"));

+ 2 - 2
src/main/resources/application-dev.yml

@@ -12,8 +12,8 @@ spring:
     database-platform: org.hibernate.spatial.dialect.postgis.PostgisDialect
   datasource:
     driver-class-name: org.postgis.DriverWrapper
-#    url: jdbc:postgresql_postGIS://101.35.200.5:39543/sysu-emap
-    url: jdbc:postgresql_postGIS://127.0.0.1:5432/sysu-emap
+    url: jdbc:postgresql_postGIS://101.35.200.5:39543/sysu-emap
+#    url: jdbc:postgresql_postGIS://127.0.0.1:5432/sysu-emap
     username: postgres
     password: postgres
     pool-name: HikariPool-1

+ 1 - 1
src/main/resources/application.yml

@@ -7,7 +7,7 @@ jsp-servlet:
 spring:
   profiles:
     # 激活:本地 dev、后台 adm。服务api
-    active: adm
+    active: dev
   mvc:
     view:
       prefix: /WEB-INF/jsp/

+ 1 - 1
src/main/webapp/WEB-INF/jsp/comm/admin2.jsp

@@ -159,7 +159,7 @@
                     <%--{ id:32, pId:3, status:2, name:"已接受的",icon :"${base}/static/images/cloudy20x20.png", Action:TaskLandAction},--%>
                     <%--{ id:33, pId:3, status:3, name:"已完成的",icon :"${base}/static/images/cloudy20x20.png", Action:TaskLandAction},--%>
                     { id:34, pId:0, name:"批量发布",icon :"${base}/static/images/cloudy20x20.png", Action: PublishTaskAction},
-                    <%--{ id:4, pId:0, name:"产量预估",icon :"${base}/static/images/cloudy20x20.png"},--%>
+                    { id:4, pId:0, name:"批量下载",icon :"${base}/static/images/cloudy20x20.png", Action: PublishTaskAction},
                     <%--{ id:5, pId:0, name:"农业灾害风险",icon :"${base}/static/images/cloudy20x20.png"},--%>
                     <%--{ id:6, pId:0, name:"土壤要素",icon :"${base}/static/images/cloudy20x20.png"},--%>
                     <%--{ id:7, pId:0, name:"适应性评估",icon :"${base}/static/images/cloudy20x20.png"}--%>

+ 14 - 0
src/test/java/com/sysu/admin/utils/shape/postgis/Main.java

@@ -0,0 +1,14 @@
+package com.sysu.admin.utils.shape.postgis;
+
+public class Main {
+    public static void main(String[] args) {
+//        PostGisUtilTest.testInsertShp();
+//        try {
+//            PostGisUtilTest.createTable();
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//        }
+
+        PostGisUtilTest.update();
+    }
+}

+ 118 - 0
src/test/java/com/sysu/admin/utils/shape/postgis/PostGisUtilTest.java

@@ -0,0 +1,118 @@
+package com.sysu.admin.utils.shape.postgis;
+
+import com.sysu.admin.MarkApplication;
+import com.sysu.admin.api.base.BaseTest;
+import com.sysu.admin.controller.crop.CropLandService;
+import com.sysu.admin.utils.shape.ShapeReader;
+import lombok.SneakyThrows;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+import java.io.*;
+import java.util.List;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = MarkApplication.class)
+public class PostGisUtilTest extends BaseTest {
+
+    static String sourceDir = "D:\\BaiduNetdiskDownload\\merge\\merge";
+    static String outputDir = "D:";
+
+    @Autowired
+    public CropLandService cropLandService;
+
+    public static String[] getTableNames(){
+        File shpList = new File(sourceDir);
+        String[] fileNames = shpList.list((dir, name) -> {
+            if(name.endsWith("shp")){
+                return true;
+            }
+            return false;
+        });
+        String[] typeNames = new String[fileNames.length];
+        for(int i=0;i<typeNames.length;i++){
+            typeNames[i] = "p_land_" + fileNames[i].substring(0, fileNames[i].indexOf(".")).toLowerCase();
+        }
+        return typeNames;
+    }
+
+    public static void createTable() throws Exception {
+
+        BufferedReader fileReader = new BufferedReader(new FileReader(new File("D:","create_table.model.txt")));
+        StringBuffer model = new StringBuffer();
+        while(fileReader.ready()){
+            model.append(fileReader.readLine());
+            model.append("\n");
+        }
+        FileWriter fileWriter = new FileWriter(new File(outputDir,"createTales.sql"));
+        for(String typeName : getTableNames()) {
+            fileWriter.append(model.toString().replaceAll("#table", typeName));
+            fileWriter.append("\n\n");
+        }
+        fileWriter.flush();
+        fileWriter.close();
+
+    }
+
+
+    @Test
+    public static void testInsertShp() {
+        File shpDir = new File(sourceDir);
+        File[] shpList = shpDir.listFiles((dir, name) -> {
+            if(name.endsWith("shp")){
+                return true;
+            }
+            return false;
+        });
+
+        try {
+            FileWriter fileWriter = new FileWriter(new File(outputDir,"insert.sql"));
+            for(File shp : shpList) {
+                String shpName = shp.getName();
+                List<String> list = ShapeReader.getShapeFile(shp);
+                System.out.println("数据准备:");
+                System.out.println(list.size());
+                getInsertSqls(fileWriter, "p_land_" + shpName.substring(0, shpName.indexOf(".")).toLowerCase(), list);
+            }
+
+            fileWriter.flush();
+            fileWriter.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    @SneakyThrows
+    public static void getInsertSqls(FileWriter fileWriter,String typeName, List<String> wkts){
+        int i=0;
+        int c = 200;
+        while(i < wkts.size()) {
+            if(i % c == 0) {
+                fileWriter.append("insert into " + typeName + "(geom) values");
+            }
+            fileWriter.append("(st_geomfromtext('");
+            fileWriter.append(wkts.get(i));
+            fileWriter.append("',4326))");
+
+            if(i % c < (c-1) && i < wkts.size() - 1){
+                fileWriter.append(",");
+            }else{
+                fileWriter.append(";");
+                fileWriter.append("\n");
+            }
+            i++;
+        }
+    }
+
+    public static void update(){
+        for(String tableName : getTableNames()){
+            System.out.println("update "+ tableName + " set gridcode = 2;");
+        }
+    }
+
+
+
+
+}