Skip to main content
Glama

tile_raster

Split raster files into manageable square tiles of specified size for easier processing and storage in GIS workflows.

Instructions

Split a raster into square tiles of a given size and save them individually.

Parameters:

  • source: input raster path.

  • tile_size: size of each tile (e.g., 256 or 512).

  • destination_dir: directory to store the tiles.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sourceYes
tile_sizeYes
destination_dirYes

Implementation Reference

  • The main handler function for the 'tile_raster' tool. It reads the source raster, generates square tiles using rasterio.windows.Window, updates the transform and profile for each tile, and writes them as individual GeoTIFF files named tile_{row}_{col}.tif in the destination directory.
    def tile_raster(
        source: str,
        tile_size: int,
        destination_dir: str
    ) -> Dict[str, Any]:
        """
        Split a raster into square tiles of a given size and save them individually.
    
        Parameters:
        - source:         input raster path.
        - tile_size:      size of each tile (e.g., 256 or 512).
        - destination_dir: directory to store the tiles.
        """
        try:
            import os
            import rasterio
            from rasterio.windows import Window
    
            src_path = os.path.expanduser(source.replace("`", ""))
            dst_dir = os.path.expanduser(destination_dir.replace("`", ""))
            os.makedirs(dst_dir, exist_ok=True)
    
            tile_count = 0
    
            with rasterio.open(src_path) as src:
                profile = src.profile.copy()
                for i in range(0, src.height, tile_size):
                    for j in range(0, src.width, tile_size):
                        window = Window(j, i, tile_size, tile_size)
                        transform = src.window_transform(window)
                        data = src.read(window=window)
    
                        out_profile = profile.copy()
                        out_profile.update({
                            "height": data.shape[1],
                            "width": data.shape[2],
                            "transform": transform
                        })
    
                        tile_path = os.path.join(dst_dir, f"tile_{i}_{j}.tif")
                        with rasterio.open(tile_path, "w", **out_profile) as dst:
                            dst.write(data)
    
                        tile_count += 1
    
            return {
                "status": "success",
                "tiles_created": tile_count,
                "message": f"{tile_count} tiles created and saved in '{dst_dir}'."
            }
    
        except Exception as e:
            raise ValueError(f"Failed to tile raster: {e}")
  • Resource function listing 'tile_raster' among available rasterio operations, indicating its registration in the MCP toolset.
    @gis_mcp.resource("gis://operation/rasterio")
    def get_rasterio_operations() -> Dict[str, List[str]]:
        """List available rasterio operations."""
        return {
            "operations": [
                "metadata_raster",
                "get_raster_crs",
                "clip_raster_with_shapefile",
                "resample_raster",
                "reproject_raster",
                "weighted_band_sum",
                "concat_bands",
                "raster_algebra",
                "compute_ndvi",
                "raster_histogram",
                "tile_raster",
                "raster_band_statistics",
                "extract_band",
                "zonal_statistics",
                "reclassify_raster",
                "focal_statistics",
                "hillshade",
                "write_raster"
            ]
        }
  • Import of rasterio_functions module in main.py, which triggers the @gis_mcp.tool() decorators to register the 'tile_raster' tool with the FastMCP server.
    from . import (
        geopandas_functions,
        shapely_functions,
        rasterio_functions,
        pyproj_functions,
        pysal_functions,
    )

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mahdin75/gis-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server