Skip to content

Instantly share code, notes, and snippets.

@ewsterrenburg
Last active June 28, 2023 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ewsterrenburg/b579e1f6c053050112a2e89f9811408d to your computer and use it in GitHub Desktop.
Save ewsterrenburg/b579e1f6c053050112a2e89f9811408d to your computer and use it in GitHub Desktop.
Load Mapstore annotations into QGIS
# -*- coding: utf-8 -*-
import json
from PyQt5.QtGui import QColor, QFont
from qgis.core import QgsPalLayerSettings, QgsTextFormat
from qgis.core import QgsTextBufferSettings, QgsVectorLayerSimpleLabeling
def turnOnLabels(layer, expression):
layer_settings = QgsPalLayerSettings()
text_format = QgsTextFormat()
text_format.setFont(QFont("Arial", 10))
text_format.setSize(10)
buffer_settings = QgsTextBufferSettings()
buffer_settings.setEnabled(True)
buffer_settings.setSize(0.10)
buffer_settings.setColor(QColor("black"))
text_format.setBuffer(buffer_settings)
layer_settings.setFormat(text_format)
layer_settings.fieldName = expression
layer_settings.isExpression = True
# Line below is the original version
# Can't figure out anymore what the exact meaning of the magic 4 is...
# Could be replaced with a proper placement if required
# layer_settings.placement = 4
layer_settings.enabled = True
layer_settings = QgsVectorLayerSimpleLabeling(layer_settings)
layer.setLabelsEnabled(True)
layer.setLabeling(layer_settings)
layer.triggerRepaint()
input_file_path = unicode(r"C:\Users\NL01031\Downloads\Annotations.json")
groupName ="Annotations"
project = QgsProject.instance()
root = project.layerTreeRoot()
group = root.addGroup(groupName)
# not the most elegant way to allow for longer descriptions, yet
# good enough for now.
# (for adding the fields in a more elegant way: https://anitagraser.com/pyqgis-101-introduction-to-qgis-python-programming-for-non-programmers/pyqgis101-creating-editing-a-new-vector-layer/ )
lineLayer = QgsVectorLayer("linestring?crs=epsg:4326&field=id:string(36)&field=title:string&field=description:string(10000)&index=yes", "PolyLines", "memory")
project.addMapLayer(lineLayer, False)
group.addLayer(lineLayer)
pointLayer = QgsVectorLayer("point?crs=epsg:4326&field=id:string(36)&field=title:string&field=description:string(10000)&field=textvalue:string&&index=yes", "Points", "memory")
project.addMapLayer(pointLayer, False)
group.addLayer(pointLayer)
polygonLayer = QgsVectorLayer("polygon?crs=epsg:4326&field=id:string(36)&field=title:string&field=description:string(10000)&index=yes", "Polygons", "memory")
project.addMapLayer(polygonLayer, False)
group.addLayer(polygonLayer)
annotationFile = open(input_file_path)
annotations = json.loads(annotationFile.read())
features = annotations['features']
for feature in features:
attributeId = feature['properties']['id']
attributeTitle = feature['properties']['title']
try:
attributeDescription = feature['properties']['description']
except KeyError:
attributeDescription = None
subParts = feature['features']
for subPart in subParts:
featureType = subPart['geometry']['type']
if featureType == 'LineString':
vertices = []
for coord in subPart['geometry']['coordinates']:
vertex = QgsPoint(coord[0], coord[1])
vertices.append(vertex)
geom = QgsGeometry.fromPolyline(vertices)
feat = QgsFeature(lineLayer.fields())
elif featureType == 'Point':
coord = subPart['geometry']['coordinates']
pointXY = QgsPointXY(coord[0], coord[1])
geom = QgsGeometry.fromPointXY(pointXY)
feat = QgsFeature(pointLayer.fields())
try:
attributeTextValue = subPart['properties']['valueText']
except KeyError:
attributeTextValue = ''
feat.setAttribute('textvalue', attributeTextValue)
elif featureType == 'Polygon':
vertices = []
if len(subPart['geometry']['coordinates']) == 0:
iface.messageBar().pushMessage("Warning", f"Polygon without vertices encountered for feature with id {attributeId}.", level=Qgis.Warning)
continue
for coord in subPart['geometry']['coordinates'][0]:
vertex = QgsPointXY(coord[0], coord[1])
vertices.append(vertex)
geom = QgsGeometry.fromPolygonXY([vertices])
geom.addRing(vertices)
feat = QgsFeature(polygonLayer.fields())
feat.setGeometry(geom)
feat.setAttribute('id', attributeId)
feat.setAttribute('title', attributeTitle)
feat.setAttribute('description', attributeDescription)
if featureType == 'LineString':
(res, outFeats) = lineLayer.dataProvider().addFeatures([feat])
elif featureType == 'Point':
(res, outFeats) = pointLayer.dataProvider().addFeatures([feat])
elif featureType == 'Polygon':
(res, outFeats) = polygonLayer.dataProvider().addFeatures([feat])
turnOnLabels(pointLayer, '"title" || \' - \' || "textvalue"')
turnOnLabels(lineLayer, '"title"')
turnOnLabels(polygonLayer, '"title"')
@estudiocad
Copy link

hi.. tks for the help...

this error

Consola de Python
Usa iface para acceder a la interfaz de la API de QGIS o digita help(iface) para más información
Aviso de advertencia: digitar instrucciones desde una fuente no confiable puede conllevar a pérdida o fuga de datos

-- coding: utf-8 --

import json
from PyQt5.QtGui import QColor, QFont
from qgis.core import QgsPalLayerSettings, QgsTextFormat
from qgis.core import QgsTextBufferSettings, QgsVectorLayerSimpleLabeling
def turnOnLabels(layer, expression):
layer_settings = QgsPalLayerSettings()
text_format = QgsTextFormat()
text_format.setFont(QFont("Arial", 10))
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
text_format.setFont(QFont("Arial", 10))
^
IndentationError: unexpected indent
text_format.setSize(10)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
text_format.setSize(10)
^
IndentationError: unexpected indent
buffer_settings = QgsTextBufferSettings()
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
buffer_settings = QgsTextBufferSettings()
^
IndentationError: unexpected indent
buffer_settings.setEnabled(True)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
buffer_settings.setEnabled(True)
^
IndentationError: unexpected indent
buffer_settings.setSize(0.10)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
buffer_settings.setSize(0.10)
^
IndentationError: unexpected indent
buffer_settings.setColor(QColor("black"))
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
buffer_settings.setColor(QColor("black"))
^
IndentationError: unexpected indent
text_format.setBuffer(buffer_settings)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
text_format.setBuffer(buffer_settings)
^
IndentationError: unexpected indent
layer_settings.setFormat(text_format)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
layer_settings.setFormat(text_format)
^
IndentationError: unexpected indent
layer_settings.fieldName = expression
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
layer_settings.fieldName = expression
^
IndentationError: unexpected indent
layer_settings.isExpression = True
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
layer_settings.isExpression = True
^
IndentationError: unexpected indent
layer_settings.placement = 4
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
layer_settings.placement = 4
^
IndentationError: unexpected indent
layer_settings.enabled = True
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
layer_settings.enabled = True
^
IndentationError: unexpected indent
layer_settings = QgsVectorLayerSimpleLabeling(layer_settings)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
layer_settings = QgsVectorLayerSimpleLabeling(layer_settings)
^
IndentationError: unexpected indent
layer.setLabelsEnabled(True)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
layer.setLabelsEnabled(True)
^
IndentationError: unexpected indent
layer.setLabeling(layer_settings)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
layer.setLabeling(layer_settings)
^
IndentationError: unexpected indent
layer.triggerRepaint()
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
layer.triggerRepaint()
^
IndentationError: unexpected indent
input_file_path = unicode(r"C:\Users\NL01031\Downloads\Seguimiento Rio Salado.json")
groupName ="Annotations"
project = QgsProject.instance()
root = project.layerTreeRoot()
group = root.addGroup(groupName)
lineLayer = QgsVectorLayer("linestring?crs=epsg:4326&field=id:string(36)&field=title:string&field=description:string&index=yes", "PolyLines", "memory")
project.addMapLayer(lineLayer, False)
<QgsMapLayer: 'PolyLines' (memory)>
group.addLayer(lineLayer)
<qgis._core.QgsLayerTreeLayer object at 0x0000026B79645DC8>
pointLayer = QgsVectorLayer("point?crs=epsg:4326&field=id:string(36)&field=title:string&field=description:string&field=textvalue:string&&index=yes", "Points", "memory")
project.addMapLayer(pointLayer, False)
<QgsMapLayer: 'Points' (memory)>
group.addLayer(pointLayer)
<qgis._core.QgsLayerTreeLayer object at 0x0000026B79645DC8>
polygonLayer = QgsVectorLayer("polygon?crs=epsg:4326&field=id:string(36)&field=title:string&field=description:string&index=yes", "Polygons", "memory")
project.addMapLayer(polygonLayer, False)
<QgsMapLayer: 'Polygons' (memory)>
group.addLayer(polygonLayer)
<qgis._core.QgsLayerTreeLayer object at 0x0000026B79645DC8>
annotationFile = open(input_file_path)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "", line 1, in
FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\NL01031\Downloads\Seguimiento Rio Salado.json'
annotations = json.loads(annotationFile.read())
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "", line 1, in
NameError: name 'annotationFile' is not defined
features = annotations['features']
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "", line 1, in
NameError: name 'annotations' is not defined
for feature in features:
attributeId = feature['properties']['id']
attributeTitle = feature['properties']['title']
try:
attributeDescription = feature['properties']['description']
except KeyError:
attributeDescription = None

subParts = feature['features']
for subPart in subParts:
	featureType = subPart['geometry']['type']
	if featureType == 'LineString':
		vertices = []
		for coord in subPart['geometry']['coordinates']:
			vertex = QgsPoint(coord[0], coord[1])
			vertices.append(vertex)
		geom = QgsGeometry.fromPolyline(vertices)
		feat = QgsFeature(lineLayer.fields())
	elif featureType == 'Point':
		coord = subPart['geometry']['coordinates']
		pointXY = QgsPointXY(coord[0], coord[1]) 
		geom = QgsGeometry.fromPointXY(pointXY)
		feat = QgsFeature(pointLayer.fields())
		try:
			attributeTextValue = subPart['properties']['valueText']
		except KeyError:
			attributeTextValue = ''
		feat.setAttribute('textvalue', attributeTextValue)
	elif featureType == 'Polygon':
		vertices = []
		for coord in subPart['geometry']['coordinates'][0]:
			vertex = QgsPointXY(coord[0], coord[1])
			vertices.append(vertex)
		geom = QgsGeometry.fromPolygonXY([vertices])
		geom.addRing(vertices)
		feat = QgsFeature(polygonLayer.fields())

Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "", line 1, in
NameError: name 'features' is not defined
feat.setGeometry(geom)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
feat.setGeometry(geom)
^
IndentationError: unexpected indent
feat.setAttribute('id', attributeId)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
feat.setAttribute('id', attributeId)
^
IndentationError: unexpected indent
feat.setAttribute('title', attributeTitle)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
feat.setAttribute('title', attributeTitle)
^
IndentationError: unexpected indent
feat.setAttribute('description', attributeDescription)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
feat.setAttribute('description', attributeDescription)
^
IndentationError: unexpected indent
if featureType == 'LineString':
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
if featureType == 'LineString':
^
IndentationError: unexpected indent
(res, outFeats) = lineLayer.dataProvider().addFeatures([feat])
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
(res, outFeats) = lineLayer.dataProvider().addFeatures([feat])
^
IndentationError: unexpected indent
elif featureType == 'Point':
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
elif featureType == 'Point':
^
IndentationError: unexpected indent
(res, outFeats) = pointLayer.dataProvider().addFeatures([feat])
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
(res, outFeats) = pointLayer.dataProvider().addFeatures([feat])
^
IndentationError: unexpected indent
elif featureType == 'Polygon':
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
elif featureType == 'Polygon':
^
IndentationError: unexpected indent
(res, outFeats) = polygonLayer.dataProvider().addFeatures([feat])
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
(res, outFeats) = polygonLayer.dataProvider().addFeatures([feat])
^
IndentationError: unexpected indent
turnOnLabels(pointLayer, '"title" || ' - ' || "textvalue"')
turnOnLabels(lineLayer, '"title"')

-- coding: utf-8 --

import json
from PyQt5.QtGui import QColor, QFont
from qgis.core import QgsPalLayerSettings, QgsTextFormat
from qgis.core import QgsTextBufferSettings, QgsVectorLayerSimpleLabeling
def turnOnLabels(layer, expression):
layer_settings = QgsPalLayerSettings()
text_format = QgsTextFormat()
text_format.setFont(QFont("Arial", 10))
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
text_format.setFont(QFont("Arial", 10))
^
IndentationError: unexpected indent
text_format.setSize(10)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
text_format.setSize(10)
^
IndentationError: unexpected indent
buffer_settings = QgsTextBufferSettings()
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
buffer_settings = QgsTextBufferSettings()
^
IndentationError: unexpected indent
buffer_settings.setEnabled(True)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
buffer_settings.setEnabled(True)
^
IndentationError: unexpected indent
buffer_settings.setSize(0.10)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
buffer_settings.setSize(0.10)
^
IndentationError: unexpected indent
buffer_settings.setColor(QColor("black"))
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
buffer_settings.setColor(QColor("black"))
^
IndentationError: unexpected indent
text_format.setBuffer(buffer_settings)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
text_format.setBuffer(buffer_settings)
^
IndentationError: unexpected indent
layer_settings.setFormat(text_format)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
layer_settings.setFormat(text_format)
^
IndentationError: unexpected indent
layer_settings.fieldName = expression
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
layer_settings.fieldName = expression
^
IndentationError: unexpected indent
layer_settings.isExpression = True
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
layer_settings.isExpression = True
^
IndentationError: unexpected indent
layer_settings.placement = 4
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
layer_settings.placement = 4
^
IndentationError: unexpected indent
layer_settings.enabled = True
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
layer_settings.enabled = True
^
IndentationError: unexpected indent
layer_settings = QgsVectorLayerSimpleLabeling(layer_settings)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
layer_settings = QgsVectorLayerSimpleLabeling(layer_settings)
^
IndentationError: unexpected indent
layer.setLabelsEnabled(True)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
layer.setLabelsEnabled(True)
^
IndentationError: unexpected indent
layer.setLabeling(layer_settings)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
layer.setLabeling(layer_settings)
^
IndentationError: unexpected indent
layer.triggerRepaint()
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
layer.triggerRepaint()
^
IndentationError: unexpected indent
input_file_path = unicode(r"C:\Users\oscar\Downloads\Seguimiento Rio Salado.json")
groupName ="Annotations"
project = QgsProject.instance()
root = project.layerTreeRoot()
group = root.addGroup(groupName)
lineLayer = QgsVectorLayer("linestring?crs=epsg:4326&field=id:string(36)&field=title:string&field=description:string&index=yes", "PolyLines", "memory")
project.addMapLayer(lineLayer, False)
<QgsMapLayer: 'PolyLines' (memory)>
group.addLayer(lineLayer)
<qgis._core.QgsLayerTreeLayer object at 0x0000026B79645EE8>
pointLayer = QgsVectorLayer("point?crs=epsg:4326&field=id:string(36)&field=title:string&field=description:string&field=textvalue:string&&index=yes", "Points", "memory")
project.addMapLayer(pointLayer, False)
<QgsMapLayer: 'Points' (memory)>
group.addLayer(pointLayer)
<qgis._core.QgsLayerTreeLayer object at 0x0000026B79645F78>
polygonLayer = QgsVectorLayer("polygon?crs=epsg:4326&field=id:string(36)&field=title:string&field=description:string&index=yes", "Polygons", "memory")
project.addMapLayer(polygonLayer, False)
<QgsMapLayer: 'Polygons' (memory)>
group.addLayer(polygonLayer)
<qgis._core.QgsLayerTreeLayer object at 0x0000026B79645B88>
annotationFile = open(input_file_path)
annotations = json.loads(annotationFile.read())
features = annotations['features']
for feature in features:
attributeId = feature['properties']['id']
attributeTitle = feature['properties']['title']
try:
attributeDescription = feature['properties']['description']
except KeyError:
attributeDescription = None

subParts = feature['features']
for subPart in subParts:
	featureType = subPart['geometry']['type']
	if featureType == 'LineString':
		vertices = []
		for coord in subPart['geometry']['coordinates']:
			vertex = QgsPoint(coord[0], coord[1])
			vertices.append(vertex)
		geom = QgsGeometry.fromPolyline(vertices)
		feat = QgsFeature(lineLayer.fields())
	elif featureType == 'Point':
		coord = subPart['geometry']['coordinates']
		pointXY = QgsPointXY(coord[0], coord[1]) 
		geom = QgsGeometry.fromPointXY(pointXY)
		feat = QgsFeature(pointLayer.fields())
		try:
			attributeTextValue = subPart['properties']['valueText']
		except KeyError:
			attributeTextValue = ''
		feat.setAttribute('textvalue', attributeTextValue)
	elif featureType == 'Polygon':
		vertices = []
		for coord in subPart['geometry']['coordinates'][0]:
			vertex = QgsPointXY(coord[0], coord[1])
			vertices.append(vertex)
		geom = QgsGeometry.fromPolygonXY([vertices])
		geom.addRing(vertices)
		feat = QgsFeature(polygonLayer.fields())

1012
1012
1012
1012
1012
feat.setGeometry(geom)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
feat.setGeometry(geom)
^
IndentationError: unexpected indent
feat.setAttribute('id', attributeId)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
feat.setAttribute('id', attributeId)
^
IndentationError: unexpected indent
feat.setAttribute('title', attributeTitle)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
feat.setAttribute('title', attributeTitle)
^
IndentationError: unexpected indent
feat.setAttribute('description', attributeDescription)
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
feat.setAttribute('description', attributeDescription)
^
IndentationError: unexpected indent
if featureType == 'LineString':
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
if featureType == 'LineString':
^
IndentationError: unexpected indent
(res, outFeats) = lineLayer.dataProvider().addFeatures([feat])
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
(res, outFeats) = lineLayer.dataProvider().addFeatures([feat])
^
IndentationError: unexpected indent
elif featureType == 'Point':
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
elif featureType == 'Point':
^
IndentationError: unexpected indent
(res, outFeats) = pointLayer.dataProvider().addFeatures([feat])
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
(res, outFeats) = pointLayer.dataProvider().addFeatures([feat])
^
IndentationError: unexpected indent
elif featureType == 'Polygon':
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
elif featureType == 'Polygon':
^
IndentationError: unexpected indent
(res, outFeats) = polygonLayer.dataProvider().addFeatures([feat])
Traceback (most recent call last):
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 168, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
raise err1
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\PROGRA1\QGIS31.12\apps\Python37\lib\codeop.py", line 133, in call
codeob = compile(source, filename, symbol, self.flags, 1)
File "", line 1
(res, outFeats) = polygonLayer.dataProvider().addFeatures([feat])
^
IndentationError: unexpected indent
turnOnLabels(pointLayer, '"title" || ' - ' || "textvalue"')
turnOnLabels(lineLayer, '"title"')

@ewsterrenburg
Copy link
Author

@estudiocad
Best to open the Editor and run from there:

image

@estudiocad
Copy link

hi, sorry for my ignorance, I was running it directly without the editor opening. I appreciate your willingness to help me. excuse my English. It works perfect. You helped me a lot. Oscar

@alex-myp
Copy link

alex-myp commented Feb 3, 2023

This is great, and exactly the sort of thing I'm looking for to download annotations from mapstore and view in QGIS, for eventual export to Geoserver. I'm wondering if there is a limit to the number of annotations it can handle, I've got an annotations dataset with several hundred annotations (point, line, and polygon), and it only seems to output some of them (105 polygons, not points or lines). Wondering if you have any advice on how to handle such a large volume of data. Screenshot with error for reference:
Screenshot 2023-02-03 163132

@ewsterrenburg
Copy link
Author

@alex-myp good to see this gist is useful :)

Larger amounts of annotations should not be a problem, it seems that something else is not going as it should be (an annotation with empty geometry perhaps?). If you're ok with sharing the input data, I could have a check.

@alex-myp
Copy link

alex-myp commented Feb 6, 2023

@ewsterrenburg than you for the reply! I have attached the input data, would really appreciate you taking a look. Thank you!

https://drive.google.com/file/d/119fNJiMeXBSQGjBoJ0jYrKyKTcXFGC28/view?usp=share_link

@ewsterrenburg
Copy link
Author

@alex-myp Thanks for your upload.

The good news: with some minor changes I'm able to see the large majority of your features.

The bad news:

  • There are two empty (sub?)-geometries in your data that the script does not handle well. --> When looking at things, I doubt all geometry edge cases are handled well as is, at least I will add a check to ignore/log those occurrences.
  • It is reporting some (≈100) cases of the text being longer than the default 255 characters --> I'll change this.
  • Seems in current QGIS the syntax for the label placement has changed --> I'll change this.

image

@ewsterrenburg
Copy link
Author

@alex-myp made some changes, now it should also work with your dataset.

@alex-myp
Copy link

alex-myp commented Feb 6, 2023

@ewsterrenburg thanks for the quick changes, my data is loading in QGIS now without issues. Appreciate the help with this, keep up the good work :)

@roelsantiago
Copy link

Good morning! Appreciate for the code as JSON cannot be loaded directly in QGIS. I would like to inquire how to do it in the python console? What are the things I need to change in the code to be able to run them in the console? sorry another newbie in python. Thanks in advance!

@ewsterrenburg
Copy link
Author

@roelsantiago
Should be as simple as:

  1. Download this script
  2. Open the Python console
  3. Choose "Show editor"
  4. Open the script
  5. Change to path to where your json file is located
  6. Run

image

@roelsantiago
Copy link

Success. great guide! Just a follow up, I notice in the python console, there's an option to "Add script Toolbox" Can this be implemented? then simply run the tools in the same manner like running a buffer? Big thanks!

@ewsterrenburg
Copy link
Author

ewsterrenburg commented Jun 28, 2023

@roelsantiago There's a few minor differences, yet indeed it can be implemented (in fact, this gist originally started as a plugin)
I included the improvements the current version and made it available here
https://drive.google.com/file/d/1vpJbGGb9Dq3U00u5rJ1bN5YGrcmg6BF3/view?usp=sharing

@roelsantiago
Copy link

Thanks for the update! Tested the plugin in QGIS 3.32 and works perfectly fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment