Browse Source

样式调整修改

zhouyuexiang 1 year ago
parent
commit
27944986ca

+ 27 - 54
tile-service/src/main/java/cn/com/taiji/tile/service/impl/TileServiceImpl.java

@@ -15,10 +15,13 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import javax.imageio.ImageIO;
+import java.awt.*;
 import java.awt.image.BufferedImage;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.*;
+import java.util.List;
 
 @Slf4j
 @Service
@@ -29,15 +32,16 @@ public class TileServiceImpl implements ITileService {
 
 
     @Override
-    public byte[] tileData(GeoRequestParam requestParam, List<LayerGroupDsView> layerGroupDsViewList,Map<String,List<LayerStyleView>> dataMaps) {
-
-        for(LayerGroupDsView layerGroupDsView:layerGroupDsViewList){
+    public byte[] tileData(GeoRequestParam requestParam, List<LayerGroupDsView> layerGroupDsViewList, Map<String, List<LayerStyleView>> dataMaps) {
+        BufferedImage image = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
+        Graphics2D g2 = image.createGraphics();
+        for (LayerGroupDsView layerGroupDsView : layerGroupDsViewList) {
 
 
             //获取图层代码对应的样式
             List<LayerStyleView> layerStyleViewList = new ArrayList<LayerStyleView>();
 
-            if(dataMaps.containsKey(layerGroupDsView.getLayerCode())){
+            if (dataMaps.containsKey(layerGroupDsView.getLayerCode())) {
 
                 layerStyleViewList = dataMaps.get(layerGroupDsView.getLayerCode());
 
@@ -52,13 +56,12 @@ public class TileServiceImpl implements ITileService {
 
             String filterCql = requestParam.getCqlFilter();
 
-            if(filterCql !=null && !"".equals(filterCql)){
+            if (filterCql != null && !"".equals(filterCql)) {
 
                 filterCqlStr.append(filterCql);
             }
 
             String bbox = requestParam.getBbox();
-
             if(bbox !=null && !"".equals(bbox)){
 
                 StringBuffer sbBbox = new StringBuffer();
@@ -80,14 +83,14 @@ public class TileServiceImpl implements ITileService {
             String layerDataType = layerGroupDsView.getLayerDataType();
             String layerDataRun = layerGroupDsView.getLayerDataRun();
 
-            if(layerDataType !=null && !"".equals(layerDataType) && "cql".equals(layerDataType)){
+            if (layerDataType != null && !"".equals(layerDataType) && "cql".equals(layerDataType)) {
 
-                if(layerDataRun !=null && !"".equals(layerDataRun)){
+                if (layerDataRun != null && !"".equals(layerDataRun)) {
 
-                    if(filterCqlStr.length() >0){
+                    if (filterCqlStr.length() > 0) {
 
-                        filterCqlStr.append(" and "+layerDataRun);
-                    }else{
+                        filterCqlStr.append(" and " + layerDataRun);
+                    } else {
 
                         filterCqlStr.append(layerDataRun);
 
@@ -110,42 +113,15 @@ public class TileServiceImpl implements ITileService {
             cqlDsParams.put(JDBCDataStoreFactory.USER.key, layerGroupDsView.getUserName());
             cqlDsParams.put(JDBCDataStoreFactory.PASSWD.key, layerGroupDsView.getUserPassword());
             cqlDsParams.put("tableName", layerGroupDsView.getTableName());
-
-
             Map<String, Object> mapParams = new HashMap<String, Object>();
-
-            if(filterCqlStr.length() >0){
-
+            if (filterCqlStr.length() > 0) {
                 mapParams.put("cql", filterCqlStr.toString());
             }
-
-
             PageResult pageResult = ecqlService.cqlQuery(cqlDsParams, mapParams);
-
             if (pageResult.getRecordCount() > 0) {
-
                 List<Map<String, Object>> cqlDataList = (List<Map<String, Object>>) pageResult.getDatas();
-
                 //3根据空间地理数据生成图层二进制数据
-//            System.out.println("返回的数据:" + cqlDataList.toString());
-
-                System.out.println("数据查询耗时:"+(System.currentTimeMillis()-currentTimeMillis));
-                long start = System.currentTimeMillis();
-                //根据图层名称在mysql表中查询样式
-                ShapeStyle shapeStyle = new ShapeStyle();
-                InputStream in = ClassLoader.getSystemResourceAsStream("images/gis-layout-tools_example-axcbypyj.png");
-                try {
-                    BufferedImage image = ImageIO.read(in);
-                    shapeStyle.setHeight(12);
-                    shapeStyle.setWidth(12);
-                    shapeStyle.setPointMarkerImage(image);
-                } catch (IOException e) {
-                    throw new RuntimeException(e);
-                }
-                byte[] tileByte = ShapeUtils.getTileByte(requestParam.getBbox(), convert(cqlDataList), shapeStyle);
-                long end = System.currentTimeMillis();
-                System.out.println("根据数据生成瓦片耗时"+(end-start));
-                return tileByte;
+                ShapeUtils.drawImage(requestParam.getBbox(), convert(cqlDataList,layerGroupDsView.getBoxColumn()), layerStyleViewList,g2);
 
             } else {
 
@@ -153,28 +129,25 @@ public class TileServiceImpl implements ITileService {
             }
 
         }
-
-        return new byte[0];
+        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+        try {
+            ImageIO.write(image, "png", buffer);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return buffer.toByteArray();
     }
 
-    public static List<POI> convert(List<Map<String, Object>> maps) {
+    public static List<POI> convert(List<Map<String, Object>> maps,String column) {
         List<POI> pois = new ArrayList<>();
         for (Map<String, Object> map : maps) {
+
             POI poi = new POI();
-            poi.setId((String) map.get("id"));
-            poi.setName((String) map.get("id"));
-            if (Objects.nonNull(map.get("location"))) {
-                poi.setShape(map.get("location").toString());
-            } else {
-                poi.setShape(String.format("POINT(%s %s)", map.get("longitude"), map.get("latitude")));
-//            poi.setLat(Double.parseDouble((String)map.get("latitude")));
-//            poi.setLon(Double.parseDouble((String)map.get("longitude")));
-            }
-            poi.setPcolor("rgb(255, 255, 0)");
+            poi.setShape(map.get(column).toString());
             pois.add(poi);
         }
         return pois;
-    }
+}
 
     @Override
     public List<?> queryElements(String bbox, Integer type, String layerName) {

+ 102 - 32
tile-service/src/main/java/cn/com/taiji/tile/util/ShapeUtils.java

@@ -1,24 +1,35 @@
 package cn.com.taiji.tile.util;
 
+import cn.com.taiji.common.model.LayerStyleView;
 import cn.com.taiji.tile.model.POI;
 import cn.com.taiji.tile.style.ShapeStyle;
 import cn.hutool.core.util.BooleanUtil;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.locationtech.jts.geom.Coordinate;
 import org.locationtech.jts.geom.Geometry;
+import org.springframework.beans.BeanUtils;
 
 import javax.imageio.ImageIO;
 import java.awt.*;
 import java.awt.image.BufferedImage;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Objects;
 import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+
+import static org.apache.coyote.http11.Constants.a;
 
 /**
  * @author zzyx 2024/1/5
  */
+@Slf4j
 public class ShapeUtils {
 
 
@@ -30,12 +41,30 @@ public class ShapeUtils {
      *
      * @param bboxStr    逗号拼接的bbox字符串
      * @param dataList   数据集合
-     * @param shapeStyle 样式
      * @return 瓦片字节数组
      */
-    public static byte[] getTileByte(String bboxStr, List<POI> dataList, ShapeStyle shapeStyle) {
+    public static byte[] getTileByte(String bboxStr, List<POI> dataList, List<LayerStyleView>  layerStyleViews) {
+        ShapeStyle shapeStyle=new ShapeStyle();
         BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
-        renderAndDetectClick(image, bboxStr, dataList, shapeStyle);
+        //样式转换
+        for (LayerStyleView layerStyleView : layerStyleViews) {
+            BeanUtils.copyProperties(layerStyleView, shapeStyle);
+
+            setColorFromHex(layerStyleView.getColor(), shapeStyle::setColor);
+            shapeStyle.setIsFill(Objects.equals(layerStyleView.getIsFill(), 1));
+
+            setColorFromHex(layerStyleView.getBorderColor(), shapeStyle::setBorderColor);
+            shapeStyle.setIsDashed(Objects.equals(layerStyleView.getIsDashed(), 1));
+
+            setDashPattern(layerStyleView.getDashPattern(), shapeStyle::setDashPattern);
+            setImage(layerStyleView.getBgImage(), shapeStyle::setBgImage);
+            shapeStyle.setIsShowName(Objects.equals(layerStyleView.getIsShowName(), 1));
+
+            setColorFromHex(layerStyleView.getFontColor(), shapeStyle::setFontColor);
+            setImage(layerStyleView.getCenterPointImage(), shapeStyle::setCenterPointImage);
+            setImage(layerStyleView.getPointMarkerImage(), shapeStyle::setPointMarkerImage);
+        }
+        renderShape(image, bboxStr, dataList, shapeStyle);
         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
         try {
             ImageIO.write(image, "png", buffer);
@@ -46,6 +75,41 @@ public class ShapeUtils {
     }
 
 
+    private static void setImage(String imageUrl, Consumer<BufferedImage> imageConsumer) {
+        if (StringUtils.isNotBlank(imageUrl)) {
+            try {
+                URL url = new URL(imageUrl);
+                BufferedImage image = ImageIO.read(url);
+                imageConsumer.accept(image);
+            } catch (IOException e) {
+                log.error("Error loading image from URL: {}", imageUrl, e);
+            }
+        }
+    }
+
+    private static void setColorFromHex(String hexColor, Consumer<Color> colorConsumer) {
+        if (StringUtils.isNotBlank(hexColor)) {
+            try {
+                int[] rgba = hexToRgba(hexColor, 255);
+                colorConsumer.accept(new Color(rgba[0], rgba[1], rgba[2], rgba[3]));
+            } catch (IllegalArgumentException e) {
+                log.error("Invalid hex color format: {}", hexColor, e);
+            }
+        }
+    }
+
+    private static void setDashPattern(String dashPatternStr, Consumer<float[]> dashPatternConsumer) {
+        if (StringUtils.isNotBlank(dashPatternStr)) {
+            String[] split = dashPatternStr.split(",");
+            float[] dashPatternArr = new float[split.length];
+            for (int i = 0; i < split.length; i++) {
+                dashPatternArr[i] = Float.parseFloat(split[i]);
+            }
+            dashPatternConsumer.accept(dashPatternArr);
+        }
+    }
+
+
     private static void drawPolygonShape(double[] bbox, Graphics2D graphics, Geometry geometry, POI poi, ShapeStyle shapeStyle) {
         drawPolygonToTile(shapeStyle, geometry, graphics, bbox);
         //是否需要设置字名称
@@ -101,7 +165,7 @@ public class ShapeUtils {
         }
         BufferedImage pointMarkerImage = shapeStyle.getPointMarkerImage();
         if (pointMarkerImage != null) {
-            graphics.drawImage(TileUtils.scaleWidthHeight(pointMarkerImage, shapeStyle.getWidth(), shapeStyle.getHeight()), pxy[0], pxy[1], null);
+            graphics.drawImage(TileUtils.scaleWidthHeight(pointMarkerImage, shapeStyle.getPointMarkerImageWidth(), shapeStyle.getPointMarkerImageHeight()), pxy[0], pxy[1], null);
         } else {
             drawPointShape(shapeStyle, graphics, pxy);
         }
@@ -182,23 +246,11 @@ public class ShapeUtils {
         }
     }
 
-    /**
-     * Converts a hexadecimal color string to RGBA format.
-     *
-     * @param hexColor The hexadecimal color string (e.g., "#2860F1").
-     * @param alpha    The alpha value (0-255) for opacity.
-     * @return An array containing the RGBA values.
-     */
     public static int[] hexToRgba(String hexColor, int alpha) {
-        // Remove the hash (#) sign if it exists
         hexColor = hexColor.replace("#", "");
-
-        // Parse the string
         int red = Integer.valueOf(hexColor.substring(0, 2), 16);
         int green = Integer.valueOf(hexColor.substring(2, 4), 16);
         int blue = Integer.valueOf(hexColor.substring(4, 6), 16);
-
-        // Return the RGBA values
         return new int[]{red, green, blue, alpha};
     }
 
@@ -250,20 +302,10 @@ public class ShapeUtils {
         dataList.parallelStream().forEach(poi -> {
             BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
             Graphics2D graphics = image.createGraphics();
-            Geometry shape = GeoUtils.getGeoUtils().createGeometry(poi.getShape());
-//            //判断数据点线面
-            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);
-            }
+            drawShape(shapeStyle, poi, bbox, graphics);
             //如果选中
             int rgb = image.getRGB(x, y);
-            System.out.println("rgb---->" + image.getRGB(x, y));
             int alpha = (rgb >> 24) & 0xff; // 提取alpha值
-//                System.out.println("RGB->"+alpha);
             if (alpha > 0) {
                 // 像素被填充
                 clickSelectedDataList.add(poi);
@@ -272,16 +314,20 @@ public class ShapeUtils {
         return clickSelectedDataList;
     }
 
-    public static void renderAndDetectClick(BufferedImage image, String bboxStr, List<POI> dataList, ShapeStyle shapeStyle) {
+    public static void renderShape(BufferedImage image, String bboxStr, List<POI> dataList, ShapeStyle shapeStyle) {
         String[] split = bboxStr.split(",");
         double[] bbox = new double[split.length];
         for (int i = 0; i < split.length; i++) {
             bbox[i] = Double.parseDouble(split[i]);
         }
         Graphics2D graphics = image.createGraphics();
-        dataList.parallelStream().forEach(poi -> {
-            Geometry shape = GeoUtils.getGeoUtils().createGeometry(poi.getShape());
-//            //判断数据点线面
+        long l = System.currentTimeMillis();
+        dataList.parallelStream().forEach(poi -> drawShape(shapeStyle, poi, bbox, graphics));
+    }
+
+    private static void drawShape(ShapeStyle shapeStyle, POI poi, double[] bbox, Graphics2D graphics) {
+        Geometry shape = GeoUtils.getGeoUtils().createGeometry(poi.getShape());
+        if(shape!=null) {
             if (StringUtils.equals(shape.getGeometryType(), "Polygon")) {
                 drawPolygonShape(bbox, graphics, shape, poi, shapeStyle);
             } else if (StringUtils.equalsAny(shape.getGeometryType(), "LinearRing", "LineString", "MultiLineString")) {
@@ -289,7 +335,31 @@ public class ShapeUtils {
             } else if (StringUtils.equals(shape.getGeometryType(), "Point")) {
                 drawPointShape(bbox, graphics, shape, poi, shapeStyle);
             }
-        });
+        }
     }
 
+    public static void drawImage(String bboxStr, List<POI> dataList, List<LayerStyleView> layerStyleViews, Graphics2D g2) {
+        ShapeStyle shapeStyle=new ShapeStyle();
+        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
+        //样式转换
+        for (LayerStyleView layerStyleView : layerStyleViews) {
+            BeanUtils.copyProperties(layerStyleView, shapeStyle);
+
+            setColorFromHex(layerStyleView.getColor(), shapeStyle::setColor);
+            shapeStyle.setIsFill(Objects.equals(layerStyleView.getIsFill(), 1));
+
+            setColorFromHex(layerStyleView.getBorderColor(), shapeStyle::setBorderColor);
+            shapeStyle.setIsDashed(Objects.equals(layerStyleView.getIsDashed(), 1));
+
+            setDashPattern(layerStyleView.getDashPattern(), shapeStyle::setDashPattern);
+            setImage(layerStyleView.getBgImage(), shapeStyle::setBgImage);
+            shapeStyle.setIsShowName(Objects.equals(layerStyleView.getIsShowName(), 1));
+
+            setColorFromHex(layerStyleView.getFontColor(), shapeStyle::setFontColor);
+            setImage(layerStyleView.getCenterPointImage(), shapeStyle::setCenterPointImage);
+            setImage(layerStyleView.getPointMarkerImage(), shapeStyle::setPointMarkerImage);
+        }
+        renderShape(image, bboxStr, dataList, shapeStyle);
+        g2.drawImage(image, 0, 0, null);
+    }
 }

+ 4 - 1
tile-web/src/main/java/cn/com/taiji/web/service/impl/GeoDataSourceServiceImpl.java

@@ -8,6 +8,7 @@ import cn.com.taiji.cql.service.IECqlService;
 import cn.com.taiji.tile.util.StringUtils;
 import cn.com.taiji.web.config.GeoConfig;
 import cn.com.taiji.web.service.IGeoDataSourceService;
+import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.date.DatePattern;
 import cn.hutool.core.date.DateTime;
 import cn.hutool.core.date.DateUtil;
@@ -82,7 +83,9 @@ public class GeoDataSourceServiceImpl implements IGeoDataSourceService {
     public Map<String,List<LayerStyleView>> layerCodeShapeStyle(List<String> layerCodeList) throws Exception {
 
         Map<String,List<LayerStyleView>> dataMaps = new HashMap<String,List<LayerStyleView>>();
-
+        if(CollUtil.isEmpty(layerCodeList)){
+            return dataMaps;
+        }
         Map<String, Object> dsParams = new HashMap<String, Object>();
         dsParams.put(JDBCDataStoreFactory.DATABASE.key, geoConfig.getDsBase());
         dsParams.put(JDBCDataStoreFactory.DBTYPE.key, geoConfig.getDbType());