2

I would like to convert a raster to a point vector by using grass:r.to.vect from a pyqgis script. My script gives no error as well as no output. Now I am stuck, what I have to change.

Here my code:

input = raster.tif
output = points.shp

processing.runalg('grass:r.to.vect',{"input": input, "feature": 1,"GRASS_OUTPUT_TYPE_PARAMETER": 1, "GRASS_REGION_PARAMETER":"%f,%f,%f,%f"%(xmin, xmax, ymin, ymax), "output":output})

So far I have tried:

Thinning the input layer before converting it to a raster. Manually running r.to.rast in QGIS works fine only on the thinned version.

processing.runalg('grass:r.thin',{"input": input, "GRASS_REGION_PARAMETER":"%f,%f,%f,%f"%(xmin, xmax, ymin, ymax), "output":output})

Loading the input layer as a QGIS Layer:

fileInfo = QFileInfo(input)
baseName = fileInfo.baseName()      
filePath = str(os.path.abspath(input))      
rlayer = QgsRasterLayer(filePath, baseName)
  • How did you define: xmin, xmax, ymin, ymax? This defines your computational region, and if the latter does not cover your raster, you get no results but also no error message... – Stefan B. Nov 17 '16 at 10:21
  • # Load data as raster layer fileInfo = QFileInfo(f) baseName = fileInfo.baseName() filePath = str(os.path.abspath(f)) rlayer = QgsRasterLayer(filePath, baseName) # Define raster extent extent = rlayer.extent() xmin = extent.xMinimum() xmax = extent.xMaximum() ymin = extent.yMinimum() ymax = extent.yMaximum() – Sophie Crommelinck Nov 17 '16 at 12:35
  • Your code might be OK (I cannot judge that). However, did you check that you got the expected results from that? And are you sure that not any "on-the-fly" projection is happening (meaning: did you double check that extent.yMinimum() returns coordinates in the Coordinate system of your input raster)? Is there some log from Processing? Maybe you have to activate it in the settings.. – Stefan B. Nov 17 '16 at 13:19
  • Remember to set the computational region to the raster map before running r.to.vect... – markusN Nov 17 '16 at 20:10
  • @Stefan B.: Yes, xmin etc. hold the expected values. Unfortunately, I get no log when running a PYQGIS script in the QGIS Python console (also not in Processing>History) @markusN: I think the computational region was set in r.to.vect with "GRASS_REGION_PARAMETER". From the help (processing.alghelp("grass:r.to.vect")), it doesn't seem necessary to set any further region parameter. Overall, I don't think it has to do with the projection or the region, because those parts have worked fine for other modules, where I defined these in the same way. – Sophie Crommelinck Nov 21 '16 at 12:40
1

With the grass7 module the raster to point conversion works fine for me:

# Define extent
extent = raster_layer.extent()
xmin = extent.xMinimum()
xmax = extent.xMaximum()
ymin = extent.yMinimum()
ymax = extent.yMaximum()

# Raster to point conversion
processing.runalg('grass7:r.to.vect',
                  {"input": raster_layer,
                   "type": 1,
                   "GRASS_REGION_PARAMETER": "%f,%f,%f,%f" % (xmin, xmax, ymin, ymax),
                   "GRASS_OUTPUT_TYPE_PARAMETER": 1,
                   "output": point_layer})

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.