Skip to main content

Alex Smith

Where is the highest 7-Eleven in Thailand?

Table of Contents

Here I describe how I used OpenStreetMap and SRTM data to answer a question nobody asked.

# Gathering Data

## OpenStreetMap: Finding Convenience Stores

OpenStreetMap is crowdsourced alternative to Google Maps, offering freely accessible geodata. Using Overpass Turbo we can easily query OSM:

  1. Click on wizard
  2. Type "convenience store" in thailand
  3. Run the query (Overpass generates it for us)
  4. Click export and download the data (I used raw JSON format)

The result is a big JSON file with ~10,000 points matching the “convenience store” type.

Overpass Turbo example

Note: Because this query returns a lot of data, it may cause browser slow-down.

## SRTM: Elevation Data

Since OSM doesn’t have elevation data, I used Shuttle Radar Topography Mission (SRTM) data from NASA. The CGIAR-CSI GeoPortal provides easy access to it:

  1. Select SRTM Data from the menu
  2. Select the regions we want and click Search
  3. Download all the ZIP files and extract the TIFFs

CGIAR-CSI GeoPortal example

Note: When selecting regions, it’s difficult to guess exactly which area each square corresponds to. You might have to use trial-and-error (you’ll see maps highlighting the areas you selected after you click Search).

# Processing Data

## Formatting OSM Data

I wrote a Ruby script to parse the JSON and generate a simple CSV containing names and coordinates:

require 'json'
require 'csv'

data = JSON.load_file('thailand-convenience-stores.json')

out = CSV.generate col_sep: "\t" do |csv|
  data['elements'].each do
    lat, lon =
      if _1['bounds']
        # Some points represent areas instead of containing an exact
        # coordinate, requiring a geometric midpoint calculation
        [
          (_1['bounds']['minlat'] + _1['bounds']['maxlat']) / 2,
          (_1['bounds']['minlon'] + _1['bounds']['maxlon']) / 2,
        ]
      else
        [_1['lat'], _1['lon']]
      end

    name = _1['tags'].slice('name:en', 'name', 'alt_name').values.compact.first ||
      'convenience store'

    csv << [name, lat, lon]
  end
end

puts out

## Getting Elevation with GDAL

Using gdallocationinfo, (part of the GDAL library) I can cross-referenced OSM coordinates with SRTM elevations:

gdallocationinfo -geoloc <path to TIFF file> <longitude> <latitude>

A shell script, looping through the CSV, queries each coordinate against SRTM tiles:

#!/bin/sh

while read -r line; do
  lat="$(echo "$line" | cut -f2)"
  lon="$(echo "$line" | cut -f3)"

  # `gdallocationinfo` only accepts one TIFF file per invocation, requiring
  # multiple calls per row, one for each TIFF
  el="$(ls elevation-data/*.tif |
    while read -r filepath; do
      gdallocationinfo -geoloc "$filepath" "$lon" "$lat" |
        grep "Value:" | sed "s/ \+Value: //"
    done)"

  echo -e "$line\t$el"
done < convenience-stores.csv

# Result

After sorting the resulting CSV, the answer is in Mae Salong, a little village in the mountains of Chiang Rai province. There are actually two 7-Elevens here, standing at an elevation of ~1,100 metres:

Not all 7-Elevens in the OSM data are labelled as such, so it takes a little manual investigation of the results to find the answer.

# Caveat

OSM is impressive but it’s not the most complete or up-to-date, especially in remote areas. Therefore, data I pulled from OSM is probably not an accurate reflection of all convenience stores in Thailand. The two 7-Elevens in Mae Salong are likely among the highest, but they might not be the highest.

# Why Bother?

The heat of Southeast Asia is concerning for an Englishman (like me) interested in living there. Higher altitudes bring cooler temperatures but fewer amenities. So: Where can I find convenience at a greater elevation? A convenience store, of course!

# Bonus

Mae Salong seems like it’s worth a visit. It’s famous for its tea plantations and Phra Boromathat Chedi (which looks amazing).