当前位置:网站首页>地图裁剪器,可以将图片裁剪成瓦片数据,主要用途是将高清卫星图像裁剪成瓦片图,可以做离线地图的开发,基于墨卡托坐标
地图裁剪器,可以将图片裁剪成瓦片数据,主要用途是将高清卫星图像裁剪成瓦片图,可以做离线地图的开发,基于墨卡托坐标
2022-04-22 07:30:00 【无极低码】
废话不多说,直接上代码
地图裁剪器,可以将图片裁剪成瓦片数据,主要用途是将高清卫星图像裁剪成瓦片图,可以做离线地图的开发,基于墨卡托坐标
地图裁剪
package com.wwp.utils.map;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.File;
import javax.imageio.ImageIO;
/**
*
*
* class name: BMapCut
* description:
* authername:wangwenping
* modified by: xx
* modified time:
* modified info :
* @version 1.0.0
*
*/
public class BMapCut {
private int minlevel;
private int maxlevel;
private int piclevel;
private double mercatorx;
private double mercatory;
private String pic;
private String savepath;
public BMapCut(String pic, double mercatorx, double mercatory,
String savepath) {
this.pic = pic;
this.mercatorx = mercatorx;
this.mercatory = mercatory;
this.savepath = savepath;
}
/**
* @param pic 图片路径
* @param minlevel最小缩放等级
* @param maxlevel最大缩放等级
* @param piclevel 原图所处缩放等级
* @param mercatorx 墨卡托x坐标
* @param mercatory墨卡托y坐标
* @param savepath输出目录
*/
public BMapCut(String pic, int minlevel, int maxlevel, int piclevel, double mercatorx,
double mercatory, String savepath) {
this.pic = pic;
this.minlevel = minlevel;
this.maxlevel = maxlevel;
this.mercatorx = mercatorx;
this.mercatory = mercatory;
this.savepath = savepath;
this.piclevel = piclevel;
}
public void cutterall() throws Exception {
for (int i = minlevel; i <= maxlevel; i++) {
cutterone(i);
}
}
public void cutterone(int level) throws Exception {
//图片中心的像素坐标(pixelx,pixely),图片中心的平面坐标即魔卡托坐标(mercatorx, mercatory)
//像素坐标 = 平面坐标 * Math.pow(2, level - 18)
double pixelx = mercatorx * Math.pow(2, level - 18);
double pixely = mercatory * Math.pow(2, level - 18);
System.out.println("pixelx : " + pixelx);
System.out.println("pixely : " + pixely);
BufferedImage bi = ImageIO.read(new File(pic));
int width = bi.getWidth();
int height = bi.getHeight();
//图片遵循原则:当前图片所属级别piclevel不缩放即像素级别相等。
//按照公式缩放:当前级别图片长度 = 原图片长度 * Math.pow(2, level - piclevel)
//minx: 图片左下角x坐标
//miny: 图片左下角y坐标
//maxx: 图片右上角x坐标
//maxy: 图片右上角y坐标
double minx = pixelx - width * Math.pow(2, level - piclevel) / 2;
double miny = pixely - height * Math.pow(2, level - piclevel) / 2;
double maxx = pixelx + width * Math.pow(2, level - piclevel) / 2;
double maxy = pixely + height * Math.pow(2, level - piclevel) / 2;
System.out.println("(minx,miny) = (" + minx + ", " + miny + ")" );
System.out.println("(maxx,maxy) = (" + maxx + ", " + maxy + ")" );
int neatminx = (int) minx / 256;
int remminx = (int) minx % 256;
int neatminy = (int) miny / 256;
int remminy = (int) miny % 256 ;
int neatmaxx = (int) maxx / 256;
int remmaxx = 256 - (int) maxx % 256;
int neatmaxy = (int) maxy / 256;
int remmaxy = 256 - (int) maxy % 256;
//(neatminx,neatminy)为图片左下角最近的整数图块坐标,neatminx到neatmaxx即当前级别下切割图块的图块坐标x
//(neatmaxx,neatmaxy)为图片右上角最近的整数图块坐标,neatminy到neatmaxy即当前级别下切割图块的图块坐标y
System.out.println("neatminx: " + neatminx);
System.out.println("neatmaxx: " + neatmaxx);
System.out.println("neatminy: " + neatminy);
System.out.println("neatmaxy: " + neatmaxy);
System.out.println("remminx width remmaxx : " + remminx + " "+ width + " "+ remmaxx );
System.out.println("remminy height remmaxy : " + remminy + " " + height +" " + remmaxy );
// 扩充原图片为width * height --- > (remminx + width + remmaxx ) * (remminy +
// height +remmaxy)
int extendwidth = (neatmaxx - neatminx + 1 ) * 256;
int extendheight = (neatmaxy - neatminy + 1 ) * 256;
System.out.println("extendwidth: " + extendwidth);
System.out.println("extendheight: " + extendheight);
BufferedImage outputimage = null;
Graphics2D g = bi.createGraphics();
BufferedImage extend = g.getDeviceConfiguration().createCompatibleImage(extendwidth, extendheight, Transparency.TRANSLUCENT);
g.dispose();
g = extend.createGraphics();
g.drawImage(extend, 0, 0, extendwidth, extendheight, null);
g.drawImage(bi.getScaledInstance((int) (width * Math.pow(2, level - piclevel)), (int)(height * Math.pow(2, level - piclevel)), Image.SCALE_SMOOTH), remminx, remmaxy, null);
//切割图片,共( neatmaxx - neatminx + 1) * (neatmaxy - neatminy + 1)份 256*256图片
String dirname = savepath.substring(0, savepath.lastIndexOf("\\")) + "\\tiles\\" + level;
System.out.println("dirname : " + dirname);
File dir = new File(dirname);
Image image = extend.getScaledInstance(extendwidth, extendheight, Image.SCALE_DEFAULT);
if(dir.exists()) {
System.out.println("创建目录失败!, 目录已存在!");
} else {
if(dir.mkdirs()) {
ImageIO.write(extend, "png", new File(dirname + savepath.substring(savepath.lastIndexOf("\\")) + ".png"));
System.out.println("savepath : " + dirname + savepath.substring(savepath.lastIndexOf("\\")));
System.out.println("extend success!");
int w = neatmaxx - neatminx + 1;
int h = neatmaxy - neatminy + 1;
for(int i = 0; i < w; i++) {
for(int j = 1; j <= h; j++) {
ImageFilter cropfilter = new CropImageFilter(256 * i, 256* (h - j), 256, 256);
Image img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(),cropfilter));
BufferedImage tag = new BufferedImage(256, 256 , BufferedImage.TYPE_INT_BGR);
Graphics2D gs = tag.createGraphics();
tag = gs.getDeviceConfiguration().createCompatibleImage(256, 256, Transparency.TRANSLUCENT);
gs.dispose();
gs = tag.createGraphics();
gs.drawImage(img, 0, 0, null);
g.dispose();
String croppicname = dirname + "\\tile" + (neatminx + i) + "_" + (neatminy + j - 1) + ".png";
ImageIO.write(tag, "png", new File(croppicname));
}
}
System.out.println("切割图片成功!");
} else {
System.out.println("创建目录失败!");
}
}
}
public static void main(String[] args) throws Exception{
BMapCut a=new BMapCut("C:\\Users\\Administrator\\Desktop\\geo\\123.png",16/*最小缩放等级*/,18/*最大缩放等级*/,18/*原图所处缩放等级*/,12958175/*墨卡托x坐标*/,4825923.77/*墨卡托y坐标*/,"C:\\输出目录");
a.cutterall();
}
}
版权声明
本文为[无极低码]所创,转载请带上原文链接,感谢
https://blog.csdn.net/a913222/article/details/124334007
边栏推荐
- 原来PID是在老王头和老斯基的斗争中诞生的
- Redis entry required
- 重新定义中国“芯”
- OpenCV读取网络摄像头视频并保存到本地
- 4E1 + 2-way Gigabit isolation network + 4-way 100m physical isolation network PDH optical transceiver
- I feel that writing a paper is a waste of time. Do I want to write a paper?
- Pycharm terminal PIP installation error: "PIP" item is recognized as the name of cmdlet, function, script file or runnable program
- AC & A2C & A3C
- 动态规划--LC518.零钱兑换II
- Redis入门必读
猜你喜欢

如何用c语言实现【猜数字游戏】

Experiment 2: mathematical basis in Data Science

Provide 4-way E1 service ports, 4-way 100m isolation network, 2m E1 private network, multi service PDH optical transceiver

tf.keras.layers.TimeDistributed函数

牛客白月赛5 【题解 数学场】

Double optical port protection 8e1 + 8-way physical isolation 100M Ethernet optical transceiver PDH optical transceiver

荧光标记的多肽/氨基酸(FITC修饰/AMC修饰)齐岳生物

tf.keras.layers.Conv?D函数

Leprechaun green elf magic strikes

Fluorescent labeled polypeptide / amino acid (FITC modified / AMC modified) Qiyue organism
随机推荐
spark sql 获取数组某index处元素
The collection palette in LabVIEW 2012 is imported into LabVIEW 2013
SCRM加速行业化,制造业也开始玩私域流量了
阻塞队列BlockingQueue
3D 沙盒游戏之人物的点击行走移动
LeetCode_ 118. Yanghui triangle_ Dynamic programming_ Int * * learning
汉源高科PDH光端机双光口保护+4路E1+4路千兆网络+4路百兆网络光端机
Phthalocyanine platinum CAS: 14075-08-2 | zinc phthalocyanine ZnPc | mesoporous silicon @ upconversion @ ZnPc photosensitizer, emission below 100nm 650nm
Visualization of unity perspective projection matrix transformation
16 way E1 optical transceiver + 4-way 100M Ethernet optical transceiver PDH optical transceiver 2m integrated service optical transceiver
Leetcode 111 Balanced binary tree (2022.04.21)
【优麒麟】22.04 LTS版本即将发布,终极预告来袭,你准备好了吗?
npm发布一个项目到npm库?
提供4路E1业务口4路百兆隔离网络2M E1专网多业务PDH光端机
概率论笔记6.3抽样分布
雙光口1+1備份8路E1+2路千兆隔離網絡4路百兆隔離PDH光端機
Click, walk and move of characters in 3D sandbox game
tf.keras.layers.InputLayer函数
Variant quick platoon: find the largest number of top k
[quick link] a necessary calculation tool for Electronic Engineers - square wave circuit aided design form