Java对点、线、面生成栅格瓦片jpg,并渲染呈现
生活随笔
收集整理的這篇文章主要介紹了
Java对点、线、面生成栅格瓦片jpg,并渲染呈现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java對點、線、面生成柵格瓦片jpg,并渲染呈現
- 1. 效果圖
- 2. 原理
- 2.1 面瓦片的生成
- 2.2 線瓦片的生成
- 2.3 多點瓦片的生成
- 3. 源碼
- 參考
這篇博客將介紹從前端HTML頁面到后端預生成柵格瓦片jpg,并提供查詢接口供前端html調用呈現效果圖;
1. 效果圖
隨機畫一個多邊形,以這個多邊形面進行柵格瓦片生成;
點線面的渲染效果圖如下:
多點為藍色,線為綠色,面為紅色
面生成為背景黑色的柵格瓦片,點線生成為背景白色的柵格瓦片效果圖如下:
紅色面的渲染效果圖如下:
級別小一些只有一張圖,和級別大一些多張圖拼合而成,背景色可以是白色的,也可以是黑色的~;
藍色點的渲染效果圖如下:
級別大一些藍色點的渲染效果圖如下:
綠色線的渲染效果圖如下:(1/500的分辨率認為點在線上)
1/20分辨率距離則認為點在線上效果圖,看起來線要寬一些;
2. 原理
2.1 面瓦片的生成
- 先計算多邊形面的Geometry經緯度范圍(外接矩形的左上角,右下角坐標);
- 經緯度坐標轉web墨卡托坐標;
- 指定某一級別計算瓦片號范圍;
- 分別遍歷每一個瓦片(計算瓦片范圍與多邊形面是否有交集,沒有交集生成一張空白圖;有交集遍歷每一個像素點轉換為web墨卡托坐標,轉換為Geometry坐標,判斷點是否在多邊形面上,在設置當前像素點顏色;不在給空白);
- 循環遍歷多個級別,重復1~4步驟,即可生成多個不同級別
2.2 線瓦片的生成
- 先計算 多線 的Geometry經緯度范圍(外接矩形的左上角,右下角坐標);
- 經緯度坐標轉web墨卡托坐標;
- 指定某一級別計算瓦片號范圍;
- 分別遍歷每一個瓦片(計算瓦片范圍與 線 是否有交集,沒有交集生成一張空白圖;有交集遍歷每一個像素點轉換為web墨卡托坐標,轉換為Geometry 點坐標, 判斷點是否在線上 ,在設置當前像素點顏色;不在給空白);
- 循環遍歷多個級別,重復1~4步驟,即可生成多個不同級別
判斷點是否在線上需要注意,可參考谷歌js提供的算法,isPointOnSegment 計算距離允許誤差在分辨率范圍內,則標識在線上;
2.3 多點瓦片的生成
- 先計算 多點 的Geometry經緯度范圍(外接矩形的左上角,右下角坐標);
- 經緯度坐標轉web墨卡托坐標;
- 指定某一級別計算瓦片號范圍;
- 分別遍歷每一個瓦片(計算瓦片范圍與 多點 是否有交集,沒有交集生成一張空白圖;有交集 求瓦片范圍與多點的交集點,然后遍歷交集點,判斷點是否在線上 ,在設置當前像素點顏色;不在給空白);
- 循環遍歷多個級別,重復1~4步驟,即可生成多個不同級別
3. 源碼
package com.demo.process;import com.demo.model.render.Pixel;
import com.demo.model.render.Tile;
import com.demo.util.JtsUtils;
import com.demo.util.MercatorTransform;
import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;import static java.lang.Math.max;
import static java.lang.Math.min;/**************************************Class Name: Geoms2JpgTile*Description: <多點、線、面生成瓦片>*@author: Seminar*@create: 2021/7/19*@since 1.0.0*************************************/
@Slf4j
public class Geoms2JpgTile {private GeometryFactory gf = new GeometryFactory();static MercatorTransform mercatorTransform = new MercatorTransform();public static int WIDTH = 256;public static int HEIGHT = 256;public static int BACKCOLOR_WHITE = ((255 << 16) | ((255 << 8) | 255));public static int BACKCOLOR_BLACK = ((0 << 16) | ((0 << 8) | 0));// 多邊形幾何String polygon;// 線幾何String lineString;// 多邊幾何String multiPoint;/*** 初始化wkt*/public void initWkt() {this.polygon = "POLYGON ((116.42486572265626 39.99500778093748, 116.51962280273439 39.886557705928475, 116.44546508789064 39.78110197709871, 116.31637573242189 39.818029898770206, 116.27655029296876 39.93817189499188, 116.42486572265626 39.99500778093748))";this.lineString = "LINESTRING (117.18292236328126 40.16208338164619, 119.01489257812501 39.48284540453334)";this.multiPoint = "MULTIPOINT ((115.48690795898439 40.12639098502455), (115.80139160156251 40.148438503139076), (115.83847045898439 39.9665957444875), (115.90850830078126 39.854937988531276), " +"(115.98403930664062 39.816975090490004), (115.84808349609376 39.769491963709), (115.60638427734376 39.707186656826565), (115.44708251953126 39.82119422647455), " +"(115.32348632812501 39.961332959837826), (115.36605834960939 40.09593265290902), (115.59951782226564 39.940277770390324), (115.74371337890626 39.842286020743394), " +"(115.58715820312501 39.79271003204449), (115.76019287109376 39.7631584037253), (115.87280273437501 39.67759833072648), (115.96481323242189 40.18307014852534), " +"(115.69152832031251 40.2155868104582), (115.63934326171876 40.073868105094846), (115.43060302734376 39.56547053068436), (115.66955566406251 39.470125122358176), " +"(115.83709716796875 39.55911824217187), (115.91949462890626 39.44679856427205), (115.54046630859376 39.42346418978385), (115.07354736328125 39.63319206567459), " +"(115.11474609375 40.092781012494065), (115.19439697265626 40.287906612507406), (114.98291015625001 39.83385008019448), (114.97467041015626 39.47224533091451), " +"(115.27130126953126 39.38101803294523), (115.59265136718751 39.34067026099156))";}/*** 面jpg瓦片生成** @param zoom* @throws IOException* @throws ParseException*/public void polygon2Tile(String wkt, int zoom) throws IOException, ParseException {Geometry geom = wkt2Geo(wkt);Geometry envelope = geom.getEnvelope();double lonMin = min(envelope.getCoordinates()[0].x, envelope.getCoordinates()[2].x);double lonMax = max(envelope.getCoordinates()[0].x, envelope.getCoordinates()[2].x);double latMin = min(envelope.getCoordinates()[0].y, envelope.getCoordinates()[2].y);double latMax = max(envelope.getCoordinates()[0].y, envelope.getCoordinates()[2].y);Tile t1 = latLng2Tile(lonMin, latMin, zoom);Tile t2 = latLng2Tile(lonMax, latMax, zoom);long tilexMin = min(t1.getX(), t2.getX());long tileyMin = min(t1.getY(), t2.getY());long tilexMax = max(t1.getX(), t2.getX());long tileyMax = max(t1.getY(), t2.getY());if (!new File("D:\\learn1\\geojson-demo\\jpg\\" + zoom).exists()) {FileUtils.forceMkdir(new File("D:\\learn1\\geojson-demo\\jpg\\" + zoom));}log.info("zoom: {}, jpgNum: {}", zoom, (tilexMax - tilexMin + 1) * (tileyMax - tileyMin + 1));for (long x = tilexMin; x <= tilexMax; x++) {for (long y = tileyMin; y <= tileyMax; y++) {log.info("x: {},y: {}", x, y);Tile tile = new Tile(x, y, zoom);Envelope enve = mercatorTransform.tile2Envelope(tile);Geometry grid = gf.toGeometry(enve);boolean intersects = grid.intersects(geom);if (intersects) {// 生成柵格瓦片jpgGeometry inter = grid.intersection(geom);double tempLatMin = enve.getMinY();double tempLatMax = enve.getMaxY(); // 緯度double tempLonMin = enve.getMinX(); // 經度double tempLonMax = enve.getMaxX();// 經緯度轉像素坐標Pixel p1 = mercatorTransform.geographic2Pixel(new Coordinate(tempLonMin, tempLatMin), zoom);Pixel p2 = mercatorTransform.geographic2Pixel(new Coordinate(tempLonMax, tempLatMax), zoom);long minX = min(p1.getX(), p2.getX());long minY = min(p1.getY(), p2.getY());long maxX = max(p1.getX(), p2.getX());long maxY = max(p1.getY(), p2.getY());BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);// 生成默認圖片for (int h = 0; h < HEIGHT; h++) {for (int w = 0; w < WIDTH; w++) {// 可修改,黑色背景或者白色背景image.setRGB(w, h, BACKCOLOR_BLACK);}}//圖像輸出// 填充面的像素for (long x1 = minX; x1 <= maxX; x1++) {for (long y1 = minY; y1 <= maxY; y1++) {// 判斷軌跡點是否位于面上// 像素坐標轉web墨卡托轉Geom經緯度坐標Coordinate coordinate = mercatorTransform.pixel2Geographic(new Pixel(x1, y1), zoom);Point point = JtsUtils.createPoint(coordinate);if (inter.contains(point)) {int pw = (int) (x1 - 256 * x);int ph = (int) (y1 - 256 * y);int rgb = ((255 << 16) | ((0 << 8) | 0));if (pw > 0 && pw < 256 && (ph > 0 && ph < 256)) {image.setRGB(pw, ph, rgb);}}}}//圖像輸出ImageIO.write(image, "jpg", new File("D:\\learn1\\geojson-demo\\jpg\\" + zoom + "\\" + x + "_" + y + ".jpg"));} /*else {BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);//單色通道提取for (int h = 0; h < HEIGHT; h++) {for (int w = 0; w < WIDTH; w++) {image.setRGB(w, h, BACKCOLOR_BLACK);}}//圖像輸出ImageIO.write(image, "jpg", new File("D:\\learn1\\geojson-demo\\jpg\\" + zoom + "\\" + x + "_" + y + ".jpg"));log.error("{} {} no datas", x, y);}*/}}}/*** 線生成jpg瓦片** @param wkt wkt幾何* @param zoom 瓦片級別* @throws IOException* @throws ParseException*/public void lineString2Tile(String wkt, int zoom) throws IOException, ParseException {Geometry geom = wkt2Geo(wkt);Geometry envelope = geom.getEnvelope();// 計算最大最小邊界框經緯度double lonMin = min(envelope.getCoordinates()[0].x, envelope.getCoordinates()[2].x);double lonMax = max(envelope.getCoordinates()[0].x, envelope.getCoordinates()[2].x);double latMin = min(envelope.getCoordinates()[0].y, envelope.getCoordinates()[2].y);double latMax = max(envelope.getCoordinates()[0].y, envelope.getCoordinates()[2].y);// 經緯度轉web墨卡托坐標,轉像素坐標,并計算瓦片號Tile t1 = latLng2Tile(lonMin, latMin, zoom);Tile t2 = latLng2Tile(lonMax, latMax, zoom);// 計算x,y最大最小瓦片號long tilexMin = min(t1.getX(), t2.getX());long tileyMin = min(t1.getY(), t2.getY());long tilexMax = max(t1.getX(), t2.getX());long tileyMax = max(t1.getY(), t2.getY());if (!new File("D:\\learn1\\geojson-demo\\jpg\\" + zoom).exists()) {FileUtils.forceMkdir(new File("D:\\learn1\\geojson-demo\\jpg\\" + zoom));}log.info("zoom: {}, jpgNum: {}", zoom, (tilexMax - tilexMin + 1) * (tileyMax - tileyMin + 1));for (long x = tilexMin; x <= tilexMax; x++) {for (long y = tileyMin; y <= tileyMax; y++) {Tile tile = new Tile(x, y, zoom);Envelope enve = mercatorTransform.tile2Envelope(tile);Geometry grid = gf.toGeometry(enve);boolean intersects = grid.intersects(geom);if (intersects) {// 生成柵格瓦片jpgdouble tempLatMin = enve.getMinY();double tempLatMax = enve.getMaxY();double tempLonMin = enve.getMinX(); // 經度double tempLonMax = enve.getMaxX(); // 緯度// 經緯度轉像素坐標Pixel p1 = mercatorTransform.geographic2Pixel(new Coordinate(tempLonMin, tempLatMin), zoom);Pixel p2 = mercatorTransform.geographic2Pixel(new Coordinate(tempLonMax, tempLatMax), zoom);long minX = min(p1.getX(), p2.getX());long minY = min(p1.getY(), p2.getY());long maxX = max(p1.getX(), p2.getX());long maxY = max(p1.getY(), p2.getY());BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);// 生成默認圖片for (int h = 0; h < HEIGHT; h++) {for (int w = 0; w < WIDTH; w++) {// 可修改,黑色背景或者白色背景image.setRGB(w, h, BACKCOLOR_WHITE);}}//圖像輸出// 填充線的像素for (long x1 = minX; x1 <= maxX; x1++) {for (long y1 = minY; y1 <= maxY; y1++) {// 像素坐標轉web墨卡托轉Geom經緯度坐標Coordinate coordinate = mercatorTransform.pixel2Geographic(new Pixel(x1, y1), zoom);Point point = gf.createPoint(coordinate);// 判斷點是否在線上 geom.intersects(point)、point.within(geom) 這倆方法都不管用,以1/500分辨率當作誤差范圍if (isPointOnSegment(point, gf.createPoint(geom.getCoordinates()[0]),gf.createPoint(geom.getCoordinates()[1]), mercatorTransform.zoomToResolution(zoom) / 500)) {
// log.info("{}", JSON.toJSONString(point.getCoordinate()));int pw = (int) (x1 - 256 * x);int ph = (int) (y1 - 256 * y);int rgb = ((0 << 16) | ((255 << 8) | 0)); // 綠色填充if (pw > 0 && pw < 256 && (ph > 0 && ph < 256)) {image.setRGB(pw, ph, rgb);}}}}//圖像輸出ImageIO.write(image, "jpg", new File("D:\\learn1\\geojson-demo\\jpg\\" + zoom + "\\" + x + "_" + y + ".jpg"));} /*else {// 構建默認圖片BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);for (int h = 0; h < HEIGHT; h++) {for (int w = 0; w < WIDTH; w++) {// 可修改,黑色背景或者白色背景image.setRGB(w, h, BACKCOLOR_WHITE);}}//圖像輸出ImageIO.write(image, "jpg", new File("D:\\learn1\\geojson-demo\\jpg\\" + zoom + "\\" + x + "_" + y + ".jpg"));log.error("{} {} no datas", x, y);}*/}}}/*** 點生成柵格jpg瓦片** @param zoom 瓦片級別* @throws IOException* @throws ParseException*/public void points2Tile(String wkt, int zoom) throws IOException, ParseException {Geometry geom = wkt2Geo(wkt);Geometry envelope = geom.getEnvelope();// 計算最大最小邊界框經緯度double lonMin = min(envelope.getCoordinates()[0].x, envelope.getCoordinates()[2].x);double lonMax = max(envelope.getCoordinates()[0].x, envelope.getCoordinates()[2].x);double latMin = min(envelope.getCoordinates()[0].y, envelope.getCoordinates()[2].y);double latMax = max(envelope.getCoordinates()[0].y, envelope.getCoordinates()[2].y);// 經緯度轉web墨卡托坐標,轉像素坐標,并計算瓦片號Tile t1 = latLng2Tile(lonMin, latMin, zoom);Tile t2 = latLng2Tile(lonMax, latMax, zoom);// 計算x,y最大最小瓦片號long tilexMin = min(t1.getX(), t2.getX());long tileyMin = min(t1.getY(), t2.getY());long tilexMax = max(t1.getX(), t2.getX());long tileyMax = max(t1.getY(), t2.getY());if (!new File("D:\\learn1\\geojson-demo\\jpg\\" + zoom).exists()) {FileUtils.forceMkdir(new File("D:\\learn1\\geojson-demo\\jpg\\" + zoom));}log.info("zoom: {}, jpgNum: {}", zoom, (tilexMax - tilexMin + 1) * (tileyMax - tileyMin + 1));for (long x = tilexMin; x <= tilexMax; x++) {for (long y = tileyMin; y <= tileyMax; y++) {log.info("x: {},y: {}", x, y);Tile tile = new Tile(x, y, zoom);Envelope enve = mercatorTransform.tile2Envelope(tile);Geometry grid = gf.toGeometry(enve);boolean intersects = grid.intersects(geom);// 生成柵格瓦片jpgif (intersects) {Geometry inter = grid.intersection(geom); // 點與瓦片的交集// 構建默認圖片BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);for (int h = 0; h < HEIGHT; h++) {for (int w = 0; w < WIDTH; w++) {// 可修改,黑色背景或者白色背景image.setRGB(w, h, BACKCOLOR_WHITE);}}//圖像輸出// 填充點的像素Coordinate[] intersectionPoints = inter.getCoordinates();for (Coordinate coor : intersectionPoints) {// 經緯度轉像素坐標Pixel pixel = mercatorTransform.geographic2Pixel(coor, zoom);int pw = (int) (pixel.getX() - 256 * x);int ph = (int) (pixel.getY() - 256 * y);int rgb = ((0 << 16) | ((0 << 8) | 255)); // 藍色填充if (pw > 0 && pw < 256 && (ph > 0 && ph < 256)) {image.setRGB(pw, ph, rgb);}}//圖像輸出ImageIO.write(image, "jpg", new File("D:\\learn1\\geojson-demo\\jpg\\" + zoom + "\\" + x + "_" + y + ".jpg"));} /*else {// 構建默認圖片BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);for (int h = 0; h < HEIGHT; h++) {for (int w = 0; w < WIDTH; w++) {// 可修改,黑色背景或者白色背景image.setRGB(w, h, BACKCOLOR_WHITE);}}//圖像輸出ImageIO.write(image, "jpg", new File("D:\\learn1\\geojson-demo\\jpg\\" + zoom + "\\" + x + "_" + y + ".jpg"));log.error("{} {} no datas", x, y);}*/}}}/*** 判斷點是否在線上** @param a 點A* @param start 線起點start* @param end 線終點end* @param resolution 誤差范圍m* @return*/public boolean isPointOnSegment(Point a, Point start, Point end, double resolution) {boolean flag = false;double startAdis = JtsUtils.distance(a, start);double endADis = JtsUtils.distance(a, end);double dis = JtsUtils.distance(start, end);if (startAdis + endADis >= dis - resolution && startAdis + endADis <= dis + resolution) {return true;}return flag;}/*** 計算經緯度所在瓦片號** @param lon 經度* @param lat 緯度* @param zoom 瓦片級別* @return*/public static Tile latLng2Tile(double lon, double lat, int zoom) {// 經緯度轉墨卡托Coordinate mkt = mercatorTransform.geographic2Mercator(new Coordinate(lon, lat));// 墨卡托轉像素Pixel pixel = mercatorTransform.mercator2Pixel(mkt, zoom);// 像素坐標所在瓦片Tile atTile = mercatorTransform.pixelAtTile(pixel);atTile.setZ(zoom);return atTile;}/*** wkt 轉geometry** @param wkt* @return* @throws ParseException*/public Geometry wkt2Geo(String wkt) throws ParseException {WKTReader reader = new WKTReader(gf);Geometry geom = reader.read(wkt);return geom;}public static void main(String[] args) throws IOException, ParseException {Geoms2JpgTile geoms2JpgTile = new Geoms2JpgTile();geoms2JpgTile.initWkt();for (int i = 9; i <= 17; i++) {if (i > 14) {continue;}geoms2JpgTile.polygon2Tile(geoms2JpgTile.polygon, i);geoms2JpgTile.lineString2Tile(geoms2JpgTile.lineString, i);geoms2JpgTile.points2Tile(geoms2JpgTile.multiPoint, i);}}
}
參考
- 高德地圖數學計算庫
總結
以上是生活随笔為你收集整理的Java对点、线、面生成栅格瓦片jpg,并渲染呈现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小人爱财下一句是什么呢?
- 下一篇: 求一个好听的读书社团名字