Representation of coordinates in GeoAlchemy2

To extend my restfull api with GPS locations, I decided to try geo-allechia. I already have a database, and I think it saves points in my database already. However, every time I try to print a point that I saved (for example, to return to the user), I get a memory address or something like this:

<WKBElement at 0x7ffad5310110; '0101000000fe47a643a7f3494049f4328ae5d61140'> 

This is pretty useless for a programmer without an initiator or for a user.

I have a simple question. How do I represent a Geometry object in a human-readable form, for example:

  POINT(40.5563 30.5567) 
+6
source share
1 answer

It will be in the format W ell- K inown B ; you can use geoalchemy2.functions.ST_AsText to convert them to WKT text format.

This will work in the database itself, so you must apply this to your query in order to query the results in WKT instead of WKB; that is, in your SQLAlchemy you choose

 Model.column.ST_AsText() 

or

 ST_AsText(Model.column) 

For databaseless conversions between WKT and WKB, you can use the shapely module. Note that wkb functions require binary, not hex:

 from shapely import wkb, wkt from binascii import unhexlify >>> binary = unhexlify(b'0101000000fe47a643a7f3494049f4328ae5d61140') >>> binary b'\x01\x01\x00\x00\x00\xfeG\xa6C\xa7\ xf3I@I \xf42\x8a\xe5\xd6\ x11@ ' >>> point = wkb.loads(binary) >>> point.x, point.y (51.903542, 4.45986) >>> wkt.dumps(point) 'POINT (51.9035420000000016 4.4598599999999999)' 
+12
source

Source: https://habr.com/ru/post/983859/


All Articles