librelist archives

« back to archive

External functions in @hybrid_property (or other?)

External functions in @hybrid_property (or other?)

From:
Jón Ragnarsson
Date:
2015-06-26 @ 14:28
Greetings,

I'm working with GIS data and need to convert coordinates between systems.
I wanted to make my life a bit easier by adding a converter to a Location 
class:

class Location(db.Model):
    x = Column(Float)
    y = Column(Float)
    ...
    @hybrid_property
    def lat(self):
        return isn93towgs84(self.x, self.y)[0]

    @hybrid_property
    def lon(self):
        return isn93towgs84(self.x, self.y)[1]

Of course, I was hoping then to be able to do something like this:
    @hybrid_method
    def inside(self, west, north, east, south):
        if float(west) > self.lon > float(east) and float(north) > 
self.lat > float(south):
            return True
        else:
            return False
And finally:
db.session.query(Location).filter(Location.inside(west,north,east,south)).all()

Is this even possible?
SQLAlchemy apparently doesn't like using python functions with queries, it
keeps passing <sqlalchemy.orm.attributes.InstrumentedAttribute> to 
isn93towgs84() instead of the property value.
Or maybe the only solution is to convert the coordinates before the query 
and use the stored x,y values?