BreadFish
BreadFish

Reputation: 85

Return valid GeoJSON with Flask + GeoAlchemy2 app

Since few days I am wondering how to make my Flask app return valid GeoJSON, here is what I got so far:

models.py

class Building(base):

    __tablename__ = 'buildings'

    id = Column(Integer, primary_key=True)
    district = Column(Unicode)
    address = Column(Unicode)
    name = Column(Unicode)
    building_type = Column(Unicode)
    mpoly = Column(Geometry('MULTIPOLYGON'))

    building = relationship("User")


# this part is done as answered here: https://stackoverflow.com/questions/41684512/cant-transform-geometry-to-geojson
    def building_to_dict(self):
        return mapping(shapely.wkb.loads(str(self.mpoly), True))
    
    def __str__(self):
        d = dict()
        d["id"] = self.id
        d["district"] = self.district
        d["address"] = self.address
        d["name"] = self.name
        d["building_type"] = self.building_type
        d["mpoly"] = self.building_to_dict()
        return shapely.dumps(d)

Now at main file I have following routing:

app.py

@app.route('/geojson')
def get_json():
    features = session.query(Building.mpoly.ST_AsGeoJSON()).all()
    return jsonify(features)

And here are my two problems:

1) Returned JSON-like response that looks like following: "{\"type\":\"MultiPolygon\",\"coordinates\":[[[[16.8933137,52.471446],...,]]]}" Is not proper GeoJSON. Features variable before jsonify looks this way: [('{"type":"MultiPolygon","coordinates":[[[[16.914159616,52.473822807],...,]]]}',)]

2) How my GeoAlchemy query should look like, to return not just geometry field, but others as well?

Any kind of hints or helps highly appreciated, thanks in advance!

Upvotes: 0

Views: 1418

Answers (1)

James Nur
James Nur

Reputation: 33

You may use marshmallow-sqlalchemy for creating json like responses.

create schemas.py

    #!schemas.py
    from marshmallow import fields
    from marshmallow_sqlalchemy import ModelSchema
    from app.models import Building
    from geoalchemy.shape import to_shape


    class BuildingSchema(ModelSchema):
        id = fields.Integer()
        district = fileds.Unicode()
        address = fileds.Unicode()
        name = fields.Unicode()
        building_type = fields.Unicode()
        mpoly = fileds.Method("geom_to_json")
    
        @staticmethod
        def geom_to_json(obj):
            mpoly = to_shape(obj.mpoly)
            return {
                    lat: mpoly.y,
                    lon: mpoly.x,
                    #and so on ... 
                    } 
        
        class Meta:
            model = Building
            exclude = ("mpoly")
            

    bulding_schema = BuildingSchema(many=True)
    

after this you can use it in your views(routes)

    from app.schemas import building_schema 


    @app.route('/geojson')
    def json():
        buildings = Buildings.query.all()
        response = building_schema.dumps(buildings)
        
        return response

Upvotes: 1

Related Questions