[Qgis-user] PyQGIS: Get project extent outside QGIS
Jorge Gustavo Rocha
jgr at geomaster.pt
Mon Nov 18 09:05:19 PST 2019
Hi,
Answer to my own question: the order was wrong.
I need to call
QgsLayerTreeMapCanvasBridge
before reading the project.
This works:
QgsApplication.setPrefixPath("/usr", True)
qgs = QgsApplication([], False)
# Load providers
qgs.initQgis()
project = QgsProject.instance()
root = project.layerTreeRoot()
canvas = QgsMapCanvas()
bridge = QgsLayerTreeMapCanvasBridge(root, canvas)
project.read(projectpath)
print(project.crs().authid())
print(canvas.extent())
print(canvas.fullExtent())
print(canvas.mapSettings().extent())
print(canvas.mapSettings().fullExtent())
print(canvas.mapSettings().visibleExtent())
Sorry for the noise.
Jorge
On 18/11/19 16:55, Jorge Gustavo Rocha wrote:
>
> 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
>
>
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
--
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/618e7db1/attachment.html>
More information about the Qgis-user
mailing list