notes by alifeee profile picture tagged maps (2) rss

return to notes / blog / website / weeknotes / linktree

here I may post some short, text-only notes, mostly about programming. source code.

tags: all (44) scripting (13) linux (5) bash (4) geojson (4) obsidian (4) android (3) github (3) html (3) jq (3) ............ see all (+63)

how to get a GPS trace of train and boat journeys # prev single next top

tags: travel, geojson, gpx, maps • 510 'words', 153 secs @ 200wpm

I like sustainable travel. I also like interrailing. I also like maps.

Let's combine all three! This winter I went via train from Sheffield to Hamburg (for Chaos Computer Club), and then on to Lapland, and back.

The map

To skip to the chase, I made a coordinates file of the trip, and you can see it here on a map:

https://geojson.io/#data=data:text/x-url,https%3A%2F%2Fraw.githubusercontent.com%2Falifeee%2Feurope-trips%2Frefs%2Fheads%2Fmain%2F2024-12%2520CCC%2Fall.geojson

It's combined from train journeys, ferry journeys, and bus journeys.

Train data

I got the train routing data in .gpx format from https://brouter.damsy.net/, selecting the "Rail" profile in the dropdown. Then, I clicked close to the stations I went to/from/past, got a nice map that looked alright, and exported it.

Bus data

I also used https://brouter.damsy.net/ for this, after I'd found it was good for trains. I just selected one of the "Car" profiles, and set my waypoints, and exported it in the same way.

Ferry data

This was different, as ferries don't use roads or train tracks [citation needed]. But! They are documented well on mapping services. So, I found the route I wanted on https://www.openstreetmap.org/ (OSM) (e.g., the Liepãja to Travemünde Ferry) by using the little questionmark "query feature" button, then opened it on https://overpass-turbo.eu/ (a website for querying OSM data) by writing the query (with the correct feature ID):

way(128069455); out geom;

Then, I can click "Export" to get the .gpx (or other format) data out.

Combining

I spent a long time trying to figure out how to combine .gpx files with ogrmerge.

However, I couldn't figure it out. .gpx is confusing, and everyone who uses it seems to use GUI tools like arcgis or qgis, while I prefer to be able to do things with a command, which I can then repeat in future.

In the end, I converted the files to .geojson (my one true love) with ogr2ogr file111.geojson file111.gpx tracks for each file, and then combined them. Handily, I'd already written a note about combining .geojson files! I wish I stuck in .geojson the whole time. .gpx gives me headaches.

The End

That's it!

I could then load the combined file into https://geojson.io/ to check all was well (it was, I expected I might have to "reverse" some paths to be "forwards"), and I uploaded it to a new GitHub repository, https://github.com/alifeee/europe-trips/.

I also laser cut a mini Europe with a line for the trip on the map, as a gift for my lover :]

back to top

getting latlong coordinates from an address with geocode.xyz # prev single next top

tags: scripting, maps • 369 'words', 111 secs @ 200wpm

I like maps. I make maps. Mostly from worse maps or data that is not in map form. See some of mine on https://alifeee.co.uk/maps/.

One thing I've been doing for a map recently is geocoding, which is turning an address (e.g., "Showroom Cinema, Paternoster Row, Sheffield") into latitude/longitude coordinates.

https://geocode.xyz/ provides a free geocoding API on https://geocode.xyz/api which is rate limited to one request per second.

Here is a script to wrap that API for using it as a script. It's not amazing but it works.

#!/bin/bash

loc="${1}"
throttled=1

while [ $throttled = 1 ]; do
  resp=$(curl -s -X POST -d locate="${loc}" -d geoit="json" "https://geocode.xyz")
  if [[ "${resp}" =~ Throttled ]]; then
    echo "throttled... retrying..." >> /dev/stderr
    throttled=1
  else
    throttled=0
  fi
  sleep 1
done

echo "got response: ${resp}" >> /dev/stderr

json=$(echo "${resp}" | jq | sed 's/ {}/""/g')

basic=$(echo "${json}" | jq -r '
.latt + "\t" +
.longt + "\t" +
.standard.confidence + "\t"'
)

standard=$(echo "${json}" | jq -r '
.standard.addresst? + "\t" +
.standard.statename? + "\t" +
.standard.city? + "\t" +
.standard.prov? + "\t" +
.standard.countryname? + "\t" +
.standard.postal? + "\t"
')

alt=$(echo "${json}" | jq -r '
.alt?.loc?.addresst + "\t" +
.alt?.loc?.statename + "\t" +
.alt?.loc?.city + "\t" +
.alt?.loc?.prov + "\t" +
.alt?.loc?.countryname + "\t" +
.alt?.loc?.postal + "\t"
')
echo "${basic}${standard}${alt}" | sed '1s/^/latitude\tlongitude\tconfidence\taddress\tstate\tcity\tprovince\tcountry\tpost code\talt address\talt state\talt city\talt province\talt country\talt postal\n/'

and then you can use it like

$ ./geocode.sh "Showroom Cinema, Paternoster Row, Sheffield"
throttled... retrying...
throttled... retrying...
got response: {   "standard" : {      "stnumber" : "1",      "addresst" : "Paternoster Row",      "statename" : "England",      "postal" : "S1",      "region" : "England",      "prov" : "UK",      "city" : "Sheffield",      "countryname" : "United Kingdom",      "confidence" : "0.9"   },   "longt" : "-1.46544",   "alt" : {},   "elevation" : {},   "latt" : "53.37756"}
latitude	longitude	confidence	address	state	city	province	country	post code	alt address	alt state	alt city	alt province	alt country	alt postal
53.37756	-1.46544	0.9	Paternoster Row	England	Sheffield	UK	United Kingdom	S1

The results are "ok". They're pretty good for street addresses, but I can see a lot of wrong results. I might try and use another API like OpenStreetMap's or (shudders) Google's.

back to top