Skip to content

Instantly share code, notes, and snippets.

@SzieberthAdam
Created January 19, 2016 14:30
Show Gist options
  • Save SzieberthAdam/078c5a4d34ba8c8a5511 to your computer and use it in GitHub Desktop.
Save SzieberthAdam/078c5a4d34ba8c8a5511 to your computer and use it in GitHub Desktop.
QGIS: Label to centroid when position is not explicitely set
# It is assumed that the layer has both "labelX" and a "labelY" attributes set as real number fields.
# Labels / Placement / Data Defined / Coordinate / X:
CASE WHEN "labelX" IS NULL THEN toreal(regexp_substr(geom_to_wkt(centroid($geometry)), '(-?\\d+\\.?\\d*) -?\\d+\\.?\\d*')) ELSE "labelX" END
# Labels / Placement / Data Defined / Coordinate / Y:
CASE WHEN "labelY" IS NULL THEN toreal(regexp_substr(geom_to_wkt(centroid($geometry)), '-?\\d+\\.?\\d* (-?\\d+\\.?\\d*)')) ELSE "labelY" END
# Labels / Placement / Data Defined / Alignment / horizontal:
'Center'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment