基于地理信息的多边形输出图片

最近需要把数据库存的多边形输出图片,在互联网上找了很多资料,都是实用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;
/**
 * 经纬度输出图形
 * @author 
 */
public class Geo2ImageHelper {

    // {"type":"Polygon","coordinates":[[[108.80069129,18.5200639],[108.80212735,18.51905788],[108.80250244,18.51966251],[108.80097529,18.52047037],[108.80093242,18.52042465],[108.80069129,18.5200639]]]}}]
    private int imageWidthHeight = 1000;

    private List<double[]> coordinates;

    private Color color;

    private OutputStream os;
/**
 * 
 * @param imageWidthHeight 图片大小像素  1000比较好
 * @param coordinates 经纬度数组
 * @param color 画的颜色
 * @param os  需要输出的流,记得输出完了关闭流,这里没有关,记得要byte[] 直接new一个bytearrayoutputstream。。。。
 */
    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) {
                //x轴心偏移
                 y_pianyi = (int) ((imageWidthHeight / 2 - (int) (t2 * xishu/2))) + 1;
                 
            } else {
                //y轴心偏移
             
                  x_pianyi = (int) ((imageWidthHeight / 2 - (int) (t1 * xishu/2)) + 1;
                
            }
        }

        {// MB 开始画图�

            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) {
        // TODO Auto-generated method stub
        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();
            }
            
            {
                //[108.79853093,18.52219834],
             
                       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();
        }

    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值