Administrator 2 år sedan
förälder
incheckning
b56b3ad91b

+ 3 - 3
src/main/java/com/sysu/admin/controller/city/CityController.java

@@ -90,7 +90,7 @@ public class CityController {
                 res = province;
                 if(province != null){
                     Point point = cropLandService.getCenterPoint(province.getGeom());
-                    province.setWkt(cropLandService.getBuffer(new Double[]{point.getX(),point.getY()}, 5000));
+                    province.setWkt(cropLandService.getBuffer(new Double[]{point.getX(),point.getY()}, 2000));
                 }
                 break;
             case 1:
@@ -98,7 +98,7 @@ public class CityController {
                 res = city;
                 if(city != null){
                     Point point = cropLandService.getCenterPoint(city.getGeom());
-                    city.setWkt(cropLandService.getBuffer(new Double[]{point.getX(),point.getY()}, 5000));
+                    city.setWkt(cropLandService.getBuffer(new Double[]{point.getX(),point.getY()}, 2000));
                 }
                 break;
             case 2:
@@ -106,7 +106,7 @@ public class CityController {
                 res = district;
                 if(district != null){
                     Point point = cropLandService.getCenterPoint(district.getGeom());
-                    district.setWkt(cropLandService.getBuffer(new Double[]{point.getX(),point.getY()}, 5000));
+                    district.setWkt(cropLandService.getBuffer(new Double[]{point.getX(),point.getY()}, 2000));
                 }
                 break;
         }

+ 5 - 2
src/main/java/com/sysu/admin/controller/crop/CropLandService.java

@@ -29,6 +29,7 @@ import org.locationtech.jts.geom.Point;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import javax.persistence.Query;
 import javax.transaction.Transactional;
 import java.io.File;
 import java.io.IOException;
@@ -96,8 +97,10 @@ public class CropLandService extends BaseService<CropLand, Long> {
     public List<CropPoint> findCropLandNoGeomByTableNamesAndBBox(String tableNames, Double x1, Double y1, Double x2, Double y2){
         String wkt = bboxToWkt(x1, y1 , x2, y2);
         AutoTableNameInterceptor.myTable.set(tableNames);
-        List<CropPoint> cropLandList = mCropPointRepository.findCropLandNoGeomByWkt(wkt);
-        return cropLandList;
+        String sql = " select  "+ CropPoint.selectColumns;
+        sql += " from p_crop  where ST_Intersects(geom,st_geomfromtext('"+wkt+"',4326)) ";
+        List<CropPoint> cropPointList = mEntityManager.createNativeQuery(sql, CropPoint.class).getResultList();
+        return cropPointList;
     }
 
     @Transactional

+ 0 - 3
src/main/java/com/sysu/admin/controller/crop/CropPointRepository.java

@@ -18,9 +18,6 @@ public interface CropPointRepository extends JpaPlusRepository<CropPoint, Long>
     @Query(value = "select ST_AsText(ST_Buffer(st_geomfromtext(?1),?2, 'quad_segs=8'))", nativeQuery = true)
     String getBuffer(String pointWKT, Double degree);
 
-    @Query(value = "select id,gridcode,crop_type,st_asgeojson(center_point) as geojson,status,receiver,name,address,growing_period" +
-            " from p_crop where ST_Intersects(geom,st_geomfromtext(?1,4326))", nativeQuery = true)
-    List<CropPoint> findCropLandNoGeomByWkt(String wkt);
 
 
 }

+ 6 - 4
src/main/java/com/sysu/admin/controller/crop/range/AutoTableNameInterceptor.java

@@ -2,8 +2,11 @@ package com.sysu.admin.controller.crop.range;
 
 import com.sysu.admin.utils.TextUtil;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.hibernate.EmptyInterceptor;
+import org.hibernate.type.Type;
 
+import java.io.Serializable;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -12,12 +15,11 @@ public class AutoTableNameInterceptor extends EmptyInterceptor {
 
     public static ThreadLocal<String> myTable = new ThreadLocal<>();
 
+
     @Override
     public String onPrepareStatement(String sql) {
-        if(sql.indexOf("p_crop") > -1) {
-            String tableName = myTable.get();
-            if (tableName == null) return sql;
-            if (tableName == null || tableName.trim().equals("")) return sql;
+        String tableName = myTable.get();
+        if(StringUtils.isNotBlank(tableName) && sql.indexOf("p_crop") > -1) {
             if (sql.indexOf("delete") > -1 || sql.indexOf("insert") > -1) {
                 return sql;
             }

+ 10 - 2
src/main/java/com/sysu/admin/controller/crop/range/LandRangeIndexService.java

@@ -3,6 +3,7 @@ package com.sysu.admin.controller.crop.range;
 import com.querydsl.core.types.Predicate;
 import com.sysu.admin.support.base.BaseService;
 import com.xiesx.fastboot.core.jpa.JpaPlusRepository;
+import org.apache.commons.lang3.ObjectUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -17,10 +18,17 @@ public class LandRangeIndexService extends BaseService<LandRangeIndex, PKRP> {
     @Autowired
     private LandRangeIndexRepository mLandRangeIndexRepository;
 
+    private static String defaultTableName = "p_crop";
+
 
     public String[] getTableNames(Double x1, Double y1,Double x2, Double y2){
         String wkt = "POLYGON (("+x1+" "+y1+", "+x1+" "+y2+", "+x2+" "+y2+", "+x2+" "+y1+", "+x1+" "+y1+"))";
-        return mLandRangeIndexRepository.findTableNameByWkt(wkt);
+        String[] res = mLandRangeIndexRepository.findTableNameByWkt(wkt);
+        if(ObjectUtils.isEmpty(res)){
+            return new String[]{defaultTableName};
+        }else{
+            return res;
+        }
     }
 
     public String getTableName(Long id){
@@ -33,7 +41,7 @@ public class LandRangeIndexService extends BaseService<LandRangeIndex, PKRP> {
         if(landRangeIndex != null){
             return landRangeIndex.getTableName();
         }
-        return null;
+        return defaultTableName;
     }
 
     public String getTableNameByCode(Integer code){

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

@@ -26,6 +26,7 @@
     <fb:css href="${base}/static/js/city-picker/city-picker.css"></fb:css>
     <fb:script src="${base}/static/js/city-picker/city-picker.data.js"></fb:script>
 
+    <fb:script src="${base}/static/package/jsts.min.js"></fb:script>
     <fb:script src="${base}/static/package/geo_server_context.js"></fb:script>
     <fb:script src="${base}/static/package/plough_land.js"></fb:script>
     <fb:script src="${base}/static/package/zsjc.js"></fb:script>

+ 17 - 21
src/main/webapp/static/package/cityland.js

@@ -47,8 +47,8 @@ CityLandAction.prototype = {
         }
         // this.context.clicks[this.id] = this
         this.obj.addTileLayer(this.context.currentMap)
+        this.context.currentMap.addLayer(this.tempVectorLayer)
         this.drawMultiPolygon.show()
-        this.tempVectorLayer.setVisible(true)
         this.context.landFilter()
         this.context.getSldStyleSign(this.obj.wmsData.params.STYLE)
         this.startStatus = true;
@@ -60,7 +60,7 @@ CityLandAction.prototype = {
             console.log("startStatus is false")
             return
         }
-        this.tempVectorLayer.setVisible(false)
+        this.context.currentMap.removeLayer(this.tempVectorLayer)
         this.obj.removeTileLayer(this.context.currentMap)
         console.log("drawMultiPolygon close")
         this.drawMultiPolygon.close()
@@ -159,19 +159,13 @@ CityLandAction.prototype = {
     ,loadTempVectorLayer(){
         let that = this
         let map = this.context.currentMap
-        let baseUrl = this.context.local_wfs_path
         const vectorSource = new ol.source.Vector({
-            format: new ol.format.GeoJSON(),
             loader: function(extent, resolution, projection, success, failure) {
-                const proj = projection.getCode();
-                let typeNames = that.obj.getVisibleLayers()
-                console.log(typeNames)
-                let projCode = "3857"
-                if(typeNames == ""){
-                    typeNames = "p_crop"
-                    projCode = "4326"
+                if(!that.context.vectorSwitch){
+                    return;
                 }
-
+                let typeNames = that.obj.getVisibleLayers()
+                console.log("typeNames",typeNames)
                 const xhr = new XMLHttpRequest();
                 xhr.open('GET', "/crop/getGeoJson?tableNames="+typeNames + "&x1=" + extent[0] + "&y1=" + extent[1]+"&x2=" + extent[2] + "&y2="+extent[3]);
                 const onError = function() {
@@ -186,11 +180,15 @@ CityLandAction.prototype = {
                             let features = []
                             let datas = res.data;
                             for(let data of datas){
-                                let feature = that.newFieldPoint(data,projCode)
+                                let feature = that.newFieldPoint(data)
                                 vectorSource.addFeature(feature)
                                 features.push(feature)
                             }
                             success(features);
+                            if(datas.length > 0){
+                                that.context.cacheBbox(extent)
+                                that.context.vectorSwitch = false
+                            }
                         }
                     } else {
                         onError();
@@ -202,11 +200,12 @@ CityLandAction.prototype = {
         });
 
         let vector = new ol.layer.Vector({
-            map: map,
             source:vectorSource,
             style: globalStyle.cropStyle,
-            minZoom:13,
-            maxZoom:20
+            minZoom:15,
+            maxZoom:22,
+            zIndex: 3,
+            visible: true
         });
 
         that.select = new ol.interaction.Select({
@@ -217,8 +216,7 @@ CityLandAction.prototype = {
         map.addInteraction(that.select)
         that.select.on("select",function(e){
             if(e.selected.length > 0){
-                console.log("coordinates",e.selected[0].getGeometry())
-                that.popup.show(e.selected[0].getGeometry().coordinates, that.getInfoHtml(e.selected[0]), e.selected[0])
+                that.popup.show(ol.extent.getCenter(e.selected[0].getGeometry().getExtent()), that.getInfoHtml(e.selected[0]), e.selected[0])
             }
         })
         return vector;
@@ -236,7 +234,6 @@ CityLandAction.prototype = {
             "图片信息":"<a style='color: whitesmoke;' href='javascript:layui.tool.open(!,!,!,!)'>点击查看</a>"
                 .format('"/crop_image/view?id='+feature.get("id")+'"','"60%"','"图片信息"','{area:["90%","90%"],shade:0.1}'),
         }
-        console.log(data)
 
         for(let key in data){
             html += "<div class='layui-row'><div class='lab' >"+key+"</div><div class='val' >"+data[key]+"</div></div>"
@@ -247,7 +244,6 @@ CityLandAction.prototype = {
     landType:{0:"其他", 1:"湿地", 2:"耕地", 3:"种植", 4:"林地", 5:"草地", 6:"水域"}
     ,
     saveTask(that, feature){
-        console.log(feature.getKeys())
         let id = feature.getId()
         layui.tool.submit("/crop/publish",{"id":feature.get("id") || id.substring(id.indexOf(".") + 1)},function(res){
             if(res.code == 0){
@@ -372,9 +368,9 @@ CityLand.prototype = {
         return tile;
     },
     visibleWMTS(tableNames){
-        console.log(tableNames)
         this.visibleLayers = tableNames
         for(let layer of this.layerData.layer){
+            console.log(layer.get("name"))
             if(tableNames.includes(layer.get("name"))){
                 layer.setVisible(true)
             }else{

+ 45 - 3
src/main/webapp/static/package/geo_server_context.js

@@ -9,6 +9,9 @@ function GeoServerContext(opt,geoserver_path){
     this.projection = opt.projection || window.projection
     this.wkt = new ol.format.WKT()
     this.geojson = new ol.format.GeoJSON()
+    this.jstsParser = new jsts.io.OL3Parser()
+    this.jstsParser.inject(ol.geom.Point, ol.geom.LineString, ol.geom.LinearRing, ol.geom.Polygon, ol.geom.MultiPoint, ol.geom.MultiLineString, ol.geom.MultiPolygon);
+
     let that = this
     this.currentMap.on("click", function(event){
         for(let id in that.clicks){
@@ -19,6 +22,15 @@ function GeoServerContext(opt,geoserver_path){
         }
     })
 
+    this.currentMap.on("movestart", function(){
+        for(let key in that.actions){
+            let action = that.actions[key]
+            if(action && action.movestart){
+                action.movestart()
+            }
+        }
+    })
+
     this.currentMap.on("moveend", function(){
         that.landFilter()
     })
@@ -123,14 +135,44 @@ GeoServerContext.prototype = {
                     let action = that.actions[key]
                     if(action && action.obj && action.obj.visibleWMTS){
                         let tableNames = res.data;
-                        if(tableNames.length == 0){
-                            tableNames.push("p_crop");
-                        }
                         action.obj.visibleWMTS(tableNames)
+                        if(that.changeBbox(bbox)){
+                            that.vectorSwitch = true
+                            action.tempVectorLayer.getSource().refresh()
+                        }else{
+                            console.log("bbox以内不需要加载")
+                        }
                     }
                 }
             }
         });
+    },bbox:undefined,
+    vectorSwitch: false
+    ,
+    cacheBbox(bbox){
+        this.bbox = bbox
+    },changeBbox(bbox){
+        if(this.bbox){
+            let oldBbox = this.bboxToGeom(this.bbox);
+            let newBbpx = this.bboxToGeom(bbox);
+            let jstsBbox1 = this.jstsParser.read(oldBbox)
+            let jstsBbox2 = this.jstsParser.read(newBbpx)
+            if(jstsBbox1.contains(jstsBbox2)){
+                return false
+            }else{
+                return true
+            }
+        }else{
+            return true
+        }
+    },
+    bboxToGeom(bbox){
+        let x1 = bbox[0]
+        let y1 = bbox[1]
+        let x2 = bbox[2]
+        let y2 = bbox[3]
+        let wktStr = "POLYGON (("+x1+" "+y1+", "+x1+" "+y2+", "+x2+" "+y2+", "+x2+" "+y1+", "+x1+" "+y1+"))";
+        return this.wkt.readGeometry(wktStr);
     }
 }
 

+ 2 - 1
src/test/java/com/sysu/admin/utils/shape/postgis/Main.java

@@ -11,8 +11,9 @@ public class Main {
 //        }
 
 //        postGisUtilTest.layers();
-        postGisUtilTest.updateCenterPoint();
+//        postGisUtilTest.updateCenterPoint();
 //        postGisUtilTest.updateSrid();
+        postGisUtilTest.ST_Extent();
 
     }
 }

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

@@ -150,6 +150,13 @@ public class PostGisUtilTest extends BaseTest {
         System.out.print("]");
     }
 
+    public void ST_Extent(){
+        for(String tableName : getTableNames()){
+            System.out.println("select '"+tableName+"',ST_Extent(geom) from "+tableName);
+            System.out.println("union all");
+        }
+    }
+