Administrator 2 年 前
コミット
8540fbdd8d

+ 7 - 2
src/main/java/com/sysu/admin/api/city/ApiCityController.java

@@ -57,12 +57,17 @@ public class ApiCityController {
     @ResponseBody
     public BaseResult subStat(@RequestBody CommonVo commonVo){
         if(StringUtils.isNotBlank(commonVo.getDistrict())){
-            return R.succ(townRepository.findList(commonVo.getDistrict()));
+            List<Town> towns = townRepository.findList(commonVo.getDistrict());
+            towns.stream().forEach(town -> town.castGeojson());
+            return R.succ(towns);
         }
         if(StringUtils.isNotBlank(commonVo.getCity())){
-            return R.succ(districtRepository.findList(commonVo.getCity().substring(0,4) + "%"));
+            List<District> districts = districtRepository.findList(commonVo.getCity().substring(0,4) + "%");
+            districts.stream().forEach(district -> district.castGeojson());
+            return R.succ(districts);
         }
         List<City> cityList = cityRepository.findListByCode("44%");
+        cityList.stream().forEach(city -> city.castGeojson());
         return R.succ(cityList);
     }
 

+ 11 - 1
src/main/java/com/sysu/admin/controller/city/City.java

@@ -1,12 +1,14 @@
 package com.sysu.admin.controller.city;
 
 import com.alibaba.fastjson.annotation.JSONField;
+import com.sysu.admin.utils.shape.GeoCastUtil;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.experimental.Accessors;
 import lombok.experimental.FieldNameConstants;
 import org.hibernate.annotations.DynamicInsert;
 import org.hibernate.annotations.DynamicUpdate;
+import org.locationtech.jts.geom.Geometry;
 import org.locationtech.jts.geom.MultiPolygon;
 
 import javax.persistence.*;
@@ -23,10 +25,11 @@ import java.util.List;
 public class City{
 
     public City(){}
-    public City(String name, String code, Double area) {
+    public City(String name, String code, Double area, Geometry geom) {
         this.name = name;
         this.code = code;
         this.area = area;
+        this.geom = (MultiPolygon)geom;
     }
 
     @Id
@@ -39,6 +42,8 @@ public class City{
 
     @Transient
     private String wkt;
+    @Transient
+    private String geojson;
 
     @Column()
     private Double area;
@@ -56,4 +61,9 @@ public class City{
     @Transient
     @JSONField(serialize = true)
     private List<District> districts;
+
+    public void castGeojson(){
+        if(geom != null)
+            this.geojson = GeoCastUtil.gjson.toString(geom);
+    }
 }

+ 1 - 1
src/main/java/com/sysu/admin/controller/city/CityRepository.java

@@ -8,7 +8,7 @@ import java.util.List;
 public interface CityRepository extends JpaPlusRepository<City, Integer> {
     City findByCode(String code);
 
-    @Query(value = "Select new City(p.name,p.code,p.area) from City p where p.code like ?1 ")
+    @Query(value = "Select new City(p.name,p.code,p.area, p.geom) from City p where p.code like ?1 ")
     List<City> findListByCode(String likeCode);
 
     @Query(value = "select st_astext(geom) from p_city where code = ?1" ,nativeQuery = true)

+ 12 - 1
src/main/java/com/sysu/admin/controller/city/District.java

@@ -1,12 +1,14 @@
 package com.sysu.admin.controller.city;
 
 import com.alibaba.fastjson.annotation.JSONField;
+import com.sysu.admin.utils.shape.GeoCastUtil;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.experimental.Accessors;
 import lombok.experimental.FieldNameConstants;
 import org.hibernate.annotations.DynamicInsert;
 import org.hibernate.annotations.DynamicUpdate;
+import org.locationtech.jts.geom.Geometry;
 import org.locationtech.jts.geom.MultiPolygon;
 
 import javax.persistence.*;
@@ -21,10 +23,11 @@ import java.util.List;
 @DynamicInsert
 @DynamicUpdate
 public class District{
-    public District(String name, String code, Double area) {
+    public District(String name, String code, Double area, Geometry geom) {
         this.name = name;
         this.code = code;
         this.area = area;
+        this.geom = (MultiPolygon)geom;
     }
     public District(){}
 
@@ -39,6 +42,9 @@ public class District{
     @Transient
     private String wkt;
 
+    @Transient
+    private String geojson;
+
     @Column()
     private Double area;
 
@@ -54,4 +60,9 @@ public class District{
     @Transient
     private List<Town> towns;
 
+    public void castGeojson(){
+        if(geom != null)
+            this.geojson = GeoCastUtil.gjson.toString(geom);
+    }
+
 }

+ 1 - 1
src/main/java/com/sysu/admin/controller/city/DistrictRepository.java

@@ -13,7 +13,7 @@ public interface DistrictRepository extends JpaPlusRepository<District, Integer>
     @Query(value = "SELECT * FROM \"p_district\" where St_within(?1, geom) limit 1", nativeQuery = true)
     District findByPoint(Geometry point);
 
-    @Query(value = "Select new District(p.name,p.code,p.area) from District p where p.code like ?1 ")
+    @Query(value = "Select new District(p.name,p.code,p.area,p.geom) from District p where p.code like ?1 ")
     List<District> findList(String likeCode);
 
     @Query(value = "select st_astext(geom) from p_district where code = ?1" ,nativeQuery = true)

+ 11 - 1
src/main/java/com/sysu/admin/controller/city/Town.java

@@ -1,12 +1,14 @@
 package com.sysu.admin.controller.city;
 
 import com.alibaba.fastjson.annotation.JSONField;
+import com.sysu.admin.utils.shape.GeoCastUtil;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.experimental.Accessors;
 import lombok.experimental.FieldNameConstants;
 import org.hibernate.annotations.DynamicInsert;
 import org.hibernate.annotations.DynamicUpdate;
+import org.locationtech.jts.geom.Geometry;
 import org.locationtech.jts.geom.MultiPolygon;
 
 import javax.persistence.*;
@@ -22,10 +24,11 @@ import javax.persistence.*;
 public class Town {
     public Town(){}
 
-    public Town(String name, Integer id, Double area) {
+    public Town(String name, Integer id, Double area, Geometry geom) {
         this.name = name;
         this.id = id;
         this.area = area;
+        this.geom = (MultiPolygon)geom;
     }
 
     @Id
@@ -42,10 +45,17 @@ public class Town {
 
     @Transient
     private String wkt;
+    @Transient
+    private String geojson;
 
     @Column()
     private String name;
 
     @Column()
     private String districtCode;
+
+    public void castGeojson(){
+        if(geom != null)
+            this.geojson = GeoCastUtil.gjson.toString(geom);
+    }
 }

+ 1 - 1
src/main/java/com/sysu/admin/controller/city/TownRepository.java

@@ -8,7 +8,7 @@ import java.util.List;
 public interface TownRepository extends JpaPlusRepository<Town, Integer> {
 
 
-    @Query(value = "Select new Town(p.name,p.id,p.area) from Town p where p.districtCode = ?1 ")
+    @Query(value = "Select new Town(p.name,p.id,p.area,p.geom) from Town p where p.districtCode = ?1 ")
     List<Town> findList(String districtCode);
 
     @Query(value = "select st_astext(geom) from p_town where id = ?1" ,nativeQuery = true)

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

@@ -4,6 +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.Coordinate;
 import org.locationtech.jts.geom.Geometry;
 import org.opengis.feature.simple.SimpleFeature;
 import org.opengis.feature.simple.SimpleFeatureType;
@@ -120,7 +121,12 @@ public class ShapeReader {
             addValue(map, feature, "area_ha");
             addValue(map, feature, "value");
             addValue(map, feature, "MAJORITY");
-            map.put("geom",GeoCastUtil.geomToWkt((Geometry)feature.getDefaultGeometry()));
+            addValue(map, feature, "ParcelType");
+            Geometry geometry = (Geometry)feature.getDefaultGeometry();
+//            for(Coordinate[] coordinate : geometry.getCoordinates()){
+//                GeoCastUtil.mactorToLonlat()
+//            }
+            map.put("geom",GeoCastUtil.geomToWkt(geometry));
             list.add(map);
         }
         return list;

+ 3 - 2
src/test/java/com/sysu/admin/utils/shape/postgis/PostGisUtilTest.java

@@ -135,7 +135,7 @@ public class PostGisUtilTest {
                 List<Map<String,Object>> list = ShapeReader.getShapeFileLeizhou(shp);
                 System.out.println("数据准备:");
                 System.out.println(list.size());
-                getInsertSqlsLeizhou(fileWriter, "leizhou_parcel_out0209", list);
+                getInsertSqlsLeizhou(fileWriter, "leizhou_parcel_out0210", list);
             }
 
             fileWriter.flush();
@@ -151,7 +151,7 @@ public class PostGisUtilTest {
         int c = 100;
         while(i < list.size()) {
             if(i % c == 0) {
-                fileWriter.append("insert into " + typeName + "(inpoly_f_1,area,simpgnflag,toc,quxian,shengdao,hupo,gdp_pop,slope,aspect,name,id_card,crop_type,area_ha,value,majority,geom) values");
+                fileWriter.append("insert into " + typeName + "(id,area,simpgnflag,toc,quxian,shengdao,hupo,gdp_pop,slope,aspect,name,id_card,crop_type,area_ha,value,majority,parceltype,geom) values");
             }
             Map<String, Object> map = list.get(i);
             fileWriter.append(" ('"+map.get("inpoly_f_1")+"',");
@@ -170,6 +170,7 @@ public class PostGisUtilTest {
             fileWriter.append("'"+map.get("area_ha")+"',");
             fileWriter.append("'"+map.get("value")+"',");
             fileWriter.append("'"+map.get("majority")+"',");
+            fileWriter.append("'"+map.get("parceltype")+"',");
             fileWriter.append("st_geomfromtext('");
             fileWriter.append(map.get("geom").toString());
             fileWriter.append("',4326))");