quarks
quarks

Reputation: 35346

Render Geolocation coordinates into image using Java

I need to print a polygon given a set of Longitude and Latitude coordinates. I am starting with just two coordinate for initial testing. The problem is Java Image API works on pixel points and that Longitude and Latitude are big decimals.

So even if I have this code:

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"));

It will just be a single pixel, which is 121 and 11 inside the 500,500 buffer image. What can be done to be able to render Geolocation coordinates into an image? I really don't need the lines just plot the points in the image is sufficient, hover the Path2D is the closest one I found since it supports double values.

Upvotes: 0

Views: 2596

Answers (3)

Ian Turton
Ian Turton

Reputation: 10976

You could use the GeoTools library which will handle coordinates (in any projection) and render them to a Graphics object which can come from an Image. There are a number of tutorials to get you started and these questions will help get you started:

Plot the longitude and latitudes on map using GeoTools

GeoTools - drawing points on image

Upvotes: 2

Surabhi Mundra
Surabhi Mundra

Reputation: 377

You can use any of the Java Image API for plotting your lat/lon list on image, the integer input of Image APIs should not be a constraint. You can first decide the min-max lat/lon that you will be plotting on your image. Then you can map your input data with the image's lat/lon bounds and calculate the plotting pixel.

Below code snippet might help you on how to map and plot on image:

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();
        }

Upvotes: 1

MvG
MvG

Reputation: 61017

After you map your coordinates to screen coordinate system, the distinction between big decimals and double should be mostly moot. You can then draw each point as a small circle. To get that done without resorting to integers, you can use e.g.

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));
}

Upvotes: 0

Related Questions