"""Titiler-based Mars tile server for MGS MOLA ClrShade global basemap""" import math from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from pyproj import CRS from morecantile import tms, TileMatrixSet from rio_tiler.io import Reader from titiler.core.factory import TilerFactory # Mars SimpleCylindrical CRS (equidistant cylindrical projection in meters) MARS_CRS = CRS.from_proj4( "+proj=eqc +lat_ts=0 +lat_0=0 +lon_0=0 +x_0=0 +y_0=0 " "+a=3396190 +b=3396190 +units=m +no_defs" ) # Mars extent in equidistant cylindrical projection # x = R × lon_rad, y = R × lat_rad _R = 3396190 _MAX_X = _R * math.pi # ≈ 10,669,442 m (corresponds to ±180°) _MAX_Y = _R * math.pi / 2 # ≈ 5,334,721 m (corresponds to ±90°) # Custom TMS for Mars with matrix_scale=[2, 1] to match Cesium GeographicTilingScheme MARS_TMS = TileMatrixSet.custom( crs=MARS_CRS, extent=(-_MAX_X, -_MAX_Y, _MAX_X, _MAX_Y), identifier="MarsCylindrical", matrix_scale=[2, 1], ) # Register custom TMS tms = tms.register({"MarsCylindrical": MARS_TMS}) # Create FastAPI app app = FastAPI( title="Titiler Mars", description="Mars MGS MOLA Color-Shaded Relief global tile server powered by Titiler", ) # Configure CORS app.add_middleware( CORSMiddleware, allow_origins=[ "http://digitalmars.com.cn", "https://digitalmars.com.cn", ], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Create COG Tiler with Mars TMS support cog = TilerFactory( reader=Reader, supported_tms=tms, ) app.include_router(cog.router, tags=["Cloud Optimized GeoTIFF"]) @app.get("/", include_in_schema=False) def landing(): """Landing page.""" return { "title": "Titiler Mars Tile Server", "description": "Mars MGS MOLA Color-Shaded Relief global tile server", "endpoints": { "tiles": "/tiles/MarsCylindrical/{z}/{x}/{y}?url=", "tilejson": "/MarsCylindrical/tilejson.json?url=", "info": "/info?url=", "preview": "/preview.png?url=&max_size=512", "docs": "/docs", }, }