By checking for a URL argument in the request.args dictionary, and then checking if the argument evaluates as true, we can determine if all of the state geometries should be returned. The GeoJSON response is generated from the state's geometry by using the to_shape function and the shapely.geometry.geo.mapping (shortened to smapping) function:
@app.route('/nba/api/v0.1/state', methods=['GET'])
def get_states():
smapping = shapely.geometry.geo.mapping
states = session.query(State).all()
data = [{"type": "Feature",
"properties":{"state":state.name,"id":state.id},
"geometry":{"type":"MultiPolygon",
"coordinates":"[Truncated]"},
} for state in states]
if "geometry" in request.args.keys():
if request.args["geometry"]=='1' or request.args["geometry"]=='True':
data = [{"type": "Feature",
"properties":{"state":state.name,"id":state.id},
"geometry":{"type":"MultiPolygon",
"coordinates":[smapping(to_shape(state.geom))["coordinates"]]},
} for state in states]
return jsonify({"type": "FeatureCollection","features":data})
If the geometry argument or parameter is not included, the geometry will be represented as truncated.