Browse Source

优化根据鼠标点击获取要素信息

zhouyuexiang 1 year ago
parent
commit
f50322277c
1 changed files with 43 additions and 27 deletions
  1. 43 27
      tile-service/src/main/java/cn/com/taiji/tile/util/ShapeUtils.java

+ 43 - 27
tile-service/src/main/java/cn/com/taiji/tile/util/ShapeUtils.java

@@ -32,7 +32,7 @@ public class ShapeUtils {
     public static final int WIDTH = 256;
     public static final int HEIGHT = 256;
 
-    public final static WKTReader reader = new WKTReader();
+    public final static WKTReader WKT_READER = new WKTReader();
 
 
     private static void setImage(String imageUrl, Consumer<BufferedImage> imageConsumer) {
@@ -110,7 +110,10 @@ public class ShapeUtils {
     }
 
     public static void drawPointShape(double[] bbox, Graphics2D graphics, Geometry geometry, Map<String, Object> poi, ShapeStyle shapeStyle) {
-        int[] pxy = TileUtils.toPixelXY(geometry.getCoordinate().x, geometry.getCoordinate().y, bbox[0], bbox[1], bbox[2], bbox[3]);
+//        int[] pxy = TileUtils.toPixelXY(geometry.getCoordinate().x, geometry.getCoordinate().y, bbox[0], bbox[1], bbox[2], bbox[3]);
+        int dx = (int) (256.0D * (geometry.getCoordinate().x - bbox[0]) / (bbox[2] - bbox[0]));
+        int dy = 256 - (int) (256.0D * (geometry.getCoordinate().y - bbox[1]) / (bbox[3] - bbox[1]));
+        int[] pxy = new int[]{dx, dy};
         //设置边框 实现轮廓
         graphics.setColor(shapeStyle.getColor());
         graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
@@ -251,28 +254,49 @@ public class ShapeUtils {
         }
     }
 
-    public static List<Map<String, Object>> detectClick(String bboxStr,List<Map<String, Object>> dataList, String shapeKey, List<LayerStyleView> layerStyleViews, int x, int y) {
+    public static List<Map<String, Object>> detectClick(String bboxStr, List<Map<String, Object>> dataList, String shapeKey, List<LayerStyleView> layerStyleViews, int x, int y) {
         List<Map<String, Object>> clickSelectedDataList = new CopyOnWriteArrayList<>();
+
+        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
         String[] split = bboxStr.split(",");
         double[] bbox = new double[split.length];
         for (int i = 0; i < split.length; i++) {
             bbox[i] = Double.parseDouble(split[i]);
         }
-        dataList.parallelStream().forEach(poi -> {
-            BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
-            Graphics2D graphics = image.createGraphics();
-            drawShape(getShapeStyle(layerStyleViews), poi, bbox, graphics, shapeKey);
-            //如果选中
-            int rgb = image.getRGB(x, y);
-            int alpha = (rgb >> 24) & 0xff; // 提取alpha值
-            if (alpha > 0) {
-                // 像素被填充
-                clickSelectedDataList.add(poi);
+        ShapeStyle shapeStyle = getShapeStyle(layerStyleViews);
+        Graphics2D graphics = image.createGraphics();
+        dataList.forEach(poi -> {
+            Geometry geometry;
+            try {
+                geometry = WKT_READER.read(poi.get(shapeKey).toString());
+            } catch (ParseException e) {
+                throw new RuntimeException(e);
+            }
+            if (geometry != null) {
+                drawGeometryShape(bbox, shapeStyle, graphics, poi, geometry);
+                int rgb = image.getRGB(x, y);
+                int alpha = (rgb >> 24) & 0xff; // 提取alpha值
+                if (alpha > 0) {
+                    // 像素被填充
+                    clickSelectedDataList.add(poi);
+                    image.setRGB(x,y,0x00000000);
+                }
             }
         });
+
         return clickSelectedDataList;
     }
 
+    private static void drawGeometryShape(double[] bbox, ShapeStyle shapeStyle, Graphics2D graphics, Map<String, Object> poi, Geometry geometry) {
+        if (StringUtils.equals(geometry.getGeometryType(), "Polygon")) {
+            drawPolygonShape(bbox, graphics, geometry, poi, shapeStyle);
+        } else if (StringUtils.equalsAny(geometry.getGeometryType(), "LinearRing", "LineString", "MultiLineString")) {
+            drawLineShape(bbox, graphics, geometry, shapeStyle);
+        } else if (StringUtils.equals(geometry.getGeometryType(), "Point")) {
+            drawPointShape(bbox, graphics, geometry, poi, shapeStyle);
+        }
+    }
+
     public static void renderShape(BufferedImage image, String bboxStr, List<Map<String, Object>> dataList, ShapeStyle shapeStyle, String shapeKey) {
         String[] split = bboxStr.split(",");
         double[] bbox = new double[split.length];
@@ -285,22 +309,14 @@ public class ShapeUtils {
 
     private static void drawShape(ShapeStyle shapeStyle, Map<String, Object> poi, double[] bbox, Graphics2D graphics, String shapeKey) {
 //        Geometry shape = GeoUtils.getGeoUtils().createGeometry(poi.get(shapeKey));
-
-        Geometry shape = null;
-        long l = System.currentTimeMillis();
+        Geometry shape;
         try {
-            shape = reader.read(poi.get(shapeKey).toString());
+            shape = WKT_READER.read(poi.get(shapeKey).toString());
         } catch (ParseException e) {
             throw new RuntimeException(e);
         }
         if (shape != null) {
-            if (StringUtils.equals(shape.getGeometryType(), "Polygon")) {
-                drawPolygonShape(bbox, graphics, shape, poi, shapeStyle);
-            } else if (StringUtils.equalsAny(shape.getGeometryType(), "LinearRing", "LineString", "MultiLineString")) {
-                drawLineShape(bbox, graphics, shape, shapeStyle);
-            } else if (StringUtils.equals(shape.getGeometryType(), "Point")) {
-                drawPointShape(bbox, graphics, shape, poi, shapeStyle);
-            }
+            drawGeometryShape(bbox, shapeStyle, graphics, poi, shape);
         }
     }
 
@@ -310,7 +326,7 @@ public class ShapeUtils {
         return image;
     }
 
-    public static ShapeStyle getShapeStyle(List<LayerStyleView> layerStyleViews){
+    public static ShapeStyle getShapeStyle(List<LayerStyleView> layerStyleViews) {
         ShapeStyle shapeStyle = new ShapeStyle();
         for (LayerStyleView layerStyleView : layerStyleViews) {
             BeanUtils.copyProperties(layerStyleView, shapeStyle);
@@ -328,8 +344,8 @@ public class ShapeUtils {
             setColorFromHex(layerStyleView.getFontColor(), shapeStyle::setFontColor);
             setImage(layerStyleView.getCenterPointImage(), shapeStyle::setCenterPointImage);
             setImage(layerStyleView.getPointMarkerImage(), shapeStyle::setPointMarkerImage);
-            if(shapeStyle.getPointMarkerImage()!=null){
-                shapeStyle.setPointMarkerImage(TileUtils.scaleWidthHeight(shapeStyle.getPointMarkerImage(),shapeStyle.getPointMarkerImageWidth(),shapeStyle.getPointMarkerImageHeight()));;
+            if (shapeStyle.getPointMarkerImage() != null) {
+                shapeStyle.setPointMarkerImage(TileUtils.scaleWidthHeight(shapeStyle.getPointMarkerImage(), shapeStyle.getPointMarkerImageWidth(), shapeStyle.getPointMarkerImageHeight()));
             }
         }
         return shapeStyle;