<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <p>Hi,</p>
    <p>Answer to my own question: the order was wrong.<br>
    </p>
    <p>I need to call </p>
    <pre><span class="n">QgsLayerTreeMapCanvasBridge</span><span class="p"></span></pre>
    <p>before reading the project.</p>
    <p>This works:</p>
    <p><tt>QgsApplication.setPrefixPath("/usr", True)</tt><tt><br>
      </tt><tt>qgs = QgsApplication([], False)</tt><tt><br>
      </tt><tt># Load providers</tt><tt><br>
      </tt><tt>qgs.initQgis()</tt><tt><br>
      </tt><tt><br>
      </tt><tt>project = QgsProject.instance()</tt></p>
    <p><tt>root = project.layerTreeRoot()</tt><tt><br>
      </tt><tt>canvas = QgsMapCanvas()</tt></p>
    <p><tt>bridge = QgsLayerTreeMapCanvasBridge(root, canvas)</tt><tt><br>
      </tt></p>
    <p><tt> </tt><tt>project.read(projectpath)</tt></p>
    <p><tt>print(project.crs().authid()) </tt><tt><br>
      </tt></p>
    <p><tt>print(canvas.extent())</tt><tt><br>
      </tt><tt>print(canvas.fullExtent())</tt><tt><br>
      </tt><tt>print(canvas.mapSettings().extent())</tt><tt><br>
      </tt><tt>print(canvas.mapSettings().fullExtent())</tt><tt><br>
      </tt><tt>print(canvas.mapSettings().visibleExtent())</tt></p>
    <p>Sorry for the noise.</p>
    <p>Jorge<br>
    </p>
    <div class="moz-cite-prefix">On 18/11/19 16:55, Jorge Gustavo Rocha
      wrote:<br>
    </div>
    <blockquote type="cite"
      cite="mid:0fe3be23-ca09-6e50-e443-71d2ed087561@geomaster.pt">
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <p>Hi,</p>
      <p>I want to read a project file and get the extent when the
        project was saved.</p>
      <p>In QGIS, <br>
      </p>
      <p><tt>iface.mapCanvas().extent()</tt></p>
      <p>does exactly what I want. But I want to run a script outside
        QGIS.<br>
      </p>
      <p>I've already tried:</p>
      <p><tt>QgsApplication.setPrefixPath("/usr", True)</tt><tt><br>
        </tt><tt>qgs = QgsApplication([], False)</tt><tt><br>
        </tt><tt># Load providers</tt><tt><br>
        </tt><tt>qgs.initQgis()</tt><tt><br>
        </tt><tt><br>
        </tt><tt>project = QgsProject.instance()</tt><tt><br>
        </tt><tt>project.read(projectpath)</tt></p>
      <p><tt>print(project.crs().authid()) </tt><tt><br>
        </tt></p>
      <p><tt>root = project.layerTreeRoot()</tt><tt><br>
        </tt><tt>canvas = QgsMapCanvas()</tt></p>
      <p><tt>bridge = QgsLayerTreeMapCanvasBridge(root, canvas)</tt><tt><br>
        </tt></p>
      <p><tt>print(canvas.extent())</tt><tt><br>
        </tt><tt>print(canvas.fullExtent())</tt><tt><br>
        </tt><tt>print(canvas.mapSettings().extent())</tt><tt><br>
        </tt><tt>print(canvas.mapSettings().fullExtent())</tt><tt><br>
        </tt><tt>print(canvas.mapSettings().visibleExtent())</tt></p>
      <p>All of these extents are much wider than the canvas extent when
        the project was saved.</p>
      <p>The extent is there, in the project file. I can read it as XML
        and get the extent.</p>
      <p><tt>  <mapcanvas annotationsVisible="1"
          name="theMapCanvas"></tt><tt><br>
        </tt><tt>    <units>meters</units></tt><tt><br>
        </tt><tt>    <extent></tt><tt><br>
        </tt><tt>      <xmin>-22630.49319827121144044</xmin></tt><tt><br>
        </tt><tt>      <ymin>208610.29666395089589059</ymin></tt><tt><br>
        </tt><tt>      <xmax>-22314.35674389618725399</xmax></tt><tt><br>
        </tt><tt>      <ymax>208889.62023152899928391</ymax></tt><tt><br>
        </tt><tt>    </extent></tt><br>
        <br>
      </p>
      <p>But I would like to use the API.</p>
      <p>Thanks in advance,</p>
      <p>Jorge<br>
      </p>
      <div class="moz-signature">-- <br>
        <title>Email Signature</title>
        <meta content="text/html; charset=UTF-8"
          http-equiv="Content-Type">
        <table style="width: 525px; font-size: 11pt; font-family: Arial,
          sans-serif;" cellspacing="0" cellpadding="0">
          <tbody>
            <tr>
              <td style="text-align:center; font-size: 10pt;
                font-family: Arial, sans-serif; border-right: 1px solid;
                border-right-color: #fb6303; width: 125px;
                padding-right: 10px; vertical-align: top;" rowspan="6"
                width="125" valign="top"> <a
                  href="https://www.geomaster.pt" target="_blank"
                  moz-do-not-send="true"><img alt="Logo"
                    style="width:105px; height:auto; border:0;"
src="https://geomaster.pt/wp-content/uploads/2017/02/cropped-geomaster300x300-1.png"
                    moz-do-not-send="true" width="85" border="0"></a> </td>
              <td style="padding-left:10px">
                <table cellspacing="0" cellpadding="0">
                  <tbody>
                    <tr>
                      <td style="font-size: 10pt; color:#0079ac;
                        font-family: Arial, sans-serif; width: 400px;
                        padding-bottom: 5px; padding-left: 10px;
                        vertical-align: top;" valign="top"> <strong><span
                            style="font-size: 14pt; font-family: Arial,
                            sans-serif; color:#fb6303;">Geomaster</span></strong><br>
                        <strong><span style="font-size: 11pt;
                            font-family: Arial, sans-serif;
                            color:black;">Jorge Gustavo Rocha</span></strong>
                        <span style="font-family: Arial, sans-serif;
                          font-size:11pt; color:#000000;"><span
                            style="padding-right: 5px; padding-left:
                            5px;"> | </span>Software Engineer</span> </td>
                    </tr>
                    <tr>
                      <td style="font-size: 10pt; color:#444444;
                        font-family: Arial, sans-serif; padding-bottom:
                        5px; padding-top: 5px; padding-left: 10px;
                        vertical-align: top; line-height:17px;"
                        valign="top"> <span><span style="color:
                            #fb6303;"><strong>e:</strong></span><span
                            style="font-size: 10pt; font-family: Arial,
                            sans-serif; color:#000000;"> <a
                              class="moz-txt-link-abbreviated"
                              href="mailto:jgr@geomaster.pt"
                              moz-do-not-send="true">jgr@geomaster.pt</a></span></span>
                        <span><span style="padding-left:
                            5px;padding-right: 5px;"> | </span><span
                            style="color: #fb6303;"><strong>m:</strong></span><span
                            style="font-size: 10pt; font-family: Arial,
                            sans-serif; color:#000000;"> +351 910 333
                            888<br>
                          </span></span> <span><span style="color:
                            #fb6303;"><strong>g:</strong></span><span
                            style="font-size: 10pt; font-family: Arial,
                            sans-serif; color:#000000;">
                            41.54094,-8.40490</span></span> <span><span
                            style="padding-left: 7px;padding-right:
                            5px;"> | </span><span style="color:
                            #fb6303;"><strong>v: </strong></span><span
                            style="font-size: 10pt; font-family: Arial,
                            sans-serif; color:#000000; padding-left:
                            7px;"> 510 906 109<br>
                          </span></span> <span> <span style="color:
                            #fb6303;"><strong>a: </strong></span> </span>
                        <span> <span style="font-size: 10pt;
                            font-family: Arial, sans-serif; color:
                            #000000;"><span> </span>Rua António Cândido
                            Pinto, 67, 4715-400 Braga<br>
                          </span></span> </td>
                    </tr>
                  </tbody>
                </table>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <pre class="moz-quote-pre" wrap="">_______________________________________________
Qgis-user mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Qgis-user@lists.osgeo.org">Qgis-user@lists.osgeo.org</a>
List info: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-user">https://lists.osgeo.org/mailman/listinfo/qgis-user</a>
Unsubscribe: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-user">https://lists.osgeo.org/mailman/listinfo/qgis-user</a></pre>
    </blockquote>
    <div class="moz-signature">-- <br>
      <title>Email Signature</title>
      <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
      <table style="width: 525px; font-size: 11pt; font-family: Arial,
        sans-serif;" cellspacing="0" cellpadding="0">
        <tbody>
          <tr>
            <td style="text-align:center; font-size: 10pt; font-family:
              Arial, sans-serif; border-right: 1px solid;
              border-right-color: #fb6303; width: 125px; padding-right:
              10px; vertical-align: top;" rowspan="6" width="125"
              valign="top"> <a href="https://www.geomaster.pt"
                target="_blank"><img alt="Logo" style="width:105px;
                  height:auto; border:0;"
src="https://geomaster.pt/wp-content/uploads/2017/02/cropped-geomaster300x300-1.png"
                  width="85" border="0"></a> </td>
            <td style="padding-left:10px">
              <table cellspacing="0" cellpadding="0">
                <tbody>
                  <tr>
                    <td style="font-size: 10pt; color:#0079ac;
                      font-family: Arial, sans-serif; width: 400px;
                      padding-bottom: 5px; padding-left: 10px;
                      vertical-align: top;" valign="top"> <strong><span
                          style="font-size: 14pt; font-family: Arial,
                          sans-serif; color:#fb6303;">Geomaster</span></strong><br>
                      <strong><span style="font-size: 11pt; font-family:
                          Arial, sans-serif; color:black;">Jorge Gustavo
                          Rocha</span></strong> <span
                        style="font-family: Arial, sans-serif;
                        font-size:11pt; color:#000000;"><span
                          style="padding-right: 5px; padding-left: 5px;">
                          | </span>Software Engineer</span> </td>
                  </tr>
                  <tr>
                    <td style="font-size: 10pt; color:#444444;
                      font-family: Arial, sans-serif; padding-bottom:
                      5px; padding-top: 5px; padding-left: 10px;
                      vertical-align: top; line-height:17px;"
                      valign="top"> <span><span style="color: #fb6303;"><strong>e:</strong></span><span
                          style="font-size: 10pt; font-family: Arial,
                          sans-serif; color:#000000;"> <a class="moz-txt-link-abbreviated" href="mailto:jgr@geomaster.pt">jgr@geomaster.pt</a></span></span>
                      <span><span style="padding-left:
                          5px;padding-right: 5px;"> | </span><span
                          style="color: #fb6303;"><strong>m:</strong></span><span
                          style="font-size: 10pt; font-family: Arial,
                          sans-serif; color:#000000;"> +351 910 333 888<br>
                        </span></span> <span><span style="color:
                          #fb6303;"><strong>g:</strong></span><span
                          style="font-size: 10pt; font-family: Arial,
                          sans-serif; color:#000000;"> 41.54094,-8.40490</span></span>
                      <span><span style="padding-left:
                          7px;padding-right: 5px;"> | </span><span
                          style="color: #fb6303;"><strong>v: </strong></span><span
                          style="font-size: 10pt; font-family: Arial,
                          sans-serif; color:#000000; padding-left: 7px;">
                          510 906 109<br>
                        </span></span> <span> <span style="color:
                          #fb6303;"><strong>a: </strong></span> </span>
                      <span> <span style="font-size: 10pt; font-family:
                          Arial, sans-serif; color: #000000;"><span> </span>Rua
                          António Cândido Pinto, 67, 4715-400 Braga<br>
                        </span></span> </td>
                  </tr>
                </tbody>
              </table>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </body>
</html>