<div dir="ltr">Hi list,<div><br></div><div>I have a RSAT-2 image and a shoreline vector (consists of 2 polygons). I want to generate a mask with the same # of lines and pixels as the RSAT-2 image, and make inside polygons as 0, outside as 1.</div>
<div><br></div><div>The example that I could find is UTM based, which I tried and sort of worked. But the mask image is not oblique as the RSAT-2 image. When I used the lat/lon projection, the gdal.RasterizeLayer() doesn't work. Would someone tell me why?</div>
<div><br></div><div>thanks,</div><div>Shawn</div><div><br></div><div><br></div><div>UTM version that worked:</div><div><div> from osgeo import gdal, ogr, gdal_array</div><div><br></div><div> # Filename of input OGR file</div>
<div> vector_file = 'shoreline_utm.shp'</div><div><br></div><div> # Open the data source and read in the extent</div><div> vector_ds = ogr.Open(vector_file)</div><div> vector_layer = vector_ds.GetLayer()</div>
<div> #vector_srs = vector_layer.GetSpatialRef()</div><div> x_min, x_max, y_min, y_max = vector_layer.GetExtent()</div><div><br></div><div> # raster Tiff that will be created</div><div> new_raster = 'shoreline_utm.tif'</div>
<div><br></div><div> pixel_spacing = 9.77<br></div><div> line_spacing = 5.5</div><div> xsize = int((x_max - x_min) / pixel_spacing)</div><div> ysize = int((y_max - y_min) / line_spacing)</div><div><br></div><div>
new_raster_ds = gdal.GetDriverByName('GTiff').Create(new_raster, xsize, ysize, 1, gdal.GDT_Byte)</div><div> new_raster_ds.SetGeoTransform((x_min, pixel_spacing, 0, y_max, 0, -line_spacing))</div><div><br></div>
<div> band = new_raster_ds.GetRasterBand(1)</div><div> band.Fill(1)</div><div> gdal.RasterizeLayer(new_raster_ds, [1], vector_layer, burn_values=[0])<br></div></div><div><br></div><div><br></div><div>lat/lon version:</div>
<div><div> from osgeo import gdal, ogr, gdal_array</div><div><br></div><div> # Filename of input OGR file</div><div> vector_file = 'shoreline.shp'</div><div><br></div><div> # Open the data source and read in the extent</div>
<div> vector_ds = ogr.Open(vector_file)</div><div> vector_layer = vector_ds.GetLayer()</div><div><br></div><div> # raster Tiff that will be created</div><div> new_raster = 'shorelineTest.tif'</div><div>
<br></div><div> # master Tiff whose size will be copied</div><div> master = 'product.xml'</div><div> master_ds = gdal.Open(master)</div><div> xsize, ysize = master_ds.RasterXSize, master_ds.RasterYSize</div>
<div> </div><div> new_raster_ds = gdal.GetDriverByName('GTiff').Create(new_raster, xsize, ysize, 1, gdal.GDT_Byte)</div><div><br></div><div> # use georeferencing from the main product.xml dataset</div><div> gdal_array.CopyDatasetInfo(master_ds, new_raster_ds)</div>
<div><br></div><div> band = new_raster_ds.GetRasterBand(1)</div><div> band.Fill(1)</div><div><br></div><div> gdal.RasterizeLayer(new_raster_ds, [1], vector_layer, burn_values=[0])<br></div></div><div><br></div></div>