I've got a bunch of GIS tables in my model that I created in flaskSQLAlchemy. Each of these models has a 'geom' field which is a WKB object. Which need to be JSON serialized into WKT or geojson, So that The API GET call would work.
I tried to use geoalchemy2 functions, but I'm stuck. I use a flask marshmallow/marshmallow-sqlalchemy combo, and I tried something like the following, with no luck.
from geoalchemy2 import functions
from marshmallow import fields
class WKTSerializationField(fields.Field):
def _serialize(self, value, attr, obj):
if value is None:
return value
else:
if type(value).__name__ == 'WKBElement':
return functions.ST_AsEWKT(value)
else:
return None
class GISModelTableSchema(ma.ModelSchema):
class Meta:
model = GISModelTable
geom = WKTSerializationField(attribute="geom")
Please provide a code example if you can, how to serialize/deserialize a field in marshmallow alchemy. Or any answer is welcomed at this point.