Cropping a Raster Image using Vector Shape

You’re welcome.

Will add some other “visualization category” feature requests under a different post.

Greetings. Any luck with implementing this feature?

Hi, @yemoku

We have implemented this feature. We are going to release it.

Hello,

You can now clip or crop a raster using a vector geometry directly with Aspose.GIS 21.7 (and later). The RasterLayer.Crop method accepts any geometry (polygon, extent, etc.) and returns a new raster where the area outside the geometry is made transparent.

Basic example (C#)

using Aspose.Gis;
using Aspose.Gis.Geometries;
using Aspose.Gis.SpatialReferencing;

// Open the source raster
using (var raster = RasterLayer.Open(@"C:\Data\satellite.tif"))
{
    // Load the AOI polygon (WKT) – ensure it uses the same SRS as the raster
    var aoi = Geometry.FromWkt(
        "POLYGON ((...))",
        SpatialReferenceSystem.Wgs84);   // or raster.SpatialReferenceSystem if different

    // If the raster uses Web Mercator, transform the AOI
    var aoiInRasterSrs = aoi.GetTransformed(raster.SpatialReferenceSystem);

    // Crop the raster
    using (var cropped = raster.Crop(aoiInRasterSrs))
    {
        // Save the result (GeoTIFF with transparent background)
        cropped.Save(@"C:\Data\satellite_cropped.tif", RasterFileFormat.GeoTiff);
    }
}

Key points

  1. Spatial reference – The geometry must be in the same coordinate system as the raster. Use GetTransformed to convert it if needed (e.g., WGS‑84 → Web Mercator).
  2. Extent vs. polygon – You can pass Extent.ToPolygon() for a simple rectangular AOI, or any complex polygon for precise clipping.
  3. Transparency – The returned raster retains the source pixel type; areas outside the geometry become fully transparent (alpha = 0).

Documentation: Cropping raster layers – includes additional samples and details on handling different spatial reference systems.

If you encounter null results, double‑check that the geometry’s coordinates match the raster’s CRS. In earlier versions this was a common cause of the issue.

Feel free to ask if you need help with coordinate transformations or further customisation.