Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting image X,Y coordinates to longitude and latitude?

Tags:

java

maps

I have set values of minimum longitude and latitude of a specific static map image. That map image is a cut of some country.

/**
 * Maximum longitude value of the map
 */
private float mapLongitudeMax;

/**
 * Minimum longitude value of the map
 */
private float mapLongitudeMin;

/**
 * Maximum latitude value of the map
 */
private float mapLatitudeMax;

/**
 * Minimum latitude value of the map
 */
private float mapLatitudeMin;

And I have a BufferedImage called mapImage.

I have a method that I wrote with a friend that receives longitude and latitude and gives you an X and a Y position approximately on the map so you can draw something on the map.

Now if I want to move my mouse around the map, I want it to show longitude/latitude of my mouse position, that means I need to create a method which converts X and Y of the mouse position to longitude and latitude, which should do the opposite of my other method.

This is my method to convert globe coordinates to image X and Y:

protected Location getCoordinatesByGlobe(float latitude, float longitude) {

    /**
     * Work out minimum and maximums, clamp inside map bounds
     */
    latitude = Math.max(mapLatitudeMin, Math.min(mapLatitudeMax, latitude));
    longitude = Math.max(mapLongitudeMin, Math.min(mapLongitudeMax, longitude));

    /**
     * We need the distance from 0 or minimum long/lat
     */
    float adjLon = longitude - mapLongitudeMin;
    float adjLat = latitude - mapLatitudeMin;

    float mapLongWidth = mapLongitudeMax - mapLongitudeMin;
    float mapLatHeight = mapLatitudeMax - mapLatitudeMin;

    float mapWidth = mapImage.getWidth();
    float mapHeight = mapImage.getHeight();

    float longPixelRatio = mapWidth / mapLongWidth;
    float latPixelRatio = mapHeight / mapLatHeight;

    int x = Math.round(adjLon * longPixelRatio) - 3;// these are offsets for the target icon that shows.. eedit laterrr @oz
    int y = Math.round(adjLat * latPixelRatio) + 3; //

    // turn it up
    y = (int) (mapHeight - y);

    return new Location(x, y);
}

Now I tried thinking, the first thought that came into my head is just doing the same in reverse... so I started doing it and I ran into problems like, I can't get the value of adjLon or adjLat without having the longitude or latitude, so this can't be simply done by reversing it. I am all new to coordinates systems so it's all a bit confusing for me but I am starting to catch it up.

Any tips for me?

EDIT (Its not possible?)

According to this answer, you can't really get real results because the earth is not flat, it can't really be converted to a flat map with longitude and latitude without implementing a real mathematical algorithm to make it work with the changes.

There are few reasons in my code why the answer can not be exact:

  1. Because of the reason above
  2. Because my X,Y values are integers and not floats.

So my question now, if it is really impossible with my method?

like image 851
Artemkller545 Avatar asked Aug 07 '16 20:08

Artemkller545


People also ask

How do I convert between various classes of coordinates?

Convert between various classes of coordinates using this tool. For decimal degrees, include a negative sign for south and west coordinates. Latitude (-90 to 90) and longitude (-180 to 180). Include up to 6 decimal places. Y and X values up to 6 decimal places. Origin (0,0) is the geographic South Pole. Y and X values up to 6 decimal places.

What is the decimal value of latitude and longitude?

For decimal degrees, include a negative sign for south and west coordinates. Latitude (-90 to 90) and longitude (-180 to 180). Include up to 6 decimal places. Y and X values up to 6 decimal places.

How do I update the coordinates in the coordinate tool?

Login Enter values into the coordinate tool and the values will automatically update. For decimal degrees, remember to include the negative sign for south and west coordinates! Errors will show in red text. Latitude (-90 to 90) and longitude (-180 to 180).

How to convert x and Y coordinates to UTM coordinates?

Its better to convert to utm coordinates, and treat that as x and y. The result will be (779260.623156606, 1429369.8665238516, 43, 'P') The first two can be treated as x,y coordinates, the 43P is the UTM Zone, which can be ignored for small areas (width upto 668 km).


1 Answers

Sadly, there's not an easy answer to this. While you can write the projection routines yourself, the easiest thing to do is probably to get a GIS library, but since I ended up doing this in C# and not Java, I don't know what's available.

The biggest piece of information you need is exactly which projection your map image uses. The Mercator Projection is quite popular, but it's not the only one. You also need to make sure that your chosen projection works for the range of latitudes and longitudes you want. The Mercator projection kind of breaks if you start going above +-70 N, so if you're doing a lot of positions at the poles that might not be the projection for you.

like image 182
Benjamin Avatar answered Nov 16 '22 03:11

Benjamin