我正在尝试创建一个使用API
烧瓶SQLAlchemy
,,,返回Marshmallow
。我希望能够使用任何地理对象(、、...)。PostGIS
GeoJson
FeatureCollection
Point
Polygone
我尝试了很多东西,但从未成功地重新创建GeoJson
FeatureCollection
格式。可以将形状强制为棉花糖模式吗?
这是 SQLAlchemy 模型:
class Locations(db.Model):
__tablename__ = 'locations'
id: int = db.Column(db.Integer, primary_key=True, autoincrement=True)
name: int = db.Column(db.String, nullable=False)
linename: str = db.Column(db.String, nullable=False)
point = db.Column(Geometry('POINT'))
这是我的棉花糖模式,
class LocationsSchema(SQLAlchemyAutoSchema):
point = fields.Method('wkt_to_geojson')
def wkt_to_geojson(self, obj):
return {'type': 'Feature', 'properties': {'linename': obj.linename}, 'geometry': shapely.geometry.mapping(to_shape(obj.point))}
class Meta:
model = Locations
locations_schema = LocationsSchema(many=True, only=["point"])
这是我的蓝图路线:
@map_data_blueprint.route('/locations', methods=['GET'])
def all_items():
locations = Locations.query.all()
serialized = locations_schema.dump(locations)
return jsonify(serialized)
这是我从 API 收到的 json:
[
{
"id": 2,
"point": {
"geometry": {
"coordinates": [
6.130649,
49.609332
],
"type": "Point"
},
"properties": {
"linename": "1"
},
"type": "Feature"
}
},
{
"id": 3,
"point": {
"geometry": {
"coordinates": [
6.126288,
49.598557
],
"type": "Point"
},
"properties": {
"linename": "1"
},
"type": "Feature"
}
}]
但我试图在这里获取格式FeatureCollection
Geojson
是一个例子。