02UrbanPortrait
City as a Face — generative cartographic portrait studio • v0.5.2
1. Overview
02Urban Portrait transforms urban vector infrastructure (roads, buildings, points) into optical portraits and cartographic engravings. The user loads a portrait image, maps it to a geographic frame, and the plugin styles vector features by luminance values sampled from the image pixels or generates an algorithmic variable-radius halftone stippling layer. This is a generative, non-destructive cartographic technique—no source geometry or attributes are ever modified. The plugin applies self-contained categorized renderers across 10 master presets (with JSON import/export), each mapped to per-preset colours, line widths, and transparencies.
The plugin is guided by an interactive 3-stage visual workflow stepper with numbered circular nodes (❶ Set up → ❷ Shape → ❸ Export) and dynamic action guides. Tab 1 handles data acquisition and geographic frame placement; Tab 2 provides art-direction presets, halftone engraving, and live rendering; Tab 3 handles QML style export and composition artwork output.
2. Feature Documentation
2.1 Luminance-Based Styling Engine
Image-to-Vector Sampling Pipeline
- Image loading: Supports PNG, JPG, JPEG, TIFF, TIF, WebP, BMP. Auto-scales images larger than 1800×1800 px. Preserves EXIF orientation via
QImageReader.setAutoTransform(True). - Geographic frame: The image aspect ratio is fitted to a geographic rectangle (canvas extent or user-drawn frame) with letterbox/pillarbox centring and no stretching. Can be screen-locked (frame follows pan/zoom) or fixed to geographic coordinates.
- Sampling: For each visible feature within the frame, the plugin computes sampling points by geometry type and sampling mode, maps world coordinates to normalised image coordinates (flipping Y), samples luminance from the downscaled image, and applies auto-contrast stretch, gamma correction, optional inversion, and optional edge emphasis.
- Rendering: A
QgsCategorizedSymbolRendereron$idapplies per-preset colours, line widths (deepest tones widest, lightest tones thinnest), and transparencies. Each of the four darker tone categories holds the ids of the features sampled into it; the fifth, catch-all category draws the Paper tone and every feature that was not sampled. Nothing in the renderer calls back into Python, so QGIS draws it at native speed in its parallel render threads, a saved project shows the same portrait without the plugin installed, and an exported QML is complete for that dataset.
2.2 Three Sampling Modes
| Mode | Line Sample Points | Polygon Sample Points |
|---|---|---|
| Fast | 1 (midpoint) | 1 (point on surface) |
| Balanced | 3 (20%, 50%, 80%) | 2 (point on surface + centroid) |
| High Quality | 5 (10%, 30%, 50%, 70%, 90%) | 2 (point on surface + centroid) |
2.3 Five Tone Rules
Five equal luminance intervals defined by TONE_BREAKS = (0, 52, 104, 156, 208, 256). Each gets per-preset colour, line width (descending from ~1.55 mm for darkest to ~0.10 mm for lightest), and opacity. When hide_highlights is active, the top band ("Paper", which also covers unsampled features) is not drawn, letting the paper show through—this is how the Ink Portrait and Sepia Blocks presets create white-space faces.
2.4 Rendering Enhancements
Visual Quality Details
Line underlay: A darker, wider base line (+0.35 mm, darkened by 250) is placed 2 layers behind the main line to make it pop from the background.
Polygon fills: Semi-transparent fills (deepest tones alpha 225, medium alpha 150) with thin outlines.
Point symbols: Small circles with outlines.
Line caps and joins: All lines use round cap and round join styles for organic, hand-drawn quality.
2.5 Halftone and Paper Colour
Halftone layer: Generate Halftone Layer writes a point memory layer over the portrait frame. Dots on long side sets the dot count along the longer frame side; the shorter side receives as many as keep the cells square. Square places dots on a regular grid, Hex offsets alternate rows like a classic print screen. Each dot's area is proportional to the darkness of its tone (radius ∝ √darkness), and the darkest tone covers its whole cell, so midtones keep their true ink share. The tone already includes contrast stretch, gamma, inversion and edge emphasis, applied exactly once. Dots are sized in map units through the radius field and inked with the preset's deepest colour.
Paper colour: with Paint the map background with the preset paper colour on, creating a portrait or a halftone layer sets the project background to the preset's paper, which the dark presets (Neon Night, Blueprint, Negative City, Cyberpunk 2077, Thermal Heatmap, Emerald Eco-Map) are designed for. The previous colour is stored in the project and returns with Restore original styles. Artwork exports print on the same paper.
2.6 Built-In OSM Downloader
One-Click City Data Acquisition
Adds an XYZ tile OpenStreetMap basemap (with duplicate detection). Downloads the current canvas view via a single Overpass API query (with custom User-Agent header). Validates that viewport is within 6 km × 6 km and 25 km2. Parses the OSM XML response through QGIS/GDAL OGR driver, splitting into three specialised memory layers: OSM Roads (MultiLineString, filtered by highway tag), OSM Buildings (MultiPolygon, filtered by building tag), and OSM Land Use (MultiPolygon, filtered by landuse tag). Each layer carries osm_id, name, and class fields. Tags are extracted from the other_tags HSTORE string via regex.
3. Art Presets
| Preset | Background | Colour Palette | Character |
|---|---|---|---|
| Ink Portrait | #f5f1e8 (warm paper) | Dark ink tones; widest lines for shadows, highlights hidden | Classic pen-and-ink portrait |
| Neon Night | #050816 (deep space) | Pink / purple / cyan / mint; all bands visible | Cyberpunk / synthwave aesthetic |
| Blueprint | #082f49 (deep navy) | Cyan / teal gradient (reversed); all bands visible | Architectural blueprint |
| Sepia Blocks | #f1e3c6 (warm cream) | Brown / sepia tones; highlights hidden | Antique map / vintage engraving |
| Negative City | #07071a (deep navy) | Indigo / violet gradient (reversed); all bands visible | Film negative / X-ray aesthetic |
| Cyberpunk 2077 | #050811 (deep night) | Violet / magenta / cyan / yellow; all bands visible | High-contrast neon poster |
| Vintage Engraving | #f6eee3 (aged paper) | Dark brown to tan; highlights hidden | Copperplate engraving |
| Thermal Heatmap | #050414 (near black) | Indigo / purple / crimson / orange / yellow; all bands visible | Thermal camera |
| Emerald Eco-Map | #030f0b (near black green) | Deep to light emerald; all bands visible | Green-infrastructure map |
| Nordic Slate | #f1f5f9 (cool light grey) | Slate greys; highlights hidden | Restrained editorial print |
4. Image Processing Pipeline
Luminance Enhancement Parameters
- Gamma correction (\(\gamma\), range 0.2–3.0, default 1.0): Applies \(L' = 255 \times (L/255)^{1/\gamma}\) after contrast stretch. Values <1.0 lighten mid-tones; values >1.0 darken them.
- Auto-contrast stretch (default on): Computes robust 1st and 99th percentile luminance boundaries from a 256-bin histogram and stretches the image to full 0–255 range.
- Invert (default off): Reverses the luminance scale (light becomes dark, dark becomes light).
- Edge emphasis (range 0.0–1.0, default 0.25): Computes per-pixel edge intensity via neighbour difference and darkens luminance proportionally. At 0.0, edges have no extra emphasis; at 1.0, strong edges become fully black.
5. Controls Summary
| Tab | Control | Description |
|---|---|---|
| Set up | Add OSM basemap | Inserts a live XYZ tile layer |
| Set up | Download this view | Fetches OSM roads, buildings, and land use for the viewport |
| Set up | Upload picture | Loads a portrait image (PNG, JPG, TIFF, WebP, BMP) |
| Set up | Vector canvas | Multi-select QListWidget of all project vector layers |
| Set up | Use canvas extent / Draw frame | Defines the geographic portrait frame |
| Set up | Screen-locked mask | When on, frame moves with pan/zoom; off = fixed geographic |
| Portrait | Preset | Dropdown with the 10 presets above plus imported palettes; a swatch shows the five tones on the preset paper |
| Portrait | Paint the map background | Checkbox (default on): sets the project background to the preset paper; Restore brings the previous colour back |
| Portrait | Sampling | Dropdown: Fast, Balanced, High Quality |
| Portrait | Gamma | QDoubleSpinBox: 0.2–3.0, step 0.1 |
| Portrait | Edge emphasis | QDoubleSpinBox: 0.0–1.0, step 0.1 |
| Portrait | Layer opacity | QDoubleSpinBox: 0.05–1.0, step 0.05 |
| Portrait | Feature limit | QSpinBox: 500–100,000, step 500, default 10,000 |
| Portrait | Auto contrast / Invert | Checkboxes for percentile stretch and luminance inversion |
| Portrait | Live update | Checkbox: re-samples on pan/zoom with 240 ms debounce |
| Portrait | Halftone: dots on long side / pattern | QSpinBox 20–200 (default 60) and Square / Hex; Generate Halftone Layer adds a point layer |
| Export | Format / DPI / Export | PNG (72–1200 dpi), PDF, or SVG; saves via file dialog |
| Export | Restore styles / Export QML | Returns layers to original renderers and the map to its previous background; saves one layer's self-contained portrait style as .qml |
6. Workflow Guide
- Click the 02Urban Portrait toolbar icon. The dock opens on the right.
- On the Set up tab, add city data. Use the built-in OSM downloader (Add basemap → pan to a neighbourhood → Download this view) or select existing project vector layers.
- Upload a portrait image via the Upload button. A preview thumbnail appears with filename, dimensions, and aspect ratio.
- Define the portrait position: either "Use canvas extent" or "Draw frame" (drag a rectangle on the map). Optionally enable "Screen-locked mask" for frame-following.
- Select vector layers to stylise from the list.
- Switch to the Portrait tab. Choose an art preset, adjust gamma, edge emphasis, and feature limit.
- Click Create portrait. The layers are restyled in place with the portrait renderer. Pan and zoom with live update enabled to see the portrait react.
- Switch to the Export tab to export as PNG, PDF, or SVG. Use "Restore original styles" to undo all changes, including the paper colour. Use "Export QML" to save a layer's portrait style; it reproduces the portrait on the same dataset (same feature ids).
7. Technical Notes
- Non-destructive operation: Original renderers are cloned before styling is applied and also stored, as renderer XML, in the layer property
zero2urbanportrait/original_renderer. All operations are reversible via Restore, also after the project has been saved and reopened. No source geometry or attributes are modified. - Saved projects: the portrait renderer is plain QGIS styling, so a saved project reopens with the portrait intact, with or without the plugin. When the dock opens on such a project it reconnects the portrait layers: with the portrait picture still on disk, live updates resume; otherwise the saved portrait stays as it is and Restore remains available.
- Live update (240 ms debounce): A
QTimerlimits re-sampling to once per 240 ms during pan/zoom, controlled by themax_featurescap (default 10,000). - Adaptive theme: All UI colours derive from the active
QPalettewith WCAG contrast compliance. The hero card intentionally remains dark for brand identity. Cyan accent switches between#06B6D4(dark theme) and#0891B2(light theme). - Project state persistence: Full settings (image path, frame coords, preset, gamma, edge, opacity, invert, auto-contrast, max features, follow-canvas, paper colour, halftone density and pattern) serialised as JSON and stored in the QGIS project under key
zero2urbanportrait/state. Re-opening restores state. - Export pipeline: Uses
QgsPrintLayoutwith a single map item filling the page. The page has exactly the canvas aspect ratio (longer side up to 420 mm), the map item uses the canvas CRS and rotation, and both the page and the map background take the canvas colour, so a dark preset is not printed on white. SVG export usesforceVectorOutput = True. - QGIS compatibility: 3.28 through 4.99. Zero external Python dependencies.
8. Literature
- Wood, D. (2010). Rethinking the Power of Maps. Guilford Press.
- Harley, J. B. (1989). Deconstructing the map. Cartographica, 26(2), 1–20. DOI: 10.3138/E635-7827-1757-9T53
- Schumann, H. & Mueller, W. (2000). Visualisierung: Grundlagen und allgemeine Methoden. Springer.
- Brewer, C. A. (2005). Designing Better Maps: A Guide for GIS Users. ESRI Press.
- Tufte, E. R. (1983). The Visual Display of Quantitative Information. Graphics Press.