【问题标题】:Render Geolocation coordinates into image using Java使用 Java 将地理位置坐标渲染到图像中
【发布时间】:2018-04-12 11:11:55
【问题描述】:

我需要在给定一组经度和纬度坐标的情况下打印一个多边形。 我只从两个坐标开始进行初始测试。问题是 Java Image API 适用于像素点,而经度和纬度是大小数。

所以即使我有这个代码:

BufferedImage bi = new BufferedImage(500, 500, BufferedImage.OPAQUE);
Graphics2D ig2 = bi.createGraphics();

double[] xArr = toDoubleArray(Arrays.asList(Double.valueOf("121.91359648077058"), Double.valueOf("121.92293884686991")));
double[] yArr = toDoubleArray(Arrays.asList(Double.valueOf("11.995724479140792"), Double.valueOf("11.999118908426375")));

Path2D path = new Path2D.Double();

path.moveTo(xArr[0], yArr[0]);
System.out.println("x[0]=" + xArr[0] + "," + "y[0]=" + yArr[0]);
for(int i = 1; i < xArr.length; ++i) {
    path.lineTo(xArr[i], yArr[i]);
    System.out.println("x[" + i + "]="  + xArr[i] + "," + "y[" + i + "]" + yArr[i]);
}
path.closePath();
ig2.draw(path);
ImageIO.write(bi, "PNG", new File("polygons.png"));

它将只是一个像素,即 500,500 缓冲区图像内的 121 和 11。 可以做些什么才能将地理位置坐标渲染到图像中?我真的不需要线条,只需在图像中绘制点就足够了,悬停Path2D 是我找到的最接近的,因为它支持double 值。

【问题讨论】:

    标签: java image geometry gis geospatial


    【解决方案1】:

    您可以使用GeoTools 库来处理坐标(在任何投影中)并将它们渲染到可以来自ImageGraphics 对象。 有一个number of tutorials 可以帮助您入门 这些问题将帮助您入门:

    Plot the longitude and latitudes on map using GeoTools

    GeoTools - drawing points on image

    【讨论】:

      【解决方案2】:

      您可以使用任何 Java Image API 在图像上绘制纬度/经度列表,Image API 的整数输入不应成为约束。 您可以首先决定要在图像上绘制的最小-最大纬度/经度。然后,您可以将输入数据与图像的纬度/经度界限进行映射并计算绘图像素。

      以下代码 sn-p 可能会帮助您了解如何在图像上进行映射和绘图:

      try {
                  int imagePixelWidth = 500;
                  int bubble_size = 50;
                  BufferedImage image = new BufferedImage(imagePixelWidth,
                          imagePixelWidth, BufferedImage.TYPE_INT_ARGB);
      
                  //Position you wish to plot
                  double lat = 11.995724479140792;
                  double lon = 121.91359648077058;
      
              // min-max plotting lat/lon of your image
                  double min_lat = 10.897564874;
                  double min_lon = 120.8975764;
                  double max_lat = 13.0975875;
                  double max_lon = 123.9759874;
      
                  Graphics2D graphics = (Graphics2D) image.getGraphics();
      
                  double latExtent = max_lat - min_lat;
                  double lonExtent = max_lon - min_lon;
      
                  double ly1 = (imagePixelWidth * (lat - min_lat)) / latExtent;
                  double lx1 = (imagePixelWidth * (lon - min_lon)) / lonExtent;
      
                  int ly = (int) (imagePixelWidth - ly1);/* pixel increases downwards. Latitude increases upwards (north direction). So you need to inverse your mapping.*/
                  int lx = (int) lx1;
      
                  graphics.setColor(new Color(0, 0, 0));
                  graphics.fillOval(lx - bubble_size / 2, ly - bubble_size / 2,
                          bubble_size, bubble_size);
      
                  ImageIO.write(image, "png", new File("/home/ist/test.png"));
      
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
      

      【讨论】:

        【解决方案3】:

        在将坐标映射到屏幕坐标系后,大小数和双精度之间的区别应该主要是没有意义的。然后,您可以将每个点绘制为一个小圆圈。要在不使用整数的情况下完成此操作,您可以使用例如

        double final r = 1.0;  // radius of dots representing points
        for(int i = 1; i < xArr.length; ++i) {
            ig2.fill(new Ellipse2D.Double(xArr[i]-r, yArr[i]-r, 2*r, 2*r));
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-05-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-27
          • 1970-01-01
          相关资源
          最近更新 更多