Skip to main content
edited title
Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

In memory Creating in-memory raster layer using PyQGIS?

Tweeted twitter.com/StackGIS/status/800707745066483712
edited tags
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

I have been researching quite a bit for creating in-memory raster layer using PyQGIS. i have done it through gdal "memory" layer. But i need QgsRasterLayer instance for displaying. I don't have the option of saving the results to disk and reading it again. I tried doing this:

target_ds = gdal.GetDriverByName('MEM').Create('ras', x_count, y_count, 1, gdal.GDT_Int32)

target_ds= processing.runalg("gdalogr:rasterize",
                      {"INPUT": ellipse_shp,
                       "FIELD": "id1",
                       "DIMENSIONS": 0,
                       "WIDTH": x_count,
                       "HEIGHT": y_count,
                       "RAST_EXT": "%f,%f,%f,%f" % (x_min, x_max, y_min, y_max),
                       "TFW": 1,
                       "RTYPE": 4,
                       "NO_DATA": 1,
                       "COMPRESS": 0,
                       "JPEGCOMPRESSION": 1,
                       "ZLEVEL": 1,
                       "PREDICTOR": 1,
                       "TILED": False,
                       "BIGTIFF": 2,
                       "EXTRA": '',
                       "OUTPUT": "aa"})

But when i wanted to use it as:

target_ds["OUTPUT"]

it gave me an error as TypeError: str cannot be converted to qgis._core.QgsRasterLayer in this context

TypeError: str cannot be converted to qgis._core.QgsRasterLayer in this context

Although QGIS documentation says it supports all gdal sources, I couldn't make it work. 

Can anyone help me  ? Thanks in advance.

I have been researching quite a bit for creating in-memory raster layer using PyQGIS. i have done it through gdal "memory" layer. But i need QgsRasterLayer instance for displaying. I don't have the option of saving the results to disk and reading it again. I tried doing this:

target_ds = gdal.GetDriverByName('MEM').Create('ras', x_count, y_count, 1, gdal.GDT_Int32)

target_ds= processing.runalg("gdalogr:rasterize",
                      {"INPUT": ellipse_shp,
                       "FIELD": "id1",
                       "DIMENSIONS": 0,
                       "WIDTH": x_count,
                       "HEIGHT": y_count,
                       "RAST_EXT": "%f,%f,%f,%f" % (x_min, x_max, y_min, y_max),
                       "TFW": 1,
                       "RTYPE": 4,
                       "NO_DATA": 1,
                       "COMPRESS": 0,
                       "JPEGCOMPRESSION": 1,
                       "ZLEVEL": 1,
                       "PREDICTOR": 1,
                       "TILED": False,
                       "BIGTIFF": 2,
                       "EXTRA": '',
                       "OUTPUT": "aa"})

But when i wanted to use it as:

target_ds["OUTPUT"]

it gave me an error as TypeError: str cannot be converted to qgis._core.QgsRasterLayer in this context

Although QGIS documentation says it supports all gdal sources, I couldn't make it work. Can anyone help me  ? Thanks in advance.

I have been researching quite a bit for creating in-memory raster layer using PyQGIS. i have done it through gdal "memory" layer. But i need QgsRasterLayer instance for displaying. I don't have the option of saving the results to disk and reading it again. I tried doing this:

target_ds = gdal.GetDriverByName('MEM').Create('ras', x_count, y_count, 1, gdal.GDT_Int32)

target_ds= processing.runalg("gdalogr:rasterize",
                      {"INPUT": ellipse_shp,
                       "FIELD": "id1",
                       "DIMENSIONS": 0,
                       "WIDTH": x_count,
                       "HEIGHT": y_count,
                       "RAST_EXT": "%f,%f,%f,%f" % (x_min, x_max, y_min, y_max),
                       "TFW": 1,
                       "RTYPE": 4,
                       "NO_DATA": 1,
                       "COMPRESS": 0,
                       "JPEGCOMPRESSION": 1,
                       "ZLEVEL": 1,
                       "PREDICTOR": 1,
                       "TILED": False,
                       "BIGTIFF": 2,
                       "EXTRA": '',
                       "OUTPUT": "aa"})

But when i wanted to use it as:

target_ds["OUTPUT"]

it gave me an error as

TypeError: str cannot be converted to qgis._core.QgsRasterLayer in this context

Although QGIS documentation says it supports all gdal sources, I couldn't make it work. 

Can anyone help me?

Source Link
Bishrant
  • 346
  • 1
  • 10

In memory raster layer PyQGIS

I have been researching quite a bit for creating in-memory raster layer using PyQGIS. i have done it through gdal "memory" layer. But i need QgsRasterLayer instance for displaying. I don't have the option of saving the results to disk and reading it again. I tried doing this:

target_ds = gdal.GetDriverByName('MEM').Create('ras', x_count, y_count, 1, gdal.GDT_Int32)

target_ds= processing.runalg("gdalogr:rasterize",
                      {"INPUT": ellipse_shp,
                       "FIELD": "id1",
                       "DIMENSIONS": 0,
                       "WIDTH": x_count,
                       "HEIGHT": y_count,
                       "RAST_EXT": "%f,%f,%f,%f" % (x_min, x_max, y_min, y_max),
                       "TFW": 1,
                       "RTYPE": 4,
                       "NO_DATA": 1,
                       "COMPRESS": 0,
                       "JPEGCOMPRESSION": 1,
                       "ZLEVEL": 1,
                       "PREDICTOR": 1,
                       "TILED": False,
                       "BIGTIFF": 2,
                       "EXTRA": '',
                       "OUTPUT": "aa"})

But when i wanted to use it as:

target_ds["OUTPUT"]

it gave me an error as TypeError: str cannot be converted to qgis._core.QgsRasterLayer in this context

Although QGIS documentation says it supports all gdal sources, I couldn't make it work. Can anyone help me ? Thanks in advance.