I have the coordinates of a geographical point in WGS84 and I want to show it on the map on a separate layer (so that it does not belong to the map layer).
Then I would like to give it a dot symbol of my choice.
How do I achieve this in QGIS with Python?
2 Answers 2
You can use the QgsVertexMarker
and QgsRubberBand
classes to show points on the map canvas as temporary markers which are not part of a layer. Here are are a couple of short examples to get you started.
Vertex Marker:
canvas = iface.mapCanvas()
lon = 131.2
lat = -12.5
pnt = QgsPointXY(lon, lat)
m = QgsVertexMarker(canvas)
m.setCenter(pnt)
m.setColor(QColor('Black'))
m.setIconType(QgsVertexMarker.ICON_CIRCLE)
m.setIconSize(12)
m.setPenWidth(1)
m.setFillColor(QColor(0, 200, 0))
#run line below to remove:
#canvas.scene().removeItem(m)
Rubber Band:
canvas = iface.mapCanvas()
lon = 131.2
lat = -12.5
pnt = QgsPointXY(lon, lat)
geom = QgsGeometry().fromPointXY(pnt)
rb = QgsRubberBand(canvas, QgsWkbTypes.PointGeometry)
rb.setColor(QColor('Blue'))
rb.setIcon(QgsRubberBand.ICON_CIRCLE)
rb.setIconSize(10)
rb.setToGeometry(geom)
rb.show()
#run line below to remove:
#rb.reset()
See the following links:
I haven't tested it, but since QGIS 3.10 you should be able to use an SVG icon with a rubber band:
-
Thanks that helped a lot. I got stuck in docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/…, with an error that
QGis.Polygon
is not defined ...Bruno Vermeulen– Bruno Vermeulen2020年06月04日 05:56:52 +00:00Commented Jun 4, 2020 at 5:56
The following script adds a memorylayer
with one point at lat/lon 20/20:
layer = QgsVectorLayer('Point?crs=epsg:4326', 'MyPoint' ,'memory')
pr = layer.dataProvider()
pt = QgsFeature()
point1 = QgsPointXY(20,20)
pt.setGeometry(QgsGeometry.fromPointXY(point1))
pr.addFeatures([pt])
layer.updateExtents()
QgsProject.instance().addMapLayer(layer)
you can set the symbol then with the normal QGIS stuff (F7->layer styling)
Explore related questions
See similar questions with these tags.