Skip to content

Instantly share code, notes, and snippets.

@smnorris
Created November 16, 2022 22:36
Show Gist options
  • Select an option

  • Save smnorris/d6381f42d2e2ad7e6828782728f04976 to your computer and use it in GitHub Desktop.

Select an option

Save smnorris/d6381f42d2e2ad7e6828782728f04976 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "5c68cdd4",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>gradient</th>\n",
" <th>mad_m3s</th>\n",
" <th>channel_width</th>\n",
" <th>stream_order</th>\n",
" <th>stream_order_parent</th>\n",
" <th>stream_order_max</th>\n",
" <th>model_spawning_cm</th>\n",
" <th>model_spawning_pk</th>\n",
" <th>edge_type</th>\n",
" <th>geom</th>\n",
" </tr>\n",
" <tr>\n",
" <th>segmented_stream_id</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>354095772.0</th>\n",
" <td>0.0384</td>\n",
" <td>NaN</td>\n",
" <td>2.15</td>\n",
" <td>2</td>\n",
" <td>NaN</td>\n",
" <td>2</td>\n",
" <td>True</td>\n",
" <td>None</td>\n",
" <td>1000</td>\n",
" <td>LINESTRING Z (1043500.655 598416.875 1.000, 10...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>354108776.0</th>\n",
" <td>0.0000</td>\n",
" <td>NaN</td>\n",
" <td>2.41</td>\n",
" <td>2</td>\n",
" <td>1.0</td>\n",
" <td>2</td>\n",
" <td>True</td>\n",
" <td>None</td>\n",
" <td>1000</td>\n",
" <td>LINESTRING Z (1052882.822 569082.975 39.000, 1...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>354108776.11000</th>\n",
" <td>0.0534</td>\n",
" <td>NaN</td>\n",
" <td>2.41</td>\n",
" <td>2</td>\n",
" <td>1.0</td>\n",
" <td>2</td>\n",
" <td>True</td>\n",
" <td>None</td>\n",
" <td>1000</td>\n",
" <td>LINESTRING Z (1052878.634 569093.147 39.000, 1...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>354109435.0</th>\n",
" <td>0.0368</td>\n",
" <td>NaN</td>\n",
" <td>3.06</td>\n",
" <td>2</td>\n",
" <td>NaN</td>\n",
" <td>2</td>\n",
" <td>True</td>\n",
" <td>None</td>\n",
" <td>1000</td>\n",
" <td>LINESTRING Z (1010311.043 459221.994 1.000, 10...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>354115253.0</th>\n",
" <td>0.0795</td>\n",
" <td>NaN</td>\n",
" <td>4.77</td>\n",
" <td>2</td>\n",
" <td>NaN</td>\n",
" <td>2</td>\n",
" <td>True</td>\n",
" <td>None</td>\n",
" <td>1000</td>\n",
" <td>LINESTRING Z (877608.695 588037.902 1.000, 877...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>360887426.20262512</th>\n",
" <td>0.0000</td>\n",
" <td>NaN</td>\n",
" <td>38.09</td>\n",
" <td>6</td>\n",
" <td>7.0</td>\n",
" <td>6</td>\n",
" <td>True</td>\n",
" <td>None</td>\n",
" <td>1250</td>\n",
" <td>LINESTRING Z (999330.487 801989.625 257.659, 9...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>360887426.20294588</th>\n",
" <td>0.0000</td>\n",
" <td>NaN</td>\n",
" <td>38.01</td>\n",
" <td>6</td>\n",
" <td>7.0</td>\n",
" <td>6</td>\n",
" <td>True</td>\n",
" <td>None</td>\n",
" <td>1250</td>\n",
" <td>LINESTRING Z (999353.841 801967.636 257.659, 9...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>360887426.20417273</th>\n",
" <td>0.0187</td>\n",
" <td>NaN</td>\n",
" <td>38.01</td>\n",
" <td>6</td>\n",
" <td>7.0</td>\n",
" <td>6</td>\n",
" <td>True</td>\n",
" <td>None</td>\n",
" <td>1250</td>\n",
" <td>LINESTRING Z (999384.919 801849.707 257.659, 9...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>360887426.20672573</th>\n",
" <td>0.0000</td>\n",
" <td>NaN</td>\n",
" <td>38.01</td>\n",
" <td>6</td>\n",
" <td>7.0</td>\n",
" <td>6</td>\n",
" <td>True</td>\n",
" <td>None</td>\n",
" <td>1250</td>\n",
" <td>LINESTRING Z (999417.026 801601.805 262.430, 9...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>360887428.721000</th>\n",
" <td>0.0257</td>\n",
" <td>NaN</td>\n",
" <td>28.66</td>\n",
" <td>5</td>\n",
" <td>NaN</td>\n",
" <td>5</td>\n",
" <td>None</td>\n",
" <td>True</td>\n",
" <td>1250</td>\n",
" <td>LINESTRING Z (1113978.540 610501.173 35.461, 1...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>19856 rows × 10 columns</p>\n",
"</div>"
],
"text/plain": [
" gradient mad_m3s channel_width stream_order \\\n",
"segmented_stream_id \n",
"354095772.0 0.0384 NaN 2.15 2 \n",
"354108776.0 0.0000 NaN 2.41 2 \n",
"354108776.11000 0.0534 NaN 2.41 2 \n",
"354109435.0 0.0368 NaN 3.06 2 \n",
"354115253.0 0.0795 NaN 4.77 2 \n",
"... ... ... ... ... \n",
"360887426.20262512 0.0000 NaN 38.09 6 \n",
"360887426.20294588 0.0000 NaN 38.01 6 \n",
"360887426.20417273 0.0187 NaN 38.01 6 \n",
"360887426.20672573 0.0000 NaN 38.01 6 \n",
"360887428.721000 0.0257 NaN 28.66 5 \n",
"\n",
" stream_order_parent stream_order_max model_spawning_cm \\\n",
"segmented_stream_id \n",
"354095772.0 NaN 2 True \n",
"354108776.0 1.0 2 True \n",
"354108776.11000 1.0 2 True \n",
"354109435.0 NaN 2 True \n",
"354115253.0 NaN 2 True \n",
"... ... ... ... \n",
"360887426.20262512 7.0 6 True \n",
"360887426.20294588 7.0 6 True \n",
"360887426.20417273 7.0 6 True \n",
"360887426.20672573 7.0 6 True \n",
"360887428.721000 NaN 5 None \n",
"\n",
" model_spawning_pk edge_type \\\n",
"segmented_stream_id \n",
"354095772.0 None 1000 \n",
"354108776.0 None 1000 \n",
"354108776.11000 None 1000 \n",
"354109435.0 None 1000 \n",
"354115253.0 None 1000 \n",
"... ... ... \n",
"360887426.20262512 None 1250 \n",
"360887426.20294588 None 1250 \n",
"360887426.20417273 None 1250 \n",
"360887426.20672573 None 1250 \n",
"360887428.721000 True 1250 \n",
"\n",
" geom \n",
"segmented_stream_id \n",
"354095772.0 LINESTRING Z (1043500.655 598416.875 1.000, 10... \n",
"354108776.0 LINESTRING Z (1052882.822 569082.975 39.000, 1... \n",
"354108776.11000 LINESTRING Z (1052878.634 569093.147 39.000, 1... \n",
"354109435.0 LINESTRING Z (1010311.043 459221.994 1.000, 10... \n",
"354115253.0 LINESTRING Z (877608.695 588037.902 1.000, 877... \n",
"... ... \n",
"360887426.20262512 LINESTRING Z (999330.487 801989.625 257.659, 9... \n",
"360887426.20294588 LINESTRING Z (999353.841 801967.636 257.659, 9... \n",
"360887426.20417273 LINESTRING Z (999384.919 801849.707 257.659, 9... \n",
"360887426.20672573 LINESTRING Z (999417.026 801601.805 262.430, 9... \n",
"360887428.721000 LINESTRING Z (1113978.540 610501.173 35.461, 1... \n",
"\n",
"[19856 rows x 10 columns]"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import os\n",
"\n",
"import pandas\n",
"import geopandas\n",
"from sqlalchemy import create_engine\n",
"import seaborn as sns\n",
"import matplotlib as mpl\n",
"import matplotlib.pyplot as plt\n",
"\n",
"sns.set_theme(style=\"ticks\")\n",
"conn = create_engine(os.environ[\"DATABASE_URL\"])\n",
"\n",
"# get all segments that have been manually identified as spawning/rearing AND\n",
"# have either channel width or discharge data\n",
"sql = \"\"\"select\n",
" s.segmented_stream_id,\n",
" s.gradient,\n",
" s.mad_m3s,\n",
" s.channel_width,\n",
" s.stream_order,\n",
" s.stream_order_parent,\n",
" s.stream_order_max,\n",
" u.model_spawning_cm_manual AS model_spawning_cm,\n",
" u.model_spawning_pk_manual AS model_spawning_pk,\n",
" s.edge_type,\n",
" s.geom\n",
"from bcfishpass.streams s\n",
"inner join bcfishpass.user_habitat_classification_qa u\n",
"on s.segmented_stream_id = u.segmented_stream_id\n",
"where \n",
" (u.model_spawning_cm_manual is not null\n",
" or u.model_spawning_pk_manual is not null)\n",
"and (s.channel_width is not null or s.mad_m3s is not null)\n",
"and s.edge_type not in (1100, 1200, 1350, 1410, 1425, 1450)\"\"\"\n",
"\n",
"df = geopandas.read_postgis(sql, conn, index_col=\"segmented_stream_id\")\n",
"# save as geojson\n",
"df.to_file(\"spawning_cm_pk.geojson\", driver='GeoJSON')\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "694e71c6",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><span style=\"color:#565656\">Make this Notebook Trusted to load map: File -> Trust Notebook</span><iframe srcdoc=\"&lt;!DOCTYPE html&gt;\n",
"&lt;head&gt; \n",
" &lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=UTF-8&quot; /&gt;\n",
" \n",
" &lt;script&gt;\n",
" L_NO_TOUCH = false;\n",
" L_DISABLE_3D = false;\n",
" &lt;/script&gt;\n",
" \n",
" &lt;style&gt;html, body {width: 100%;height: 100%;margin: 0;padding: 0;}&lt;/style&gt;\n",
" &lt;style&gt;#map {position:absolute;top:0;bottom:0;right:0;left:0;}&lt;/style&gt;\n",
" &lt;script src=&quot;https://cdn.jsdelivr.net/npm/leaflet@1.6.0/dist/leaflet.js&quot;&gt;&lt;/script&gt;\n",
" &lt;script src=&quot;https://code.jquery.com/jquery-1.12.4.min.js&quot;&gt;&lt;/script&gt;\n",
" &lt;script src=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;\n",
" &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.js&quot;&gt;&lt;/script&gt;\n",
" &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/leaflet@1.6.0/dist/leaflet.css&quot;/&gt;\n",
" &lt;link rel=&quot;stylesheet&quot; href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css&quot;/&gt;\n",
" &lt;link rel=&quot;stylesheet&quot; href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css&quot;/&gt;\n",
" &lt;link rel=&quot;stylesheet&quot; href=&quot;https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css&quot;/&gt;\n",
" &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.css&quot;/&gt;\n",
" &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/gh/python-visualization/folium/folium/templates/leaflet.awesome.rotate.min.css&quot;/&gt;\n",
" \n",
" &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width,\n",
" initial-scale=1.0, maximum-scale=1.0, user-scalable=no&quot; /&gt;\n",
" &lt;style&gt;\n",
" #map_78a5eb24f6de54629784b1271893ec52 {\n",
" position: relative;\n",
" width: 100.0%;\n",
" height: 100.0%;\n",
" left: 0.0%;\n",
" top: 0.0%;\n",
" }\n",
" &lt;/style&gt;\n",
" \n",
" \n",
" &lt;style&gt;\n",
" .foliumtooltip {\n",
" \n",
" }\n",
" .foliumtooltip table{\n",
" margin: auto;\n",
" }\n",
" .foliumtooltip tr{\n",
" text-align: left;\n",
" }\n",
" .foliumtooltip th{\n",
" padding: 2px; padding-right: 8px;\n",
" }\n",
" &lt;/style&gt;\n",
" \n",
" \n",
" &lt;style&gt;\n",
" .foliumpopup {\n",
" margin: auto;\n",
" }\n",
" .foliumpopup table{\n",
" margin: auto;\n",
" }\n",
" .foliumpopup tr{\n",
" text-align: left;\n",
" }\n",
" .foliumpopup th{\n",
" padding: 2px; padding-right: 8px;\n",
" }\n",
" &lt;/style&gt;\n",
" \n",
"&lt;/head&gt;\n",
"&lt;body&gt; \n",
" \n",
" &lt;div class=&quot;folium-map&quot; id=&quot;map_78a5eb24f6de54629784b1271893ec52&quot; &gt;&lt;/div&gt;\n",
" \n",
"&lt;/body&gt;\n",
"&lt;script&gt; \n",
" \n",
" var map_78a5eb24f6de54629784b1271893ec52 = L.map(\n",
" &quot;map_78a5eb24f6de54629784b1271893ec52&quot;,\n",
" {\n",
" center: [52.31554999898977, -125.04947095970888],\n",
" crs: L.CRS.EPSG3857,\n",
" zoom: 10,\n",
" zoomControl: true,\n",
" preferCanvas: false,\n",
" }\n",
" );\n",
" L.control.scale().addTo(map_78a5eb24f6de54629784b1271893ec52);\n",
"\n",
" \n",
"\n",
" \n",
" \n",
" var tile_layer_cca8005ca899564ce454d5c148eaa632 = L.tileLayer(\n",
" &quot;https://a.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png&quot;,\n",
" {&quot;attribution&quot;: &quot;\\u0026copy; \\u003ca href=\\&quot;https://www.openstreetmap.org/copyright\\&quot;\\u003eOpenStreetMap\\u003c/a\\u003e contributors \\u0026copy; \\u003ca href=\\&quot;https://carto.com/attributions\\&quot;\\u003eCARTO\\u003c/a\\u003e&quot;, &quot;detectRetina&quot;: false, &quot;maxNativeZoom&quot;: 20, &quot;maxZoom&quot;: 20, &quot;minZoom&quot;: 0, &quot;noWrap&quot;: false, &quot;opacity&quot;: 1, &quot;subdomains&quot;: &quot;abc&quot;, &quot;tms&quot;: false}\n",
" ).addTo(map_78a5eb24f6de54629784b1271893ec52);\n",
" \n",
" \n",
" map_78a5eb24f6de54629784b1271893ec52.fitBounds(\n",
" [[48.35120484281356, -130.54514487948578], [56.27989515516599, -119.55379703993201]],\n",
" {}\n",
" );\n",
" \n",
" \n",
" function geo_json_8d02309b087e1fca124fc71d6c8cbbdc_styler(feature) {\n",
" switch(feature.id) {\n",
" default:\n",
" return {&quot;color&quot;: &quot;red&quot;, &quot;fillColor&quot;: &quot;red&quot;, &quot;fillOpacity&quot;: 0.5, &quot;weight&quot;: 2};\n",
" }\n",
" }\n",
" function geo_json_8d02309b087e1fca124fc71d6c8cbbdc_highlighter(feature) {\n",
" switch(feature.id) {\n",
" default:\n",
" return {&quot;fillOpacity&quot;: 0.75};\n",
" }\n",
" }\n",
" function geo_json_8d02309b087e1fca124fc71d6c8cbbdc_pointToLayer(feature, latlng) {\n",
" var opts = {&quot;bubblingMouseEvents&quot;: true, &quot;color&quot;: &quot;#3388ff&quot;, &quot;dashArray&quot;: null, &quot;dashOffset&quot;: null, &quot;fill&quot;: true, &quot;fillColor&quot;: &quot;#3388ff&quot;, &quot;fillOpacity&quot;: 0.2, &quot;fillRule&quot;: &quot;evenodd&quot;, &quot;lineCap&quot;: &quot;round&quot;, &quot;lineJoin&quot;: &quot;round&quot;, &quot;opacity&quot;: 1.0, &quot;radius&quot;: 2, &quot;stroke&quot;: true, &quot;weight&quot;: 3};\n",
" \n",
" let style = geo_json_8d02309b087e1fca124fc71d6c8cbbdc_styler(feature)\n",
" Object.assign(opts, style)\n",
" \n",
" return new L.CircleMarker(latlng, opts)\n",
" }\n",
"\n",
" function geo_json_8d02309b087e1fca124fc71d6c8cbbdc_onEachFeature(feature, layer) {\n",
" layer.on({\n",
" mouseout: function(e) {\n",
" if(typeof e.target.setStyle === &quot;function&quot;){\n",
" geo_json_8d02309b087e1fca124fc71d6c8cbbdc.resetStyle(e.target);\n",
" }\n",
" },\n",
" mouseover: function(e) {\n",
" if(typeof e.target.setStyle === &quot;function&quot;){\n",
" const highlightStyle = geo_json_8d02309b087e1fca124fc71d6c8cbbdc_highlighter(e.target.feature)\n",
" e.target.setStyle(highlightStyle);\n",
" }\n",
" },\n",
" });\n",
" };\n",
" var geo_json_8d02309b087e1fca124fc71d6c8cbbdc = L.geoJson(null, {\n",
" onEachFeature: geo_json_8d02309b087e1fca124fc71d6c8cbbdc_onEachFeature,\n",
" \n",
" style: geo_json_8d02309b087e1fca124fc71d6c8cbbdc_styler,\n",
" pointToLayer: geo_json_8d02309b087e1fca124fc71d6c8cbbdc_pointToLayer\n",
" });\n",
"\n",
" function geo_json_8d02309b087e1fca124fc71d6c8cbbdc_add (data) {\n",
" geo_json_8d02309b087e1fca124fc71d6c8cbbdc\n",
" .addData(data)\n",
" .addTo(map_78a5eb24f6de54629784b1271893ec52);\n",
" }\n",
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment