3

I have the following:

  1. A PNG or JPG of a random width and height, in pixels, that represents a partial map of the world (i.e. a rectangle of the United States).
  2. The four corners of this image are represented by real latitude and longitude values that if plotted, would represent 4 corner points of the United States (LatTop, LatBottom, LongLeft, LongRight).
  3. A coordinate that, if plotted, would represent a point somewhere inside of this "rectangle" (Latitude, Longitude).

My question is, what type of projection or formula would I use to calculate the X and Y coordinates (in pixels based off of the original PNG/JPG image) of the coordinate provided in #3 above?

Some example values would be:

  • ImageWidth = 1312
  • ImageHeight = 576
  • LatTop = 30.244381
  • LatBottom = 29.529928
  • LongLeft = -96.764958
  • LongRight = -94.404619
  • Coordinate Latitude = 30.100000
  • Coordinate Longitude = 95.100000

I found some information regarding Plate Carrée projections but I could not successfully apply it to this problem. Perhaps I am missing something.

  • Are you looking to use a GIS suite or some language specific code? +1 for an interesting problem. – Paul Oct 12 '13 at 2:59
  • I will be writing code to solve this problem. Most likely in .NET/C#. – thehin Oct 12 '13 at 3:03
1

If your source map is using a mercator projection you can use the following solution (which was taken from this Stackoverflow post - if you found it helpful don't forget to vote up the original answer)

Just for reference and the convenience of other seekers, I'm going to copy/paste the Pseudo code example from the above link:

latitude    = 41.145556; // (φ)
longitude   = -73.995;   // (λ)

mapWidth    = 200;
mapHeight   = 100;

// get x value
x = (mapWidth*(180+longitude)/360)%mapWidth+(mapWidth/2);

// convert from degrees to radians
latRad = latitude*PI/180;
// get y value
mercN = log(tan((PI/4)+(latRad/2)));

y     = (mapHeight/2)-(mapWidth*mercN/(2*PI));
0

After further research and scouring of gis.stackexchange.com, I was able to come up with suitable formulas to solve this problem. Below is the code:

public static PointF GetPixelCoordinatesInBounds(double width, double height, double top, double bottom, double left, double right, double latitude, double longitude)
{
    bottom = bottom * (Math.PI / 180);
    top = top * (Math.PI / 180);
    left = left * (Math.PI / 180);
    right = right * (Math.PI / 180);
    latitude = latitude * (Math.PI / 180);
    longitude = longitude * (Math.PI / 180);

    var ymin = Math.Log(Math.Tan((bottom / 2) + (Math.PI / 4)));
    var ymax = Math.Log(Math.Tan((top / 2) + (Math.PI / 4)));
    var xfactor = width / (right - left);
    var yfactor = height / (ymax - ymin);

    var x = (longitude - left) * xfactor;
    var y = (ymax - Math.Log(Math.Tan((latitude / 2) + (Math.PI / 4)))) * yfactor;

    return new PointF((float)x, (float)y);
}

It seems this question was already answered here: https://gis.stackexchange.com/a/72050.

  • I think , this is for mercator projection. someone please confirm. – arango_86 Jan 15 '19 at 8:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.