Skip to content

Instantly share code, notes, and snippets.

@giswqs
Created May 11, 2025 03:10
Show Gist options
  • Save giswqs/ad7972bf544e861cd7015070b922d978 to your computer and use it in GitHub Desktop.
Save giswqs/ad7972bf544e861cd7015070b922d978 to your computer and use it in GitHub Desktop.
Military Grid Reference System (MGRS)
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "7933de33",
"metadata": {},
"source": [
"# Download Military Grid Reference System (MGRS) 100km grid lines\n",
"\n",
"https://earth-info.nga.mil/index.php?dir=coordsys&action=mgrs-100km-polyline-dloads"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cc3852a2",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import requests\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "863e666c",
"metadata": {},
"outputs": [],
"source": [
"url = \"https://github.com/opengeos/datasets/releases/download/world/mgrs_grid_zone.csv\"\n",
"# missing 49X, 50X, 58X\n",
"# Zone 28 files only contain .shp files without dbf files\n",
"df = pd.read_csv(url)\n",
"zones = df[\"GZD\"].to_list()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "73b86227",
"metadata": {},
"outputs": [],
"source": [
"for index, zone in enumerate(zones):\n",
" print(f\"Downloading {index + 1}/{len(zones)}: {zone}\")\n",
"\n",
" # URL for the file\n",
" url = f\"https://earth-info.nga.mil/php/download.php?file=MGRS_100kmSQ_ID_{zone}\"\n",
" filename = f\"MGRS_100kmSQ_ID_{zone}.zip\"\n",
" if os.path.exists(filename):\n",
" # print(f\"File {filename} already exists. Skipping download.\")\n",
" continue\n",
" else:\n",
" print(f\"File {filename} does not exist. Downloading...\")\n",
" # Custom headers to mimic a browser\n",
" headers = {\n",
" \"User-Agent\": \"Mozilla/5.0\"\n",
" }\n",
"\n",
" # Send the GET request\n",
"\n",
" try:\n",
" response = requests.get(url, headers=headers)\n",
"\n",
" # Check if download was successful\n",
" if response.status_code == 200 and \"Content-Disposition\" in response.headers:\n",
" filename = response.headers[\"Content-Disposition\"].split(\"filename=\")[-1].strip('\"')\n",
" with open(filename, \"wb\") as f:\n",
" f.write(response.content)\n",
" print(f\"Downloaded: {filename}\")\n",
" else:\n",
" print(f\"Failed to download. Status code: {response.status_code}\")\n",
" except Exception as e:\n",
" print(f\"An error occurred: {e}\")\n",
" continue\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "693e23d7",
"metadata": {},
"outputs": [],
"source": [
"import leafmap"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3badb4fe",
"metadata": {},
"outputs": [],
"source": [
"files = leafmap.find_files(\".\", ext=\".shp\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "15f1b21d",
"metadata": {},
"outputs": [],
"source": [
"len(files)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "25b008d2",
"metadata": {},
"outputs": [],
"source": [
"gdf = leafmap.merge_vector(files, crs=\"EPSG:4326\", return_gdf=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "59ed1b1f",
"metadata": {},
"outputs": [],
"source": [
"gdf"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "36c58e90",
"metadata": {},
"outputs": [],
"source": [
"gdf[[\"100kmSQ_ID\", \"GZD\", \"MGRS\", \"geometry\"]].to_file(\"MGRS_100km.gpkg\")"
]
},
{
"cell_type": "markdown",
"id": "8f2e44f6",
"metadata": {},
"source": [
"Convert to PMTiles\n",
"\n",
"```bash\n",
"ogr2ogr -skipfailures -f PMTiles -dsco MINZOOM=0 -dsco MAXZOOM=15 MGRS_100km.pmtiles MGRS_100km.parquet\n",
"```"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "geo",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment