Sharable utilities (coordinates conversion)

Estimated duration : 45 minutes

Presentation :

Let's consider a game where the map displays my location as well as the location of other players and all sorts of ennemies moving around the city.
  • When writing code for sprites, we will use canvas or pixel coordinates,
  • Our geo-location as well as the geo-location of other players and ennemies will be known with coordinates in degrees of longitude and latitude,
  • Maps will be spatially defined by their center in longitude and latitude, and pixel size (width and height) in meters whch depends on map scale,
  • Distances between 2 points with coordinates in degrees will be measured in meters.
Screen Pixels, geographic degrees,cartographic distances, ... are different "coordinate systems",
We need to switch from each coordinate system to the other ones, and thta's what we will do here :
  • Convert location of other players (longitude and latitude degrees from their GPS) to our screen canvas(with pixel coordinates),
  • Compute distance in meters or kilometers between 2 poinst known by their Latitude and longitude in meters,
  • ...

Reminder on geographic coordinates (latitude and longitude) :

May be we can remind what are the Longitude, Latitude and altitude of a point : image111.png
  • Longitude λ is an angle that specifies the east-west position of a point on the Earth's surface. 0° at the Prime Meridian in Greenwich. It ranges from −180° westward to +180° eastward.
  • Latitude φ is an angle that specifies the north–south position of a point on the Earth's surface. It ranges from -90° at the south pole, 0° at the equator to +90° at the north pole.
  • Altitude is the height in meters above mean sea level (MSL), not to be confused with height above ground (AGL). Terrain elevation is the altitude (MSL) of the ground itself

What you will do :

  • Review/understand what coordinate systems are at stake,
  • Review the list of conversion and computation routines needed,
  • (optional)Look at conversion rules and algorithms,
  • Practice their use.

What you will learn :

  • List of coordinates systems,
  • List of useful conversion routines (and underlying assumptions),
  • (optional)conversion methods and algorithms,
  • use of conversion procedures.

Input Resources :

Conversion libraries are available at :

Lesson content :

What do we need :

The functions we need (and that are made available) are :
function (procedure)function name and parameters
length in meters of a degree of latitudedegreeOfLatitudeMeters()
length in meters of a degree of longitudedegreeOfLongitudeMeters(latitude)
distance in meters between 2 points from their longitude and latitude (on the spherical earth) :distanceMeters(lat1, long1, lat2,long2)
width in degrees of a pixelpixelWidthInDegrees(zoom)
height in degrees of a pixelpixelHeightInDegrees(zoom, latitude)
size of a pixel in meterspixelsizeInMeters(zoom, latitude)
longitude of pixel at xlongitudeFromX(x, canvasWidth,centerlat, centerLong,zoom)
latitude of pixel at y
X pixel coordinate from latitude longitudeFromLatLong (longitude, canvasWidth, centerLatitude, centerLongitude, zoom)
Y pixel coordinate from latitude : yFromLat (latitude, canvasHeight, centerLatitude, zoom)

Don’t be afraid : :

You will NOT have to code these functions, and you do not need to understand the details of what's inside.
But You need to have a clear view of what these functions do and when to use them (we will see examples).
You should also be able to check that they work well on practical cases. (We have done error checking, but Errors are still possible. When you reuse : trust but check).

Length in meters of one degree of latitude and of longitude

image113.png As an effort to share universal units, the meter was first defined in 1791 as 1/10 000 000 of half of a meridian of the earth.

A degree of latitude is then equal to 10 000 000 m / 90° ~ 111 111 m. However, we will here assume a spherical earth with an average radius rounded to 6 371 km Leading to 1° = 111 195 m. The reason is that we want to use a single constant for all formulas : the earth (or planet) radius.

A degree of longitude changes with latitude :
length of a degree of longitude = length of a degree of latitude * cosine (latitude)
with the corresponding App inventor code here

The length of a degree of latitude in meters is then given by : image115.png

The length of a degree of longitude in meters is then given by : image117.png

Distance between 2 points known by their longitude λ and latitude φ

image119.png The shortest distance between 2 points over the earth is also called the “great circle” distance (different from the straight line through the earth).
When distances are limited you may use the Pythagorean theorem :
image121.png
leading to
image123.png
Which has a precision of 1% within 4000 km. But we will use the more precise Vincenty formula (see Wikipedia) of the angular distance :

image125.png
Which we multiply by the radius of the earth, to get the distance in meters.

The distance in meters between 2 points on the earth known by their longitudes and latitudes lat1, long1, lat2,long2 is then:
image127.png

Pixel Width and height in degrees of longitude and latitude

The operation that converts latitude and longitude (λ,φ) to pixel coordinates (x,y) is the "web Mercator" projection (see Wikipedia) with the following formulas :
image129.png
From which follows in radians :
image131.png
or for us in degrees :
image133.png
Translated into the following blocks :

image135.png
image137.png

Pixel size in meters

  • Pixel height in meters = pixel height in radians multiplied by radius of a meridian [i.e. Rearth]
  • Pixel width in meters = pixel width in radians multiplied by radius of the parallel at given latitude [i.e. Rearth * cos(φ) ]
    image139.png
Formulas and corresponding blocks are the same for width and height (i.e. square pixels):
image141.png

longitude and latitude from pixel-canvas coordinates

image143.png
Canvas pixel coordinates are illustred by the figure on the right with origin is top left.
  • in green : canvas pixels with center position and x,y values,
  • in red : center latitude and longitude, pixel height and width,
  • in blue : physical screen coordinates (canvas units).

When map is oriented north, x increases with longitude and y decreases with latitude (opposite direction).

For pixel x,y returned by App Inventor blocks it follows :
image145.png
longitude from pixel x coordinate and canvas center lat
image147.png

Latitude from pixel y coordinate image149.png
pixel-canvas coordinates from longitude and latitude Inversion of the previous functions is straightforward,
a point with "latitude, longitude" coordinates should be plotted at x, y given by :
image151.png
If x or y are less than 0 or greater than canvas width or height, point is out of current map view.
X pixel coordinate from latitude longitude and center
image153.png
Y pixel coordinate from latitude and center image155.png