Skip to main content
deleted 186 characters in body
Source Link
Matt
  • 17.9k
  • 4
  • 23
  • 59

You can run this script in the Python Script Editor in QGIS and it will symbolise your layer with a colour ramp based on values in the storeys field.

To automate it, it could be put inside a function and then connected to the lyr.layerModified signal. It will refresh the symbology when a feature is added, even with a new value.

lyr_name = 'my layer'     # the layer you want to symbolise
ramp_name = 'RdGy'        # can be any of the named colour ramps in QGIS (making a custom ramp is also possible)
value_field = 'storeys'   # the field holding the number of storeys

# get the layer
lyr = QgsProject.instance().mapLayersByName(lyr_name)[0]

# get the 'storeys' field index
idx = lyr.fields().indexFromName(value_field)

# build a colour ramp
default_style = QgsStyle().defaultStyle()
color_ramp = default_style.colorRamp(ramp_name)
color_ramp.invert()    # with the RdGy colour ramp give low number of storeys grey and high number red

# instantiate a default symbol for the layer type
symbol = QgsSymbol.defaultSymbol(lyr.geometryType())

# empty list to store the categorised renderer categories
categories = []

# get a sorted list of unique values from the storeys field and iterate through them
for u in sorted(lyr.uniqueValues(idx)):
    # make a render category for each value (category value, symbol, category label)
    cat = QgsRendererCategory(u, symbol, str(u))
    # append it to the list of categories
    categories.append(cat)

# make a categorised renderer with the list of categories
renderer = QgsCategorizedSymbolRenderer(value_field, categories)

# apply the colour ramp to the renderer
renderer.updateColorRamp(color_ramp)

# apply the renderer to the layer
lyr.setRenderer(renderer)

# refresh the symbology
lyr.triggerRepaint()

You can run this script in the Python Script Editor in QGIS and it will symbolise your layer with a colour ramp based on values in the storeys field.

To automate it, it could be put inside a function and then connected to the lyr.layerModified signal. It will refresh the symbology when a feature is added, even with a new value.

lyr_name = 'my layer'     # the layer you want to symbolise
ramp_name = 'RdGy'        # can be any of the named colour ramps in QGIS (making a custom ramp is also possible)
value_field = 'storeys'   # the field holding the number of storeys

# get the layer
lyr = QgsProject.instance().mapLayersByName(lyr_name)[0]

# get the 'storeys' field index
idx = lyr.fields().indexFromName(value_field)

# build a colour ramp
default_style = QgsStyle().defaultStyle()
color_ramp = default_style.colorRamp(ramp_name)
color_ramp.invert()    # with the RdGy colour ramp give low number of storeys grey and high number red

# instantiate a default symbol for the layer type
symbol = QgsSymbol.defaultSymbol(lyr.geometryType())

# empty list to store the categorised renderer categories
categories = []

# get a sorted list of unique values from the storeys field and iterate through them
for u in sorted(lyr.uniqueValues(idx)):
    # make a render category for each value (category value, symbol, category label)
    cat = QgsRendererCategory(u, symbol, str(u))
    # append it to the list of categories
    categories.append(cat)

# make a categorised renderer with the list of categories
renderer = QgsCategorizedSymbolRenderer(value_field, categories)

# apply the colour ramp to the renderer
renderer.updateColorRamp(color_ramp)

# apply the renderer to the layer
lyr.setRenderer(renderer)

# refresh the symbology
lyr.triggerRepaint()

You can run this script in the Python Script Editor in QGIS and it will symbolise your layer with a colour ramp based on values in the storeys field.

lyr_name = 'my layer'     # the layer you want to symbolise
ramp_name = 'RdGy'        # can be any of the named colour ramps in QGIS (making a custom ramp is also possible)
value_field = 'storeys'   # the field holding the number of storeys

# get the layer
lyr = QgsProject.instance().mapLayersByName(lyr_name)[0]

# get the 'storeys' field index
idx = lyr.fields().indexFromName(value_field)

# build a colour ramp
default_style = QgsStyle().defaultStyle()
color_ramp = default_style.colorRamp(ramp_name)
color_ramp.invert()    # with the RdGy colour ramp give low number of storeys grey and high number red

# instantiate a default symbol for the layer type
symbol = QgsSymbol.defaultSymbol(lyr.geometryType())

# empty list to store the categorised renderer categories
categories = []

# get a sorted list of unique values from the storeys field and iterate through them
for u in sorted(lyr.uniqueValues(idx)):
    # make a render category for each value (category value, symbol, category label)
    cat = QgsRendererCategory(u, symbol, str(u))
    # append it to the list of categories
    categories.append(cat)

# make a categorised renderer with the list of categories
renderer = QgsCategorizedSymbolRenderer(value_field, categories)

# apply the colour ramp to the renderer
renderer.updateColorRamp(color_ramp)

# apply the renderer to the layer
lyr.setRenderer(renderer)

# refresh the symbology
lyr.triggerRepaint()
deleted 128 characters in body
Source Link
Matt
  • 17.9k
  • 4
  • 23
  • 59

You can run this script in the Python Script Editor in QGIS and it will symbolise your layer with a colour ramp based on values in the storeys field.

To automate it, it could be put inside a function and then connected to the lyr.layerModified signal. It will refresh the symbology when a feature is added, even with a new value. I don't have time to test right now but I am hoping that could be handled through a macro that fires when the project is opened.

lyr_name = 'my layer'     # the layer you want to symbolise
ramp_name = 'RdGy'        # can be any of the named colour ramps in QGIS (making a custom ramp is also possible)
value_field = 'storeys'   # the field holding the number of storeys

# get the layer
lyr = QgsProject.instance().mapLayersByName(lyr_name)[0]

# get the 'storeys' field index
idx = lyr.fields().indexFromName(value_field)

# build a colour ramp
default_style = QgsStyle().defaultStyle()
color_ramp = default_style.colorRamp(ramp_name)
color_ramp.invert()    # with the RdGy colour ramp give low number of storeys grey and high number red

# instantiate a default symbol for the layer type
symbol = QgsSymbol.defaultSymbol(lyr.geometryType())

# empty list to store the categorised renderer categories
categories = []

# get a sorted list of unique values from the storeys field and iterate through them
for u in sorted(lyr.uniqueValues(idx)):
    # make a render category for each value (category value, symbol, category label)
    cat = QgsRendererCategory(u, symbol, str(u))
    # append it to the list of categories
    categories.append(cat)

# make a categorised renderer with the list of categories
renderer = QgsCategorizedSymbolRenderer(value_field, categories)

# apply the colour ramp to the renderer
renderer.updateColorRamp(color_ramp)

# apply the renderer to the layer
lyr.setRenderer(renderer)

# refresh the symbology
lyr.triggerRepaint()

You can run this script in the Python Script Editor in QGIS and it will symbolise your layer with a colour ramp based on values in the storeys field.

To automate it, it could be put inside a function and then connected to the lyr.layerModified signal. It will refresh the symbology when a feature is added, even with a new value. I don't have time to test right now but I am hoping that could be handled through a macro that fires when the project is opened.

lyr_name = 'my layer'     # the layer you want to symbolise
ramp_name = 'RdGy'        # can be any of the named colour ramps in QGIS (making a custom ramp is also possible)
value_field = 'storeys'   # the field holding the number of storeys

# get the layer
lyr = QgsProject.instance().mapLayersByName(lyr_name)[0]

# get the 'storeys' field index
idx = lyr.fields().indexFromName(value_field)

# build a colour ramp
default_style = QgsStyle().defaultStyle()
color_ramp = default_style.colorRamp(ramp_name)
color_ramp.invert()    # with the RdGy colour ramp give low number of storeys grey and high number red

# instantiate a default symbol for the layer type
symbol = QgsSymbol.defaultSymbol(lyr.geometryType())

# empty list to store the categorised renderer categories
categories = []

# get a sorted list of unique values from the storeys field and iterate through them
for u in sorted(lyr.uniqueValues(idx)):
    # make a render category for each value (category value, symbol, category label)
    cat = QgsRendererCategory(u, symbol, str(u))
    # append it to the list of categories
    categories.append(cat)

# make a categorised renderer with the list of categories
renderer = QgsCategorizedSymbolRenderer(value_field, categories)

# apply the colour ramp to the renderer
renderer.updateColorRamp(color_ramp)

# apply the renderer to the layer
lyr.setRenderer(renderer)

# refresh the symbology
lyr.triggerRepaint()

You can run this script in the Python Script Editor in QGIS and it will symbolise your layer with a colour ramp based on values in the storeys field.

To automate it, it could be put inside a function and then connected to the lyr.layerModified signal. It will refresh the symbology when a feature is added, even with a new value.

lyr_name = 'my layer'     # the layer you want to symbolise
ramp_name = 'RdGy'        # can be any of the named colour ramps in QGIS (making a custom ramp is also possible)
value_field = 'storeys'   # the field holding the number of storeys

# get the layer
lyr = QgsProject.instance().mapLayersByName(lyr_name)[0]

# get the 'storeys' field index
idx = lyr.fields().indexFromName(value_field)

# build a colour ramp
default_style = QgsStyle().defaultStyle()
color_ramp = default_style.colorRamp(ramp_name)
color_ramp.invert()    # with the RdGy colour ramp give low number of storeys grey and high number red

# instantiate a default symbol for the layer type
symbol = QgsSymbol.defaultSymbol(lyr.geometryType())

# empty list to store the categorised renderer categories
categories = []

# get a sorted list of unique values from the storeys field and iterate through them
for u in sorted(lyr.uniqueValues(idx)):
    # make a render category for each value (category value, symbol, category label)
    cat = QgsRendererCategory(u, symbol, str(u))
    # append it to the list of categories
    categories.append(cat)

# make a categorised renderer with the list of categories
renderer = QgsCategorizedSymbolRenderer(value_field, categories)

# apply the colour ramp to the renderer
renderer.updateColorRamp(color_ramp)

# apply the renderer to the layer
lyr.setRenderer(renderer)

# refresh the symbology
lyr.triggerRepaint()
added 314 characters in body
Source Link
Matt
  • 17.9k
  • 4
  • 23
  • 59

You can run this script in the Python Script Editor in QGIS and it will symbolise your layer with a colour ramp based on values in the storeys field.

To automate it, it could be put inside a function and then connected to the lyr.layerModified signal. It will refresh the symbology when a feature is added, even with a new value. I don't have time to test right now but I am hoping that could be handled through a macro that fires when the project is opened.

lyr_name = 'my layer'     # the layer you want to symbolise
ramp_name = 'RdGy'        # can be any of the named colour ramps in QGIS (making a custom ramp is also possible)
value_field = 'storeys'   # the field holding the number of storeys

# get the layer
lyr = QgsProject.instance().mapLayersByName(lyr_name)[0]

# get the 'storeys' field index
idx = lyr.fields().indexFromName(value_field)

# build a colour ramp
default_style = QgsStyle().defaultStyle()
color_ramp = default_style.colorRamp(ramp_name)
color_ramp.invert()    # with the RdGy colour ramp give low number of storeys grey and high number red

# instantiate a default symbol for the layer type
symbol = QgsSymbol.defaultSymbol(lyr.geometryType())

# empty list to store the categorised renderer categories
categories = []

# get a sorted list of unique values from the storeys field and iterate through them
for u in sorted(lyr.uniqueValues(idx)):
    # make a render category for each value (category value, symbol, category label)
    cat = QgsRendererCategory(u, symbol, str(u))
    # append it to the list of categories
    categories.append(cat)

# make a categorised renderer with the list of categories
renderer = QgsCategorizedSymbolRenderer(value_field, categories)

# apply the colour ramp to the renderer
renderer.updateColorRamp(color_ramp)

# apply the renderer to the layer
lyr.setRenderer(renderer)

# refresh the symbology
lyr.triggerRepaint()

You can run this script in the Python Script Editor in QGIS and it will symbolise your layer with a colour ramp based on values in the storeys field.

lyr_name = 'my layer'     # the layer you want to symbolise
ramp_name = 'RdGy'        # can be any of the named colour ramps in QGIS (making a custom ramp is also possible)
value_field = 'storeys'   # the field holding the number of storeys

# get the layer
lyr = QgsProject.instance().mapLayersByName(lyr_name)[0]

# get the 'storeys' field index
idx = lyr.fields().indexFromName(value_field)

# build a colour ramp
default_style = QgsStyle().defaultStyle()
color_ramp = default_style.colorRamp(ramp_name)
color_ramp.invert()    # with the RdGy colour ramp give low number of storeys grey and high number red

# instantiate a default symbol for the layer type
symbol = QgsSymbol.defaultSymbol(lyr.geometryType())

# empty list to store the categorised renderer categories
categories = []

# get a sorted list of unique values from the storeys field and iterate through them
for u in sorted(lyr.uniqueValues(idx)):
    # make a render category for each value (category value, symbol, category label)
    cat = QgsRendererCategory(u, symbol, str(u))
    # append it to the list of categories
    categories.append(cat)

# make a categorised renderer with the list of categories
renderer = QgsCategorizedSymbolRenderer(value_field, categories)

# apply the colour ramp to the renderer
renderer.updateColorRamp(color_ramp)

# apply the renderer to the layer
lyr.setRenderer(renderer)

# refresh the symbology
lyr.triggerRepaint()

You can run this script in the Python Script Editor in QGIS and it will symbolise your layer with a colour ramp based on values in the storeys field.

To automate it, it could be put inside a function and then connected to the lyr.layerModified signal. It will refresh the symbology when a feature is added, even with a new value. I don't have time to test right now but I am hoping that could be handled through a macro that fires when the project is opened.

lyr_name = 'my layer'     # the layer you want to symbolise
ramp_name = 'RdGy'        # can be any of the named colour ramps in QGIS (making a custom ramp is also possible)
value_field = 'storeys'   # the field holding the number of storeys

# get the layer
lyr = QgsProject.instance().mapLayersByName(lyr_name)[0]

# get the 'storeys' field index
idx = lyr.fields().indexFromName(value_field)

# build a colour ramp
default_style = QgsStyle().defaultStyle()
color_ramp = default_style.colorRamp(ramp_name)
color_ramp.invert()    # with the RdGy colour ramp give low number of storeys grey and high number red

# instantiate a default symbol for the layer type
symbol = QgsSymbol.defaultSymbol(lyr.geometryType())

# empty list to store the categorised renderer categories
categories = []

# get a sorted list of unique values from the storeys field and iterate through them
for u in sorted(lyr.uniqueValues(idx)):
    # make a render category for each value (category value, symbol, category label)
    cat = QgsRendererCategory(u, symbol, str(u))
    # append it to the list of categories
    categories.append(cat)

# make a categorised renderer with the list of categories
renderer = QgsCategorizedSymbolRenderer(value_field, categories)

# apply the colour ramp to the renderer
renderer.updateColorRamp(color_ramp)

# apply the renderer to the layer
lyr.setRenderer(renderer)

# refresh the symbology
lyr.triggerRepaint()
added 30 characters in body
Source Link
Matt
  • 17.9k
  • 4
  • 23
  • 59
Loading
Source Link
Matt
  • 17.9k
  • 4
  • 23
  • 59
Loading