Skip to content

Instantly share code, notes, and snippets.

@jlaura
Created December 7, 2022 16:16
Show Gist options
  • Save jlaura/75c6819f286d13b94458949b9df0c63d to your computer and use it in GitHub Desktop.
Save jlaura/75c6819f286d13b94458949b9df0c63d to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "93a6f9f0-5401-47c2-8f23-0297040548f5",
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import os\n",
"import requests\n",
"\n",
"from pyproj import CRS\n",
"WGS84_CRS = CRS.from_epsg(4326)\n",
"import morecantile\n",
"\n",
"from dataclasses import dataclass, field, asdict\n",
"import json\n",
"# try to add Area of Use to each CRS first\n",
"# use the .name and .area_of_use.bounds\n",
"\n",
"matrix_scale_mercator = [1, 1]\n",
"matrix_scale_platecur = [2, 1]\n",
"\n",
"@dataclass()\n",
"class Tmsparam(object):\n",
" # Bounding box of the Tile Matrix Set, (left, bottom, right, top).\n",
" extent: tuple[float, float, float, float]\n",
" # Tile Matrix Set coordinate reference system\n",
" crs: CRS\n",
" # Width of each tile of this tile matrix in pixels (default is 256).\n",
" tile_width: int = 256\n",
" # Height of each tile of this tile matrix in pixels (default is 256).\n",
" tile_height: int = 256\n",
" # Tiling schema coalescence coefficient, below you can just pass in either matrix_scale lists defined above\n",
" matrix_scale: list = field(default_factory=lambda: matrix_scale_platecur)\n",
" # Extent's coordinate reference system, as a pyproj CRS object.\n",
" extent_crs: CRS | None = None\n",
" # Tile Matrix Set minimum zoom level (default is 0).\n",
" minzoom: int = 0\n",
" # Tile Matrix Set maximum zoom level (default is 24).\n",
" # TODO: find doc page on going from min/max zoom to GSD/degree per pixel\n",
" maxzoom: int = 24\n",
" # Tile Matrix Set title (default is 'Custom TileMatrixSet')\n",
" title: str = \"Custom TileMatrixSet\"\n",
" # Tile Matrix Set identifier (default is 'Custom')\n",
" identifier: str = \"Custom\"\n",
" # Geographic (lat,lon) coordinate reference system (default is EPSG:4326)\n",
" geographic_crs: CRS = WGS84_CRS\n",
"\n",
" def __post_init__(self):\n",
" # try to get the geographic_crs\n",
" geographic_crs = self.crs.geodetic_crs\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e4f7f587-8999-4e61-b59a-d51a805394b6",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/jlaura/miniconda3/envs/planetcantile/lib/python3.11/site-packages/urllib3/connectionpool.py:1045: InsecureRequestWarning: Unverified HTTPS request is being made to host 'raw.githubusercontent.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings\n",
" warnings.warn(\n"
]
}
],
"source": [
"# Grab the wkts that mirror the OCG source and parse out just the GeogCRS in a hacktastic way.\n",
"resp = requests.get('https://raw.githubusercontent.com/pdssp/planet_crs_registry/main/data/result.wkts', verify=False) # ... #securityfails\n",
"\n",
"geocrss = []\n",
"for wkt_str in resp.text.split(os.linesep + os.linesep):\n",
" if 'GEOGCRS' in wkt_str[:7]:\n",
" geocrss.append(wkt_str)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a3c925b7-1760-4539-9652-d3df44e53308",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"GEOGCRS[\"Sun (2015) - Sphere / Ocentric\",\n",
" DATUM[\"Sun (2015) - Sphere\",\n",
" \tELLIPSOID[\"Sun (2015) - Sphere\", 695700000, 0,\n",
"\t\tLENGTHUNIT[\"metre\", 1, ID[\"EPSG\", 9001]]]],\n",
" \tPRIMEM[\"Reference Meridian\", 0,\n",
" ANGLEUNIT[\"degree\", 0.0174532925199433, ID[\"EPSG\", 9122]]],\n",
"\tCS[ellipsoidal, 2],\n",
"\t AXIS[\"geodetic latitude (Lat)\", north,\n",
"\t ORDER[1],\n",
"\t ANGLEUNIT[\"degree\", 0.0174532925199433]],\n",
"\t AXIS[\"geodetic longitude (Lon)\", east,\n",
"\t ORDER[2],\n",
"\t ANGLEUNIT[\"degree\", 0.0174532925199433]],\n",
"\tID[\"IAU\", 1000, 2015],\n",
"\tREMARK[\"Source of IAU Coordinate systems: doi://10.1007/s10569-017-9805-5\"]]\n"
]
}
],
"source": [
"print(geocrss[0])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "2fc6d323-f6c2-46c0-a61c-bfcdefc060b3",
"metadata": {},
"outputs": [],
"source": [
"# Build a crss list dynamically. like Andrew's\n",
"\n",
"crss = []\n",
"for crs in geocrss:\n",
" try:\n",
" crs_obj = CRS(crs)\n",
" except Exception as e:\n",
" # These are triaxial definition and they fail. \n",
" continue\n",
" title = crs_obj.name\n",
" \n",
" # There has to be an easier way to get at the id?\n",
" crs_as_dict = json.loads(crs_obj.to_json())\n",
" i = crs_as_dict['id']\n",
" identifier = f'{i[\"authority\"]}_{i[\"code\"]}_{i[\"version\"]}'\n",
"\n",
" tmsp = Tmsparam(\n",
" crs=crs_obj,\n",
" extent=(-180.0, -90.0, 180.0, 90.0),\n",
" title=title,\n",
" identifier=identifier,\n",
" maxzoom=22, # set lower for 200mpp MOLA\n",
" geographic_crs=crs_obj\n",
" )\n",
" \n",
" crss.append(tmsp)\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "6995264d-51fb-48d0-91a7-06fe7b079a19",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"wrote ./IAU_1000_2015_tms.json\n",
"wrote ./IAU_19900_2015_tms.json\n",
"wrote ./IAU_19901_2015_tms.json\n",
"wrote ./IAU_29900_2015_tms.json\n",
"wrote ./IAU_30100_2015_tms.json\n",
"wrote ./IAU_39900_2015_tms.json\n",
"wrote ./IAU_39901_2015_tms.json\n",
"wrote ./IAU_40100_2015_tms.json\n",
"wrote ./IAU_40200_2015_tms.json\n",
"wrote ./IAU_49900_2015_tms.json\n",
"wrote ./IAU_49901_2015_tms.json\n",
"wrote ./IAU_50100_2015_tms.json\n",
"wrote ./IAU_50200_2015_tms.json\n",
"wrote ./IAU_50300_2015_tms.json\n",
"wrote ./IAU_50301_2015_tms.json\n",
"wrote ./IAU_50400_2015_tms.json\n",
"wrote ./IAU_50401_2015_tms.json\n",
"wrote ./IAU_50500_2015_tms.json\n",
"wrote ./IAU_50600_2015_tms.json\n",
"wrote ./IAU_50601_2015_tms.json\n",
"wrote ./IAU_50700_2015_tms.json\n",
"wrote ./IAU_50701_2015_tms.json\n",
"wrote ./IAU_50800_2015_tms.json\n",
"wrote ./IAU_50801_2015_tms.json\n",
"wrote ./IAU_50900_2015_tms.json\n",
"wrote ./IAU_50901_2015_tms.json\n",
"wrote ./IAU_51000_2015_tms.json\n",
"wrote ./IAU_51001_2015_tms.json\n",
"wrote ./IAU_51100_2015_tms.json\n",
"wrote ./IAU_51101_2015_tms.json\n",
"wrote ./IAU_51200_2015_tms.json\n",
"wrote ./IAU_51201_2015_tms.json\n",
"wrote ./IAU_51300_2015_tms.json\n",
"wrote ./IAU_51301_2015_tms.json\n",
"wrote ./IAU_51400_2015_tms.json\n",
"wrote ./IAU_51500_2015_tms.json\n",
"wrote ./IAU_51600_2015_tms.json\n",
"wrote ./IAU_59900_2015_tms.json\n",
"wrote ./IAU_59901_2015_tms.json\n",
"wrote ./IAU_60100_2015_tms.json\n",
"wrote ./IAU_60200_2015_tms.json\n",
"wrote ./IAU_60300_2015_tms.json\n",
"wrote ./IAU_60400_2015_tms.json\n",
"wrote ./IAU_60500_2015_tms.json\n",
"wrote ./IAU_60600_2015_tms.json\n",
"wrote ./IAU_60700_2015_tms.json\n",
"wrote ./IAU_60800_2015_tms.json\n",
"wrote ./IAU_60801_2015_tms.json\n",
"wrote ./IAU_60900_2015_tms.json\n",
"wrote ./IAU_61000_2015_tms.json\n",
"wrote ./IAU_61100_2015_tms.json\n",
"wrote ./IAU_61200_2015_tms.json\n",
"wrote ./IAU_61300_2015_tms.json\n",
"wrote ./IAU_61400_2015_tms.json\n",
"wrote ./IAU_61500_2015_tms.json\n",
"wrote ./IAU_61600_2015_tms.json\n",
"wrote ./IAU_61700_2015_tms.json\n",
"wrote ./IAU_61800_2015_tms.json\n",
"wrote ./IAU_63200_2015_tms.json\n",
"wrote ./IAU_63300_2015_tms.json\n",
"wrote ./IAU_63400_2015_tms.json\n",
"wrote ./IAU_63500_2015_tms.json\n",
"wrote ./IAU_64900_2015_tms.json\n",
"wrote ./IAU_64901_2015_tms.json\n",
"wrote ./IAU_65300_2015_tms.json\n",
"wrote ./IAU_69900_2015_tms.json\n",
"wrote ./IAU_69901_2015_tms.json\n",
"wrote ./IAU_70100_2015_tms.json\n",
"wrote ./IAU_70200_2015_tms.json\n",
"wrote ./IAU_70300_2015_tms.json\n",
"wrote ./IAU_70400_2015_tms.json\n",
"wrote ./IAU_70500_2015_tms.json\n",
"wrote ./IAU_70600_2015_tms.json\n",
"wrote ./IAU_70700_2015_tms.json\n",
"wrote ./IAU_70800_2015_tms.json\n",
"wrote ./IAU_70900_2015_tms.json\n",
"wrote ./IAU_71000_2015_tms.json\n",
"wrote ./IAU_71100_2015_tms.json\n",
"wrote ./IAU_71200_2015_tms.json\n",
"wrote ./IAU_71300_2015_tms.json\n",
"wrote ./IAU_71400_2015_tms.json\n",
"wrote ./IAU_71500_2015_tms.json\n",
"wrote ./IAU_79900_2015_tms.json\n",
"wrote ./IAU_79901_2015_tms.json\n",
"wrote ./IAU_80100_2015_tms.json\n",
"wrote ./IAU_80200_2015_tms.json\n",
"wrote ./IAU_80201_2015_tms.json\n",
"wrote ./IAU_80300_2015_tms.json\n",
"wrote ./IAU_80301_2015_tms.json\n",
"wrote ./IAU_80400_2015_tms.json\n",
"wrote ./IAU_80401_2015_tms.json\n",
"wrote ./IAU_80500_2015_tms.json\n",
"wrote ./IAU_80501_2015_tms.json\n",
"wrote ./IAU_80600_2015_tms.json\n",
"wrote ./IAU_80601_2015_tms.json\n",
"wrote ./IAU_80700_2015_tms.json\n",
"wrote ./IAU_80701_2015_tms.json\n",
"wrote ./IAU_80800_2015_tms.json\n",
"wrote ./IAU_89900_2015_tms.json\n",
"wrote ./IAU_89901_2015_tms.json\n",
"wrote ./IAU_90100_2015_tms.json\n",
"wrote ./IAU_90101_2015_tms.json\n",
"wrote ./IAU_99900_2015_tms.json\n",
"wrote ./IAU_99901_2015_tms.json\n",
"wrote ./IAU_100000500_2015_tms.json\n",
"wrote ./IAU_100000501_2015_tms.json\n",
"wrote ./IAU_100001200_2015_tms.json\n",
"wrote ./IAU_100003600_2015_tms.json\n",
"wrote ./IAU_100003601_2015_tms.json\n",
"wrote ./IAU_100004100_2015_tms.json\n",
"wrote ./IAU_100009300_2015_tms.json\n",
"wrote ./IAU_100010700_2015_tms.json\n",
"wrote ./IAU_200000100_2015_tms.json\n",
"wrote ./IAU_200000101_2015_tms.json\n",
"wrote ./IAU_200000400_2015_tms.json\n",
"wrote ./IAU_200001600_2015_tms.json\n",
"wrote ./IAU_200002100_2015_tms.json\n",
"wrote ./IAU_200005200_2015_tms.json\n",
"wrote ./IAU_200021600_2015_tms.json\n",
"wrote ./IAU_200025300_2015_tms.json\n",
"wrote ./IAU_200043300_2015_tms.json\n",
"wrote ./IAU_200043301_2015_tms.json\n",
"wrote ./IAU_200051100_2015_tms.json\n",
"wrote ./IAU_200286700_2015_tms.json\n",
"wrote ./IAU_200417900_2015_tms.json\n",
"wrote ./IAU_202514300_2015_tms.json\n",
"wrote ./IAU_243101000_2015_tms.json\n",
"wrote ./IAU_951101000_2015_tms.json\n"
]
}
],
"source": [
"for tmsp in crss:\n",
" # create the tms object\n",
" tms = morecantile.TileMatrixSet.custom(**asdict(tmsp))\n",
" _d = json.loads(tms.json(exclude_none=True)) # get to pretty printed json\n",
" with open(f'./{tmsp.identifier}_tms.json', 'w') as dst: # Swapped to the identified since it is a clean string\n",
" # dump to json\n",
" json.dump(_d, dst, indent=4, ensure_ascii=False)\n",
" print(f'wrote {dst.name}')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "37e654e4-0060-463a-ab06-3a68bbe9c05f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "planetcantile",
"language": "python",
"name": "planetcantile"
},
"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.11.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment