Skip to content

Instantly share code, notes, and snippets.

@tjmcewan
Created July 26, 2025 07:46
Show Gist options
  • Save tjmcewan/f5b145a640aa0451c84a87a8fa0cf338 to your computer and use it in GitHub Desktop.
Save tjmcewan/f5b145a640aa0451c84a87a8fa0cf338 to your computer and use it in GitHub Desktop.
Country boundaries polygons for country-appending points. Based on Movisda's coastal-buffered polygons.
#!/usr/bin/env bash
set -euo pipefail
# Movisda is a great source for admin boundaries because they have relatively
# precise land boundary tracing, and buffered coastal boundary tracing, so we
# don't miss coastal points that would fall outside imprecise boundaries.
curl -O https://osm.download.movisda.io/admin/Admin-latest.geojson
# This query selects country polygons (admin_level = 2) and resolves overlaps.
# For any two intersecting polygons, the overlapping area is subtracted
# from the polygon that has the higher `osm_id`, ensuring no gaps are created.
# ST_Overlaps is used instead of ST_Intersects to only consider significant
# area-based overlaps, not simple boundary touches.
# ST_MakeValid is used to fix any invalid geometries in the
# source data, which resolves TopologyExceptions.
datestamp=$(date +%Y%m%d)
query="
load spatial;
copy (
with countries as (
select
*,
st_makevalid(geom) as valid_geom
from st_read('Admin-latest.geojson')
where admin_level = 2
),
areas_to_subtract as (
select
c1.osm_id,
st_union_agg(c2.valid_geom) as clipping_geom
from
countries as c1
join
countries as c2 on st_overlaps(c1.valid_geom, c2.valid_geom) and c1.osm_id > c2.osm_id
group by
c1.osm_id
)
select
c.osm_id,
c.osm_version,
c.prefix,
c.bytes,
c.subareas,
c.timestamp,
c.admin_level,
c.name_en,
c.name,
c.cx,
c.cy,
coalesce(st_difference(c.valid_geom, ats.clipping_geom), c.valid_geom) as geom
from
countries as c
left join
areas_to_subtract as ats on c.osm_id = ats.osm_id
) to 'boundaries-${datestamp}.parquet'
;
"
duckdb -c "$query"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment