<div dir="ltr">Ok, I figured it out on my own. Here're the code snippets for computing the center of a layer in geographic coordinates, and then doing the same for the current viewport.<div><br></div><div>First by layer:<br><div><br></div><div><div> layerCenter = layer.extent().center()</div><div><br></div><div> if layer.crs().geographicFlag() : # if already lat/long, do nothing<br></div><div> return layerCenter</div><div> else :</div><div> sourceCRC = layer.crs() # get the coordinate reference system being used</div><div> # 4326 is WGS 84</div><div> coordinateTransform = QgsCoordinateTransform(sourceCRC.toWkt(), QgsCoordinateReferenceSystem(4326).toWkt())</div><div> return coordinateTransform.transform(layerCenter)</div><div><br></div></div></div><div>And now the center of the viewport in geographic coordinates:</div><div><br></div><div><div> layerCenter = mapCanvas.extent().center()</div><div><br></div><div> if mapCanvas.mapSettings().hasCrsTransformEnabled() : # if projected, do inverse projection to geographic coordinates<br></div><div> sourceCRC = mapCanvas.mapSettings().destinationCrs() # get the coordinate reference system being used</div><div> # 4326 is WGS 84</div><div> coordinateTransform = QgsCoordinateTransform(sourceCRC.toWkt(), QgsCoordinateReferenceSystem(4326).toWkt())</div><div> inverseLayerCenter = coordinateTransform.transform(layerCenter)</div><div> return inverseLayerCenter<br></div><div> else :</div><div> return layerCenter</div></div><div><br></div><div><br></div><div>Do you think these would make for good additions to the <a href="http://docs.qgis.org/2.2/en/docs/pyqgis_developer_cookbook/snippets.html">"Code Snippets" section</a> for the online documentation?</div><div><br></div><div>Cheers,</div><div><br></div><div>Mark</div><div class="gmail_extra">-- <br><a href="mailto:mcoletti@gmail.com" target="_blank">mcoletti@gmail.com</a><br><div><br></div>
</div></div>