|
@@ -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);
|
|
|
+ }
|
|
|
}
|