We use the GSHHG vector coastline for landmasking our SAR images. The coastline takes the form of vector polygons at different resolutions of which the Shapefile format is the easiest to process with Python using the GDAL/OGR libraries.
The SAR image typically comes as a GeoTIFF (at least for Radarsat-2 and Sentinel-2) with GCPs. This can be opened using the GDAL in Python and a coordinate transformation (image coordinates to longitude/latitude) set up using code like:
where inds is the file handle to the open GeoTIFF.
With the GSHHG Shapefile open using OGR in Python, we then loop through the feature geometries and get the component nodes of each polygon. These can be converted from longitude/latitude to image coordinates by:
where p is the point coordinate (longitude, latitude). The important parameter of TransformPoint is that 1, which requests an invert transformation (i.e. from longitude/latitude to image coordinates. You can then use the GDAL and OGR functions to create an output mask image file of the same dimensions as the input SAR image, and draw the landmask geometries onto that.
I found the way out , if you are using Python 3 then go for the following:
First we have to download an appropriate shapefile of the region we wish to mask, then there is a beautiful functionality available in gdal called as gdalwarp. We need to just open the anaconda prompt and from there just type in :`