Interactive widgets

At the beginning of the chapter, you learned how to query and map incidents based on a date. In Jupyter, you can use interactive widgets to change values. The code will help us in how you can use ipywidgets to import interact, which will allow you to insert a DatePicker so that you can select a date to interact with the Notebook:

The previous code imports interact and the DatePicker widget. At its simplest, the previous screenshot shows a decorator and function to allow interactively selecting a date and displaying it as a string.

When the DatePicker changes, x (the DatePicker) is sent to the function theDate(x), and x is printed as a string. The actual return value is datetime.date.

Using the DatePicker widget, you can pass a date value to an SQL query, and then map the results. When the DatePicker changes, you can erase the map and then display the new results. The following code will show you how:

from ipywidgets import interact, interactive, fixed, interact_manual,DatePicker
import ipywidgets as widgets

@widgets.interact(x=DatePicker())
def theDate(x):

if x:
for l in map.layers[1:]:
map.remove_layer(l)
nohyphen=str(x).replace("-","")
d=datetime.datetime.strptime(nohyphen,'%Y%m%d').date()
cursor.execute("SELECT ST_AsGeoJSON(geom) from incidents where date
= '{}' ".format(str(d)))
c=cursor.fetchall()

for x in c:
layer=json.loads(x[0])
layergeojson=GeoJSON(data=layer)
map.add_layer(layergeojson)
return len(c)

else:
pass

The previous code creates an interactive DatePicker widget. The code has an if...else statement because, on the first pass, x will be none. The DatePicker is not selected, so we pass on the first go around.

Next, the code grabs all the layers on the map, and removes them using map.remove_layer(), starting at the second ([1:]) layer. Why the second layer? Because the first layer on the map is the TileLayer—the basemap. We want that to stay, and only remove the markers that were added from the SQL query.

The code then strips the hyphens from the date string and converts it into a datetime. Once it is a datetime, you can pass it to the SQL query.

The next code block is the same block you have used throughout this chapter to add the query results to the map. 

Selecting a date of November 2, 2017, is shown in the following screenshot: 

And when selecting November 8, 2017, the map is redrawn and shown in the following screenshot:

These screenshots were generated immediately following the reselection of a date. A user can use a DatePicker drop-down to requery the data in your PostGIS database.

In Jupyter, if you set the value of a variable to a string or an integer, you will get a number slider or a text box. In the following screenshot, the decorator has x="None", with None being a string. The text None is a placeholder to create the text box. This creates a text box with the word None in it:

The code in the previous screenshot is presented as follows. The code will allow you to type the name of an area command, and then display the incidents within that area command:

@widgets.interact(x="None")
def areaCommand(x):
if x:
for l in map.layers[1:]:
map.remove_layer(l)
cursor.execute("SELECT ST_AsGeoJSON(i.geom) FROM incidents i
JOIN areacommand acmd ON
ST_Intersects(acmd.geom, i.geom) WHERE acmd.name like'{}' and
date >= NOW() - interval '10
day';".format(x))
c=cursor.fetchall()

for x in c:
layer=json.loads(x[0])
layergeojson=GeoJSON(data=layer)
map.add_layer(layergeojson)
return c
else:
pass

The previous code starts with the decorator and a string. This will draw the text box. The areaCommand() function acts exactly as the date example mentioned earlier, but passes a string to the SQL query. It returns the results of the query, and draws the incidents on the map. 

The following screenshot shows the return values for NORTHWEST:

 

The following screenshot shows the map when the user types NORTHWEST in the text box:

In this section, you have learned how to perform queries on your spatial data, and how to map the results. In the next section, you will learn how to chart the results of your queries.

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

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