This tutorial demonstrates how to change the rendering of different single symbol renderers from within qgisremote
:
Get some polygon data
borders <- raster::getData("GADM", country = "LUX", level = 2)
# start with blank project
library(qgisremote)
iface.newProject()
## NULL
# add data
l <- iface.addVectorLayer(borders)
# get default renderer
r <- mapLayer.renderer(l)
# inspect renderer
as.character(r)
## [1] "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE xml>\n<renderer-v2 forceraster=\"0\" symbollevels=\"0\" type=\"singleSymbol\" enableorderby=\"0\">\n <symbols>\n <symbol alpha=\"1\" clip_to_extent=\"1\" type=\"fill\" name=\"0\">\n <layer pass=\"0\" class=\"SimpleFill\" locked=\"0\">\n <prop k=\"border_width_map_unit_scale\" v=\"0,0,0,0,0,0\"/>\n <prop k=\"color\" v=\"181,121,72,255\"/>\n <prop k=\"joinstyle\" v=\"bevel\"/>\n <prop k=\"offset\" v=\"0,0\"/>\n <prop k=\"offset_map_unit_scale\" v=\"0,0,0,0,0,0\"/>\n <prop k=\"offset_unit\" v=\"MM\"/>\n <prop k=\"outline_color\" v=\"0,0,0,255\"/>\n <prop k=\"outline_style\" v=\"solid\"/>\n <prop k=\"outline_width\" v=\"0.26\"/>\n <prop k=\"outline_width_unit\" v=\"MM\"/>\n <prop k=\"style\" v=\"solid\"/>\n </layer>\n </symbol>\n </symbols>\n <rotation/>\n <sizescale scalemethod=\"diameter\"/>\n</renderer-v2>\n"
qgisxml(r)
## name attrname value
## 1 renderer-v2 forceraster 0
## 2 renderer-v2 symbollevels 0
## 3 renderer-v2 type singleSymbol
## 4 renderer-v2 enableorderby 0
## 5 symbol alpha 1
## 6 symbol clip_to_extent 1
## 7 symbol type fill
## 8 symbol name 0
## 9 layer pass 0
## 10 layer class SimpleFill
## 11 layer locked 0
## 12 layer border_width_map_unit_scale 0,0,0,0,0,0
## 13 layer color 181,121,72,255
## 14 layer joinstyle bevel
## 15 layer offset 0,0
## 16 layer offset_map_unit_scale 0,0,0,0,0,0
## 17 layer offset_unit MM
## 18 layer outline_color 0,0,0,255
## 19 layer outline_style solid
## 20 layer outline_width 0.26
## 21 layer outline_width_unit MM
## 22 layer style solid
## 23 sizescale scalemethod diameter
Set fill transparent with dashed blue outline:
qgisxml(r, 'layer', 'color') <- '0,0,0,0'
qgisxml(r, 'layer', 'outline_color') <- 'blue'
qgisxml(r, 'layer', 'outline_style') <- 'dash'
qgisxml(r, 'layer', 'outline_width') <- 0.3
# add offset to where lines are drawn
qgisxml(r, 'offset')
## [1] "0,0"
qgisxml(r, 'offset') <- '1,2'
mapLayer.renderer(l) <- r
raster::plotRGB(mapCanvas.saveAsImage())
Simply switching a polygon layer over to the line
symbol type does not work, since the line renderer will simply fail to show polygon data on the canvas.
Want to style polygons using a line renderer only? Add the data as a line layer:
borders <- as(borders, 'SpatialLines')
l <- iface.addVectorLayer(borders, 'borderlines')
r <- mapLayer.renderer(l)
qgisxml(r)
## name attrname value
## 1 renderer-v2 forceraster 0
## 2 renderer-v2 symbollevels 0
## 3 renderer-v2 type singleSymbol
## 4 renderer-v2 enableorderby 0
## 5 symbol alpha 1
## 6 symbol clip_to_extent 1
## 7 symbol type line
## 8 symbol name 0
## 9 layer pass 0
## 10 layer class SimpleLine
## 11 layer locked 0
## 12 layer capstyle square
## 13 layer customdash 5;2
## 14 layer customdash_map_unit_scale 0,0,0,0,0,0
## 15 layer customdash_unit MM
## 16 layer draw_inside_polygon 0
## 17 layer joinstyle bevel
## 18 layer line_color 70,235,10,255
## 19 layer line_style solid
## 20 layer line_width 0.26
## 21 layer line_width_unit MM
## 22 layer offset 0
## 23 layer offset_map_unit_scale 0,0,0,0,0,0
## 24 layer offset_unit MM
## 25 layer use_custom_dash 0
## 26 layer width_map_unit_scale 0,0,0,0,0,0
## 27 sizescale scalemethod diameter
# compare the offset and non-offset borders
raster::plotRGB(mapCanvas.saveAsImage())
l <- iface.addVectorLayer('https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_50m_populated_places.geojson')
r <- mapLayer.renderer(l)
as.character(r)
## [1] "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE xml>\n<renderer-v2 forceraster=\"0\" symbollevels=\"0\" type=\"singleSymbol\" enableorderby=\"0\">\n <symbols>\n <symbol alpha=\"1\" clip_to_extent=\"1\" type=\"marker\" name=\"0\">\n <layer pass=\"0\" class=\"SimpleMarker\" locked=\"0\">\n <prop k=\"angle\" v=\"0\"/>\n <prop k=\"color\" v=\"181,91,137,255\"/>\n <prop k=\"horizontal_anchor_point\" v=\"1\"/>\n <prop k=\"joinstyle\" v=\"bevel\"/>\n <prop k=\"name\" v=\"circle\"/>\n <prop k=\"offset\" v=\"0,0\"/>\n <prop k=\"offset_map_unit_scale\" v=\"0,0,0,0,0,0\"/>\n <prop k=\"offset_unit\" v=\"MM\"/>\n <prop k=\"outline_color\" v=\"0,0,0,255\"/>\n <prop k=\"outline_style\" v=\"solid\"/>\n <prop k=\"outline_width\" v=\"0\"/>\n <prop k=\"outline_width_map_unit_scale\" v=\"0,0,0,0,0,0\"/>\n <prop k=\"outline_width_unit\" v=\"MM\"/>\n <prop k=\"scale_method\" v=\"diameter\"/>\n <prop k=\"size\" v=\"2\"/>\n <prop k=\"size_map_unit_scale\" v=\"0,0,0,0,0,0\"/>\n <prop k=\"size_unit\" v=\"MM\"/>\n <prop k=\"vertical_anchor_point\" v=\"1\"/>\n </layer>\n </symbol>\n </symbols>\n <rotation/>\n <sizescale scalemethod=\"diameter\"/>\n</renderer-v2>\n"
qgisxml(r)
## name attrname value
## 1 renderer-v2 forceraster 0
## 2 renderer-v2 symbollevels 0
## 3 renderer-v2 type singleSymbol
## 4 renderer-v2 enableorderby 0
## 5 symbol alpha 1
## 6 symbol clip_to_extent 1
## 7 symbol type marker
## 8 symbol name 0
## 9 layer pass 0
## 10 layer class SimpleMarker
## 11 layer locked 0
## 12 layer angle 0
## 13 layer color 181,91,137,255
## 14 layer horizontal_anchor_point 1
## 15 layer joinstyle bevel
## 16 layer name circle
## 17 layer offset 0,0
## 18 layer offset_map_unit_scale 0,0,0,0,0,0
## 19 layer offset_unit MM
## 20 layer outline_color 0,0,0,255
## 21 layer outline_style solid
## 22 layer outline_width 0
## 23 layer outline_width_map_unit_scale 0,0,0,0,0,0
## 24 layer outline_width_unit MM
## 25 layer scale_method diameter
## 26 layer size 2
## 27 layer size_map_unit_scale 0,0,0,0,0,0
## 28 layer size_unit MM
## 29 layer vertical_anchor_point 1
## 30 sizescale scalemethod diameter
Change the marker type and colour:
qgisxml(r, 'layer', 'name') <- 'triangle'
qgisxml(r, 'color') <- 'blue'
mapLayer.renderer(l) <- r
mapCanvas.zoomOut()
## [1] 522106.9
raster::plotRGB(mapCanvas.saveAsImage())
Get some SRTM elevation data and add it to QGIS.
elevation <- raster::getData("alt", country = "DNK")
iface.newProject()
## NULL
l <- iface.addRasterLayer(elevation)
mapCanvas.zoomToFullExtent()
## [1] 1875079
r <- mapLayer.renderer(l)
as.character(r)
## [1] "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE xml>\n<QgsSingleBandGrayRenderer>\n <rasterrenderer gradient=\"BlackToWhite\" opacity=\"1\" alphaBand=\"-1\" type=\"singlebandgray\" grayBand=\"1\">\n <rasterTransparency/>\n <contrastEnhancement>\n <minValue>-0.988</minValue>\n <maxValue>92.866</maxValue>\n <algorithm>StretchToMinimumMaximum</algorithm>\n </contrastEnhancement>\n </rasterrenderer>\n</QgsSingleBandGrayRenderer>\n"
qgisxml(r)
## name attrname value
## 1 rasterrenderer gradient BlackToWhite
## 2 rasterrenderer opacity 1
## 3 rasterrenderer alphaBand -1
## 4 rasterrenderer type singlebandgray
## 5 rasterrenderer grayBand 1
Tile layers might be different in how their data is retrieved, but they are otherwise simply raster layers which are displayed using a singlebandcolordata
renderer.
l <- iface.addTileLayer('http://c.tile.openstreetmap.org/{z}/{x}/{y}.png')
qgisxml(mapLayer.renderer(l))
## name attrname value
## 1 rasterrenderer opacity 1
## 2 rasterrenderer alphaBand -1
## 3 rasterrenderer band 1
## 4 rasterrenderer type singlebandcolordata
Their display can primarily be changed by adjusting lower-level options in their corresponding style:
## name attrname value
## 1 rasterrenderer opacity 1
## 2 rasterrenderer alphaBand -1
## 3 rasterrenderer band 1
## 4 rasterrenderer type singlebandcolordata
## 5 brightnesscontrast brightness 0
## 6 brightnesscontrast contrast 0
## 7 huesaturation colorizeGreen 128
## 8 huesaturation colorizeOn 0
## 9 huesaturation colorizeRed 255
## 10 huesaturation colorizeBlue 128
## 11 huesaturation grayscaleMode 0
## 12 huesaturation saturation 0
## 13 huesaturation colorizeStrength 100
## 14 rasterresampler maxOversampling 2