need help with geoalchemy2 reading geometry

527 views
Skip to first unread message

Brian Wilson

unread,
Sep 18, 2013, 9:25:28 PM9/18/13
to geoal...@googlegroups.com
Patience with newbie requested...
There are so few examples that I have been pounding my head against the wall trying to get a handle on geoalchemy2.

Currently I simply need to access the geometry of a point.. the lat and the lon values.
I have been able to put points into a PostGIS database using geoalchemy2 but now I need to get them back out.
Seems like it should be very very simple but nothing I have tried works.

        addr = model.get_address(gid) # code I wrote that returns an object with geometery in geom
        geom = addr.geom.wkt
        print 'geometry',geom
        geometry <sqlalchemy.sql.expression._FunctionGenerator object at 0x7ffaec02bc90>

        geom = func.ST_AsText(address.geom)
        print 'wkt', geom
        wkt ST_AsText(ST_GeomFromWKB(:ST_GeomFromWKB_1, :ST_GeomFromWKB_2))

Should I even be using geoalchemy2? Seems like there is more chance to get up to speed with geoalchemy because docs seem more complete.

I can create a geometry and save it like this
    feature = WKTElement('POINT(%f %f)' % lonlat, 4326)
    addr.geom = feature

I can see my points in psql

 select ST_AsText(geom) from demo_sacramento.dispatch_address;
                 st_astext                
-------------------------------------------
 POINT(-121.485641535929 38.5787155607204)
 POINT(-121.502793900221 38.5808477185013)
 POINT(-121.491743028093 38.5739087832124)
 POINT(-121.491590196883 38.5833198395094)
 POINT(-121.489603391149 38.5692303677729)
 POINT(-121.501148025649 38.5725668720257)
 POINT(-121.497045095465 38.5801952114188)
(7 rows)




Thanks Brian

Brian Wilson

unread,
Sep 19, 2013, 1:30:13 PM9/19/13
to geoal...@googlegroups.com
In the grand tradition of mailing lists, answering my own question,
I suspect there is some obvious direct way to do this but for now I can use the shapely interface

from geoalchemy2.shape import to_shape

address = model.get_address(gid)
point = to_shape(address.geom)
print "LatLon:", point.y, point.x


LatLon: 38.5808477185 -121.5027939


This allows me to move my project along for now just fine.

Brian

Eric Lemoine

unread,
Sep 20, 2013, 2:57:47 AM9/20/13
to geoal...@googlegroups.com
Hi


point_shape = to_shape(addr.geom)
print point_shape.x, point_shape.y

Another option is to request the x and y values from PostGIS directly. For example:

session.query(func.ST_X(Address.geom), func.ST_Y(Address.geom))

Cheers,


--
Eric

Eric Lemoine

unread,
Sep 23, 2013, 2:39:40 PM9/23/13
to geoal...@googlegroups.com
Didn't you see my response?

--
Eric
Reply all
Reply to author
Forward
0 new messages