Convert lat/long to pixel X&Y co-ordinates

  • Last Update :
  • Latest Visit :

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL),If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome., help? What is 'CodeProject'? General FAQ Ask a Question Bugs and Suggestions Article Help Forum About Us ,Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.

double scale_x = width / lon_range;
double scale_y = height / lat_range;
double x = (lon - lon0) * scale_x;
double y = (lat - lat0) * scaly_y;

Suggestion : 2

I've never done it before, but it seems to me like it would be fairly straight forward math, no?, Are works created by US government employees in the performance of their duties treated as public domain outside the US? , Stack Exchange network consists of 180 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. ,i did it with calculate distance between top and bottom but i got below:

I've never done it before, but it seems to me like it would be fairly straight forward math, no?

35.74706129 - 35.66569172 =
   0.08136957 = distance between top and bottom / 600 px =
   0.00013561595 = distance of one side of pixel to the other.

Suggestion : 3

My map takes up 100% width and height of the browser screen.. I have a draggable div that is super imposed over the map, I am capturing this divs position using offset(); to get the pixel top and left values currently.,For some reason I am having a hard time in getting the import to resolve; is there a specific order this one should be called?,My goals would be to convert these values to latitude and longitude... Could something like this be possible? , convert pixel x,y position on map to lat and long?

   Not sure what gave you that idea. The application would simply be:

var point = map.toMap(new ScreenPoint(coordinatesBx, coordinatesBy))‍;
console.log("Lat: " + point.getLatitude() + ", Lon: " + point.getLongitude());‍‍

Suggestion : 4

Regarding our task, we need to project GCS coordinates (latitude and longitude) on a flat image.,The original conversion algorithm works with latitude and longitude values in radians. We need a function that will convert degrees to radians:,Let’s determine coordinates of the New York and Melbourne:,Now we have our horizontal offset adapted to our needs.We need to calculate vertical offset:

As previously shown, northing and easting equations both require radius. We can find radius using a simple formula:

R = C / (2 * PI)

where C is circumference, namely the equator’s length.
The length of the equator on the Mercator projection equals the width of a map image. Then the radius equation will look like the following:

const radius = mapWidth / (2 * Math.PI);

Suggestion : 5

All my lines are precalculated with WGS82 coordinates and well tested.,The problem starts here TileSystem.LatLongToPixelXY As you see in my example for the same coordinates and different level of details (1-20) it gives weird X and Y,I am implementing CustomMapTileDataSource feature. So far I can draw custom Tiles on fly with some shapes (see image below).

 private async void customDataSource_BitmapRequestedAsync(CustomMapTileDataSource sender, MapTileBitmapRequestedEventArgs args)
     var deferral = args.Request.GetDeferral();
     const int tileSide = 256;
     TileSystem.TileXYToPixelXY(args.X, args.Y, out int pixelX, out int pixelY);
     var rect = new List<BasicGeoposition>();
     // Add Top Left Geo Point
     TileSystem.PixelXYToLatLong(pixelX, pixelY, args.ZoomLevel, out double latTopLeft, out double lngTopLeft);
     rect.Add(new BasicGeoposition() { Latitude = latTopLeft, Longitude = lngTopLeft });
     // Add Top Right Geo Point
     TileSystem.PixelXYToLatLong(pixelX + tileSide, pixelY, args.ZoomLevel, out double latTopRight, out double lngTopRight);
     rect.Add(new BasicGeoposition() { Latitude = latTopRight, Longitude = lngTopRight });
     // Add Bottom Right Geo Point
     TileSystem.PixelXYToLatLong(pixelX + tileSide, pixelY + tileSide, args.ZoomLevel, out double latBottomRight, out double lngBottomRight);
     rect.Add(new BasicGeoposition() { Latitude = latBottomRight, Longitude = lngBottomRight });
     // Add Bottom Left Geo Point
     TileSystem.PixelXYToLatLong(pixelX, pixelY + tileSide, args.ZoomLevel, out double latBottomLeft, out double lngBottomLeft);
     rect.Add(new BasicGeoposition() { Latitude = latBottomLeft, Longitude = lngBottomLeft });
     var filterdLines = GetLinesByGeoRect(rect);
      args.Request.PixelData = await CreateBitmapAsStreamAsync(filterdLines, args.ZoomLevel);

Suggestion : 6

Hi I have a little problem here. I have latitude, longitude and zoom of center of my map window (openstreet map). I have dimension map window. I also have pixel positions of specific events on the map in the window. Can Anyone help me how to convert these pixel positions into latitude and longitude coordinates or any other coordinate system that I will be able to visualize on different map later ? Here is an illustration of my problem. ,Further note: Mercator latitude coordinates do not actually cover -90° to 90° degrees as your image would suggest.,The projection you are displaying the map in is the so called spherical Mercator projection, you can convert Mercator coordinates to WGS84 lat/lon with the formulas from (you will need to scale from you screen pixels or whatever to Mercator meters but that is a simple exercise left for the reader).,transform coordinate system of window to global

code below

def LatLontoXY(lat_center, lon_center, zoom):
   C = (256 / (2 * pi)) * 2 ** zoom

x = C * (math.radians(lon_center) + pi)
y = C * (pi - math.log(math.tan((pi / 4) + math.radians(lat_center) / 2)))

return x, y

def xy2LatLon(lat_center, lon_center, zoom, width_internal, height_internal, pxX_internal, pxY_internal):

   xcenter, ycenter = LatLontoXY(lat_center, lon_center, zoom)

xPoint = xcenter - (width_internal / 2 - pxX_internal)
ypoint = ycenter - (height_internal / 2 - pxY_internal)

C = (256 / (2 * pi)) * 2 ** zoom
M = (xPoint / C) - pi
N = -(ypoint / C) + pi

lon_Point = math.degrees(M)
lat_Point = math.degrees((math.atan(math.e ** N) - (pi / 4)) * 2)

return lat_Point, lon_Point