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

2014-09-11 Thread Gary Sherman

On 9/11/14 1:09 PM, Mark Coletti wrote:


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?


Hi Mark,

The snippets are contained in the documentation set:
https://github.com/qgis/QGIS-Documentation/blob/master/source/docs/pyqgis_developer_cookbook/snippets.rst

If you update snippets.rst it will get pushed to the website.

-gary

--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Gary Sherman

Founder, QGIS Project
Consulting: geoapt.com
Publishing: locatepress.com

We work virtually anywhere
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer


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

2014-09-11 Thread Mark Coletti
On Wed, Sep 10, 2014 at 7:05 AM, Leyan  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?
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer

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

2014-09-10 Thread Leyan

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.


Regards,

Leyan
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer


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

2014-09-08 Thread Mark Coletti
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.

First by layer:

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)

And now the center of the viewport in geographic coordinates:

layerCenter = mapCanvas.extent().center()

if mapCanvas.mapSettings().hasCrsTransformEnabled() : # if
projected, do inverse projection to geographic coordinates
sourceCRC = mapCanvas.mapSettings().destinationCrs() # get the
coordinate reference system being used
# 4326 is WGS 84
coordinateTransform = QgsCoordinateTransform(sourceCRC.toWkt(),
QgsCoordinateReferenceSystem(4326).toWkt())
inverseLayerCenter = coordinateTransform.transform(layerCenter)
return inverseLayerCenter
else :
return layerCenter


Do you think these would make for good additions to the "Code Snippets"
section

for the online documentation?

Cheers,

Mark
-- 
mcole...@gmail.com
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer

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

2014-09-08 Thread Mark Coletti
On Fri, Jun 13, 2014 at 7:24 PM, Nathan Woodrow  wrote:

> Hey Mark
>
> I think you are over thinking it ;)
>
> Iface.mapCanvas().extent() will give you the event of the view.
> Extent.center() for the middle.
>
> You can transform this using the coordinate transformation classes.
>
>
> That'd be nice ... if I could get a hold of the CRS that the map canvas is
using.  So, I can get the center point, but I'd like to transform it to
geographic coordinates, but do not know the CRS that the returned extent is
using.  I've grepped through the API and do not see how to get this
information.

(And, it would seem there is a delta between the documented API and the
actual API available.  Will the documented API be updated soon?)

Cheers,

Mark
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer

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

2014-06-13 Thread Nathan Woodrow
Hey Mark

I think you are over thinking it ;)

Iface.mapCanvas().extent() will give you the event of the view.
Extent.center() for the middle.

You can transform this using the coordinate transformation classes.

Nathan
On Jun 14, 2014 8:59 AM, "Mark Coletti"  wrote:

> First, long time no see!  Been a while since I've posted to this mailing
> list.  ;)
>
> I'm developing a plug-in that will allow a user to use the Twitter REST
> API to create a layer of tweets.  Yes, I already know of the articles on
> the net regarding how to do that manually.  However, I'm wrapping that
> functionality in a nice GUI to shield the lay user from messing about with
> code.
>
> I already have the plugin working with specified lat/lon coordinates and a
> given radius.  That's the easy part.  What I'd also like to do is have the
> user specify such things as "gimme the tweets for the current layer extent"
> or "gimme the tweets for just the area in the viewport."  I *suspect* that
> these are easy, but my first pass through the available high level
> documentation didn't give me a whole lot to steer by.
>
> For the first scenario, I would need to do the following:
>
>
>1. get the current layer
>2. get the MBR for said layer
>3. compute the centroid/midpoint
>4. project to lat/lon if necessary
>5. compute distance from midpoint to edge
>
> Step #1 would, I presume, entail talking to the singleton layer manager to
> get the currently selected layer.  Step #2 would presumably be available in
> the corresponding QgsLayer object.  Step #3 I could use the GEOS geometry
> ops to compute.  Step #4 would be a bit tricky since I'd have to first
> determine if reprojection was even necessary, and if so then jump through
> the hoops to get my lat/lon.  Step #5 I note the existence of QgsDistance,
> which I could use to compute the distance from the centroid to an edge of
> the layer MBR; doing a flat or ellipsoidal distance calculation would
> depend on the CRS of the selected layer, natch.
>
> My nagging feeling is "surely this, or a subset of these, is/are common
> use cases" ... and that there'd be corresponding syntactic sugar for
> implementing them.  The QgsDistance class is sort of an example since it
> presumably handles projection and geometric distance headaches for you in
> desired units.
>
> So, does there exist such a set of convenience classes/functions?
>  Similarly does there exist somewhere a set of corresponding code snippets?
>  (The official online code snippets don't cover these, alas.)
>
> Cheers!
>
> Mark
> --
> mcole...@gmail.com
>
>
> ___
> Qgis-developer mailing list
> Qgis-developer@lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer