queryarena function

The arena function below selects a random arena for the initial GET request, getting data from the database model about the selected NBA arena. The queries themselves are handled by the queryarena function.

In this function, the name of the selected arena is accepted as a parameter. It is used to query (or filter) all of the Arenas model objects. This object-relational mapping (ORMfilter method requires a field as a parameter; in this case, the field is called name1. As an example of what the filter is doing,  if the name of the arena is Oracle Arena, the filter translated to English would be find all NBA arenas with the name Oracle Arena. The results of the filter method are returned as a list, so the first result is retrieved from the list using zero-indexing. A result is an object representing the data row from the Arenas class that met the filter parameters:

def queryarena(name):
arena = Arenas.objects.filter(name1=name)[0]
state = US_States.objects.filter(geom__intersects=arena.geom)
if state:
state = state[0]
county = Counties.objects.filter(geom__contains=arena.geom)[0]
district = Districts.objects.filter(geom__contains=arena.geom)[0]
popup = "This arena is called " + arena.name1 + " and it's
located at "
popup += str(round(arena.geom.x,5))+ "," +
str(round(arena.geom.y,5) )
popup += "It is located in " +state.state + " and in the county
of " + county.county
popup += " and in Congressional District " + district.district
return arena.name1, arena.geom.y, arena.geom.x, popup
else:
return arena.name1, arena.geom.y, arena.geom.x, arena.name1 + "
is not in the United States"

Once the arena object is instantiated, its geometry field is used in a filter operation. Instead of using a field to filter, however, this filter uses geospatial analysis. Passing arena.geom to the geom__intersects method (provided by GeoDjango) performs an intersect operation to find the state in which the arena resides. An if/else conditional checks to ensure that the arena is located in the United States (for instance, not Toronto's arena) to determine the correct value to return.

If the arena is located inside the United States, the arena geometry is again used to determine the county and congressional district that contain the arena. This time, the geospatial operation is geom_contains. The filters return a county object and a district object. They are used to generate the custom popup that will be added to the map marker on the leaflet map. This popup contains the longitude and latitude of the arena, the name of the arena, and the name of its county, state, and the number of the congressional district within its state

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

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