123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- package cn.com.taiji.tile.util;
- import cn.com.taiji.tile.model.POI;
- import cn.com.taiji.tile.model.Point;
- import org.apache.commons.lang3.StringUtils;
- import org.locationtech.jts.geom.Coordinate;
- import org.locationtech.jts.geom.Geometry;
- import javax.imageio.ImageIO;
- import java.awt.*;
- import java.awt.image.BufferedImage;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.util.*;
- import java.util.List;
- /**
- * @author zzyx 2023/12/5
- */
- public class TileUtils {
- public static byte[] getTile(List<Map<String,Object>> maps, String bboxStr){
- List<POI> list = convert(maps);
- return setColorTile(list,bboxStr);
- }
- public static List<POI> convert(List<Map<String, Object>> maps) {
- 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"));
- poi.setShape(map.get("location").toString());
- poi.setLat(0d);
- poi.setLon(0d);
- poi.setPcolor("rgb(255, 255, 0)");
- pois.add(poi);
- }
- return pois;
- }
- public static BufferedImage scaleWidthHeight(BufferedImage image, int width, int height) {
- BufferedImage i2 = new BufferedImage(width, height, 2);
- i2.getGraphics().drawImage(image.getScaledInstance(width, height, 4), 0, 0, null);
- return i2;
- }
- public static boolean ins(Point point1, Point point2) {
- double distance = point1.distanceTo(point2);
- double markerMusure = 12.0D;
- return (markerMusure >= distance);
- }
- public static byte[] setColorTile(List<POI> poiList, String bboxStr) {
- BufferedImage image = new BufferedImage(256, 256, 2);
- String[] split = bboxStr.split(",");
- // 接受前端传递的bbox 进行解析
- double[] bbox = new double[split.length];
- for (int i = 0; i < split.length; i++) {
- bbox[i] = Double.parseDouble(split[i]);
- }
- Graphics2D g = (Graphics2D) image.getGraphics();
- g.setColor(Color.GREEN);
- for (POI poi : poiList) {
- TileUtils.drawShapeToTile(poi, g, bbox, 1);
- }
- ByteArrayOutputStream buffer = new ByteArrayOutputStream();
- try {
- ImageIO.write(image, "png", buffer);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return buffer.toByteArray();
- }
- public static void drawShapeToTile(POI poi, Graphics2D graphics, double[] bbox, int lineSize) {
- Geometry shape = GeoUtils.getGeoUtils().createGeometry(poi.getShape());
- if(StringUtils.isNotBlank(poi.getPcolor())) {
- String[] rgb = StringUtils.substring(poi.getPcolor(), poi.getPcolor().indexOf("rgb(") + 4, poi.getPcolor().length() - 1).split(",");
- Color color = new Color(Integer.parseInt(rgb[0].trim()), Integer.parseInt(rgb[1].trim()), Integer.parseInt(rgb[2].trim()));
- graphics.setColor(color);
- }
- if(shape!=null&&shape.getCoordinate()!=null&&StringUtils.equals(shape.getGeometryType(),"Point")){
- int[] pxy = TileUtils.toPixelXY(shape.getCoordinate().getX(),shape.getCoordinate().getY(), bbox[0], bbox[1], bbox[2], bbox[3]);
- graphics.drawArc(pxy[0], pxy[1], 6, 6, 0, 360);
- graphics.fillArc(pxy[0], pxy[1], 6, 6, 0, 360);
- }
- if (shape != null && shape.getCoordinates() != null && (shape.getCoordinates()).length > 1 &&
- StringUtils.isNotBlank(poi.getPcolor()) && StringUtils.indexOf(poi.getPcolor(), "rgb(") >= 0) {
- if (StringUtils.equals(shape.getGeometryType(), "Polygon") ||
- StringUtils.equals(shape.getGeometryType(), "LinearRing") ||
- StringUtils.equals(shape.getGeometryType(), "LineString") ||
- StringUtils.equals(shape.getGeometryType(), "MultiLineString")) {
- int[] xpoint = new int[(shape.getCoordinates()).length];
- int[] ypoint = new int[(shape.getCoordinates()).length];
- for (int i = 0; i < (shape.getCoordinates()).length; i++) {
- Coordinate c = shape.getCoordinates()[i];
- int[] pxy = toPixelXY(c.getX(), c.getY(), bbox[0], bbox[1], bbox[2], bbox[3]);
- xpoint[i] = pxy[0];
- ypoint[i] = pxy[1];
- }
- if (lineSize <= 0) {
- lineSize = 1;
- }
- graphics.setStroke(new BasicStroke(lineSize));
- //抗锯齿
- graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- if (StringUtils.equals(shape.getGeometryType(), "Polygon")) {
- //fill 是将设置的颜色全部填充
- graphics.fill(new Polygon(xpoint, ypoint, shape.getCoordinates().length));
- // graphics.drawPolygon(xpoint, ypoint, (shape.getCoordinates()).length);
- } else if (StringUtils.equalsAny(shape.getGeometryType(), "LinearRing", "MultiLineString", "LineString")) {
- graphics.drawPolyline(xpoint, ypoint, (shape.getCoordinates()).length);
- }
- }
- }
- }
- public byte[] newHotImageTileWithPoiList(List<POI> poiList, String bbox) {
- HotImageTile tile = new HotImageTile();
- return tile.setHotImage(poiList, bbox);
- }
- public static int[] toPixelXY(double x, double y, double minX, double minY, double maxX, double maxY) {
- int dx = (int) (256.0D * (x - minX) / (maxX - minX));
- int dy = 256 - (int) (256.0D * (y - minY) / (maxY - minY));
- return new int[]{dx, dy};
- }
- }
|