Tutorial: Spatial subset using Shapefile in snappy

Snappy use a WKT file for spatial subset. I found everywhere but there was no option for read shapfile in snappy. Here I am sharing my experience how to convert shapefile to WKT file and subset image in snappy using polygon. Hope it will be helpful for you.

import snappy
from snappy import ProductIO
from snappy import GPF
from snappy import HashMap
from snappy import ProductUtils
from snappy import WKTReader
import gc
import os
from snappy import jpy
import geopandas as gpd


# read image and do all your preprocessing with image.
 img=ProductIO.readProduct(r'image with path')

 #Now we have to read the shapfile.

 shp=gpd.read_file(r'shapefile with path')     # here you have to read your polygon shapefile
 
shp     # output for my case.
 Out[111]: 
    Id                                           geometry
 0   0  POLYGON ((88.13664 22.50243, 88.12930 22.56486...

geom=str(shp['geometry'][0])     # get the geometry of polygon shapefile as string. 

# Here we create thw WKT file from the Shapefile.

geom = WKTReader().read(geom)

# now we can subset the image with the bounded area of shapefile.

par = HashMap()
par.put('copyMetadata', True)
par.put('geoRegion', geom)
im_sub = GPF.createProduct('Subset', par, image) #Spatial subset.

Here is the output of image Width and Height for my case:

Width:7730 Height:5388 # main corrected image
Width:1131 Height:1335 # after subset

This is how you can perform a spatial subset in snappy module using polygon shapefile.

@SubhadipDatta
Satellite Image Analyst

4 Likes

Thank you for sharing. I moved the topic to the show room.