[gdal-dev] Fwd: Problem with gdal.Rasterize utility : argument GDALDatasetShadow in wrapper_GDALRasterizeDestName

François ALLAIN frallain at gmail.com
Fri Jul 15 10:32:14 PDT 2016


I am now trying to do this process as an in-memory process.
I coudl figure out how to load the .tif in-memory :

 # Create in-memory file and initialize it with the content
gdal.FileFromMemBuffer('/vsimem/tiffinmem', self.file)

# Open the in-memory file
ds = gdal.Open('/vsimem/tiffinmem', gdal.GA_Update)

But i've got some problem to get a
<osgeo.gdal.Dataset; proxy of <Swig Object of type 'GDALDatasetShadow *'
from a geojson... Here are my vain attemps:


 #create an output datasource in memory
outdriver=ogr.GetDriverByName('MEMORY')
source=outdriver.CreateDataSource('memData')
# <osgeo.ogr.DataSource; proxy of <Swig Object of type 'OGRDataSourceShadow
*'
#open the memory datasource with write access
tmp=outdriver.Open('memData',1)

import os
import sys
from osgeo import gdal, ogr, osr
geojson =
"""{"type":"Polygon","coordinates":[[[-76.4603750,45.4506926],[-76.4603632,45.4506998],[-76.4603522,45.4506914],[-76.4603639,45.4506845],[-76.4603750,45.4506926]]]}"""
geometry = ogr.CreateGeometryFromJson(geojson)
# print type(geometry)
# <class 'osgeo.ogr.Geometry'>

I know that i need to build a  GDALDatasetShadow object from the
OGRDataSourceShadow
object and the osgeo.ogr.Geometry object but i've got no clue after
searching on the web :
http://www.gdal.org/classGDALDataset.html#a9cb8585d0b3c16726b08e25bcc94274a
https://pcjericks.github.io/py-gdalogr-cookbook/vector_layers.html#load-data-to-memory
https://pcjericks.github.io/py-gdalogr-cookbook/vector_layers.html#spatial-filter

Could please give a hint ?

I would be grateful.

Cheers


François ALLAIN
+33 651399906


On Fri, Jul 15, 2016 at 9:45 AM, François ALLAIN <frallain at gmail.com> wrote:

> My bad... I finally got it working with your code :
>
> import os
> import sys
> from osgeo import gdal, ogr, osr
>
> geojson_filename = "my.geojson"
> driver = ogr.GetDriverByName("GeoJSON")
> vector_ds  = gdal.OpenEx( geojson_filename, gdal.OF_VECTOR )
>
> geotiff_filename = "my.tif"
> ds = gdal.Open(geotiff_filename, gdal.GA_Update)
>
> ret = gdal.Rasterize(ds,
>                     vector_ds,
>                     bands = [4],
>                     inverse = True,
>                     burnValues = [0])
>
> if ret != 1:
>     print 'fail'
> else:
>     print 'success'
>
> Cheers and thanks again
>
> François
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20160715/4b062650/attachment.html>


More information about the gdal-dev mailing list