GEO Data Handling and Mapbox Integration

Geographical hierarchy

The project basically follows the Wikitravel Geographical Hierarchy approach of organizing geopgraphical units:

Lat, Lon, GeoJSON

  • Bangkok Latitude is 13.736717 and longitude is 100.523186.

  • Google notation would be @13.7244416,100.3529157 (Lat, Lon)!!!

  • GeoJSON position coordinates would be `` (Lon,Lat)!!!

"geometry": {
    "type": "Point",
    "coordinates": [100.523186, 13.736717]
}
A position is an array of numbers. There MUST be two or more elements. The first two elements are longitude and latitude, or easting and northing, precisely in that order and using decimal numbers. Altitude or elevation MAY be included as an optional third element.
— rfc7946 GeoJSON Point
https://tools.ietf.org/html/rfc7946#section-3.1.2

Examples and RegEx for goole maps URLs

Finding the correct RegEx expression for your map’s URL: Scrape latitude and longitude data from a Google Maps link

Google format is LATitude followed by LONngitude and Z (altitude? data grid? we don’t know and don’t need) Examples:

@41.25907,13.4254605,15z  Sperlonga
@-0.9175391,9.2298915,7z Gabun Äquator
@1.3143394,103.7041653,11z Singapur

If the latitude and longitude in the URL is preceded by an "@" symbol, e.g. https://www.google.ca/maps/@43.6474528,-79.3799409,14.78z):

Table 1. RegEx for Google Notation

Type

RegEx

Latitude

\@(-?[\d\.]*)

Longitude

\@\,([-?\d\.])

Launch Google Maps and perform a specific action

https://www.google.com/maps/search/?api=1&query=1.3143394,103.7041653  (lands in Singapur)

How to find nearest location using lat/lon from sql db

SELECT id,name,
       (3959 * acos(cos(radians(50)) * cos(radians(coordinates[2])) *
        cos(radians(coordinates[1]) - radians(8.5)) + sin(radians(50)) *
        sin(radians(coordinates[2])))) AS distance
FROM place ORDER BY distance asc

Reverse Geocoding

Often we have the coordinates of an entity, but not the country code. There are lots of possibilities for this so-called reverse lookup, most of which are outlined in the excellent Stackoverflow Thread Given the lat/long coordinates, how can we find out the city/country?. But when you take a closer look, most of them have severe usage constrains (e.g. you’re not allowed to retain the data, retrieval must be triggered by user interaction, results must be displayed on a map and so forth), or require offline installations. Nomatim and its Reverse API look the most promising so far. The Usage policy merely demands moderate usage (1 request per second which could be achieved by Rate limiting) and other bulk restrictions, but this is OK for our user case.

Test Bangkok Wat Arun @ lat=13.7435571 lon=100.4898632:

$ curl -sS 'https://nominatim.openstreetmap.org/reverse?lat=13.7435571&lon=100.4898632&format=jsonv2' | jq .
  1. JSON Response for วัดอรุณราชวรารามราชวรมหาวิหาร"

{
  "place_id": 106523360,
  "licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
  "osm_type": "way",
  "osm_id": 23481741,
  "lat": "13.743913150000001",
  "lon": "100.48848833622938",
  "place_rank": 30,
  "category": "amenity",
  "type": "place_of_worship",
  "importance": 0.3817760669396329,
  "addresstype": "amenity",
  "name": "วัดอรุณราชวรารามราชวรมหาวิหาร",
  "display_name": "วัดอรุณราชวรารามราชวรมหาวิหาร, 158, Thanon Wang Doem, แขวงวัดอรุณ, เขตบางกอกใหญ่, กรุงเทพมหานคร, 10600, ประเทศไทย",
  "address": {
    "amenity": "วัดอรุณราชวรารามราชวรมหาวิหาร",
    "house_number": "158",
    "road": "Thanon Wang Doem",
    "quarter": "แขวงวัดอรุณ",
    "suburb": "เขตบางกอกใหญ่",
    "city": "กรุงเทพมหานคร",
    "state": "กรุงเทพมหานคร",
    "postcode": "10600",
    "country": "ประเทศไทย",
    "country_code": "th"
  },
  "boundingbox": [
    "13.7427134",
    "13.7450935",
    "100.4869145",
    "100.4898908"
  ]
}