How to get feat.geometry() from SpatiaLite in pyQGis ?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to get feat.geometry() from SpatiaLite in pyQGis ?

Geo DrinX
Hello all,


as in object, within pyQGis  I need to read the geometry of a SpatiaLite vector layer.

This is my source code:

layer = iface.mapCanvas().currentLayer()
iter = layer.getFeatures()
for feat in iter:
    geom = feat.geometry()
    elem = geom.asPolyline()
    print("ELEM %s\n") %(elem)    
    for p1 in elem:
        x1,y1 = p1.x(),p1.y()
        print("XY= %lf %lf\n") %(x1, y1)


But... it returns only:

ELEM []
ELEM []
ELEM []
ELEM []

etcetera, instead of something like this :

ELEM [(2.37786e+06,4.60302e+06), (2.37784e+06,4.60304e+06)]

... that I had with a "normal" qgis layer.

Sure, I miss something.

Somebody can help me to understand how to get geometry from SpatiaLite ?


Thank you in advance

Roberto




_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: How to get feat.geometry() from SpatiaLite in pyQGis ?

mmekuria
Hi, Roberto:

I think the query from layer.getFeatures() returns an iterator and that means you use the nextFeature(feat) to get the next feature until it fails to return a feature, then it means you have reached the end of the iterator list.

iter = layer.getFeatures()
while iter.nextFeature(feat):
    geom = feat.geometry()
    elem = geom.asPolyline()
    print("ELEM %s\n") %(elem)    
    for p1 in elem:
        x1,y1 = p1.x(),p1.y()
        print("XY= %lf %lf\n") %(x1, y1)


Should work for you,

Maaza Christos, PhD
www.Axumcorp.com
Reply | Threaded
Open this post in threaded view
|

Re: How to get feat.geometry() from SpatiaLite in pyQGis ?

giulianc
In reply to this post by Geo DrinX
Il giorno Thu, 5 Dec 2013 11:10:03 +0100
Geo DrinX <[hidden email]> ha scritto:

> Hello all,

Hi Roberto (sorry for my bad english :-)

> as in object, within pyQGis  I need to read the geometry of a
> SpatiaLite vector layer.
>
> This is my source code:
>
> layer = iface.mapCanvas().currentLayer()
> iter = layer.getFeatures()
> for feat in iter:
>     geom = feat.geometry()
>     elem = geom.asPolyline()
>     print("ELEM %s\n") %(elem)
>     for p1 in elem:
>         x1,y1 = p1.x(),p1.y()
>         print("XY= %lf %lf\n") %(x1, y1)
>
>
> But... it returns only:
>
> ELEM []
> ELEM []
> ELEM []
> ELEM []
> .....
> Somebody can help me to understand how to get geometry from
> SpatiaLite ?
>

I tried in python console on a Spatialite linestring layer:
        cLayer=iface.activeLayer()
        iter=cLayer.getFeatures()
        feat=iter.next()
        geom=feat.geometry()
        ln=geom.asPolyline()
        pnt=ln[0]
        print pnt.x(),pnt.y()
with the correct answer:
        1511590.31375 5099925.78455
(but I'm on a old 1.9.0-Master)


> Thank you in advance
>
> Roberto

bye,
giuliano

_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: How to get feat.geometry() from SpatiaLite in pyQGis ?

Geo DrinX
In reply to this post by mmekuria
Hi Christos (!),


sorry but your code works only in QGis 1.8 version.
I am using the 2.0.1 version and pyQGis methods are changed:

BTW, my problem is: 

- using a SHP file layer,  elements are returned
- using SpatiaLite layer,  elements are returned, but each is empty.


It 'a known issue?
Someone in the list has tried it?


Roberto


2013/12/6 mmekuria <[hidden email]>
Hi, Roberto:

I think the query from layer.getFeatures() returns an iterator and that
means you use the nextFeature(feat) to get the next feature until it fails
to return a feature, then it means you have reached the end of the iterator
list.

iter = layer.getFeatures()
while iter.nextFeature(feat):
    geom = feat.geometry()
    elem = geom.asPolyline()
    print("ELEM %s\n") %(elem)
    for p1 in elem:
        x1,y1 = p1.x(),p1.y()
        print("XY= %lf %lf\n") %(x1, y1)


Should work for you,





-----
Maaza Christos, PhD
www.Axumcorp.com

--
View this message in context: http://osgeo-org.1560.x6.nabble.com/How-to-get-feat-geometry-from-SpatiaLite-in-pyQGis-tp5092891p5093041.html
Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.
_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-developer


_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-developer