[Qgis-user] PyQGIS: Get project extent outside QGIS

Jorge Gustavo Rocha jgr at geomaster.pt
Mon Nov 18 08:55:41 PST 2019


Hi,

I want to read a project file and get the extent when the project was saved.

In QGIS,

iface.mapCanvas().extent()

does exactly what I want. But I want to run a script outside QGIS.

I've already tried:

QgsApplication.setPrefixPath("/usr", True)
qgs = QgsApplication([], False)
# Load providers
qgs.initQgis()

project = QgsProject.instance()
project.read(projectpath)

print(project.crs().authid())

root = project.layerTreeRoot()
canvas = QgsMapCanvas()

bridge = QgsLayerTreeMapCanvasBridge(root, canvas)

print(canvas.extent())
print(canvas.fullExtent())
print(canvas.mapSettings().extent())
print(canvas.mapSettings().fullExtent())
print(canvas.mapSettings().visibleExtent())

All of these extents are much wider than the canvas extent when the
project was saved.

The extent is there, in the project file. I can read it as XML and get
the extent.

  <mapcanvas annotationsVisible="1" name="theMapCanvas">
    <units>meters</units>
    <extent>
      <xmin>-22630.49319827121144044</xmin>
      <ymin>208610.29666395089589059</ymin>
      <xmax>-22314.35674389618725399</xmax>
      <ymax>208889.62023152899928391</ymax>
    </extent>

But I would like to use the API.

Thanks in advance,

Jorge

-- 
Email Signature
Logo <https://www.geomaster.pt> 	
*Geomaster*
*Jorge Gustavo Rocha* | Software Engineer
*e:*jgr at geomaster.pt | *m:*+351 910 333 888
*g:*41.54094,-8.40490 | *v: *510 906 109
*a: * Rua António Cândido Pinto, 67, 4715-400 Braga

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20191118/a6812ac7/attachment.html>


More information about the Qgis-user mailing list