我有下面的SQLAlchemy模型。
class LocationModel(db.Model, BaseModel):
"""
Location Model
"""
__tablename__ = 'location'
geolocation = db.Column(ga.Geometry('POINT', dimension=2, srid=4326))
def __init__(self, data):
self.geolocation = self.__format_geolocation()
# variables for other functions
self.result_quantity = data.get('result_quantity')
def save_event_local(self):
db.session.add(self)
db.session.flush()
def __format_geolocation(self):
if self.latitude or self.longitude == None:
return None
else:
return 'SRID=4326;POINT({} {})'.format(self.longitude, self.latitude)我想返回这样的原始数据(棉花糖):
class LocationSchema(Schema):
"""
Location Schema
"""
geolocation = fields.Raw()不幸的是,我得到了以下错误:
TypeError: Object of type WKBElement is not JSON serializable我不知道为什么会这样。
如果我知道如何序列化反序列化Geoalchemy2对象,或者可能只是返回原始数据,以便可以在前端使用它。
谢谢,
发布于 2022-07-05 10:59:13
通过以下操作,我终于能够解决我的问题:
from geoalchemy2 import functions
functions.ST_AsGeoJSON(LocationModel.geolocation).label('geolocation'))这给了我一个产出:
{
"geolocation": "{\"type\":\"Point\",\"coordinates\":[-70.77919769,-27.26119995]}"
}https://stackoverflow.com/questions/72818060
复制相似问题