Geographic Information Systems Asked by GisNoob on August 25, 2021
I am using QGIS 3.14 and the Python console (pyQGIS). I will be tracking a line from GPS co-ordinates and I want to be able to adjust the screen and zoom level accordingly. Here is my code:
canvas=iface.mapCanvas() #get a reference of the canvas where maps are displayed
#Drawing Polyline
polyline = QgsRubberBand(canvas, False) # False = not a polygon
points =[]
points.append(QgsPoint(-1232443,1318350))
points.append(QgsPoint(-1332443,1418350))
points.append(QgsPoint(-1432443,1518350))
polyline.setToGeometry(QgsGeometry.fromPolyline(points), None)
polyline.setColor(QColor(0, 128, 255))
polyline.setWidth(3)
#This is how I am adding points and plotting them. (Just for testing)
points.append(QgsPoint(-1532443,1618350))
polyline.setToGeometry(QgsGeometry.fromPolyline(points), None)
points.append(QgsPoint(-1632443,1718350))
polyline.setToGeometry(QgsGeometry.fromPolyline(points), None)
How can I adjust the screen size and zoomlevel accordingly?
In short you set canvas extent to bounding box of geometry. So following your lines of code you do it like this:
canvas.setExtent(polyline.asGeometry().boundingBox())
It might be good idea to zoom out a bit too like this:
canvas.zoomScale(round((canvas.scale() + canvas.scale()/10)/100)*100)
And sometimes you need to refresh canvas too to make sure you see all fresh:
canvas.refresh()
Answered by Miro on August 25, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP