Просмотр исходного кода

调整经纬度距离计算逻辑

liwh 1 год назад
Родитель
Сommit
e52ac01a3e

+ 5 - 0
tile-web/src/main/java/cn/com/taiji/web/model/DeviceInfo.java

@@ -86,4 +86,9 @@ public class DeviceInfo implements Serializable {
      * 行政规划中心编码
      */
     private String civilcode;
+
+    /**
+     * 距离
+     */
+    private Double distance;
 }

+ 43 - 4
tile-web/src/main/java/cn/com/taiji/web/service/impl/EnterpriseServiceImpl.java

@@ -12,8 +12,12 @@ import cn.com.taiji.web.vo.DeviceVo;
 import cn.com.taiji.web.vo.EnterpriseVo;
 import com.alibaba.fastjson.JSONObject;
 import lombok.extern.slf4j.Slf4j;
+import org.geotools.api.referencing.crs.CoordinateReferenceSystem;
 import org.geotools.feature.visitor.CountVisitor;
+import org.geotools.geometry.jts.JTS;
 import org.geotools.jdbc.JDBCDataStoreFactory;
+import org.geotools.referencing.crs.DefaultGeographicCRS;
+import org.locationtech.jts.geom.Coordinate;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -112,9 +116,9 @@ public class EnterpriseServiceImpl implements IEnterpriseService {
         //组转cql查询条件
         StringBuffer filterCql = new StringBuffer();
 
-        String wkt = "POINT ("+deviceVo.getLon()+" "+deviceVo.getLat()+")";
+        String wkt = "'POINT("+deviceVo.getLon()+" "+deviceVo.getLat()+")'";
 
-        filterCql.append("(DWITHIN(location," + wkt + ", "+deviceVo.getRadius()/100+", kilometers))");
+        filterCql.append("(DWITHIN(location," + wkt + ", "+deviceVo.getRadius()+", kilometers))");
 
 
 
@@ -134,12 +138,38 @@ public class EnterpriseServiceImpl implements IEnterpriseService {
 
                 }
 
-                DeviceInfo deviceInfo = JSONObject.parseObject(JSONObject.toJSONString(dataMap), DeviceInfo.class);
-                deviceInfoList.add(deviceInfo);
+
+
+                try {
+
+                    DeviceInfo deviceInfo = JSONObject.parseObject(JSONObject.toJSONString(dataMap), DeviceInfo.class);
+
+                    CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
+
+                    Coordinate c1 = new Coordinate(Double.parseDouble(deviceInfo.getLongitude()), Double.parseDouble(deviceInfo.getLatitude()));
+                    Coordinate c2 = new Coordinate(deviceVo.getLon(), deviceVo.getLat());
+
+                    double dist = JTS.orthodromicDistance(c1, c2, crs);
+
+                    if (dist <= deviceVo.getRadius()*1000){
+
+                        deviceInfo.setDistance(dist);
+
+                        deviceInfoList.add(deviceInfo);
+                    }
+
+
+
+                }catch (Exception exception){
+                    exception.printStackTrace();
+                }
+
 
             }
         }
 
+        Collections.sort(deviceInfoList, new DeviceComparator());
+
         System.out.println("查询条件:"+filterCql.toString()+" 记录数:"+deviceInfoList.size());
 
 
@@ -206,4 +236,13 @@ public class EnterpriseServiceImpl implements IEnterpriseService {
 
         return Result.of(staticsInfo,0,"操作成功!");
     }
+
+    class  DeviceComparator implements Comparator<DeviceInfo>{
+
+        @Override
+        public int compare(DeviceInfo o1, DeviceInfo o2) {
+
+            return o1.getDistance().compareTo(o2.getDistance());
+        }
+    }
 }