From b1ede3a4938b8e050764898b72113d5daaec7760 Mon Sep 17 00:00:00 2001 From: Carl Boettiger Date: Sat, 26 Oct 2024 04:37:06 +0000 Subject: [PATCH] better maps on the way! --- data/nh2.html | 38 ++++++ reading/spatial-2.ipynb | 270 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 289 insertions(+), 19 deletions(-) create mode 100644 data/nh2.html diff --git a/data/nh2.html b/data/nh2.html new file mode 100644 index 0000000..d7ee905 --- /dev/null +++ b/data/nh2.html @@ -0,0 +1,38 @@ + + + +My Awesome Map + + + + + + +
+ + + \ No newline at end of file diff --git a/reading/spatial-2.ipynb b/reading/spatial-2.ipynb index 91c16c9..03776a1 100644 --- a/reading/spatial-2.ipynb +++ b/reading/spatial-2.ipynb @@ -32,7 +32,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "9eee4222e406459a9ee6f14f9327a784", + "model_id": "4ba34cfb1f8d49dca90796a2edb6a91d", "version_major": 2, "version_minor": 0 }, @@ -83,7 +83,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "237d8fb05c3f4794b2f823cb555a927d", + "model_id": "7b80292a327440709314055939aef2f6", "version_major": 2, "version_minor": 1 }, @@ -144,7 +144,7 @@ " " ], "text/plain": [ - "" + "" ] }, "execution_count": 6, @@ -173,25 +173,41 @@ "source": [ "## Map styling \n", "\n", - "Leafmap allows us to add many _layers_ to our map from various data sources, and gives us a rich set of tools for styling the look and feel of most of those data layers. The example below illustrates how we can use the data column called \"fill\", which corresponds to the redlining \"grade\" associated with each layer, \n", - "\n" + "Leafmap allows us to add many _layers_ to our map from various data sources, and gives us a rich set of tools for styling the look and feel of most of those data layers. The example below illustrates some more complex patterns for generating a rich map.\n", + "\n", + "- We introduce a basemap with `add_basemap()`. This is just a background image, that is tiled to re-draw to the appropriate zoom and window. However, note that basemaps are just background images, we cannot extract actual data from these sources.\n", + "- We have added source data by pointing to a remote URL. This provides much better performance than embedding the JSON data directly, especially when the data become very large. This requires the data is hosted at a highly available provider, such as GitHub or another cloud service. The original host source won't work unfortunately. \n", + "- We have used a custom filter inside the source data to select our city of interest. This could be omitted.\n", + "- We have colored our polygons according to grade given in the data. Specifically, we noticed that the column \"fill\" already contains color codes in HEX format corresponding to the redlining \"grade\" associated with each layer.\n", + "- We have added a final layer of text labels based on the \"label\" column. \n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "82852946-0439-4dfc-8cdd-750817b2fe86", + "metadata": {}, + "outputs": [], + "source": [ + "import json\n", + "geojson = json.loads(city.to_json())" ] }, { "cell_type": "code", "execution_count": 8, - "id": "22704317-bcc5-421b-b78b-5b9149124b3f", + "id": "2198d392-6c4c-48de-bf89-743431fa2ec8", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "fca6c9d5762a424e90e6de4eb77fec44", + "model_id": "d5ee65d4c790469b829ef69332933e0b", "version_major": 2, "version_minor": 1 }, "text/plain": [ - "Map(height='600px', map_options={'bearing': 0, 'center': (0, 20), 'pitch': 0, 'style': 'https://tiles.openfreeā€¦" + "Map(height='600px', map_options={'bearing': 0, 'center': (-72.9, 41.3), 'pitch': 0, 'style': 'https://basemapsā€¦" ] }, "execution_count": 8, @@ -200,19 +216,47 @@ } ], "source": [ - "m = leafmap.Map(style=\"positron\")\n", + "m = leafmap.Map(center=[-72.9, 41.3], zoom=12)\n", "\n", + "source = {\n", + " \"type\": \"geojson\", \n", + "# 'data': geojson,\n", + " 'data': 'https://data.source.coop/cboettig/us-boundaries/mappinginequality.json',\n", + " 'filter': ['==', ['get', 'city'], 'New Haven']\n", + "}\n", + "layer = {\n", + " \"id\": \"geojson-layer\",\n", + " \"type\": \"fill\",\n", + " \"source\": \"geojson\",\n", + " 'paint': {\n", + " \"fill-color\": [\"get\", \"fill\"], # color by the column called \"fill\"\n", + " \"fill-opacity\": 0.8,\n", + " },\n", + "}\n", "\n", - "paint = {\n", - " \"fill-color\": [\"get\", \"fill\"], # color by the column called \"fill\"\n", - " \"fill-opacity\": 0.8,\n", + "text_layer = {\n", + " \"id\": \"labels\",\n", + " \"type\": \"symbol\",\n", + " \"source\": \"geojson\",\n", + " \"layout\": {\n", + " \"text-field\": [\"get\", \"label\"],\n", + " \"text_size\": 14,\n", + " 'text-font': ['Open Sans Bold'],\n", + " \"text-anchor\": \"center\",\n", + " },\n", + " 'paint': { 'text-color': '#ffffff'}\n", "}\n", "\n", - "m.add_basemap(\"Esri.NatGeoWorldMap\")\n", - "m.add_gdf(city, layer_type=\"fill\", name=\"Redlining\", paint=paint)\n", + "\n", + "m.add_basemap(\"USGS.USTopo\")\n", + "m.add_source(\"geojson\", source)\n", + "m.add_layer(layer)\n", + "m.add_layer(text_layer)\n", "\n", "m.add_layer_control()\n", + "\n", "m.to_html(\"../data/nh2.html\", overwrite=True)\n", + "\n", "m" ] }, @@ -226,10 +270,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "7aa49687-4c6f-466c-811c-e57637ce1bb7", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "\n", + " \n", + " " + ], + "text/plain": [ + "" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#| hide-input\n", "# display map on course website by using an iframe to the output URL \n", @@ -238,13 +305,178 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "b17a5c05-d69b-423b-b9ff-d94a645279f8", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['OpenStreetMap',\n", + " 'Google Maps',\n", + " 'Google Satellite',\n", + " 'Google Terrain',\n", + " 'Google Hybrid',\n", + " 'FWS NWI Wetlands',\n", + " 'FWS NWI Wetlands Raster',\n", + " 'NLCD 2021 CONUS Land Cover',\n", + " 'NLCD 2019 CONUS Land Cover',\n", + " 'NLCD 2016 CONUS Land Cover',\n", + " 'NLCD 2013 CONUS Land Cover',\n", + " 'NLCD 2011 CONUS Land Cover',\n", + " 'NLCD 2008 CONUS Land Cover',\n", + " 'NLCD 2006 CONUS Land Cover',\n", + " 'NLCD 2004 CONUS Land Cover',\n", + " 'NLCD 2001 CONUS Land Cover',\n", + " 'USGS NAIP Imagery',\n", + " 'USGS NAIP Imagery False Color',\n", + " 'USGS NAIP Imagery NDVI',\n", + " 'USGS Hydrography',\n", + " 'USGS 3DEP Elevation',\n", + " 'USGS 3DEP Elevation Index',\n", + " 'ESA Worldcover 2020',\n", + " 'ESA Worldcover 2020 S2 FCC',\n", + " 'ESA Worldcover 2020 S2 TCC',\n", + " 'ESA Worldcover 2021',\n", + " 'ESA Worldcover 2021 S2 FCC',\n", + " 'ESA Worldcover 2021 S2 TCC',\n", + " 'BaseMapDE.Color',\n", + " 'BaseMapDE.Grey',\n", + " 'BasemapAT.basemap',\n", + " 'BasemapAT.grau',\n", + " 'BasemapAT.highdpi',\n", + " 'BasemapAT.orthofoto',\n", + " 'BasemapAT.overlay',\n", + " 'BasemapAT.surface',\n", + " 'BasemapAT.terrain',\n", + " 'CartoDB.DarkMatter',\n", + " 'CartoDB.DarkMatterNoLabels',\n", + " 'CartoDB.DarkMatterOnlyLabels',\n", + " 'CartoDB.Positron',\n", + " 'CartoDB.PositronNoLabels',\n", + " 'CartoDB.PositronOnlyLabels',\n", + " 'CartoDB.Voyager',\n", + " 'CartoDB.VoyagerLabelsUnder',\n", + " 'CartoDB.VoyagerNoLabels',\n", + " 'CartoDB.VoyagerOnlyLabels',\n", + " 'CyclOSM',\n", + " 'Esri.AntarcticBasemap',\n", + " 'Esri.AntarcticImagery',\n", + " 'Esri.ArcticImagery',\n", + " 'Esri.ArcticOceanBase',\n", + " 'Esri.ArcticOceanReference',\n", + " 'Esri.NatGeoWorldMap',\n", + " 'Esri.OceanBasemap',\n", + " 'Esri.WorldGrayCanvas',\n", + " 'Esri.WorldImagery',\n", + " 'Esri.WorldPhysical',\n", + " 'Esri.WorldShadedRelief',\n", + " 'Esri.WorldStreetMap',\n", + " 'Esri.WorldTerrain',\n", + " 'Esri.WorldTopoMap',\n", + " 'FreeMapSK',\n", + " 'Gaode.Normal',\n", + " 'Gaode.Satellite',\n", + " 'HikeBike.HikeBike',\n", + " 'HikeBike.HillShading',\n", + " 'JusticeMap.americanIndian',\n", + " 'JusticeMap.asian',\n", + " 'JusticeMap.black',\n", + " 'JusticeMap.hispanic',\n", + " 'JusticeMap.income',\n", + " 'JusticeMap.multi',\n", + " 'JusticeMap.nonWhite',\n", + " 'JusticeMap.plurality',\n", + " 'JusticeMap.white',\n", + " 'MtbMap',\n", + " 'NASAGIBS.ASTER_GDEM_Greyscale_Shaded_Relief',\n", + " 'NASAGIBS.BlueMarble',\n", + " 'NASAGIBS.BlueMarble3031',\n", + " 'NASAGIBS.BlueMarble3413',\n", + " 'NASAGIBS.BlueMarbleBathymetry3031',\n", + " 'NASAGIBS.BlueMarbleBathymetry3413',\n", + " 'NASAGIBS.MEaSUREsIceVelocity3031',\n", + " 'NASAGIBS.MEaSUREsIceVelocity3413',\n", + " 'NASAGIBS.ModisAquaBands721CR',\n", + " 'NASAGIBS.ModisAquaTrueColorCR',\n", + " 'NASAGIBS.ModisTerraAOD',\n", + " 'NASAGIBS.ModisTerraBands367CR',\n", + " 'NASAGIBS.ModisTerraBands721CR',\n", + " 'NASAGIBS.ModisTerraChlorophyll',\n", + " 'NASAGIBS.ModisTerraLSTDay',\n", + " 'NASAGIBS.ModisTerraSnowCover',\n", + " 'NASAGIBS.ModisTerraTrueColorCR',\n", + " 'NASAGIBS.ViirsEarthAtNight2012',\n", + " 'NASAGIBS.ViirsTrueColorCR',\n", + " 'OPNVKarte',\n", + " 'OneMapSG.Default',\n", + " 'OneMapSG.Grey',\n", + " 'OneMapSG.LandLot',\n", + " 'OneMapSG.Night',\n", + " 'OneMapSG.Original',\n", + " 'OpenAIP',\n", + " 'OpenFireMap',\n", + " 'OpenRailwayMap',\n", + " 'OpenSeaMap',\n", + " 'OpenSnowMap.pistes',\n", + " 'OpenStreetMap.BZH',\n", + " 'OpenStreetMap.BlackAndWhite',\n", + " 'OpenStreetMap.CH',\n", + " 'OpenStreetMap.DE',\n", + " 'OpenStreetMap.HOT',\n", + " 'OpenStreetMap.Mapnik',\n", + " 'OpenTopoMap',\n", + " 'SafeCast',\n", + " 'Stadia.AlidadeSatellite',\n", + " 'Stadia.AlidadeSmooth',\n", + " 'Stadia.AlidadeSmoothDark',\n", + " 'Stadia.OSMBright',\n", + " 'Stadia.Outdoors',\n", + " 'Stadia.StamenTerrain',\n", + " 'Stadia.StamenTerrainBackground',\n", + " 'Stadia.StamenTerrainLabels',\n", + " 'Stadia.StamenTerrainLines',\n", + " 'Stadia.StamenToner',\n", + " 'Stadia.StamenTonerBackground',\n", + " 'Stadia.StamenTonerLabels',\n", + " 'Stadia.StamenTonerLines',\n", + " 'Stadia.StamenTonerLite',\n", + " 'Stadia.StamenWatercolor',\n", + " 'Strava.All',\n", + " 'Strava.Ride',\n", + " 'Strava.Run',\n", + " 'Strava.Water',\n", + " 'Strava.Winter',\n", + " 'SwissFederalGeoportal.JourneyThroughTime',\n", + " 'SwissFederalGeoportal.NationalMapColor',\n", + " 'SwissFederalGeoportal.NationalMapGrey',\n", + " 'SwissFederalGeoportal.SWISSIMAGE',\n", + " 'TopPlusOpen.Color',\n", + " 'TopPlusOpen.Grey',\n", + " 'USGS.USImagery',\n", + " 'USGS.USImageryTopo',\n", + " 'USGS.USTopo',\n", + " 'WaymarkedTrails.cycling',\n", + " 'WaymarkedTrails.hiking',\n", + " 'WaymarkedTrails.mtb',\n", + " 'WaymarkedTrails.riding',\n", + " 'WaymarkedTrails.skating',\n", + " 'WaymarkedTrails.slopes',\n", + " 'nlmaps.grijs',\n", + " 'nlmaps.luchtfoto',\n", + " 'nlmaps.pastel',\n", + " 'nlmaps.standaard',\n", + " 'nlmaps.water']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "## list all basemaps known to leafmap\n", - "#list(leafmap.basemaps.keys())" + "list(leafmap.basemaps.keys())" ] }, {