Skip to content

3 Tiles and Geography

Oliver Heilig edited this page Apr 21, 2024 · 13 revisions

This section explains the basics of the mercator projection used by nearly all web maps. Most geospatial data sets you'll find today are defined by latitude/longitude values, referring to the World Geodetic Sytstem (WGS84). When you display this data on a map (i.e. screen), you always do a map projection. Even if you paint the latitude/longitude values directly as pixel coordinates, you implicitly do a projection, the plate carrée projection. Some people refer to this method as "unprojected", but this isn't really correct. Any mapping of coordinates on the globe to a paper or screen involves some kind of projection.

The projection used by today's web maps (and by the tiling system which is used here) is the Mercator Projection. (In fact it is some sloppy kind of "Spherical Mercator", but that doesn't matter for us). In contrast to the direct lat/lon-projection, the mercator projection is conformal, which means it preserves shapes and angles, and that makes it a very appropriate projection for aerial imagery or navigation systems. One disadvantage is the distortion of sizes away from the equator, which makes Greenland appear as big as Africa, while Africa is in fact 5 times larger than Greenland.

In this step we write a tile provider that renders all latitude and longitude lines that are contained within a tile.

https://spatialtutorial.azurewebsites.net/03-TilesAndGeographyHandler.ashx?x=33&y=23&z=6

Latitude Lines

The typical tile rendering steps are as follows:

  • Get the latitude/longitude bounds for the tile key
  • Loop through all elements that are within these bounds
  • Transform the coordinates of those elements to screen (=tile) coordinates and render them

We need two basic transformations for this:

  • To query the elements for a tile, we have to get the mercator bounds for the tile key, and then transform it to WGS (lat/lon).
  • To display a geographic (lat/lon) coordinate on a screen/tile, we first have to project the coordinate to the mercator system, and then shift/scale it to the screen/tile viewport.

For these transformations i made a tools class

https://github.com/ptv-logistics/SpatialTutorial/blob/master/TransformTools.cs

with the functions

public static Rect TileToWgs(uint x, uint y, uint z)
public static Point WgsToTile(uint x, uint y, uint z, Point wgsPoint)

The rendering handler calculates the wgs bounds and renders the lines with integral latitudes and longitudes that are within these bounds.

https://github.com/ptv-logistics/SpatialTutorial/blob/master/03-TilesAndGeographyHandler.ashx.cs

The result is a map with the latitude/longitude grid

https://spatialtutorial.azurewebsites.net/03-TilesAndGeography.html