Written by Frédéric Haziza
The code to produce would typically start by importing the necessary
functions and classes from the db
module.
from db import HomeDB
# Initilizing the database
database = HomeDB('uppsala.sqlite')
database.connect()
selection = database.select('rooms > 1 and rooms < 3 and area > 58 and rent < 3000')
database.disconnect()
# Looping now through the selection
for home in selection:
# Do something
# Then plot
The above code essentially initializes the database, fetches some selection, based on some criteria, and plots the result on a map. Yes, it’s visual, it’ fun.
You could
use the template file db.py
, that
you have to complete.
You can of course look at the documentation from the sourcecode
itself, using the help
function.
$ python
>>> import db
>>> help(db.plot)
The plot function has a few keyword parameters.
plot(selection,
output='selection.html',
special=None,
zoom=12,
latitude=59.83732598851705,
longitude=17.64549846959149,
radius=5000)
Outputs the selection to map file. Section is a list of HomeEntry.
The center is given by latitude and longitude and marked on the map with a blue marker.
The map draws a radius around the center.
When the special is used, an extra green marker is ploted in the map.
For each home, it is possible to call the following functions:
home
.get_location()home
.get_price()home
.get_area()For a list of homes, it is possible to sort it by price using the function below.
sort_by_price(homes, reverse=False)
reverse
is set to true, the list is instead sorted from expensive to cheapThe haversine formula determines the great-circle distance between two points on a sphere given their longitudes and latitudes. This will be useful for filtering the selection using the radius.
$ python
>>> import db
>>> help(db.haversine)
Help on function haversine in module db:
haversine(lat1, lon1, lat2, lon2)
Calculate the great circle distance (in m) between two points
on the earth (specified in decimal degrees)