Getting a district by ID

Passing the integer district ID will return only the requested representation of the district. The geometry is converted to GeoJSON format using shapely and the to_shape method from geoalchemy2.shape:

@app.route('/nba/api/v0.1/district/<int:district_id>', methods=['GET'])def get_district(district_id):  district = session.query(District).get(district_id)  shp = to_shape(district.geom)  geojson = shapely.geometry.geo.mapping(shp)  data = [{"type": "Feature",  "properties":{"district":district.district,"id":district.id},   "geometry":{"type":"MultiPolygon",   "coordinates":[geojson['coordinates']]},  }]  return jsonify({"type": "FeatureCollection","features":data})

Get Mastering Geospatial Analysis with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.