Skip to content

Instantly share code, notes, and snippets.

@wboykinm
Last active October 4, 2023 20:41
Show Gist options
  • Save wboykinm/4550c13c30baee99febc6f02028b0ec6 to your computer and use it in GitHub Desktop.
Save wboykinm/4550c13c30baee99febc6f02028b0ec6 to your computer and use it in GitHub Desktop.
BUILD A DATASET OF TILES AT A GIVEN ZOOM LEVEL THAT INTERSECT LAND
# BUILD A DATASET OF TILES AT A GIVEN ZOOM LEVEL THAT INTERSECT LAND
# USAGE: bash land_tiles.sh <integer zoom level between 0 and 22>
# Set target metatile zoom
ZOOM=$1
BBOX="[-180,-90,180,90]"
# Create metatiles at desired zoom
echo ${BBOX} | \
mercantile tiles ${ZOOM} | \
mercantile shapes > \
z${ZOOM}_metatiles.geojsonl
# Get Natural Earth countries
curl -O https://naciscdn.org/naturalearth/10m/cultural/ne_10m_admin_0_countries.zip
unzip ne_10m_admin_0_countries.zip
# Join countries and metatiles
ogr2ogr \
-f GeoJSON \
z${ZOOM}_land_metatiles.geojson \
z${ZOOM}_metatiles.geojsonl \
-dialect sqlite \
-sql "
SELECT
m.id,
REPLACE(REPLACE(REPLACE(m.id,', ','-'),'(',''),')','') AS layer,
m.Geometry,
GROUP_CONCAT(c.Name) AS countries
FROM 'z${ZOOM}_metatiles.geojsonl'.z${ZOOM}_metatiles m
JOIN 'ne_10m_admin_0_countries.shp'.ne_10m_admin_0_countries c
ON ST_Intersects(m.Geometry, c.Geometry)
WHERE c.Name NOT IN ('Antarctica')
GROUP BY m.id,m.Geometry
"
# Clean up
rm -rf ne_10m_admin_0_countries.* z${ZOOM}_metatiles.geojsonl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment