{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For this exercise you are given a database containing information about real estate around\n", "Uppsala. You are also given a Python module that helps working the database.\n", "\n", "- The database (`uppsala.sqlite`) and the code (`db.py` and `homes.py`) are found in the `download` directory.\n", "- Read the module's [documentation](../../files/db_module_documentation.html)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The module `db.py` will help you produce a HTML file whit your results plotted on a map. This set-up uses OpenStreetMap. \n", "\n", "![result](../../img/mapOSM.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The center is defined as the point:\n", "\n", "`latitude: 59.865795990339876`\n", "\n", "`longitude: 17.64583576202392`\n", "\n", "\n", "- Open `homes.py` and use it to solve the exercise" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Questions:\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "a) What is the price of the cheapest house around the given center and within 2 kilometers?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "b) Plot the 100 cheapest houses in this area." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "c) Find the most expensive house that has an area larger than 10, and print its price and area." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "d) Print or plot this house." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----\n", "




\n", "

Proposed solution

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**a) and b)**\n", "\n", "First, we do all imports and set the coordinates of the 'center'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from db import HomeDB\n", "from db import haversine as get_distance\n", "from db import plot, sort_by_price\n", "\n", "lat = 59.865795990339876\n", "lng = 17.64583576202392\n", "radius = 2000 # in m\n", "\n", "\n", "db = HomeDB('../../files/uppsala.sqlite')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We define a help function that gets all houses that are central and returns them sorted by price:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def cheap_central():\n", " \"\"\" Find the within 2000m from the center and sort by increasing price \"\"\"\n", " db.connect()\n", " homes = db.select() # Let's get all homes and then sort and filter them\n", " db.disconnect()\n", " # TODO sort homes by price and keep only homes that are close to the center" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's start by picking homes that are central. We create a new list, `selected`, where all central houses will be put. But how do we know if a home is central?\n", "\n", "- we get it's location\n", "- we use the `get_distance()` from `db` to check the distance between the home and the given center" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", "selected = []\n", "for home in homes:\n", " h_lat, h_lon = home.get_location()\n", " distance = get_distance(h_lat, h_lon, lat, lng)\n", " if distance <= 2000:\n", " selected.append(home)\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next step is to sort the homes by price. The function `sort_by_price()` from `db` is useful here!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", "sorted_selected = sort_by_price(selected)\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's put it all together:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def cheap_central():\n", " \"\"\" Find the within 2000m from the center and sort by increasing price \"\"\"\n", " db.connect()\n", " homes = db.select()\n", " db.disconnect()\n", " selected = []\n", " for home in homes:\n", " h_lat, h_lon = home.get_location()\n", " distance = get_distance(h_lat, h_lon, lat, lng)\n", " if distance <= 2000:\n", " selected.append(home)\n", " sorted_selected = sort_by_price(selected)\n", " return sorted_selected" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For a), we just print the first item of the list" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def part_a():\n", " print('Cheapest house: ' + str(cheap_central()[0].get_price()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For b), we select the 100 first items and plot them. `outfile` is the name of the resulting html file." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def part_b(outfile):\n", " num = 100\n", " central = cheap_central()\n", " pos = set()\n", " for home in central[:num]:\n", " pos.add(home.get_location())\n", " plot(central[:num],\n", " output=outfile,\n", " special=central[0],\n", " zoom=14,\n", " latitude=lat,\n", " longitude=lng,\n", " radius=radius\n", " )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**c) and d)**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We define another help function to find the most expensive houses." ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "def expensive():\n", " \"\"\" Sort houses by price per square meter \"\"\"\n", " db.connect()\n", " # use the built in filtering in db.select to get houses with a big enough area\n", " homes = db.select('area > 10')\n", " db.disconnect()\n", " expensive = sort_by_price(homes, reverse=True)\n", " return expensive" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For c), we get the first item and print it." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def part_c():\n", " house = expensive()[0]\n", " print('Most expensive:' + str(house.get_price()) + '. Square meters: ' +str(house.get_area()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For d), we plot it. Again, `outfile` is the name of the resulting html file." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def part_d(outfile):\n", " house = expensive()[0]\n", " plot([house],\n", " output=outfile,\n", " special=house,\n", " zoom=14,\n", " latitude=lat,\n", " longitude=lng,\n", " radius=radius\n", " )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Optionally, add a main function to print all answers." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Part a:\n", "Cheapest house: 330000.0\n", "\n", "Part b: printed to selection_b.html\n", "\n", "Part c:\n", "Most expensive:20000000.0. Square meters: 120.0\n", "\n", "Part d: printed to selection_d.html\n" ] } ], "source": [ "if __name__ == \"__main__\":\n", " print('Part a:')\n", " part_a()\n", " print()\n", " part_b('selection_b.html')\n", " print('Part b: printed to selection_b.html')\n", " print()\n", " print('Part c:')\n", " part_c()\n", " print()\n", " part_d('selection_d.html')\n", " print('Part d: printed to selection_d.html')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `selection_b.html`:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![result](../../img/mapOSM.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `selection_d.html`:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![result](../../img/ex4_4_2.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Bonus exercises**\n", "\n", "- Find out what types of houses there are.\n", "- Plot all houses of type 'Gård'. \n", "\n", "- Print a circle of houses, like so:\n", "\n", "![result](../../img/mapBonus.png)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }