[Qgis-developer] Getting center of layer in lat/long

Mark Coletti mcoletti at gmail.com
Thu Sep 11 14:09:09 PDT 2014


On Wed, Sep 10, 2014 at 7:05 AM, Leyan <ouyang.leyan.ml at hotmail.com> wrote:

> On 09/09/2014 05:36 AM, Mark Coletti wrote:
>
>>         layerCenter = layer.extent().center()
>>
>>         if layer.crs().geographicFlag() : # if already lat/long, do
>> nothing
>>             return layerCenter
>>         else :
>>             sourceCRC = layer.crs() # get the coordinate reference system
>> being used
>>             # 4326 is WGS 84
>>             coordinateTransform = QgsCoordinateTransform(sourceCRC.toWkt(),
>> QgsCoordinateReferenceSystem(4326).toWkt())
>>             return coordinateTransform.transform(layerCenter)
>>
>>  If you want to be rigorous, there are different lat/long coordinate
> system, it is not only WGS84. The difference will be small of course, but I
> do not see the point of specifically testing for this case while it does
> not add anything.


I'm aware that there exist myriad geographic coordinate systems.  However,
I'm as yet aware of a QgsCoordinateTransform that will allow me to convert
from a projected coordinate reference system to a "gee, I don't care,
whatever geographic coordinate system."  I just referenced a popular
geographic coordinate system because it was good enough for my purposes.

However, this may be a valid concern if this does become a snippet on the
web site.  So, I'll ask again: how do I get this code added as a snippet to
the web site since it's likely a common use case for plugin development?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20140911/3216fd75/attachment.html>


More information about the Qgis-developer mailing list