A geospatial query

By adding one more URL component, the API is spatially enabled. Passing an arena ID and adding "/intersect" will use spatial queries to find data describing the requested NBA Arena. In this view function, the County and District tables are queried using an intersect filter (that is, the county containing the arena is identified using a point in polygon function). The underlying state is retrieved using a table relation between the county and the state. All of the geometry and the selected fields are returned:

@app.route('/nba/api/v0.1/arena/<int:arena_id>/intersect', methods=['GET'])
def arena_intersect(arena_id):
arena = session.query(Arena).get(arena_id)
county = session.query(County).filter(County.geom.ST_Intersects(arena.geom)).first()
district=session.query(District).filter(District.geom.ST_Intersects(arena.geom))
district = district.first()
if county != None:
data = [{"type": "Feature", "properties": {"name":arena.name, "id":arena.id,} ,
"geometry":{"type":"Point", "coordinates":[round(arena.longitude,6), round(arena.latitude,6)]},
},{"type": "Feature", "properties": {"name":county.name, "id":county.id,} ,
"geometry":{"type":"MultiPolygon",
"coordinates":[shapely.geometry.geo.mapping(to_shape(county.geom))]},
},{"type": "Feature", "properties": {"name":district.district, "id":district.id,},
"geometry":{"type":"MultiPolygon",
"coordinates":[shapely.geometry.geo.mapping(to_shape(district.geom))]},
},{"type": "Feature", "properties": {"name":county.state_ref.name, "id":county.state_ref.id,}, "geometry":{"type":"MultiPolygon",
"coordinates":[shapely.geometry.geo.mapping(to_shape(county.state_ref.geom))]},
}]
return jsonify({"type": "FeatureCollection","features":data})
else:
return redirect('/nba/api/v0.1/arena/' + str(arena_id))

To ensure that the function is valid, the if conditional checks if the arena is inside a US county; if not, the county, district, and state objects are not used. Instead, the request is redirected to the non-geospatial query view function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset