shuhao 2 weeks ago
parent
commit
887346e731

+ 2 - 2
admin/src/main/java/com/flyer/foster/component/CustomMetaObjectHandler.java

@@ -24,7 +24,7 @@ public class CustomMetaObjectHandler implements MetaObjectHandler {
         String strObj = JSON.toJSONString(metaObject.getOriginalObject());
         JSONObject jsonObject = JSON.parseObject(strObj);
         if (StrUtil.isBlank(jsonObject.getString("createdBy"))) {
-            this.setFieldValByName("createdBy", StpUtil.getLoginId(), metaObject);
+//            this.setFieldValByName("createdBy", StpUtil.getLoginId(), metaObject);
         }
         if (StrUtil.isBlank(jsonObject.getString("createdTime"))) {
             this.setFieldValByName("createdTime", LocalDateTime.now(), metaObject);
@@ -36,7 +36,7 @@ public class CustomMetaObjectHandler implements MetaObjectHandler {
         String strObj = JSON.toJSONString(metaObject.getOriginalObject());
         JSONObject jsonObject = JSON.parseObject(strObj);
         if (StrUtil.isBlank(jsonObject.getString("updatedBy"))) {
-            this.setFieldValByName("updatedBy", StpUtil.getLoginId(), metaObject);
+//            this.setFieldValByName("updatedBy", StpUtil.getLoginId(), metaObject);
         }
         if (StrUtil.isBlank(jsonObject.getString("updatedTime"))) {
             this.setFieldValByName("updatedTime", LocalDateTime.now(), metaObject);

+ 12 - 1
admin/src/main/java/com/flyer/foster/controller/app/AppWorldMapController.java

@@ -1,12 +1,17 @@
 package com.flyer.foster.controller.app;
 
+import com.flyer.foster.entity.WorldMap;
 import com.flyer.foster.service.IWorldMapService;
+import com.flyer.util.GeoCastUtil;
 import com.flyer.util.R;
+import org.locationtech.jts.geom.Coordinate;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.util.List;
+
 /**
  * 世界地图(小程序)
  *
@@ -25,6 +30,12 @@ public class AppWorldMapController {
      */
     @GetMapping("/list")
     public R getList() {
-        return R.ok().result(iWorldMapService.list());
+        List<WorldMap> worldMaps = iWorldMapService.list();
+        for(WorldMap worldMap:worldMaps){
+            Coordinate coordinate = GeoCastUtil.pixelToLatLng(0,worldMap.getHeight(), worldMap.getWidth(),worldMap.getHeight());
+            Coordinate coordinate2 = GeoCastUtil.pixelToLatLng(worldMap.getWidth(),0,worldMap.getWidth(),worldMap.getHeight());
+            worldMap.setExtent(new Double[]{coordinate.x,coordinate2.x,coordinate.y,coordinate2.y});
+        }
+        return R.ok().result(worldMaps);
     }
 }

+ 4 - 0
admin/src/main/java/com/flyer/foster/dto/GardenRespDTO.java

@@ -21,6 +21,10 @@ public class GardenRespDTO {
 
     private String wkt;
 
+    private Double latitude;
+
+    private Double longitude;
+
     private String icon;
 
     private Integer birdseyeGardenId;

+ 14 - 0
admin/src/main/java/com/flyer/foster/entity/WorldMap.java

@@ -39,6 +39,20 @@ public class WorldMap implements Serializable {
     private String baseMapUrl;
 
     /**
+     * 地图
+     */
+    private Double width;
+
+
+    /**
+     * 地图
+     */
+    private Double height;
+
+    @TableField(exist = false)
+    private Double[] extent;
+
+    /**
      * 状态-{0.不可用 1.可用}
      */
     private Integer status;

+ 14 - 0
admin/src/main/java/com/flyer/foster/service/impl/GardenServiceImpl.java

@@ -12,14 +12,19 @@ import com.flyer.foster.dto.*;
 import com.flyer.foster.entity.AppInfo;
 import com.flyer.foster.entity.Area;
 import com.flyer.foster.entity.Garden;
+import com.flyer.foster.entity.WorldMap;
 import com.flyer.foster.mapper.IGardenMapper;
 import com.flyer.foster.service.IAppInfoService;
 import com.flyer.foster.service.IAreaService;
 import com.flyer.foster.service.IGardenService;
+import com.flyer.foster.service.IWorldMapService;
 import com.flyer.foster.util.HttpUtils;
 import com.flyer.foster.util.WeChatApiUtil;
 import com.flyer.foster.util.WxAccessToken;
+import com.flyer.util.GeoCastUtil;
+import com.flyer.util.R;
 import okhttp3.Response;
+import org.locationtech.jts.geom.Coordinate;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -51,11 +56,20 @@ public class GardenServiceImpl extends ServiceImpl<IGardenMapper, Garden> implem
 
     @Autowired
     private IAppInfoService iAppInfoService;
+    @Autowired
+    private IWorldMapService iWorldMapService;
 
     @Override
     public IPage<GardenRespDTO> getPageList(IPage<GardenRespDTO> page, GardenQueryDTO queryDTO) {
         IPage<GardenRespDTO> pageList = baseMapper.getPageList(page, queryDTO);
+        List<WorldMap> worldMaps = iWorldMapService.list();
+        WorldMap worldMap = worldMaps.get(0);
+
         for (GardenRespDTO record : pageList.getRecords()) {
+            Coordinate coordinate = GeoCastUtil.wktToGeom(record.getWkt()).getCoordinate();
+            coordinate = GeoCastUtil.pixelToLatLng(coordinate.getX(), worldMap.getHeight() - coordinate.getY(), worldMap.getWidth(), worldMap.getHeight());
+            record.setLatitude(coordinate.y);
+            record.setLongitude(coordinate.x);
             // 获取区域
             List<String> areaNameList = iAreaService.lambdaQuery()
                     .eq(Area::getGardenId, record.getId())

+ 0 - 1
admin/src/main/resources/application-dev.yml

@@ -38,7 +38,6 @@ sa-token:
   token-name: token
   is_read_cookie: false
   # token 有效期(单位:秒) 默认30天,-1 代表永久有效
-  timeout: 2592000
   # token 最低活跃频率(单位:秒),如果 token 超过此时间没有访问系统就会被冻结,默认-1 代表不限制,永不冻结
   active-timeout: -1
   # 是否允许同一账号多地同时登录 (为 true 时允许一起登录, 为 false 时新登录挤掉旧登录)

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

@@ -15,7 +15,6 @@ sa-token:
   # token 名称(同时也是 cookie 名称)
   token-name: satoken
   # token 有效期(单位:秒) 默认30天,-1 代表永久有效
-  timeout: 2592000
   # token 最低活跃频率(单位:秒),如果 token 超过此时间没有访问系统就会被冻结,默认-1 代表不限制,永不冻结
   active-timeout: -1
   # 是否允许同一账号多地同时登录 (为 true 时允许一起登录, 为 false 时新登录挤掉旧登录)

+ 70 - 2
common/pom.xml

@@ -17,9 +17,17 @@
     </properties>
 
     <dependencies>
+        <!-- test -->
         <dependency>
             <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-web</artifactId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
         </dependency>
 
         <dependency>
@@ -28,6 +36,12 @@
         </dependency>
 
         <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <version>1.5.9.RELEASE</version>
+        </dependency>
+
+        <dependency>
             <groupId>com.baomidou</groupId>
             <artifactId>mybatis-plus-boot-starter</artifactId>
         </dependency>
@@ -94,5 +108,59 @@
             <groupId>cn.dev33</groupId>
             <artifactId>sa-token-spring-boot-starter</artifactId>
         </dependency>
+
+        <dependency>
+            <groupId>org.mylib</groupId>
+            <artifactId>GeoLib</artifactId>
+            <version>1.0</version>
+        </dependency>
     </dependencies>
-</project>
+
+    <repositories>
+        <repository>
+            <id>osgeo</id>
+            <name>OSGeo Release Repository</name>
+            <url>https://repo.osgeo.org/repository/release/</url>
+            <snapshots><enabled>false</enabled></snapshots>
+            <releases><enabled>false</enabled></releases>
+        </repository>
+        <repository>
+            <id>apache.spring-cloud</id>
+            <name>Apache spring-cloud Repository</name>
+            <url>https://www.apache.org/licenses/LICENSE-2.0</url>
+        </repository>
+    </repositories>
+    <!-- 编译 -->
+    <build>
+        <finalName>foster-common</finalName>
+        <resources>
+            <resource>
+                <directory>src/main/resources</directory>
+                <includes>
+                    <include>**/*.*</include>
+                </includes>
+                <filtering>false</filtering>
+            </resource>
+        </resources>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-jar-plugin</artifactId>
+                <configuration>
+                    <archive>
+                        <manifest>
+                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
+                        </manifest>
+                    </archive>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-surefire-plugin</artifactId>
+                <configuration>
+                    <skipTests>true</skipTests><!-- 忽略测试 -->
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>

+ 1 - 1
common/src/main/java/com/flyer/enums/StatusCodeEnum.java

@@ -14,7 +14,7 @@ import lombok.ToString;
 @AllArgsConstructor
 @ToString
 public enum StatusCodeEnum {
-    SUCCESS(2000, "成功"),
+    SUCCESS(0, "成功"),
 
     UNAUTHORIZED(3101, "用户未登录,无法访问"),
     NO_AUTHENTICATION(3102, "没有操作权限"),

+ 331 - 0
common/src/main/java/com/flyer/util/GeoCastUtil.java

@@ -0,0 +1,331 @@
+package com.flyer.util;
+
+import org.apache.commons.lang3.ObjectUtils;
+import org.geotools.geojson.geom.GeometryJSON;
+import org.geotools.geometry.jts.JTS;
+import org.geotools.referencing.CRS;
+import org.geotools.referencing.GeodeticCalculator;
+import org.geotools.referencing.crs.DefaultGeographicCRS;
+import org.locationtech.jts.geom.*;
+import org.locationtech.jts.io.ParseException;
+import org.locationtech.jts.io.WKTReader;
+import org.locationtech.jts.io.WKTWriter;
+import org.opengis.referencing.FactoryException;
+import org.opengis.referencing.crs.CoordinateReferenceSystem;
+import org.opengis.referencing.operation.MathTransform;
+import org.opengis.referencing.operation.TransformException;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.List;
+
+public class GeoCastUtil {
+    public static GeometryJSON gjson = new GeometryJSON(20);
+    private static WKTWriter wKTWriter2 = new WKTWriter();
+    private static WKTReader wktReader2 = new WKTReader();
+
+    public static String geomToWkt(Geometry geometry){
+        return wKTWriter2.write(geometry);
+    }
+
+    public static Geometry wktToGeom(String wkt){
+        try {
+            return wktReader2.read(wkt);
+        } catch (ParseException e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+    /**
+     * 创建矩形wkt
+     * @param points
+     * @return
+     */
+    public static String createPolygonWkt(List<double[]> points){
+        if(ObjectUtils.isEmpty(points)){
+            return "";
+        }
+        StringBuffer sb = new StringBuffer(points.size() * 50);
+        sb.append("MULTIPOLYGON(((");
+        for(double[] point : points){
+            sb.append(point[0]);
+            sb.append(" ");
+            sb.append(point[1]);
+            sb.append(",");
+        }
+        sb.append(points.get(0)[0]);
+        sb.append(" ");
+        sb.append(points.get(0)[1]);
+        sb.append(")))");
+        return sb.toString();
+    }
+
+    private static GeometryCollection castGeometryCollection2(GeometryJSON fjson, GeometryCollection geometryCollection) throws IOException {
+        if (geometryCollection.getGeometryType().equals("GeometryCollection")) {
+            return geometryCollection;
+        }
+        String[] geometriesJsonArray = new String[geometryCollection.getNumGeometries()];
+        int length = 0;
+        for(int i=0; i< geometryCollection.getNumGeometries(); i++){
+            Geometry geometry = geometryCollection.getGeometryN(i);
+            geometriesJsonArray[i] = fjson.toString(geometry);
+            length += geometriesJsonArray[i].length();
+        }
+        StringBuffer result = new StringBuffer(length + 100);
+        result.append("{\"type\": \"GeometryCollection\",\"geometries\": [");
+        result.append(geometriesJsonArray[0]);
+        for(int i=1;i< geometriesJsonArray.length;i++){
+            result.append(",").append(geometriesJsonArray[i]);
+        }
+        result.append("]}");
+        return fjson.readGeometryCollection(new StringReader(result.toString()));
+    }
+
+    public static GeometryCollection castGeometryCollection(GeometryJSON fjson, Geometry geometry) throws IOException {
+        if (geometry.getGeometryType().equals("GeometryCollection")) {
+            return (GeometryCollection)geometry;
+        }
+        if (geometry instanceof GeometryCollection) {
+            return castGeometryCollection2(fjson, (GeometryCollection)geometry);
+        }
+        String geometryJson = fjson.toString(geometry);
+        StringBuffer result = new StringBuffer(geometryJson.length() + 100);
+        result.append("{\"type\": \"GeometryCollection\",\"geometries\": [");
+        result.append(geometryJson);
+        result.append("]}");
+        return fjson.readGeometryCollection(new StringReader(result.toString()));
+    }
+
+    public static Geometry readGeometry(Object input, String type, GeometryJSON gjson) throws IOException {
+        switch(type){
+            case "Point":
+                return gjson.readPoint(input);
+            case "MultiPoint":
+                return gjson.readMultiPoint(input);
+            case "LineString":
+                return gjson.readLine(input);
+            case "MultiLineString":
+                return gjson.readMultiLine(input);
+            case "Polygon":
+                return gjson.readPolygon(input);
+            case "MultiPolygon":
+                return gjson.readMultiPolygon(input);
+        }
+        return null;
+    }
+
+
+
+    public static Double[] lonlatToMactor(Double[] point){
+        CoordinateReferenceSystem crsTarget = null;
+        // 投影转换
+        MathTransform transform = null;
+        try {
+            crsTarget = CRS.decode("EPSG:3857");
+            transform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, crsTarget);
+        } catch (FactoryException e) {
+            e.printStackTrace();
+        }
+        Coordinate sourcePoint = new Coordinate(point[0],point[1]);
+        Coordinate targetPoint = new Coordinate();
+        try {
+            JTS.transform(sourcePoint, targetPoint, transform);
+        } catch (TransformException e) {
+            e.printStackTrace();
+        }
+        return new Double[]{targetPoint.getX(), targetPoint.getY()};
+    }
+
+    public static double[] mactorToLonlat(double[] point, CoordinateReferenceSystem srcTarget){
+        // 投影转换
+        MathTransform transform = null;
+        try {
+            transform = CRS.findMathTransform(srcTarget, DefaultGeographicCRS.WGS84);
+        } catch (FactoryException e) {
+            e.printStackTrace();
+        }
+        Coordinate sourcePoint = new Coordinate(point[0],point[1]);
+        Coordinate targetPoint = new Coordinate();
+        try {
+            JTS.transform(sourcePoint, targetPoint, transform);
+        } catch (TransformException e) {
+            e.printStackTrace();
+        }
+        return new double[]{targetPoint.getX(),targetPoint.getY()};
+    }
+
+    public static double[] mactorToLonlat(double[] point){
+        CoordinateReferenceSystem srcTarget = null;
+        // 投影转换
+        MathTransform transform = null;
+        try {
+            srcTarget = CRS.decode("EPSG:3857");
+            transform = CRS.findMathTransform(srcTarget, DefaultGeographicCRS.WGS84);
+        } catch (FactoryException e) {
+            e.printStackTrace();
+        }
+        Coordinate sourcePoint = new Coordinate(point[0],point[1]);
+        Coordinate targetPoint = new Coordinate();
+        try {
+            JTS.transform(sourcePoint, targetPoint, transform);
+        } catch (TransformException e) {
+            e.printStackTrace();
+        }
+        return new double[]{targetPoint.getX(),targetPoint.getY()};
+    }
+
+    /**
+     * 根据多边形类型计算出多边形面积,单位(平方米)
+     * @param geometry 多边形对象
+     * @return 面积
+     */
+    public static double getArea(Geometry geometry){
+        CoordinateReferenceSystem source = null;
+        try {
+            source = CRS.decode("CRS:84");
+        } catch (FactoryException e) {
+            e.printStackTrace();
+        }
+        CoordinateReferenceSystem target = null;
+        try {
+            target = CRS.decode("EPSG:3857");
+        } catch (FactoryException e) {
+            e.printStackTrace();
+        }
+        MathTransform transform = null;
+        try {
+            transform = CRS.findMathTransform(source, target, true);
+        } catch (FactoryException e) {
+            e.printStackTrace();
+        }
+        Geometry transform1 = null;
+        try {
+            transform1 = JTS.transform(geometry, transform);
+        } catch (TransformException e) {
+            e.printStackTrace();
+        }
+        double area = transform1.getArea();
+        return area;
+    }
+
+//    public static double toFlex(double d){
+//        BigDecimal bd = new BigDecimal(d);
+//        String s = bd.toPlainString();
+//        int index = s.indexOf(".");
+//        String last = s.substring(index + 1);
+//        if(last.length() > 4){
+//            last = last.substring(0,4);
+//        }
+//        return Double.parseDouble(s.substring(0,index + 1) + last);
+//    }
+
+    public static int dist84(Point p1,Point p2){
+        // 84坐标系构造GeodeticCalculator
+        GeodeticCalculator geodeticCalculator = new GeodeticCalculator(DefaultGeographicCRS.WGS84);
+        // 起点经纬度
+        geodeticCalculator.setStartingGeographicPoint(p1.getX(),p1.getY());
+        // 末点经纬度
+        geodeticCalculator.setDestinationGeographicPoint(p2.getX(),p2.getY());
+        // 计算距离,单位:米
+        double orthodromicDistance = geodeticCalculator.getOrthodromicDistance();
+        return (int)orthodromicDistance;
+    }
+
+    public static double miToMu(double m){
+        double c = 666.66666666667;
+        return m / c;
+    }
+
+    public static double mToDegree(Integer m){
+        double degree = m / (2 * Math.PI * 6371004) * 360;
+        return degree;
+    }
+
+    /**
+     * 返回面积 单位亩
+     * @param geom
+     * @return
+     */
+    public static double getAreaOfMu(Geometry geom){
+        return miToMu(getArea(geom));
+    }
+
+
+    /**
+     * 转为geom并合并
+     * @param geom
+     * @return
+     */
+    public static Geometry unionToGeom(Geometry... geom){
+        if(geom.length == 0){
+            return null;
+        }
+        if(geom.length == 1){
+            return geom[0];
+        }
+        Geometry res = geom[0];
+        for(int i=1;i<geom.length;i++){
+            res = res.union(geom[i]);
+        }
+        return res;
+    }
+
+    /**
+     * 转为geom并合并
+     * @param wkt
+     * @return
+     */
+    public static Geometry unionToGeom(String... wkt){
+        if(wkt.length == 1){
+            return wktToGeom(wkt[0]);
+        }
+        Geometry res = wktToGeom(wkt[0]);
+        for(int i=1;i<wkt.length;i++){
+            res = res.union(wktToGeom(wkt[i]));
+        }
+        return res;
+    }
+
+    public static double distance(Point p1, Point p2){
+        double distance = p1.distance(p2);
+        return distance;
+    }
+
+    private static GeometryFactory geometryFactory = new GeometryFactory();
+
+    // 假设的参数113.746646,22.970229
+    private static final double REFERENCE_LATITUDE = 22.970229; // 参考点的纬度
+    private static final double REFERENCE_LONGITUDE = 113.746646; // 参考点的经度
+    private static final double M_PER_PIXEL_X = 0.00001; // 每个像素的米数(x方向)
+    private static final double M_PER_PIXEL_Y = 0.00001; // 每个像素的米数(y方向)
+
+    public static Coordinate pixelToLatLng(double pixelX, double pixelY, double imageWidth, double imageHeight) {
+        // 计算像素到米的转换
+        double metersX = pixelX * M_PER_PIXEL_X;
+        double metersY = pixelY * M_PER_PIXEL_Y;
+
+        // 将米转换为经纬度
+        // 假设地球是平坦的,这里只是简单的加减
+        double latitude = REFERENCE_LATITUDE - (metersY / 111320); // 111320米为1度纬度距离的近似值
+        double longitude = REFERENCE_LONGITUDE + (metersX / (111320 * Math.cos(Math.toRadians(REFERENCE_LATITUDE)))); // 1度经度距离随纬度变化
+
+        return new Coordinate(longitude, latitude);
+    }
+
+    public static void main(String[] args) {
+        int pixelX = 100;
+        int pixelY = 100;
+        int imageWidth = 1000;
+        int imageHeight = 1000;
+
+        Coordinate latLng = pixelToLatLng(pixelX, pixelY, imageWidth, imageHeight);
+        System.out.println("Latitude: " + latLng.y + ", Longitude: " + latLng.x);
+    }
+
+
+}
+
+
+
+

+ 14 - 2
common/src/main/java/com/flyer/util/R.java

@@ -1,5 +1,6 @@
 package com.flyer.util;
 
+import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.flyer.enums.StatusCodeEnum;
 import lombok.Data;
 
@@ -13,7 +14,10 @@ import lombok.Data;
 public class R {
     private Integer code;
     private String message;
-    private Object result;
+    private Object data;
+    private Long total;
+    private Long size;
+    private Long current;
 
     /**
      * 构造函数私有化
@@ -65,7 +69,15 @@ public class R {
     }
 
     public <T> R result(T result) {
-        this.setResult(result);
+        if (result instanceof IPage) {
+            IPage page = (IPage) result;
+            this.setData(page.getRecords());
+            this.setTotal(page.getTotal());
+            this.setSize(page.getSize());
+            this.setCurrent(page.getCurrent());
+        }else{
+            this.setData(result);
+        }
         return this;
     }