1

I have a list of coordinates points(x,y) of some multipolygon like this:

[[x1,y1][x2,y2][x3,y3] ... [xN,yN]]

Also can convert between lat/lon and UTM coord systems.

When we use gdal.Raesterize like on this answer:

gdal.RasterizeLayer(raster_dataset, [1], shape_layer, None, None, [1], ['ALL_TOUCHED=TRUE'])

we get third argument in this method shape_layer from .shp file:

shape_datasource = ogr.Open(file_name.shp)
shape_layer = shape_datasource.GetLayer()

So, how can I use gdal.RasterizeLayer() with my list of coordinates to make mask for image instead shape_layer?

Andrii
  • 465
  • 2
  • 8
  • 23

1 Answers1

4

Create an ogr 'Memory' layer to rasterize from.

Here's an example from the GDAL/OGR test suite:

# Create a memory layer to rasterize from.
rast_ogr_ds = \
          ogr.GetDriverByName('Memory').CreateDataSource( 'wrk' )
rast_mem_lyr = rast_ogr_ds.CreateLayer( 'poly', srs=sr )

# Add a polygon.
wkt_geom = 'POLYGON((1020 1030,1020 1045,1050 1045,1050 1030,1020 1030))'

feat = ogr.Feature( rast_mem_lyr.GetLayerDefn() )
feat.SetGeometryDirectly( ogr.Geometry(wkt = wkt_geom) )

rast_mem_lyr.CreateFeature( feat )

# Run the algorithm.
err = gdal.RasterizeLayer( target_ds, [3,2,1], rast_mem_lyr,
                           burn_values = [200,220,240] )

And another example from a previous question: https://gis.stackexchange.com/a/131250/2856

You can create WKT from your list of coordinates using something like:

coords = [[0,0],[0,1],[1,1],[1,0]]
wkt = 'POLYGON ((%s))' % ','.join(map(' '.join, [map(str, i) for i in coords+[coords[0]]]))

Or you could look at rasterio which is a much nicer GDAL based library to do the rasterizing.

user2856
  • 65,736
  • 6
  • 115
  • 196