5

I'm running gdal_rasterize via a python pipe. When using the -l option, the raster is created just fine but I can't manage to get -sql option to work. Well my raster will be created but consists only of NAN's. What am I missing?

    rasterize_args = ['gdal_rasterize',
                      '-a', attribute_name,
                      '-sql', "select {attribute} FROM {layer}".format(attribute=attribute_name, layer=layer_name),
                      #'-l', layer_name,
                      '-a_nodata', str(no_data_value),
                      '-co', "COMPRESS=DEFLATE",
                      '-a_srs', srs.ExportToWkt(),
                      '-te', str(minx), str(miny), str(maxx), str(maxy),
                      '-ts', str(ds.RasterXSize), str(ds.RasterYSize),
                      ]
    if where_clause:
        rasterize_args.extend(['-where', where_clause])

    rasterize_args.extend([input_source, destination_raster])
    logger.debug("The gdal_rasterize cmd args are: %s", rasterize_args)
    ps = subprocess.Popen(
        args=rasterize_args,
        stdout=subprocess.PIPE
    )
    output = ps.communicate()[0]
2

You need to select a geometry to rasterize, and not just an attribute. For example:

SELECT gid, geom, attr FROM my_table WHERE name='Newbridge';

Furthermore, I'm not sure if you can mix -sql with -where. It's not documented how they would interact, but I'd do some testing with this.

Also, I would run this directly through Python with gdal.RasterizeLayer (see API docs) rather than through a subprocess. You would need to create or open a writable GDAL dataset and an OSR datasource which you can throw ExecuteSQL(sql) at to get a rasterizable layer. It might take a while to figure out how to make it work in Python, but I've found it worthwhile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.