Java – convert image X and Y coordinates to longitude and latitude?

I set the minimum longitude and latitude values for a specific static map image That map picture is a silhouette of a 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;

I have a bufferedimage named mapimage

I have a method I wrote with my friends. It receives longitude and latitude and gives you an X and y position on the map so that you can draw something on the map

Now, if I want to move the mouse on the map, I want it to display the longitude / latitude of my mouse position, which means I need to create a method to convert the X and y of the mouse position into longitude and latitude, which should be the opposite of my other method

This is how I convert earth coordinates to images 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 try to think, the first thought in my mind is to do the same thing in reverse... So I began to do this. I encountered a similar problem. I got the value or latitude of adjlon or adjlat without longitude, so this can't be done simply by inversion I was unfamiliar with the coordinate system, so it was a bit confusing for me, but I began to catch up with it

Any tips?

Edit (impossible?)

According to this answer, you can't really get the real results, because the earth is not flat, it can't really convert into a plane map with longitude and latitude without implementing a real mathematical algorithm to adapt it to changes

There are several reasons for inaccurate answers in my code:

>For the above reasons > because my x and Y values are integers rather than floating point numbers

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

Solution

Sadly, this is not a simple answer Although you can write your own projection routines, the easiest way may be to get the GIS library, but since I finally perform this operation in c# rather than Java, I don't know what is available

The biggest information you need is the projection used by the map image The Mercator project is very popular, but it is not the only one You also need to make sure that the selected projection is suitable for the latitude and longitude range you want If you start to exceed - 70 n, the Mercator projection will be interrupted, so if you do a lot of positions at the pole, it may not be suitable for you

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>