TileUtils.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package cn.com.taiji.tile.util;
  2. import cn.com.taiji.tile.model.POI;
  3. import cn.com.taiji.tile.model.Point;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.locationtech.jts.geom.Coordinate;
  6. import org.locationtech.jts.geom.Geometry;
  7. import javax.imageio.ImageIO;
  8. import java.awt.*;
  9. import java.awt.image.BufferedImage;
  10. import java.io.ByteArrayOutputStream;
  11. import java.io.IOException;
  12. import java.util.*;
  13. import java.util.List;
  14. /**
  15. * @author zzyx 2023/12/5
  16. */
  17. public class TileUtils {
  18. public static byte[] getTile(List<Map<String,Object>> maps, String bboxStr){
  19. List<POI> list = convert(maps);
  20. return setColorTile(list,bboxStr);
  21. }
  22. public static List<POI> convert(List<Map<String, Object>> maps) {
  23. List<POI> pois = new ArrayList<>();
  24. for (Map<String, Object> map : maps) {
  25. POI poi = new POI();
  26. poi.setId((String) map.get("id"));
  27. poi.setName((String) map.get("id"));
  28. poi.setShape(map.get("location").toString());
  29. poi.setLat(0d);
  30. poi.setLon(0d);
  31. poi.setPcolor("rgb(255, 255, 0)");
  32. pois.add(poi);
  33. }
  34. return pois;
  35. }
  36. public static BufferedImage scaleWidthHeight(BufferedImage image, int width, int height) {
  37. BufferedImage i2 = new BufferedImage(width, height, 2);
  38. i2.getGraphics().drawImage(image.getScaledInstance(width, height, 4), 0, 0, null);
  39. return i2;
  40. }
  41. public static boolean ins(Point point1, Point point2) {
  42. double distance = point1.distanceTo(point2);
  43. double markerMusure = 12.0D;
  44. return (markerMusure >= distance);
  45. }
  46. public static byte[] setColorTile(List<POI> poiList, String bboxStr) {
  47. BufferedImage image = new BufferedImage(256, 256, 2);
  48. String[] split = bboxStr.split(",");
  49. // 接受前端传递的bbox 进行解析
  50. double[] bbox = new double[split.length];
  51. for (int i = 0; i < split.length; i++) {
  52. bbox[i] = Double.parseDouble(split[i]);
  53. }
  54. Graphics2D g = (Graphics2D) image.getGraphics();
  55. g.setColor(Color.GREEN);
  56. for (POI poi : poiList) {
  57. TileUtils.drawShapeToTile(poi, g, bbox, 1);
  58. }
  59. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  60. try {
  61. ImageIO.write(image, "png", buffer);
  62. } catch (IOException e) {
  63. e.printStackTrace();
  64. }
  65. return buffer.toByteArray();
  66. }
  67. public static void drawShapeToTile(POI poi, Graphics2D graphics, double[] bbox, int lineSize) {
  68. Geometry shape = GeoUtils.getGeoUtils().createGeometry(poi.getShape());
  69. if(StringUtils.isNotBlank(poi.getPcolor())) {
  70. String[] rgb = StringUtils.substring(poi.getPcolor(), poi.getPcolor().indexOf("rgb(") + 4, poi.getPcolor().length() - 1).split(",");
  71. Color color = new Color(Integer.parseInt(rgb[0].trim()), Integer.parseInt(rgb[1].trim()), Integer.parseInt(rgb[2].trim()));
  72. graphics.setColor(color);
  73. }
  74. if(shape!=null&&shape.getCoordinate()!=null&&StringUtils.equals(shape.getGeometryType(),"Point")){
  75. int[] pxy = TileUtils.toPixelXY(shape.getCoordinate().getX(),shape.getCoordinate().getY(), bbox[0], bbox[1], bbox[2], bbox[3]);
  76. graphics.drawArc(pxy[0], pxy[1], 6, 6, 0, 360);
  77. graphics.fillArc(pxy[0], pxy[1], 6, 6, 0, 360);
  78. }
  79. if (shape != null && shape.getCoordinates() != null && (shape.getCoordinates()).length > 1 &&
  80. StringUtils.isNotBlank(poi.getPcolor()) && StringUtils.indexOf(poi.getPcolor(), "rgb(") >= 0) {
  81. if (StringUtils.equals(shape.getGeometryType(), "Polygon") ||
  82. StringUtils.equals(shape.getGeometryType(), "LinearRing") ||
  83. StringUtils.equals(shape.getGeometryType(), "LineString") ||
  84. StringUtils.equals(shape.getGeometryType(), "MultiLineString")) {
  85. int[] xpoint = new int[(shape.getCoordinates()).length];
  86. int[] ypoint = new int[(shape.getCoordinates()).length];
  87. for (int i = 0; i < (shape.getCoordinates()).length; i++) {
  88. Coordinate c = shape.getCoordinates()[i];
  89. int[] pxy = toPixelXY(c.getX(), c.getY(), bbox[0], bbox[1], bbox[2], bbox[3]);
  90. xpoint[i] = pxy[0];
  91. ypoint[i] = pxy[1];
  92. }
  93. if (lineSize <= 0) {
  94. lineSize = 1;
  95. }
  96. graphics.setStroke(new BasicStroke(lineSize));
  97. //抗锯齿
  98. graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  99. if (StringUtils.equals(shape.getGeometryType(), "Polygon")) {
  100. //fill 是将设置的颜色全部填充
  101. graphics.fill(new Polygon(xpoint, ypoint, shape.getCoordinates().length));
  102. // graphics.drawPolygon(xpoint, ypoint, (shape.getCoordinates()).length);
  103. } else if (StringUtils.equalsAny(shape.getGeometryType(), "LinearRing", "MultiLineString", "LineString")) {
  104. graphics.drawPolyline(xpoint, ypoint, (shape.getCoordinates()).length);
  105. }
  106. }
  107. }
  108. }
  109. public byte[] newHotImageTileWithPoiList(List<POI> poiList, String bbox) {
  110. HotImageTile tile = new HotImageTile();
  111. return tile.setHotImage(poiList, bbox);
  112. }
  113. public static int[] toPixelXY(double x, double y, double minX, double minY, double maxX, double maxY) {
  114. int dx = (int) (256.0D * (x - minX) / (maxX - minX));
  115. int dy = 256 - (int) (256.0D * (y - minY) / (maxY - minY));
  116. return new int[]{dx, dy};
  117. }
  118. }