0

I have multiple shapefiles extracted from OSM vector data, as shown in the image bellow:

enter image description here

How can I render this into a raster of a specified resolution? Is there a way to do that using QGIS or Python?

  • Set the map canvas background color to black. Export map to image. – csk Apr 18 at 21:01
  • @csk I want to save the images at a resolution similar to the satellite images I have (0.5m). With the export to image, It does not give me an option to set the resolution. – FJ_Abbasi Apr 21 at 19:39
2

You could merge the shapefiles using "Merge Vector Layers" found under Vector > Data Management Tools > Merge Vector Layers. This allows you to choose all the shapefiles you want to merge.

For the first step, you can only merge vector layers that have the same geometry type. Here is an example of the merge vector layer tool.

First, you can get help using this tool by running:

processing.algorithmHelp('qgis:mergevectorlayers')

Now, to run it you could do something like:

import processing

#I copy and pasted the full path names of example layers I have into a list
layer_paths = ['/Users/ep9k/Desktop/BRE/Avery_parcels_2018_10_12/nc_avery_parcels_poly_2018_10_12.shp', '/Users/ep9k/Desktop/BRE/Avery_parcels_2018_10_12/nc_avery_parcels_poly_2018_10_12.shp']

parameters = {'LAYERS': layer_paths,
            'CRS': None                       #uses default CRS
            'OUTPUT': path/to/output/here     #or use 'memory:' to save as memory layer

processing.runAndLoadResults('qgis:mergevectorlayers', parameters)

The 2nd step Then you can convert vector to raster using GDAL: Rasterize.

You can get help for this tool by running:

processing.algorithmHelp('gdal:rasterize')

If you run the GDAL rasterize tool successfully (the GUI version of the tool), the 'log' tab provides you all the parameters. I ran the tool and used the output log to write this next part in python.

import processing
from qgis.core import *

parameters = {'INPUT': 'path/of/your/input/layer',
        'FIELD': None,           #you probably want to choose a field from your vector layers
        'BURN': 0,
        'UNITS': 1,
        'WIDTH': 0,
        'HEIGHT': 0,
        'EXTENT': rectangle,     #QgsRectangle. This will be output extent of all your merged shapefiles
        'NODATA': 0,
        'OPTIONS': '',
        'DATA_TYPE': 5,
        'INIT': None,
        'OUTPUT': 'path/to/your/output/layer'}   #whatever you want to name your output. Can be use a .tif

processing.runAndLoadResults('gdal:rasterize', parameters)
  • 1
    "Merge Vector Layers" doesn't works for different geometries as I have polygons and lines. – FJ_Abbasi Apr 19 at 15:45
  • ok. In that case, I think CSK's comment is probably better – Erich Purpur Apr 19 at 17:31
0

Project menu > Import Export >Export map to image. As you can see below, this method does give you the option to set the resolution of the output image.

enter image description here

  • how is this approach different from burning the vector into raster using gdal_rasterize? Is the output of both approaches same? would it make any difference as I have to use these images as labels to the corresponding satellite images for a machine learning task. – FJ_Abbasi Apr 23 at 8:58
  • >"is the output of both approaches the same?" - I don't know. You can easily test this yourself. >"how is this approach different..." - This method is a lot easier, and it doesn't require you to merge vector layers with different geometry types. >"would it make any difference..." - You have a better understanding of your needs and goals than I do. Try both methods and see which one best suits your needs. If neither works for you, post a follow-up question where you explain exactly what your goals are and why each of these methods doesn't meet your needs. – csk Apr 23 at 16:23
  • Is it possible to somehow automate this process, as I have to deal with hundreds of such images? – FJ_Abbasi Apr 24 at 15:37

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.