Skip to content
Will Roper edited this page Feb 17, 2021 · 7 revisions

Draw long lines on top:

click control draw order in style pain, and enter $length in expression editor

draw order

Colouring in...

Make your lines and addresses the same colour, and spin your polling stations to spot when they're on top of each other.

from random import randrange


def set_address_symbol(colour):
    address_symbol_properties = {
        'name': 'circle',
        'outline_color': '0,0,0,0',
        'outline_style': 'no',
        'size': '0.9',
        'color': colour
    }
    address_symbol = QgsMarkerSymbol.createSimple(
        address_symbol_properties
    )
    return address_symbol
    
def set_line_symbol(colour):
    line_symbol_properties = {
        'line_color': colour,
        'line_width': '0.5'
    }
    line_symbol = QgsLineSymbol.createSimple(
        line_symbol_properties
    )
    line_symbol.setOpacity(0.6)
    return line_symbol

def set_station_symbol(colour):
    x_symbol = QgsMarkerSymbol.createSimple({
        'name': 'cross',
        'angle': f'{randrange(0, 90)}', 
        'color': colour,
        'outline_color': colour, 
        'outline_style': 'solid', 
        'outline_width': '1',
        'size': '10'
    })
    return x_symbol

def update_categories(categories, symbol, fid):
    category = QgsRendererCategory(fid, symbol, str(fid))
    categories.append(category)

def apply_categories_render_to_layer(layer, categories, field):
    renderer = QgsCategorizedSymbolRenderer(field, categories)
    if renderer is not None:
        layer.setRenderer(renderer)
    layer.triggerRepaint()

addresses = QgsProject.instance().mapLayersByName(
    'pollingstations_residential_address_view')[0]
lines = QgsProject.instance().mapLayersByName(
    'pollingstations_lines_view')[0]
stations = QgsProject.instance().mapLayersByName(
    'pollingstations_pollingstation')[0]

fni = stations.fields().indexFromName('internal_council_id')
station_ids = stations.uniqueValues(fni)

address_categories = []
line_categories = []
station_categories = []
    
for station_id in station_ids:
    colour = '%d, %d, %d' % (randrange(0, 256), randrange(0, 256), randrange(0, 256))

    address_symbol = set_address_symbol(colour)
    line_symbol = set_line_symbol(colour)
    station_symbol = set_station_symbol(colour)
    
    update_categories(address_categories, address_symbol, station_id)
    update_categories(line_categories, line_symbol, station_id)
    update_categories(station_categories, station_symbol, station_id)


apply_categories_render_to_layer(
    addresses, address_categories, 'polling_station_id'
)
apply_categories_render_to_layer(
    lines, line_categories, 'internal_council_id'
)
apply_categories_render_to_layer(
    stations, station_categories, 'internal_council_id'
)