最近需要把数据库存的多边形输出图片,在互联网上找了很多资料,都是实用geotools转换shp文件为图片文件。太复杂,一堆代码看的复杂。
直接附上代码吧
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
public class Geo2ImageHelper {
private int imageWidthHeight = 1000;
private List<double[]> coordinates;
private Color color;
private OutputStream os;
public Geo2ImageHelper(int imageWidthHeight, List<double[]> coordinates, Color color, OutputStream os) {
super();
this.imageWidthHeight = imageWidthHeight;
this.coordinates = coordinates;
this.color = color;
this.os = os;
}
public void draw() throws IOException {
BufferedImage image = new BufferedImage(imageWidthHeight, imageWidthHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) image.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,imageWidthHeight,imageWidthHeight);
g.setColor(color);
g.setStroke(new BasicStroke(5.0f));
double minLng = 0D;
double maxLng = 0D;
double minLat = 0D;
double maxLat = 0D;
double xishu = 0D;
int x_pianyi = 0;
int y_pianyi = 0;
{
for (double[] d : coordinates) {
if (minLng == 0D || d[0] < minLng) {
minLng = d[0];
}
if (maxLng == 0D || d[0] > maxLng) {
maxLng = d[0];
}
if (minLat == 0D || d[1] < minLat) {
minLat = d[1];
}
if (maxLat == 0D || d[1] > maxLat) {
maxLat = d[1];
}
}
}
{
double t1 = maxLng - minLng;
double t2 = maxLat - minLat;
double t3 = t1 > t2 ? t1 : t2;
xishu = imageWidthHeight / t3;
if (t1 > t2) {
y_pianyi = (int) ((imageWidthHeight / 2 - (int) (t2 * xishu/2))) + 1;
} else {
x_pianyi = (int) ((imageWidthHeight / 2 - (int) (t1 * xishu/2)) + 1;
}
}
{
double[] tmpPoint = null;
for (double[] d : coordinates) {
if (tmpPoint == null) {
tmpPoint = d;
continue;
}
int x1 = (int) ((maxLng - tmpPoint[0]) * xishu);
int y1 = (int) ((maxLat - tmpPoint[1]) * xishu);
int x2 = (int) ((maxLng - d[0]) * xishu);
int y2 = (int) ((maxLat - d[1]) * xishu);
x1 = x1 >= 0 ? x1 : 1000 + x1;
y1 = y1 >= 0 ? y1 : 1000 + y1;
x2 = x2 >= 0 ? x2 : 1000 + x2;
y2 = y2 >= 0 ? y2 : 1000 + y2;
x1=+imageWidthHeight - x1-x_pianyi;
y1=y_pianyi+y1;
x2=imageWidthHeight - x2-x_pianyi;
y2=y_pianyi+y2;
{
if(x1>imageWidthHeight-5){
x1=imageWidthHeight-5;
}else if(x1<5){
x1=5;
}
if(x2>imageWidthHeight-5){
x2=imageWidthHeight-5;
}else if(x2<5){
x2=5;
}
if(y1>imageWidthHeight-5){
y1=imageWidthHeight-5;
}else if(y1<5){
y1=5;
}
if(y2>imageWidthHeight-5){
y2=imageWidthHeight-5;
}else if(y2<5){
y2=5;
}
}
g.drawLine(x1, y1, x2, y2);
tmpPoint = d;
}
}
g.dispose();
ImageIO.write(image, "JPG", os);
}
public static void main(String[] args) {
try {
{
List<double[]> c = new ArrayList<>();
c.add(new double[]{108.80069129d, 18.5200639d});
c.add(new double[]{108.80212735D, 18.51905788D});
c.add(new double[]{108.80250244D, 18.51966251D});
c.add(new double[]{108.80097529D, 18.52047037D});
c.add(new double[]{108.80093242D, 18.52042465D});
c.add(new double[]{108.80069129D, 18.5200639D});
FileOutputStream fos = new FileOutputStream(new File("d:\\aa.jpg"));
Geo2ImageHelper h = new Geo2ImageHelper(1000, c, Color.RED, fos);
h.draw();
fos.flush();
fos.close();
}
{
List<double[]> c = new ArrayList<>();
c.add(new double[]{108.79898037,18.52201225});
c.add(new double[]{108.79989549,18.52330581});
c.add(new double[]{108.80028934,18.52387232});
c.add(new double[]{108.80034694,18.52401966});
c.add(new double[]{108.79997587,18.52430799});
c.add(new double[]{108.79974541,18.52394305});
c.add(new double[]{108.79966504,18.52382619});
c.add(new double[]{108.79960074,18.52369917});
c.add(new double[]{108.79945606,18.52349085});
c.add(new double[]{108.79925244,18.52321141});
c.add(new double[]{108.79903274,18.52288624});
c.add(new double[]{108.79875281,18.52250431});
c.add(new double[]{108.79853093,18.52219834});
c.add(new double[]{108.79898037,18.52201225});
FileOutputStream fos = new FileOutputStream(new File("d:\\bb.jpg"));
Geo2ImageHelper h = new Geo2ImageHelper(1000, c, Color.RED, fos);
h.draw();
fos.flush();
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}