|
@@ -1,30 +1,26 @@
|
|
package cn.com.taiji.tile.util;
|
|
package cn.com.taiji.tile.util;
|
|
|
|
|
|
import cn.com.taiji.common.model.LayerStyleView;
|
|
import cn.com.taiji.common.model.LayerStyleView;
|
|
-import cn.com.taiji.tile.model.POI;
|
|
|
|
import cn.com.taiji.tile.style.ShapeStyle;
|
|
import cn.com.taiji.tile.style.ShapeStyle;
|
|
import cn.hutool.core.util.BooleanUtil;
|
|
import cn.hutool.core.util.BooleanUtil;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.locationtech.jts.geom.Coordinate;
|
|
import org.locationtech.jts.geom.Coordinate;
|
|
import org.locationtech.jts.geom.Geometry;
|
|
import org.locationtech.jts.geom.Geometry;
|
|
|
|
+import org.locationtech.jts.io.ParseException;
|
|
|
|
+import org.locationtech.jts.io.WKTReader;
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.BeanUtils;
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
import javax.imageio.ImageIO;
|
|
import java.awt.*;
|
|
import java.awt.*;
|
|
import java.awt.image.BufferedImage;
|
|
import java.awt.image.BufferedImage;
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
-import java.io.InputStream;
|
|
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
-import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
import java.util.Objects;
|
|
import java.util.Objects;
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
import java.util.function.Consumer;
|
|
import java.util.function.Consumer;
|
|
-import java.util.stream.Collectors;
|
|
|
|
-
|
|
|
|
-import static org.apache.coyote.http11.Constants.a;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
* @author zzyx 2024/1/5
|
|
* @author zzyx 2024/1/5
|
|
@@ -36,43 +32,7 @@ public class ShapeUtils {
|
|
public static final int WIDTH = 256;
|
|
public static final int WIDTH = 256;
|
|
public static final int HEIGHT = 256;
|
|
public static final int HEIGHT = 256;
|
|
|
|
|
|
- /**
|
|
|
|
- * 获取瓦片字节数组
|
|
|
|
- *
|
|
|
|
- * @param bboxStr 逗号拼接的bbox字符串
|
|
|
|
- * @param dataList 数据集合
|
|
|
|
- * @return 瓦片字节数组
|
|
|
|
- */
|
|
|
|
- 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);
|
|
|
|
- //样式转换
|
|
|
|
- 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);
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- }
|
|
|
|
- return buffer.toByteArray();
|
|
|
|
- }
|
|
|
|
|
|
+ public final static WKTReader reader = new WKTReader();
|
|
|
|
|
|
|
|
|
|
private static void setImage(String imageUrl, Consumer<BufferedImage> imageConsumer) {
|
|
private static void setImage(String imageUrl, Consumer<BufferedImage> imageConsumer) {
|
|
@@ -110,15 +70,15 @@ public class ShapeUtils {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- private static void drawPolygonShape(double[] bbox, Graphics2D graphics, Geometry geometry, POI poi, ShapeStyle shapeStyle) {
|
|
|
|
|
|
+ private static void drawPolygonShape(double[] bbox, Graphics2D graphics, Geometry geometry, Map<String, Object> poi, ShapeStyle shapeStyle) {
|
|
drawPolygonToTile(shapeStyle, geometry, graphics, bbox);
|
|
drawPolygonToTile(shapeStyle, geometry, graphics, bbox);
|
|
//是否需要设置字名称
|
|
//是否需要设置字名称
|
|
- if (BooleanUtil.isTrue(shapeStyle.getIsShowName()) && StringUtils.isNotBlank(poi.getName())) {
|
|
|
|
|
|
+ if (BooleanUtil.isTrue(shapeStyle.getIsShowName()) && StringUtils.isNotBlank(poi.get("name").toString())) {
|
|
org.locationtech.jts.geom.Point centroid = geometry.getCentroid();
|
|
org.locationtech.jts.geom.Point centroid = geometry.getCentroid();
|
|
int[] pxy = TileUtils.toPixelXY(centroid.getX(), centroid.getY(), bbox[0], bbox[1], bbox[2], bbox[3]);
|
|
int[] pxy = TileUtils.toPixelXY(centroid.getX(), centroid.getY(), bbox[0], bbox[1], bbox[2], bbox[3]);
|
|
//设置字体样式
|
|
//设置字体样式
|
|
Font font = new Font("Arial", Font.TRUETYPE_FONT, shapeStyle.getFontSize());
|
|
Font font = new Font("Arial", Font.TRUETYPE_FONT, shapeStyle.getFontSize());
|
|
- String s = poi.getName();
|
|
|
|
|
|
+ String s = poi.get("name").toString();
|
|
graphics.setColor(shapeStyle.getFontColor());
|
|
graphics.setColor(shapeStyle.getFontColor());
|
|
graphics.setFont(font);
|
|
graphics.setFont(font);
|
|
graphics.drawString(s, pxy[0], pxy[1]);
|
|
graphics.drawString(s, pxy[0], pxy[1]);
|
|
@@ -149,23 +109,23 @@ public class ShapeUtils {
|
|
graphics.drawPolyline(xpoint, ypoint, geometry.getCoordinates().length);
|
|
graphics.drawPolyline(xpoint, ypoint, geometry.getCoordinates().length);
|
|
}
|
|
}
|
|
|
|
|
|
- public static void drawPointShape(double[] bbox, Graphics2D graphics, Geometry geometry, POI poi, ShapeStyle shapeStyle) {
|
|
|
|
|
|
+ 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]);
|
|
//设置边框 实现轮廓
|
|
//设置边框 实现轮廓
|
|
graphics.setColor(shapeStyle.getColor());
|
|
graphics.setColor(shapeStyle.getColor());
|
|
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
//设置字体
|
|
//设置字体
|
|
//设置字体样式
|
|
//设置字体样式
|
|
- if (BooleanUtil.isTrue(shapeStyle.getIsShowName())) {
|
|
|
|
|
|
+ if (BooleanUtil.isTrue(shapeStyle.getIsShowName()) && StringUtils.isNotBlank(poi.get("name").toString())) {
|
|
Font font = new Font("Arial", Font.BOLD, shapeStyle.getFontSize());
|
|
Font font = new Font("Arial", Font.BOLD, shapeStyle.getFontSize());
|
|
- String s = poi.getName();
|
|
|
|
|
|
+ String s = poi.get("name").toString();
|
|
graphics.setColor(shapeStyle.getFontColor());
|
|
graphics.setColor(shapeStyle.getFontColor());
|
|
graphics.setFont(font);
|
|
graphics.setFont(font);
|
|
graphics.drawString(s, pxy[0], pxy[1]);
|
|
graphics.drawString(s, pxy[0], pxy[1]);
|
|
}
|
|
}
|
|
BufferedImage pointMarkerImage = shapeStyle.getPointMarkerImage();
|
|
BufferedImage pointMarkerImage = shapeStyle.getPointMarkerImage();
|
|
if (pointMarkerImage != null) {
|
|
if (pointMarkerImage != null) {
|
|
- graphics.drawImage(TileUtils.scaleWidthHeight(pointMarkerImage, shapeStyle.getPointMarkerImageWidth(), shapeStyle.getPointMarkerImageHeight()), pxy[0], pxy[1], null);
|
|
|
|
|
|
+ graphics.drawImage(pointMarkerImage, pxy[0], pxy[1], null);
|
|
} else {
|
|
} else {
|
|
drawPointShape(shapeStyle, graphics, pxy);
|
|
drawPointShape(shapeStyle, graphics, pxy);
|
|
}
|
|
}
|
|
@@ -291,9 +251,8 @@ public class ShapeUtils {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- public static List<POI> detectClick(String bboxStr, List<POI> dataList, ShapeStyle shapeStyle, int x, int y) {
|
|
|
|
- List<POI> clickSelectedDataList = new CopyOnWriteArrayList<>();
|
|
|
|
-// renderAndDetectClick(image, bboxStr, dataList, shapeStyle, x, y, clickSelectedDataList, true);
|
|
|
|
|
|
+ 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<>();
|
|
String[] split = bboxStr.split(",");
|
|
String[] split = bboxStr.split(",");
|
|
double[] bbox = new double[split.length];
|
|
double[] bbox = new double[split.length];
|
|
for (int i = 0; i < split.length; i++) {
|
|
for (int i = 0; i < split.length; i++) {
|
|
@@ -302,7 +261,7 @@ public class ShapeUtils {
|
|
dataList.parallelStream().forEach(poi -> {
|
|
dataList.parallelStream().forEach(poi -> {
|
|
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
|
|
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
|
|
Graphics2D graphics = image.createGraphics();
|
|
Graphics2D graphics = image.createGraphics();
|
|
- drawShape(shapeStyle, poi, bbox, graphics);
|
|
|
|
|
|
+ drawShape(getShapeStyle(layerStyleViews), poi, bbox, graphics, shapeKey);
|
|
//如果选中
|
|
//如果选中
|
|
int rgb = image.getRGB(x, y);
|
|
int rgb = image.getRGB(x, y);
|
|
int alpha = (rgb >> 24) & 0xff; // 提取alpha值
|
|
int alpha = (rgb >> 24) & 0xff; // 提取alpha值
|
|
@@ -314,20 +273,27 @@ public class ShapeUtils {
|
|
return clickSelectedDataList;
|
|
return clickSelectedDataList;
|
|
}
|
|
}
|
|
|
|
|
|
- public static void renderShape(BufferedImage image, String bboxStr, List<POI> dataList, ShapeStyle shapeStyle) {
|
|
|
|
|
|
+ public static void renderShape(BufferedImage image, String bboxStr, List<Map<String, Object>> dataList, ShapeStyle shapeStyle, String shapeKey) {
|
|
String[] split = bboxStr.split(",");
|
|
String[] split = bboxStr.split(",");
|
|
double[] bbox = new double[split.length];
|
|
double[] bbox = new double[split.length];
|
|
for (int i = 0; i < split.length; i++) {
|
|
for (int i = 0; i < split.length; i++) {
|
|
bbox[i] = Double.parseDouble(split[i]);
|
|
bbox[i] = Double.parseDouble(split[i]);
|
|
}
|
|
}
|
|
Graphics2D graphics = image.createGraphics();
|
|
Graphics2D graphics = image.createGraphics();
|
|
- long l = System.currentTimeMillis();
|
|
|
|
- dataList.parallelStream().forEach(poi -> drawShape(shapeStyle, poi, bbox, graphics));
|
|
|
|
|
|
+ dataList.parallelStream().forEach(poi -> drawShape(shapeStyle, poi, bbox, graphics, shapeKey));
|
|
}
|
|
}
|
|
|
|
|
|
- private static void drawShape(ShapeStyle shapeStyle, POI poi, double[] bbox, Graphics2D graphics) {
|
|
|
|
- Geometry shape = GeoUtils.getGeoUtils().createGeometry(poi.getShape());
|
|
|
|
- if(shape!=null) {
|
|
|
|
|
|
+ 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();
|
|
|
|
+ try {
|
|
|
|
+ shape = reader.read(poi.get(shapeKey).toString());
|
|
|
|
+ } catch (ParseException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ if (shape != null) {
|
|
if (StringUtils.equals(shape.getGeometryType(), "Polygon")) {
|
|
if (StringUtils.equals(shape.getGeometryType(), "Polygon")) {
|
|
drawPolygonShape(bbox, graphics, shape, poi, shapeStyle);
|
|
drawPolygonShape(bbox, graphics, shape, poi, shapeStyle);
|
|
} else if (StringUtils.equalsAny(shape.getGeometryType(), "LinearRing", "LineString", "MultiLineString")) {
|
|
} else if (StringUtils.equalsAny(shape.getGeometryType(), "LinearRing", "LineString", "MultiLineString")) {
|
|
@@ -338,10 +304,14 @@ public class ShapeUtils {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- public static void drawImage(String bboxStr, List<POI> dataList, List<LayerStyleView> layerStyleViews, Graphics2D g2) {
|
|
|
|
- ShapeStyle shapeStyle=new ShapeStyle();
|
|
|
|
|
|
+ public static BufferedImage drawImage(String bboxStr, List<Map<String, Object>> dataList, String shapeKey, List<LayerStyleView> layerStyleViews) {
|
|
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
|
|
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
|
|
- //样式转换
|
|
|
|
|
|
+ renderShape(image, bboxStr, dataList, getShapeStyle(layerStyleViews), shapeKey);
|
|
|
|
+ return image;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static ShapeStyle getShapeStyle(List<LayerStyleView> layerStyleViews){
|
|
|
|
+ ShapeStyle shapeStyle = new ShapeStyle();
|
|
for (LayerStyleView layerStyleView : layerStyleViews) {
|
|
for (LayerStyleView layerStyleView : layerStyleViews) {
|
|
BeanUtils.copyProperties(layerStyleView, shapeStyle);
|
|
BeanUtils.copyProperties(layerStyleView, shapeStyle);
|
|
|
|
|
|
@@ -358,8 +328,10 @@ public class ShapeUtils {
|
|
setColorFromHex(layerStyleView.getFontColor(), shapeStyle::setFontColor);
|
|
setColorFromHex(layerStyleView.getFontColor(), shapeStyle::setFontColor);
|
|
setImage(layerStyleView.getCenterPointImage(), shapeStyle::setCenterPointImage);
|
|
setImage(layerStyleView.getCenterPointImage(), shapeStyle::setCenterPointImage);
|
|
setImage(layerStyleView.getPointMarkerImage(), shapeStyle::setPointMarkerImage);
|
|
setImage(layerStyleView.getPointMarkerImage(), shapeStyle::setPointMarkerImage);
|
|
|
|
+ if(shapeStyle.getPointMarkerImage()!=null){
|
|
|
|
+ shapeStyle.setPointMarkerImage(TileUtils.scaleWidthHeight(shapeStyle.getPointMarkerImage(),shapeStyle.getPointMarkerImageWidth(),shapeStyle.getPointMarkerImageHeight()));;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- renderShape(image, bboxStr, dataList, shapeStyle);
|
|
|
|
- g2.drawImage(image, 0, 0, null);
|
|
|
|
|
|
+ return shapeStyle;
|
|
}
|
|
}
|
|
}
|
|
}
|