Getting a county by ID

After retrieving all counties using the get_counties function, the ID of a specific county can be passed to this function. Using session.query.(County).get(county_id) allows for the retrieval of the county of interest:

@app.route('/nba/api/v0.1/county/<int:county_id>', methods=['GET'])def get_county(county_id):  county = session.query(County).get(county_id)  shp = to_shape(county.geom)  geojson = shapely.geometry.geo.mapping(shp)  data = [{"type": "Feature",  "properties":{"name":county.name, "state":county.state.name},   "geometry":{"type":"MultiPolygon",   "coordinates":[geojson]},  }]  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.