1

I have a very simple problem, but I can't figure out how to achieve this in QGIS using Python.

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
  • 1
    what's the purpose of this ?
    – Snaileater
    May 4, 2020 at 13:01
  • Purpose: via a postal code api I get a coordinate that I want to put in the map.
    – SWP_IB
    May 4, 2020 at 13:37

2 Answers 2

8

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:

https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/canvas.html#rubber-bands-and-vertex-markers

https://qgis.org/api/classQgsVertexMarker.html

https://qgis.org/api/classQgsRubberBand.html

I haven't tested it, but since QGIS 3.10 you should be able to use an svg icon with a rubber band:

https://qgis.org/api/classQgsRubberBand.html#a4b45f866c927ca488bdb9eddda678830

1
5

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)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.