From ee44cddb8504fa20e0e32de4fbae9898a3f56f7d Mon Sep 17 00:00:00 2001 From: fmckenna Date: Wed, 13 Nov 2024 22:04:51 -0800 Subject: [PATCH] fmk - updating footprint example with colab notebook --- examples/footprint/brails_footprint.ipynb | 319 ++++++++++++++++++++++ examples/footprint/footprint.rst | 12 + 2 files changed, 331 insertions(+) create mode 100644 examples/footprint/brails_footprint.ipynb diff --git a/examples/footprint/brails_footprint.ipynb b/examples/footprint/brails_footprint.ipynb new file mode 100644 index 0000000..9d29431 --- /dev/null +++ b/examples/footprint/brails_footprint.ipynb @@ -0,0 +1,319 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "77ee23d9-c278-4ba6-a1f5-9ab882c79489", + "metadata": {}, + "source": [ + "# BRAILS Footprint Example\n", + "Written by fmk" + ] + }, + { + "cell_type": "markdown", + "id": "3c43c1e3-9c92-47a3-9ea5-824e5d134263", + "metadata": {}, + "source": [ + "## Install BRAILS++ and Plotly" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "24c05ac7-5d53-4186-bed9-cf650d11b6a5", + "metadata": {}, + "outputs": [], + "source": [ + "!pip install git+https://github.com/NHERI-SimCenter/BrailsPlusPlus\n", + "!pip install plotly\n", + "!pip install folium" + ] + }, + { + "cell_type": "markdown", + "id": "114549e3-5e63-4ec0-bc06-71529a7b3897", + "metadata": {}, + "source": [ + "## Import Required Packages" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "71cf2a7c-796b-4f5f-8b93-8452f9972850", + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "import plotly.express as px\n", + "import plotly.graph_objects as go\n", + "import folium\n", + "from brails import Importer" + ] + }, + { + "cell_type": "markdown", + "id": "567090d2-6ea4-4e12-9c1a-e01501388f75", + "metadata": {}, + "source": [ + "## Create a RegionBoundary for Area of Interest" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "659971d9-4ac3-4efa-a7ff-cca0c82838ce", + "metadata": {}, + "outputs": [], + "source": [ + "LOCATION = \"Tiburon, CA\"\n", + "\n", + "# Create an Importer instance:\n", + "importer = Importer()\n", + "\n", + "# Create a region boundary:\n", + "region_boundary_class = importer.get_class('RegionBoundary')\n", + "region_boundary_object = region_boundary_class({'type': 'locationName',\n", + " 'data': LOCATION})" + ] + }, + { + "cell_type": "markdown", + "id": "9577bd83-b5ca-44c2-8475-147ba7315b78", + "metadata": {}, + "source": [ + "## Create a Footprint Scraper and scrape the inventory\n", + "\n", + "options for scraper: OSM_FootprintSrcaper, USA_FootprintScraper, MS_FootprintScraper" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "64ade594-871c-48f9-af88-867d387a7d55", + "metadata": {}, + "outputs": [], + "source": [ + "SCRAPER = \"MS_FootprintScraper\"\n", + "\n", + "# Scrape the building inventory from OSM:\n", + "scraper_class = importer.get_class(SCRAPER)\n", + "scraper = scraper_class({'length': 'ft'})\n", + "scraper_inventory = scraper.get_footprints(region_boundary_object)\n" + ] + }, + { + "cell_type": "markdown", + "id": "932525c2-0118-4e88-af29-d246eb7e80e3", + "metadata": {}, + "source": [ + "## Plot It\n", + "\n", + "NOTE: if using MS_FootprintScraper the fields in output are different, go back to example page and have a look at feature properties" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "9f8154ad-2bf5-4966-b01f-5920fe554dd7", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
Make this Notebook Trusted to load map: File -> Trust Notebook
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory_footprints, _ = scraper_inventory.get_coordinates()\n", + "\n", + "all_coords = [coord for path in inventory_footprints for coord in path]\n", + "center_lat = sum(point[1] for point in all_coords) / len(all_coords)\n", + "center_lon = sum(point[0] for point in all_coords) / len(all_coords)\n", + "m = folium.Map(location=(center_lat, center_lon), tiles=\"cartodbpositron\", zoom_start=13)\n", + "\n", + "geojson_data = scraper_inventory.get_geojson()\n", + "\n", + "g = folium.GeoJson(\n", + " geojson_data,\n", + " name=\"geojson\",\n", + " tooltip=folium.GeoJsonTooltip(fields=['buildingheight','fpAreas'], sticky=False)\n", + ").add_to(m)\n", + "\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2624e828-e8eb-435d-9983-9a55897400d9", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.10.11" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/examples/footprint/footprint.rst b/examples/footprint/footprint.rst index 24c33c9..8b650db 100644 --- a/examples/footprint/footprint.rst +++ b/examples/footprint/footprint.rst @@ -37,6 +37,18 @@ The example when run will also prints out a two inventory subset of the data obt .. literalinclude:: output.txt :linenos: + +Footprint Notebook +------------------ + +Here is a link to a Jupyter Notebook that runs the basic code, accompanied by graphics to better illustrate the output. + +.. raw:: html + + + + + .. note::