Skip to main content

The MGRS grid

MGRSGrid splits an area of interest (AOI) into the 10 km squares of the Military Grid Reference System (MGRS). Each square is an odc.geo GeoBox with a fixed CRS, affine transform and shape, so rasters resampled onto the same square at the same resolution line up pixel for pixel. A square’s MGRS code and the resolution are enough to uniquely define and reproduce it.

Code
import contextily as cx
import geopandas as gpd
import matplotlib.pyplot as plt
from shapely import box

from freezebase.mgrs import MGRSGrid

alps_box = box(7.8, 46.45, 8.15, 46.65)
alps = MGRSGrid(alps_box).to_geodataframe()
alps_utm = alps.to_crs(int(alps.epsg.iloc[0]))
alps_aoi = gpd.GeoDataFrame(geometry=[alps_box], crs=4326)

fig, ax = plt.subplots(figsize=(9, 6))
alps_utm.plot(ax=ax, facecolor="tab:blue", alpha=0.15, edgecolor="tab:blue", linewidth=1.0)
for code, center in zip(alps_utm.mgrs_code, alps_utm.geometry.centroid):
    ax.annotate(code, (center.x, center.y), ha="center", va="center", fontsize=9)
alps_aoi.to_crs(alps_utm.crs).boundary.plot(ax=ax, color="black", linewidth=2.0, linestyle="--")
cx.add_basemap(ax, source=cx.providers.Esri.WorldGrayCanvas, crs=alps_utm.crs, attribution_size=6)
ax.set_axis_off()

The twelve 10 km squares selected by an AOI in the Bernese Alps, each labeled with its MGRS code. The map is drawn in UTM zone 32N, where the squares are axis-aligned. The AOI (dashed) is a longitude–latitude box.

Why a fixed grid

Combining a Sentinel-1 scene, a Sentinel-2 composite, a DEM and a rasterized outline needs a common grid, because each usually arrives in its own projection, resolution and tiling scheme. MGRSGrid defines that grid independently of the inputs: for an MGRS code and resolution, every pipeline derives the same CRS, transform and shape. Sources resampled onto a square are then pixel-aligned with each other.

Why 10 km × 10 km MGRS squares

MGRS is an established metric reference system that Sentinel-2 already uses, and 10 km squares are a practical size to process:

  1. Metric and established. Each square lies on the meter-based lattice of a UTM zone and is axis-aligned in that projection, so a 10 m pixel is 10 m on the grid rather than a varying fraction of a degree.
  2. Shared with Sentinel-2. A freezebase code such as 32TMS35 names a 10 km square inside the 100 km MGRS square of Sentinel-2 tile T32TMS. The parent tile is therefore visible in the code, and compatible 10 m data can be retiled without a grid shift.
  3. A practical unit of work. At 10 m resolution, a 100 km square is 10,000 × 10,000 pixels and a 10 km square is 1,000 × 1,000. The smaller squares make it practical to download, cache and process only those touched by scattered AOIs.
  4. No overlap within a UTM zone. Sentinel-2 product tiles are 109.8 km wide and overlap their neighbors1. freezebase squares step by exactly 10 km, so squares of the same UTM zone never overlap. Squares of neighboring zones can (see Good to know).

Select the squares for an AOI

MGRSGrid takes an AOI in WGS84 and selects the squares that intersect it. This AOI around Geneva crosses 6° E, the boundary between UTM zones 31 and 32:

from shapely import box

from freezebase.mgrs import MGRSGrid

aoi = box(5.8, 46.0, 6.5, 46.4)  # bare Shapely means WGS84 here
grid = MGRSGrid(aoi)

len(grid)
48

to_geodataframe() returns one row per square, in WGS84:

gdf = grid.to_geodataframe()
gdf.head()
mgrs_code zone hemisphere easting northing epsg geometry
0 31TGL19 31 N 710000 5090000 32631 POLYGON ((5.83757 45.92833, 5.83822 45.94117, ...
1 31TGL29 31 N 720000 5090000 32631 POLYGON ((5.96638 45.92506, 5.96706 45.9379, 5...
2 31TGL39 31 N 730000 5090000 32631 POLYGON ((6.09516 45.92164, 6.09588 45.93448, ...
3 31TGM10 31 N 710000 5100000 32631 POLYGON ((5.84216 46.01822, 5.84282 46.03106, ...
4 31TGM11 31 N 710000 5110000 32631 POLYGON ((5.84678 46.10811, 5.84745 46.12095, ...

Each row contains the square’s MGRS code, its UTM zone and hemisphere, the native EPSG code and the south-west corner in UTM meters. The EPSG column shows that this AOI spans two zones:

gdf.groupby(["zone", "epsg"]).size()
zone  epsg 
31    32631    18
32    32632    30
dtype: int64
NoteHow squares are selected
  • A bare Shapely geometry is taken to be in WGS84. A GeoSeries or GeoDataFrame must declare WGS84 explicitly.
  • Every square that intersects the AOI is returned whole, so the grid covers slightly more than the AOI.
  • A square that only touches the AOI boundary counts as intersecting.
  • resolution sets the pixel size inside each square. It does not change which squares are selected.

Each square is a GeoBox

Indexing or iterating over the grid yields MGRSGeoBox objects, a subclass of odc.geo.geobox.GeoBox that adds the mgrs_code:

square = grid[0]
square

MGRSGeoBox

Dimensions
1,000x1,000
EPSG
32631
Resolution
10m
Cell
100px
WKT
PROJCRS["WGS 84 / UTM zone 31N",
    BASEGEOGCRS["WGS 84",
        ENSEMBLE["World Geodetic System 1984 ensemble",
            MEMBER["World Geodetic System 1984 (Transit)"],
            MEMBER["World Geodetic System 1984 (G730)"],
            MEMBER["World Geodetic System 1984 (G873)"],
            MEMBER["World Geodetic System 1984 (G1150)"],
            MEMBER["World Geodetic System 1984 (G1674)"],
            MEMBER["World Geodetic System 1984 (G1762)"],
            MEMBER["World Geodetic System 1984 (G2139)"],
            MEMBER["World Geodetic System 1984 (G2296)"],
            ELLIPSOID["WGS 84",6378137,298.257223563,
                LENGTHUNIT["metre",1]],
            ENSEMBLEACCURACY[2.0]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433]],
        ID["EPSG",4326]],
    CONVERSION["UTM zone 31N",
        METHOD["Transverse Mercator",
            ID["EPSG",9807]],
        PARAMETER["Latitude of natural origin",0,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",3,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["Scale factor at natural origin",0.9996,
            SCALEUNIT["unity",1],
            ID["EPSG",8805]],
        PARAMETER["False easting",500000,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",0,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["(E)",east,
            ORDER[1],
            LENGTHUNIT["metre",1]],
        AXIS["(N)",north,
            ORDER[2],
            LENGTHUNIT["metre",1]],
    USAGE[
        SCOPE["Navigation and medium accuracy spatial referencing."],
        AREA["Between 0°E and 6°E, northern hemisphere between equator and 84°N, onshore and offshore. Algeria. Andorra. Belgium. Benin. Burkina Faso. Denmark - North Sea. France. Germany - North Sea. Ghana. Luxembourg. Mali. Netherlands. Niger. Nigeria. Norway. Spain. Togo. United Kingdom (UK) - North Sea."],
        BBOX[0,0,84,6]],
    ID["EPSG",32631]]

A GeoBox is the CRS, affine transform and shape of a pixel grid:

from odc.geo.geobox import GeoBox

print(f"mgrs_code:  {square.mgrs_code}")
print(f"crs:        {square.crs}")
print(f"shape:      {square.shape}")
print(f"resolution: {square.resolution}")
print(f"transform:\n{square.transform}")
print(f"is GeoBox:  {isinstance(square, GeoBox)}")
mgrs_code:  31TGL19
crs:        EPSG:32631
shape:      Shape2d(x=1000, y=1000)
resolution: Resolution(x=10, y=-10)
transform:
| 10.00, 0.00, 710000.00|
| 0.00,-10.00, 5100000.00|
| 0.00, 0.00, 1.00|
is GeoBox:  True

A square works wherever a GeoBox is accepted, and odc.geo properties such as extent and boundingbox work on it. Operations that derive a different box, such as padding, slicing or reprojection, return a plain GeoBox without mgrs_code, because the result no longer covers the square the code names.

Rebuild a square from its code

MGRSGeoBox.from_mgrs() rebuilds a square from its code and resolution, so a code stored in a filename, a database or a job description needs no AOI:

from freezebase.mgrs import MGRSGeoBox, mgrs_to_crs

square = MGRSGeoBox.from_mgrs("32TMS35", resolution=10.0)
print(square)
print(mgrs_to_crs(square.mgrs_code))
MGRSGeoBox('32TMS35')
EPSG:32632

Choosing a resolution

The resolution must be positive and divide 10 km evenly. Resolutions such as 10 m, 20 m and 100 m therefore give whole-pixel grids that nest into each other by an integer factor:

for resolution in (10.0, 20.0, 100.0):
    cell = MGRSGeoBox.from_mgrs("32TMS35", resolution=resolution)
    print(f"{resolution:>5.0f} m -> {cell.shape}")
   10 m -> Shape2d(x=1000, y=1000)
   20 m -> Shape2d(x=500, y=500)
  100 m -> Shape2d(x=100, y=100)

Good to know

The grid covers 80° S to 84° N only. freezebase implements the UTM part of MGRS, not the polar UPS grids.

Squares of neighboring UTM zones can overlap. Each zone is projected on its own, and a square along a zone edge is kept whole, so it can extend into the neighboring zone. An AOI that crosses the edge can therefore select squares from both zones that cover some of the same ground. In exchange, each square remains square and axis-aligned in its own UTM CRS, and no part of the AOI is left uncovered. Drawing the Geneva example in one map projection shows the two grids:

Code
from matplotlib.patches import Patch
import pyproj

web_mercator = pyproj.CRS(3857)
squares = gdf.to_crs(web_mercator)
aoi_gdf = gpd.GeoDataFrame(geometry=[aoi], crs=4326).to_crs(web_mercator)
zone_colors = {31: "tab:blue", 32: "tab:red"}

fig, ax = plt.subplots(figsize=(9, 6))
for zone, color in zone_colors.items():
    squares[squares.zone == zone].plot(
        ax=ax, facecolor=color, alpha=0.25, edgecolor=color, linewidth=1.0
    )
aoi_gdf.boundary.plot(ax=ax, color="black", linewidth=2.0, linestyle="--")
cx.add_basemap(ax, source=cx.providers.Esri.WorldGrayCanvas, crs=web_mercator, attribution_size=6)
ax.legend(
    handles=[
        *(
            Patch(facecolor=color, alpha=0.25, edgecolor=color, label=f"UTM zone {zone}")
            for zone, color in zone_colors.items()
        ),
        Patch(facecolor="none", edgecolor="black", linestyle="--", label="AOI"),
    ],
    loc="lower right",
)
ax.set_axis_off()

The 10 km grids of UTM zones 31 and 32 over the Geneva AOI. Each is axis-aligned in its own projection, so they appear rotated and overlap near the boundary when displayed together.

Next steps

Reading and writing rasters reprojects a public DEM onto square 32TMS35. VRTs does the same without writing an intermediate file, using vrt.reproject.

Footnotes

  1. Bauer-Marschallinger, B., & Falkner, K. (2023). Wasting petabytes: A survey of the Sentinel-2 UTM tiling grid and its spatial overhead. ISPRS Journal of Photogrammetry and Remote Sensing, 202, 682–690. https://doi.org/10.1016/j.isprsjprs.2023.07.015↩︎