From 52db27da4907037ab7e94e1e8c6c3d0aefc23a93 Mon Sep 17 00:00:00 2001 From: Manushi Majumdar Date: Tue, 24 Sep 2024 13:41:35 -0400 Subject: [PATCH 1/4] 2.4.0 changes to mapping guide 3 for spatial data --- ...izing_spatial_data_on_the_map_widget.ipynb | 1486 ++++++++++++++++- 1 file changed, 1485 insertions(+), 1 deletion(-) diff --git a/guide/10-mapping-and-visualization/part3_visualizing_spatial_data_on_the_map_widget.ipynb b/guide/10-mapping-and-visualization/part3_visualizing_spatial_data_on_the_map_widget.ipynb index 0599313af..941f9fb8a 100644 --- a/guide/10-mapping-and-visualization/part3_visualizing_spatial_data_on_the_map_widget.ipynb +++ b/guide/10-mapping-and-visualization/part3_visualizing_spatial_data_on_the_map_widget.ipynb @@ -1 +1,1485 @@ -{"cells":[{"cell_type":"markdown","id":"6b1de307","metadata":{},"source":["# Part 3 - Visualizing spatial data on the map widget\n"]},{"cell_type":"markdown","id":"fdc477d2","metadata":{"toc":true},"source":["

Table of Contents

\n","
\n"]},{"cell_type":"markdown","id":"ff03e232","metadata":{},"source":["First, to use the map widget, call `gis.map()` and assign it to a variable that you can then query to bring up the widget in the notebook:\n"]},{"cell_type":"code","execution_count":5,"id":"b9e456a7","metadata":{},"outputs":[],"source":["import arcgis\n","from arcgis.gis import GIS\n","# Create a GIS object, as an anonymous user for this example\n","gis = GIS()"]},{"cell_type":"code","execution_count":75,"id":"041e3c1a","metadata":{},"outputs":[{"data":{"text/html":[""],"text/plain":[""]},"metadata":{},"output_type":"display_data"}],"source":["# Create a map widget\n","map1 = gis.map('USA') # Passing a place name to the constructor\n","# will initialize the extent of the map.\n","map1"]},{"cell_type":"markdown","id":"b0cec17e","metadata":{},"source":["## Adding single layer\n","\n","### Adding Web Layer to the map\n","\n","You can add a number of different layer objects, such as `FeatureLayer`, `FeatureCollection`, `ImageryLayer`, or `MapImageLayer`, to the map. For instance, you can use the following cell to add a `FeatureLayer`:\n"]},{"cell_type":"code","execution_count":3,"id":"7158617d","metadata":{},"outputs":[{"data":{"text/plain":[""]},"execution_count":3,"metadata":{},"output_type":"execute_result"}],"source":["world_countries_item = gis.content.get('ac80670eb213440ea5899bbf92a04998')\n","world_countries_layer = world_countries_item.layers[0]\n","world_countries_layer"]},{"cell_type":"code","execution_count":4,"id":"8ca675d2","metadata":{},"outputs":[],"source":["map1.add_layer(world_countries_layer, options={'opacity': 0.4})"]},{"cell_type":"markdown","id":"e46c76f0","metadata":{},"source":["Similar to `FeatureLayers`, you can also add `ImageryLayers` items. You can also specify either a built-in raster function or a custom one for rendering.\n"]},{"cell_type":"code","execution_count":76,"id":"ddf5017e","metadata":{},"outputs":[{"data":{"text/html":[""],"text/plain":[""]},"metadata":{},"output_type":"display_data"}],"source":["map2 = gis.map('USA') # Passing a place name to the constructor\n","# will initialize the extent of the map.\n","map2"]},{"cell_type":"code","execution_count":2,"id":"7849a7d8","metadata":{},"outputs":[{"data":{"text/html":["
\n","
\n"," \n"," \n"," \n","
\n","\n","
\n"," Pansharpened Landsat\n"," \n","
Landsat pansharpened and multitemporal 15m imagery rendered on-the-fly as Natural Color with DRA for visualization and analysis. It is updated daily with new imagery directly sourced from the USGS Landsat collection on AWS.Imagery Layer by esri\n","
Last Modified: June 28, 2021\n","
1 comments, 221,993 views\n","
\n","
\n"," "],"text/plain":[""]},"execution_count":2,"metadata":{},"output_type":"execute_result"}],"source":["landsat_item = GIS().content.search(\n"," \"Landsat 8 Views\", \"Imagery Layer\", max_items=2)[0]\n","\n","landsat_item"]},{"cell_type":"code","execution_count":8,"id":"848f179d","metadata":{},"outputs":[],"source":["map2.add_layer(landsat_item)"]},{"cell_type":"markdown","id":"6c1824c9","metadata":{},"source":["To zoom to one or more layers, call the `zoom_to_layer()` method and pass a layer or list of layers that you want to snap your map to. The supplied item can be a single instance or a list of an `Item`, `Layer`, `DataFrame`, `FeatureSet`, or `FeatureCollection`.\n","\n","The code below shows how to zoom to a single layer:\n"]},{"cell_type":"code","execution_count":9,"id":"99967386","metadata":{},"outputs":[],"source":["map2.zoom_to_layer(landsat_item)"]},{"cell_type":"code","execution_count":77,"id":"cb1ec183","metadata":{},"outputs":[{"data":{"text/html":[""],"text/plain":[""]},"metadata":{},"output_type":"display_data"}],"source":["map3 = gis.map('USA') # Passing a place name to the constructor\n","# will initialize the extent of the map.\n","map3"]},{"cell_type":"code","execution_count":15,"id":"2605cae8","metadata":{},"outputs":[{"data":{"text/html":["
\n","
\n"," \n"," \n"," \n","
\n","\n","
\n"," Koppen-Geiger Observed and Predicted Climate Shifts\n"," \n","
This time-enabled Map Service displays observed and predicted climate shifts from 1901-2100 using Koppen-Geiger climate classification.Map Image Layer by Intl_User_Community\n","
Last Modified: December 30, 2019\n","
6 comments, 301,869 views\n","
\n","
\n"," "],"text/plain":[""]},"execution_count":15,"metadata":{},"output_type":"execute_result"}],"source":["prep_item = GIS().content.search(\"Precipitation\",\n"," \"Map Image Layer\", max_items=2)[1]\n","\n","prep_item"]},{"cell_type":"code","execution_count":18,"id":"adfff312","metadata":{},"outputs":[{"data":{"text/plain":[""]},"execution_count":18,"metadata":{},"output_type":"execute_result"}],"source":["prep_layer = prep_item.layers[1]\n","prep_layer"]},{"cell_type":"code","execution_count":19,"id":"552a8135","metadata":{},"outputs":[],"source":["map3.add_layer(prep_layer)\n","map3.zoom_to_layer(prep_layer)"]},{"cell_type":"markdown","id":"d50b33ab","metadata":{},"source":["### Adding local and in-memory layers to the map\n","\n","#### Add a Spatially-Enabled Data Frame (SeDF)\n","\n","We can read census data on major cities and load the data into a `Spatially Enabled DataFrame (SeDF)`, which will then allow us to visualize the points by calling the `plot()` method off the `SeDF`'s spatial accessor and passing the map you created to it.\n"]},{"cell_type":"code","execution_count":21,"id":"6ecbe534","metadata":{"scrolled":true},"outputs":[{"data":{"text/html":["
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
FIDNAMECLASSSTSTFIPSPLACEFIPSCAPITALPOP_CLASSPOPULATIONPOP2010...MARHH_NO_CMHH_CHILDFHH_CHILDFAMILIESAVE_FAM_SZHSE_UNITSVACANTOWNER_OCCRENTER_OCCSHAPE
0306Purdue UniversityCensus Designated PlaceIN18186238861236512183...2449234912.66136210441254{\"x\": -9676280.259129459, \"y\": 4928486.7435240...
1600StanfordCensus Designated PlaceCA06067390661517913809...689133212302.773999867903123{\"x\": -13599607.21449169, \"y\": 4498534.6279709...
2682Fort CarsonCensus Designated PlaceCO08082737061389113813...3193942123193.592629180152434{\"x\": -11664455.032700792, \"y\": 4684537.534384...
3742StorrsCensus Designated PlaceCT09097398061556915344...27614184892.691737166592979{\"x\": -8043406.213645851, \"y\": 5131741.8140288...
41035Camp Pendleton SouthCensus Designated PlaceCA06061056161045010616...3893716925003.692865296112558{\"x\": -13066535.250599463, \"y\": 3925680.810605...
\n","

5 rows × 50 columns

\n","
"],"text/plain":[" FID NAME CLASS ST STFIPS PLACEFIPS \\\n","0 306 Purdue University Census Designated Place IN 18 1862388 \n","1 600 Stanford Census Designated Place CA 06 0673906 \n","2 682 Fort Carson Census Designated Place CO 08 0827370 \n","3 742 Storrs Census Designated Place CT 09 0973980 \n","4 1035 Camp Pendleton South Census Designated Place CA 06 0610561 \n","\n"," CAPITAL POP_CLASS POPULATION POP2010 ... MARHH_NO_C MHH_CHILD \\\n","0 6 12365 12183 ... 244 9 \n","1 6 15179 13809 ... 689 13 \n","2 6 13891 13813 ... 319 39 \n","3 6 15569 15344 ... 276 14 \n","4 6 10450 10616 ... 389 37 \n","\n"," FHH_CHILD FAMILIES AVE_FAM_SZ HSE_UNITS VACANT OWNER_OCC RENTER_OCC \\\n","0 23 491 2.66 1362 104 4 1254 \n","1 32 1230 2.77 3999 86 790 3123 \n","2 421 2319 3.59 2629 180 15 2434 \n","3 18 489 2.69 1737 166 592 979 \n","4 169 2500 3.69 2865 296 11 2558 \n","\n"," SHAPE \n","0 {\"x\": -9676280.259129459, \"y\": 4928486.7435240... \n","1 {\"x\": -13599607.21449169, \"y\": 4498534.6279709... \n","2 {\"x\": -11664455.032700792, \"y\": 4684537.534384... \n","3 {\"x\": -8043406.213645851, \"y\": 5131741.8140288... \n","4 {\"x\": -13066535.250599463, \"y\": 3925680.810605... \n","\n","[5 rows x 50 columns]"]},"execution_count":21,"metadata":{},"output_type":"execute_result"}],"source":["# create an anonymous connection to ArcGIS Online and get a public item\n","census_item = gis.content.search(\n"," \"USA Major Cities\", item_type=\"Feature layer\", outside_org=True)[0]\n","census_flayer = census_item.layers[0]\n","\n","# Specify a SQL query and get a sub-set of the original data as a DataFrame\n","census_df = census_flayer.query(where=\"AGE_45_54 < 450\").sdf\n","\n","# Visualize the top 5 records\n","census_df.head()"]},{"cell_type":"code","execution_count":78,"id":"6579f286","metadata":{},"outputs":[{"data":{"text/html":[""],"text/plain":[""]},"metadata":{},"output_type":"display_data"}],"source":["map4 = GIS().map(\"United States\")\n","map4"]},{"cell_type":"code","execution_count":23,"id":"a643166f","metadata":{},"outputs":[],"source":["map4.zoom = 4\n","map4.center = [39, -98]"]},{"cell_type":"code","execution_count":24,"id":"2ea643de","metadata":{},"outputs":[{"data":{"text/plain":["True"]},"execution_count":24,"metadata":{},"output_type":"execute_result"}],"source":["census_df.spatial.plot(map_widget=map4)"]},{"cell_type":"markdown","id":"16e9bb5e","metadata":{},"source":["#### Add an in-memory FeatureSet\n","\n","Besides adding in the the `SeDF`, we can also add a `FeatureSet`. For example, below we will search for the USA freeway layer and visualize it by adding it to the map widget as a `FeatureSet` object returned by the query method. Once added, we then draw it with default line symbols.\n"]},{"cell_type":"code","execution_count":6,"id":"5bb9779c","metadata":{},"outputs":[{"data":{"text/html":["
\n","
\n"," \n"," \n"," \n","
\n","\n","
\n"," USA Freeway System\n"," \n","
This layer presents rural and urban interstate highways.Feature Layer Collection by esri_dm\n","
Last Modified: January 07, 2020\n","
2 comments, 4,104,310 views\n","
\n","
\n"," "],"text/plain":[""]},"execution_count":6,"metadata":{},"output_type":"execute_result"}],"source":["freeway_item = gis.content.search('title:USA Freeway System AND owner:esri_dm',\n"," item_type='Feature Layer')[0]\n","freeway_item"]},{"cell_type":"code","execution_count":7,"id":"c90adf65","metadata":{},"outputs":[{"data":{"text/plain":[" 7 features"]},"execution_count":7,"metadata":{},"output_type":"execute_result"}],"source":["freeway_sdf = freeway_item.layers[0]\n","freeway_fset = freeway_sdf.query(where=\"DIST_KM>2800\", as_df=False)\n","freeway_fset"]},{"cell_type":"code","execution_count":79,"id":"9d67b60e","metadata":{},"outputs":[{"data":{"text/html":[""],"text/plain":[""]},"metadata":{},"output_type":"display_data"}],"source":["map5 = gis.map('USA', 10)\n","map5"]},{"cell_type":"code","execution_count":32,"id":"dc437d33","metadata":{},"outputs":[],"source":["map5.center = [34.05, -118.2]\n","map5.zoom = 12\n","map5.basemap = 'dark-gray'"]},{"cell_type":"code","execution_count":35,"id":"e8fa2a03","metadata":{},"outputs":[],"source":["map5.add_layer(freeway_fset, {'renderer': 'ClassedSizeRenderer',\n"," 'field_name': 'DIST_KM',\n"," 'opacity': 0.75})"]},{"cell_type":"markdown","id":"ac40c28f","metadata":{},"source":["#### Adding a local raster\n","\n","A local raster can be read into the map widget as a `numpy` array with `arcgis.raster` imported on the machine.\n","\n","
\n"," Note: Local raster can only be loaded for display when `arcpy` is installed on the current environment.\n","
\n"]},{"cell_type":"code","execution_count":36,"id":"6ab8df0e","metadata":{},"outputs":[],"source":["import numpy\n","import matplotlib.pyplot as plt\n","from arcgis.raster import Raster"]},{"cell_type":"code","execution_count":1,"id":"6b11f5c6","metadata":{},"outputs":[{"data":{"text/html":[""],"text/plain":[""]},"execution_count":1,"metadata":{},"output_type":"execute_result"}],"source":["map6 = GIS().map(\"Newark, NJ\")\n","map6"]},{"cell_type":"code","execution_count":null,"id":"9b3e7942","metadata":{},"outputs":[],"source":["raster = Raster(r'../../static/img/newark_nj_1922.jpg',\n"," extent={\"xmin\": -74.22655,\n"," \"ymin\": 40.712216,\n"," \"xmax\": -74.12544,\n"," \"ymax\": 40.773941,\n"," \"spatialReference\": {\"wkid\": 4326}},\n"," opacity=0.85)\n","map6.add_layer(raster)"]},{"cell_type":"markdown","id":"20a21e01","metadata":{},"source":["To immediately release the in-app memory immediate, run the follow cell to remove the raster layer:\n"]},{"cell_type":"code","execution_count":null,"id":"98facb06","metadata":{},"outputs":[],"source":["map6.remove_layers([raster,])"]},{"cell_type":"markdown","id":"d628726f","metadata":{},"source":["## Other operations\n"]},{"cell_type":"markdown","id":"b71f8e59","metadata":{},"source":["### Drawing graphics and geometries\n","\n","#### Draw geometries\n"]},{"cell_type":"code","execution_count":81,"id":"f0cd179d","metadata":{},"outputs":[{"data":{"text/html":[""],"text/plain":[""]},"metadata":{},"output_type":"display_data"}],"source":["map7 = gis.map(\"Port of Long Beach\")\n","map7.basemap = \"satellite\"\n","map7.zoom = 6\n","map7"]},{"cell_type":"markdown","id":"5d246a62","metadata":{},"source":["When it comes to adding geometries, we can either create a geometry object and use the `draw` method to apply the object onto our display, or we can input `point`, `polyline`, or `polygon` as keywords in the `draw()` method, which will then trigger a user interactive drawing session on the widget.\n"]},{"cell_type":"code","execution_count":69,"id":"8bede951","metadata":{},"outputs":[],"source":["from arcgis.geometry import Point\n","\n","pt = Point({\"x\": -118.15, \"y\": 33.80,\n"," \"spatialReference\": {\"wkid\": 4326}})"]},{"cell_type":"code","execution_count":70,"id":"71ac82e4","metadata":{},"outputs":[],"source":["map7.draw(pt)"]},{"cell_type":"code","execution_count":72,"id":"72c97901","metadata":{},"outputs":[],"source":["map7.draw('polyline')"]},{"cell_type":"code","execution_count":73,"id":"c014a9ef","metadata":{},"outputs":[],"source":["map7.draw('polygon')"]},{"cell_type":"markdown","id":"26bf5886","metadata":{},"source":["#### Draw with symbols and pop-ups\n","\n","The `draw()` method can be used to draw a deterministic shape on the map widget when the input shape is a known `Geometry` object, a list of coordinate pairs, a `FeatureSet`, or a `dict` object that represents a geometry. The other way to use `draw` is to provide users with an interactive sketch and display experience when the input shape is one of the expected strings, e.g. \"circle\", \"ellipse\", \"Polygon\", \"Polyline\", \"MultiPoint\", \"Point\", \"rectangle\", \"triangle\".\n","\n","```\n","draw(shape, popup=None, symbol=None, attributes=None)\n","# The draw method draws a shape on the map widget.\n","```\n","\n","
\n"," Note: Anything can be drawn from known `Geometry` objects, coordinate pairs, and `FeatureSet` objects.\n","
\n","\n","The `shape` argument can be one of these four objects:\n","\n","- Known `Geometry` objects: Shape is one of the following: circle, ellipse, Polygon, Polyline, MultiPoint, Point, rectangle, triangle.\n","- Coordinate pair: specified shape as a list of [lat, long]. Eg: [34, -81]\n","- `FeatureSet`: shape can be a FeatureSet object.\n","- `Dict` object representing a `geometry`.\n","\n","The `popup` parameter is optional, and if it used, it must be a `dict` containing title and content as keys that will be displayed when the shape is clicked. In the case of a `FeatureSet`, title and content are names of attributes of the features in the `FeatureSet`, rather than actual string values for title and content.\n","\n","The `symbol` parameter is also optional, and if it is used, it should be a `dict` object. See the Symbol Objects page in the ArcGIS REST API documentation for more information. A default symbol is used if one is not specified. A helper utility to get the symbol format for several predefined symbols is available at the Esri symbol selector.\n","\n","The `attributes` parameter is again optional, and if it is used, it should be a `dict` object that specifies a `dict` containing name value pairs of fields and field values associated with the graphic.\n"]},{"cell_type":"markdown","id":"98472698","metadata":{},"source":["The examples we have seen so far have not been applied with customized symbols and pop-ups. Next, let's follow a quick example that visualizes a predefined Point object with the `symbol` parameter and draws the nearby hospitals with customized symbols and a self-defined `pop-up`:\n"]},{"cell_type":"code","execution_count":11,"id":"7e918cff","metadata":{},"outputs":[{"data":{"text/html":[""],"text/plain":[""]},"execution_count":11,"metadata":{},"output_type":"execute_result"}],"source":["from arcgis.gis import GIS\n","map7b = GIS().map(\"Port of Long Beach\")\n","map7b.basemap = 'gray'\n","map7b.zoom = 6\n","map7b"]},{"cell_type":"code","execution_count":6,"id":"98f341b8","metadata":{},"outputs":[],"source":["house_symbol = {\"angle\": 0, \"xoffset\": 0, \"yoffset\": 0, \"type\": \"esriPMS\",\n"," \"url\": \"http://static.arcgis.com/images/Symbols/Shapes/RedStarLargeB.png\",\n"," \"contentType\": \"image/png\", \"width\": 24, \"height\": 24}\n","map7b.draw(pt, symbol=house_symbol)"]},{"cell_type":"markdown","id":"4418f8cd","metadata":{},"source":["Now, let's use the `geocode` method to find a list of 10 hospitals near the defined Point object:\n"]},{"cell_type":"code","execution_count":8,"id":"f7dd1099","metadata":{},"outputs":[{"data":{"text/plain":["10"]},"execution_count":8,"metadata":{},"output_type":"execute_result"}],"source":["from arcgis.geocoding import geocode\n","hospitals = geocode('hospital', location=[-118.15, 33.80], max_locations=10)\n","len(hospitals)"]},{"cell_type":"code","execution_count":10,"id":"e9598921","metadata":{},"outputs":[],"source":["hospital_symbol = {\"angle\": 0, \"xoffset\": 0, \"yoffset\": 0, \"type\": \"esriPMS\",\n"," \"url\": \"http://static.arcgis.com/images/Symbols/SafetyHealth/Hospital.png\",\n"," \"contentType\": \"image/png\", \"width\": 24, \"height\": 24}\n","\n","for place in hospitals:\n"," popup = {\"title\": place['attributes']['PlaceName'],\n"," \"content\": place['attributes']['Place_addr']}\n"," map7b.draw(place['location'], symbol=hospital_symbol, popup=popup)"]},{"cell_type":"markdown","id":"7ef4f735","metadata":{},"source":["#### Draw sketches interactively\n","\n","You can draw or sketch graphics on the map interactively using the `draw()` method. For instance, you can draw and annotate `rectangles`, `ellipses`, `arrow marks`, etc. as shown below:\n"]},{"cell_type":"code","execution_count":80,"id":"003e1db1","metadata":{},"outputs":[{"data":{"text/html":[""],"text/plain":[""]},"metadata":{},"output_type":"display_data"}],"source":["usa_map = GIS().map(\"USA\")\n","usa_map"]},{"cell_type":"code","execution_count":48,"id":"48dd4e80","metadata":{},"outputs":[],"source":["usa_map.draw('rectangle')"]},{"cell_type":"code","execution_count":49,"id":"1e0a1e69","metadata":{},"outputs":[],"source":["usa_map.draw('circle')"]},{"cell_type":"markdown","id":"f8b73c1c","metadata":{},"source":["If the graphics are no longer needed, you can use the `clear_graphics` method to remove them.\n"]},{"cell_type":"code","execution_count":45,"id":"74b3aef6","metadata":{},"outputs":[],"source":["usa_map.clear_graphics()"]},{"cell_type":"markdown","id":"8eb4a3b4","metadata":{},"source":["### Customizing layer symbology\n","\n","While calling the `add_layer()` method, you can specify a set of renderer instructions as a dictionary to the `options` parameter. You have seen how to set the transparency for a layer in previous cells. The `opacity` value ranges from 0 - 1, with 0 being fully transparent and 1 being fully opaque.\n","\n","You can make use of the `\"smart mapping\"` capability to render feature layers with symbology that varies based on an attribute field of that layer. The cell below adds the \"USA Freeway System\" layer to the map and changes the width of the line segments based on the length of the freeway. Unlike in `map5` where freeways are classified in sizes, here you will see freeways classified in colors. For more, checkout smart mapping.\n"]},{"cell_type":"code","execution_count":82,"id":"3fd0f9fe","metadata":{},"outputs":[{"data":{"text/html":[""],"text/plain":[""]},"metadata":{},"output_type":"display_data"}],"source":["map5b = gis.map('USA', 10)\n","map5b"]},{"cell_type":"code","execution_count":51,"id":"57be64df","metadata":{},"outputs":[],"source":["map5b.center = [34.05, -118.2]\n","map5b.zoom = 12\n","map5b.basemap = 'dark-gray'"]},{"cell_type":"code","execution_count":54,"id":"fba8321b","metadata":{},"outputs":[],"source":["map5b.add_layer(freeway_fset, {'renderer': 'ClassedColorRenderer',\n"," 'field_name': 'DIST_KM',\n"," 'opacity': 0.5})"]},{"cell_type":"markdown","id":"20d931da","metadata":{},"source":["### Querying layers in a map\n","\n","You can list the layers added to the map using the `layers` property.\n"]},{"cell_type":"code","execution_count":56,"id":"63b889a7","metadata":{},"outputs":[{"data":{"text/plain":["[]"]},"execution_count":56,"metadata":{},"output_type":"execute_result"}],"source":["map1.layers"]},{"cell_type":"code","execution_count":61,"id":"095c2fe3","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["[]\n"]}],"source":["map5.layers"]},{"cell_type":"markdown","id":"de76e6c3","metadata":{},"source":["### Removing layers in a map\n","\n","To remove one or more layers, call the `remove_layers()` method and pass a list of layers that you want removed. To get a list of valid layers that can be removed, call the layers' `property` as shown in the previous cell.\n","\n","The code below shows how to remove the USA freeways layer:\n"]},{"cell_type":"code","execution_count":null,"id":"29f6a011","metadata":{},"outputs":[],"source":["map2.remove_layers(layers=[landsat_item])"]},{"cell_type":"markdown","id":"783a3ee4","metadata":{},"source":["### Viewing legend\n","\n","Enable the display of a `legend` on the map widget by setting the boolean to `True`:\n"]},{"cell_type":"code","execution_count":11,"id":"d49814fb","metadata":{},"outputs":[],"source":["map5b.legend = True"]},{"cell_type":"code","execution_count":12,"id":"f4ab1e64","metadata":{},"outputs":[{"data":{"text/html":[""],"text/plain":[""]},"execution_count":12,"metadata":{},"output_type":"execute_result"}],"source":["map5b.take_screenshot()"]},{"cell_type":"markdown","id":"f1b32299","metadata":{},"source":["Finally, you can turn the legend display off when it no longer needed by executing the following cell:\n"]},{"cell_type":"code","execution_count":63,"id":"9f5cf4f7","metadata":{},"outputs":[],"source":["map5b.legend = False"]},{"cell_type":"markdown","id":"1ad8fdcd","metadata":{},"source":["## Conclusion\n","\n","In Part 3 of this guide series, we have explored how to add single layers (`Web Layer`, local raster, or an in-memory `SeDF`) to a map, how to draw graphics and geometries, and how to perform other visualization operations. In the next chapter, we will explore how to display time-enabled data, the nature of drawing with the ArcGIS API for Javascript, and the best practices when rendering a large number of layers in a single map widget.\n","\n","Back to Top\n"]}],"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.7.11"},"toc":{"base_numbering":1,"nav_menu":{},"number_sections":false,"sideBar":true,"skip_h1_title":true,"title_cell":"Table of Contents","title_sidebar":"Contents","toc_cell":true,"toc_position":{},"toc_section_display":true,"toc_window_display":false},"varInspector":{"cols":{"lenName":16,"lenType":16,"lenVar":40},"kernels_config":{"python":{"delete_cmd_postfix":"","delete_cmd_prefix":"del ","library":"var_list.py","varRefreshCmd":"print(var_dic_list())"},"r":{"delete_cmd_postfix":") ","delete_cmd_prefix":"rm(","library":"var_list.r","varRefreshCmd":"cat(var_dic_list()) "}},"types_to_exclude":["module","function","builtin_function_or_method","instance","_Feature"],"window_display":false}},"nbformat":4,"nbformat_minor":5} +{ + "cells": [ + { + "cell_type": "markdown", + "id": "6b1de307", + "metadata": {}, + "source": [ + "# Part 3 - Visualizing spatial data on the map widget" + ] + }, + { + "cell_type": "markdown", + "id": "fdc477d2", + "metadata": { + "toc": true + }, + "source": [ + "

Table of Contents

\n", + "" + ] + }, + { + "cell_type": "markdown", + "id": "ff03e232", + "metadata": {}, + "source": [ + "First, to use the map widget, call `gis.map()` and assign it to a variable that you can then query to bring up the widget in the notebook:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "b9e456a7", + "metadata": {}, + "outputs": [], + "source": [ + "import arcgis\n", + "from arcgis.gis import GIS\n", + "# Create a GIS object, as an anonymous user for this example\n", + "gis = GIS()" + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "id": "041e3c1a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 91, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Create a map widget\n", + "map1 = gis.map('USA') # Passing a place name to the constructor\n", + " # will initialize the extent of the map.\n", + "map1" + ] + }, + { + "cell_type": "markdown", + "id": "b0cec17e", + "metadata": {}, + "source": [ + "## Adding single layer\n", + "\n", + "### Adding Web Layer to the map\n", + "\n", + "You can add a number of different layer objects, such as `FeatureLayer`, `FeatureCollection`, `ImageryLayer`, or `MapImageLayer`, to the map. For instance, you can use the following cell to add a `FeatureLayer`:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "7158617d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "world_countries_item = gis.content.get('ac80670eb213440ea5899bbf92a04998')\n", + "world_countries_layer = world_countries_item.layers[0]\n", + "world_countries_layer" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "8ca675d2", + "metadata": {}, + "outputs": [], + "source": [ + "map1.content.add(world_countries_layer, options={'opacity':0.4})" + ] + }, + { + "cell_type": "markdown", + "id": "e46c76f0", + "metadata": {}, + "source": [ + "Similar to `FeatureLayers`, you can also add `ImageryLayers` items. You can also specify either a built-in raster function or a custom one for rendering." + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "id": "ddf5017e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 96, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "map2 = gis.map('USA') # Passing a place name to the constructor\n", + " # will initialize the extent of the map.\n", + "map2" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "id": "7849a7d8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "
\n", + " \n", + " \n", + " \n", + "
\n", + "\n", + "
\n", + " Pansharpened Landsat\n", + " \n", + "
Landsat 8 and 9 pansharpened and multitemporal 15m imagery rendered on-the-fly as Natural Color with DRA for visualization and analysis. It is updated daily with new imagery directly sourced from the USGS Landsat collection on AWS.
Imagery Layer by esri\n", + "
Last Modified: August 24, 2022\n", + "
1 comments, 429,412 views\n", + "
\n", + "
\n", + " " + ], + "text/plain": [ + "" + ] + }, + "execution_count": 93, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "landsat_item = GIS().content.search(\"Landsat 8 Views\", \"Imagery Layer\", max_items=2)[0]\n", + "landsat_item" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "848f179d", + "metadata": {}, + "outputs": [], + "source": [ + "map2.content.add(landsat_item)" + ] + }, + { + "cell_type": "markdown", + "id": "6c1824c9", + "metadata": {}, + "source": [ + "To zoom to one or more layers, call the `zoom_to_layer()` method and pass a layer or list of layers that you want to snap your map to. The supplied item can be a single instance or a list of an `Item`, `Layer`, `DataFrame`, `FeatureSet`, or `FeatureCollection`.\n", + "\n", + "The code below shows how to zoom to a single layer:" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "id": "99967386", + "metadata": {}, + "outputs": [], + "source": [ + "map2.content.add(landsat_item)\n", + "map2.zoom_to_layer(landsat_item)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "cb1ec183", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "map3 = gis.map('USA') # Passing a place name to the constructor\n", + " will initialize the extent of the map.\n", + "map3" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "2605cae8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "
\n", + " \n", + " \n", + " \n", + "
\n", + "\n", + "
\n", + " U.S. Historical Precipitation Trends 1985 to 2010\n", + " \n", + "
Historical precipitation trends from 1895 to 2010. Precipitation is shown as percent annual precipitation change per century. Results are based on weather station data.
Map Image Layer by DSCGIS\n", + "
Last Modified: May 26, 2020\n", + "
0 comments, 5,564 views\n", + "
\n", + "
\n", + " " + ], + "text/plain": [ + "" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "prep_item = GIS().content.search(\"Precipitation\", \"Map Image Layer\", max_items=2)[1]\n", + "prep_item" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "adfff312", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "prep_layer = prep_item.layers[1]\n", + "prep_layer" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "552a8135", + "metadata": {}, + "outputs": [], + "source": [ + "map3.content.add(prep_item)\n", + "map3.zoom_to_layer(prep_layer)" + ] + }, + { + "cell_type": "markdown", + "id": "d50b33ab", + "metadata": {}, + "source": [ + "### Adding local and in-memory layers to the map \n", + "\n", + "#### Add a Spatially-Enabled Data Frame (SeDF)\n", + "\n", + "We can read census data on major cities and load the data into a `Spatially Enabled DataFrame (SeDF)`, which will then allow us to visualize the points by calling the `plot()` method off the `SeDF`'s spatial accessor and passing the map you created to it." + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "6ecbe534", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
FIDNAMECLASSSTSTFIPSPLACEFIPSCAPITALPOP_CLASSPOPULATIONPOP2010...MARHH_NO_CMHH_CHILDFHH_CHILDFAMILIESAVE_FAM_SZHSE_UNITSVACANTOWNER_OCCRENTER_OCCSHAPE
0306Purdue UniversityCensus Designated PlaceIN18186238861236512183...2449234912.66136210441254{\"x\": -9676280.259129459, \"y\": 4928486.7435240...
1600StanfordCensus Designated PlaceCA06067390661517913809...689133212302.773999867903123{\"x\": -13599607.21449169, \"y\": 4498534.6279709...
2682Fort CarsonCensus Designated PlaceCO08082737061389113813...3193942123193.592629180152434{\"x\": -11664455.032700792, \"y\": 4684537.534384...
3742StorrsCensus Designated PlaceCT09097398061556915344...27614184892.691737166592979{\"x\": -8043406.213645851, \"y\": 5131741.8140288...
41035Camp Pendleton SouthCensus Designated PlaceCA06061056161045010616...3893716925003.692865296112558{\"x\": -13066535.250599463, \"y\": 3925680.810605...
\n", + "

5 rows × 50 columns

\n", + "
" + ], + "text/plain": [ + " FID NAME CLASS ST STFIPS PLACEFIPS \\\n", + "0 306 Purdue University Census Designated Place IN 18 1862388 \n", + "1 600 Stanford Census Designated Place CA 06 0673906 \n", + "2 682 Fort Carson Census Designated Place CO 08 0827370 \n", + "3 742 Storrs Census Designated Place CT 09 0973980 \n", + "4 1035 Camp Pendleton South Census Designated Place CA 06 0610561 \n", + "\n", + " CAPITAL POP_CLASS POPULATION POP2010 ... MARHH_NO_C MHH_CHILD \\\n", + "0 6 12365 12183 ... 244 9 \n", + "1 6 15179 13809 ... 689 13 \n", + "2 6 13891 13813 ... 319 39 \n", + "3 6 15569 15344 ... 276 14 \n", + "4 6 10450 10616 ... 389 37 \n", + "\n", + " FHH_CHILD FAMILIES AVE_FAM_SZ HSE_UNITS VACANT OWNER_OCC RENTER_OCC \\\n", + "0 23 491 2.66 1362 104 4 1254 \n", + "1 32 1230 2.77 3999 86 790 3123 \n", + "2 421 2319 3.59 2629 180 15 2434 \n", + "3 18 489 2.69 1737 166 592 979 \n", + "4 169 2500 3.69 2865 296 11 2558 \n", + "\n", + " SHAPE \n", + "0 {\"x\": -9676280.259129459, \"y\": 4928486.7435240... \n", + "1 {\"x\": -13599607.21449169, \"y\": 4498534.6279709... \n", + "2 {\"x\": -11664455.032700792, \"y\": 4684537.534384... \n", + "3 {\"x\": -8043406.213645851, \"y\": 5131741.8140288... \n", + "4 {\"x\": -13066535.250599463, \"y\": 3925680.810605... \n", + "\n", + "[5 rows x 50 columns]" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# create an anonymous connection to ArcGIS Online and get a public item\n", + "census_item = gis.content.get(\"85d0ca4ea1ca4b9abf0c51b9bd34de2e\")\n", + "census_flayer = census_item.layers[0]\n", + "\n", + "# Specify a SQL query and get a sub-set of the original data as a DataFrame\n", + "census_df = census_flayer.query(where=\"AGE_45_54 < 450\").sdf\n", + "\n", + "# Visualize the top 5 records\n", + "census_df.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "6579f286", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "map4 = GIS().map(\"United States\")\n", + "map4" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "a643166f", + "metadata": {}, + "outputs": [], + "source": [ + "map4.zoom = 4\n", + "map4.center = [39,-98]" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "2ea643de", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "census_df.spatial.plot(map_widget= map4)" + ] + }, + { + "cell_type": "markdown", + "id": "16e9bb5e", + "metadata": {}, + "source": [ + "#### Add an in-memory FeatureSet\n", + "\n", + "Besides adding in the the `SeDF`, we can also add a `FeatureSet`. For example, below we will search for the USA freeway layer and visualize it by adding it to the map widget as a `FeatureSet` object returned by the query method. Once added, we then draw it with default line symbols." + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "5bb9779c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "
\n", + " \n", + " \n", + " \n", + "
\n", + "\n", + "
\n", + " USA Freeway System\n", + " \n", + "
This layer presents rural and urban interstate highways.
Feature Layer Collection by esri_dm\n", + "
Last Modified: February 06, 2023\n", + "
5 comments, 5,479,816 views\n", + "
\n", + "
\n", + " " + ], + "text/plain": [ + "" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "freeway_item = gis.content.search('title:USA Freeway System AND owner:esri_dm', \n", + " item_type = 'Feature Layer')[0]\n", + "freeway_item" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "c90adf65", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + " 7 features" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "freeway_sdf = freeway_item.layers[0]\n", + "freeway_fset = freeway_sdf.query(where=\"DIST_KM>2800\", as_df=False)\n", + "freeway_fset" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "id": "9d67b60e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "map5 = gis.map('USA')\n", + "map5" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "id": "dc437d33", + "metadata": {}, + "outputs": [], + "source": [ + "map5.zoom = 6\n", + "map5.center = [39,-98]" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "id": "e8fa2a03", + "metadata": {}, + "outputs": [], + "source": [ + "map5.content.add(freeway_fset, options={'opacity':0.75})" + ] + }, + { + "cell_type": "markdown", + "id": "ac40c28f", + "metadata": {}, + "source": [ + "#### Adding a local raster\n", + "\n", + "A local raster can be read into the map widget as a `numpy` array with `arcgis.raster` imported on the machine.\n", + "\n", + "
\n", + " Note: Local raster can only be loaded for display when `arcpy` is installed on the current environment.\n", + "
" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "6ab8df0e", + "metadata": {}, + "outputs": [], + "source": [ + "import numpy\n", + "import matplotlib.pyplot as plt\n", + "from arcgis.raster import Raster" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "6b11f5c6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "map6 = GIS().map(\"Newark, NJ\")\n", + "map6" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0bb7a69e-89f2-47d8-8d3e-68b92fd17023", + "metadata": {}, + "outputs": [], + "source": [ + "raster = Raster(r'../../static/img/newark_nj_1922.jpg',\n", + " extent = {\"xmin\":-74.22655,\n", + " \"ymin\":40.712216,\n", + " \"xmax\":-74.12544,\n", + " \"ymax\":40.773941,\n", + " \"spatialReference\":{\"wkid\":4326}},\n", + " opacity = 0.85)\n", + "map6.content.add(raster)" + ] + }, + { + "cell_type": "markdown", + "id": "20a21e01", + "metadata": {}, + "source": [ + "To immediately release the in-app memory immediate, run the follow cell to remove the raster layer:" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "98facb06", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "map6.content.remove(0)" + ] + }, + { + "cell_type": "markdown", + "id": "d628726f", + "metadata": {}, + "source": [ + "## Other operations" + ] + }, + { + "cell_type": "markdown", + "id": "b71f8e59", + "metadata": {}, + "source": [ + "### Drawing graphics and geometries\n", + "\n", + "#### Draw geometries" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "f0cd179d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "map7 = gis.map(\"Port of Long Beach\")\n", + "map7.basemap.basemap = \"satellite\"\n", + "map7" + ] + }, + { + "cell_type": "markdown", + "id": "5d246a62", + "metadata": {}, + "source": [ + "When it comes to adding geometries, we can either create a geometry object and use the `draw` method to apply the object onto our display, or we can input `point`, `polyline`, or `polygon` as keywords in the `draw()` method, which will then trigger a user interactive drawing session on the widget." + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "c3f1e821-c2f0-435c-bba7-d19bb5f44c8a", + "metadata": {}, + "outputs": [], + "source": [ + "map7.zoom = 6" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "8bede951", + "metadata": {}, + "outputs": [], + "source": [ + "from arcgis.geometry import Geometry\n", + "\n", + "pt = Geometry({\"x\" : -118.15, \"y\" : 33.80, \n", + " \"spatialReference\" : {\"wkid\" : 4326}})" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "71ac82e4", + "metadata": {}, + "outputs": [], + "source": [ + "map7.content.draw(pt)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "72c97901", + "metadata": {}, + "outputs": [], + "source": [ + "line = Geometry({\n", + " \"paths\":[\n", + " [[-118.38, 33.82], [-118.38, 33.92], [-118.18, 33.92]]\n", + " ],\n", + " \"spatialReference\":{\"wkid\":4326}\n", + "})\n", + "map7.content.draw(line)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "c014a9ef", + "metadata": {}, + "outputs": [], + "source": [ + "polygon = Geometry({\n", + " \"rings\":[\n", + " [[-118.38, 33.82], [-118.38, 33.92], [-118.18, 33.92], [-118.18, 33.82]]\n", + " ],\n", + " \"spatialReference\":{\"wkid\":4326}\n", + "})\n", + "map7.content.draw(polygon)" + ] + }, + { + "cell_type": "markdown", + "id": "26bf5886", + "metadata": {}, + "source": [ + "#### Draw with symbols and pop-ups\n", + "\n", + "The `draw()` method can be used to draw a deterministic shape on the map widget when the input shape is a known `Geometry` object, a list of coordinate pairs, a `FeatureSet`, or a `dict` object that represents a geometry. The other way to use `draw` is to provide users with an interactive sketch and display experience when the input shape is one of the expected strings, e.g. \"circle\", \"ellipse\", \"Polygon\", \"Polyline\", \"MultiPoint\", \"Point\", \"rectangle\", \"triangle\".\n", + "\n", + "\n", + "```\n", + "draw(shape, popup=None, symbol=None, attributes=None)\n", + "# The draw method draws a shape on the map widget.\n", + "```\n", + "\n", + "
\n", + " Note: Anything can be drawn from known `Geometry` objects, coordinate pairs, and `FeatureSet` objects.\n", + "
\n", + "\n", + "The `shape` argument can be one of these four objects:\n", + " - Known `Geometry` objects: Shape is one of the following: circle, ellipse, Polygon, Polyline, MultiPoint, Point, rectangle, triangle. \n", + " - Coordinate pair: specified shape as a list of [lat, long]. Eg: [34, -81] \n", + " - `FeatureSet`: shape can be a FeatureSet object.\n", + " - `Dict` object representing a `geometry`.\n", + "\n", + "The `popup` parameter is optional, and if it used, it must be a `dict` containing title and content as keys that will be displayed when the shape is clicked. In the case of a `FeatureSet`, title and content are names of attributes of the features in the `FeatureSet`, rather than actual string values for title and content.\n", + "\n", + "The `symbol` parameter is also optional, and if it is used, it should be a `dict` object. See the Symbol Objects page in the ArcGIS REST API documentation for more information. A default symbol is used if one is not specified. A helper utility to get the symbol format for several predefined symbols is available within the Esri symbol selector.\n", + "\n", + "The `attributes` parameter is again optional, and if it is used, it should be a `dict` object that specifies a `dict` containing name value pairs of fields and field values associated with the graphic." + ] + }, + { + "cell_type": "markdown", + "id": "98472698", + "metadata": {}, + "source": [ + "The examples we have seen so far have not been applied with customized symbols and pop-ups. Next, let's follow a quick example that visualizes a predefined Point object with the `symbol` parameter and draws the nearby hospitals with customized symbols and a self-defined `pop-up`:" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "7e918cff", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from arcgis.gis import GIS\n", + "map7b = GIS().map(\"Port of Long Beach\")\n", + "map7b.basemap.basemap ='gray-vector'\n", + "map7b" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "21e3e544-edca-494d-a157-1207e299aa77", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "15.0" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "map7b.zoom" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "98f341b8", + "metadata": {}, + "outputs": [], + "source": [ + "from arcgis.map.symbols import PictureMarkerSymbolEsriPMS\n", + "from arcgis.map.popups import PopupInfo\n", + "\n", + "house_symbol = PictureMarkerSymbolEsriPMS(**{\"angle\":0,\"xoffset\":0,\"yoffset\":0,\"type\":\"esriPMS\",\n", + " \"url\":\"http://static.arcgis.com/images/Symbols/Shapes/RedStarLargeB.png\",\n", + " \"contentType\":\"image/png\",\"width\":24,\"height\":24})\n", + "map7b.content.draw(pt, symbol=house_symbol)" + ] + }, + { + "cell_type": "markdown", + "id": "4418f8cd", + "metadata": {}, + "source": [ + "Now, let's use the `geocode` method to find a list of 10 hospitals near the defined Point object:" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "f7dd1099", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from arcgis.geocoding import geocode\n", + "hospitals = geocode('hospital', location=[-118.15, 33.80], max_locations=10)\n", + "len(hospitals)" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "7ed90fd6-66c1-4b7f-9f7c-8a355de957f6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-118.145887142854" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "hospitals[0]['attributes']['X']" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "e9598921", + "metadata": {}, + "outputs": [], + "source": [ + "hospital_symbol = PictureMarkerSymbolEsriPMS(angle = 0, xoffset = 0, yoffset = 0,\n", + " url = \"http://static.arcgis.com/images/Symbols/SafetyHealth/Hospital.png\",\n", + " content_type = \"image/png\", width = 24, height = 24)\n", + "hospital_symbol.type = \"esriPMS\"\n", + "\n", + "\n", + "for place in hospitals:\n", + " popup= PopupInfo(**{\"title\" : place['attributes']['PlaceName'], \n", + " \"description\" : place['attributes']['Place_addr']})\n", + " location = Geometry({\"x\" : place['attributes']['X'], \"y\" : place['attributes']['Y'], \n", + " \"spatialReference\" : {\"wkid\" : 4326}})\n", + " map7b.content.draw(location, symbol=hospital_symbol, popup=popup)" + ] + }, + { + "cell_type": "markdown", + "id": "7ef4f735", + "metadata": {}, + "source": [ + "#### Draw sketches interactively\n", + "\n", + "You can draw or sketch graphics on the map interactively using the `draw()` method. For instance, you can draw and annotate `rectangles`, `ellipses`, `arrow marks`, etc. as shown below:" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "003e1db1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "usa_map = GIS().map(\"USA\")\n", + "usa_map" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "48dd4e80", + "metadata": {}, + "outputs": [], + "source": [ + "# create rectangle geometry\n", + "from arcgis.geometry import Geometry\n", + "usa_polygon = Geometry({\n", + " \"rings\" : [[[-131, 55], [-131, 20], [-63, 20], [-63, 55]]],\n", + " \"spatialReference\" : {\"wkid\" : 4326}\n", + "})\n", + "usa_map.content.draw(usa_polygon)" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "id": "1d1505fe-f306-4a3a-aa00-6eb037a1ef6d", + "metadata": {}, + "outputs": [], + "source": [ + "usa_map.zoom = 3\n", + "usa_map.center = [39,-98]" + ] + }, + { + "cell_type": "markdown", + "id": "f8b73c1c", + "metadata": {}, + "source": [ + "If the graphics are no longer needed, you can use the `.content.remove()` method to remove them." + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "74b3aef6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "usa_map.content.remove(0)" + ] + }, + { + "cell_type": "markdown", + "id": "8eb4a3b4", + "metadata": {}, + "source": [ + "### Customizing layer symbology\n", + "\n", + "While calling the `.content.add()` method, you can specify a set of renderer instructions as a dictionary to the `options` parameter. You have seen how to set the transparency for a layer in previous cells. The `opacity` value ranges from 0 - 1, with 0 being fully transparent and 1 being fully opaque.\n", + "\n", + "You can make use of the `\"smart mapping\"` capability to render feature layers with symbology that varies based on an attribute field of that layer. The cell below adds the \"USA Freeway System\" layer to the map and changes the width of the line segments based on the length of the freeway. Unlike in `map5` where freeways are classified in sizes, here you will see freeways classified in colors. For more, checkout smart mapping." + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "id": "3fd0f9fe", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "map5b = gis.map('USA')\n", + "map5b" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "id": "57be64df", + "metadata": {}, + "outputs": [], + "source": [ + "map5b.zoom = 4\n", + "map5b.center = [39,-98]" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "id": "fba8321b", + "metadata": {}, + "outputs": [], + "source": [ + "map5b.content.add(freeway_fset, options={'opacity':0.9})" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "id": "16622c0a", + "metadata": {}, + "outputs": [], + "source": [ + "sm_manager = map5b.content.renderer(0).smart_mapping()\n", + "sm_manager.class_breaks_renderer(break_type='color', field=\"DIST_KM\")" + ] + }, + { + "cell_type": "markdown", + "id": "20d931da", + "metadata": {}, + "source": [ + "### Querying layers in a map \n", + "\n", + "You can list the layers added to the map using the `content.layers` property." + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "id": "63b889a7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[]" + ] + }, + "execution_count": 77, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "map1.content.layers" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "id": "095c2fe3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[]" + ] + }, + "execution_count": 82, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "map5.content.layers" + ] + }, + { + "cell_type": "markdown", + "id": "de76e6c3", + "metadata": {}, + "source": [ + "### Removing layers in a map\n", + "\n", + "To remove one or more layers, call the `.content.remove()` method and pass a list of layers that you want removed. To get a list of valid layers that can be removed, call the `.content.layers` property as shown in the previous cell.\n", + "\n", + "The code below shows how to remove the USA freeways layer:" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "id": "29f6a011", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 88, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "map1.content.remove(0)" + ] + }, + { + "cell_type": "markdown", + "id": "783a3ee4", + "metadata": {}, + "source": [ + "### Viewing legend\n", + "\n", + "Enable the display of a `legend` on the map widget by setting the boolean to `True`:" + ] + }, + { + "cell_type": "code", + "execution_count": 89, + "id": "d49814fb", + "metadata": {}, + "outputs": [], + "source": [ + "map5b.legend.enabled = True" + ] + }, + { + "cell_type": "markdown", + "id": "f1b32299", + "metadata": {}, + "source": [ + "Finally, you can turn the legend display off when it no longer needed by executing the following cell:" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "id": "9f5cf4f7", + "metadata": {}, + "outputs": [], + "source": [ + "map5b.legend.enabled = False" + ] + }, + { + "cell_type": "markdown", + "id": "1ad8fdcd", + "metadata": {}, + "source": [ + "## Conclusion\n", + "\n", + "In Part 3 of this guide series, we have explored how to add single layers (`Web Layer`, local raster, or an in-memory `SeDF`) to a map, how to draw graphics and geometries, and how to perform other visualization operations. In the next chapter, we will explore how to display time-enabled data, the nature of drawing with the ArcGIS API for Javascript, and the best practices when rendering a large number of layers in a single map widget.\n", + "\n", + "Back to Top" + ] + } + ], + "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.11.9" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": false, + "sideBar": true, + "skip_h1_title": true, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": true, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false + }, + "varInspector": { + "cols": { + "lenName": 16, + "lenType": 16, + "lenVar": 40 + }, + "kernels_config": { + "python": { + "delete_cmd_postfix": "", + "delete_cmd_prefix": "del ", + "library": "var_list.py", + "varRefreshCmd": "print(var_dic_list())" + }, + "r": { + "delete_cmd_postfix": ") ", + "delete_cmd_prefix": "rm(", + "library": "var_list.r", + "varRefreshCmd": "cat(var_dic_list()) " + } + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From dd4c2c0a99cb723d8477bb988191e12efa1d7fa4 Mon Sep 17 00:00:00 2001 From: Manushi Majumdar Date: Tue, 24 Sep 2024 16:56:51 -0400 Subject: [PATCH 2/4] 2.4.0 changes - removal of 2 sections, added 1 --- ...ng_updating_and_embedding_map_widget.ipynb | 532 +++++++++++++++++- 1 file changed, 531 insertions(+), 1 deletion(-) diff --git a/guide/10-mapping-and-visualization/part5_saving_updating_and_embedding_map_widget.ipynb b/guide/10-mapping-and-visualization/part5_saving_updating_and_embedding_map_widget.ipynb index e0b09188c..2b7b7a13b 100644 --- a/guide/10-mapping-and-visualization/part5_saving_updating_and_embedding_map_widget.ipynb +++ b/guide/10-mapping-and-visualization/part5_saving_updating_and_embedding_map_widget.ipynb @@ -1 +1,531 @@ -{"cells":[{"cell_type":"markdown","metadata":{},"source":["# Part 5 - Saving, embedding and exporting the map widget\n"]},{"cell_type":"markdown","metadata":{"toc":true},"source":["

Table of Contents

\n","\n"]},{"cell_type":"markdown","metadata":{},"source":["## Saving a web map\n","\n","Starting with the Python API version 1.3, you can save the map widget as a web map in your `GIS`. This process persists the basemaps, layers added (with or without your custom symbology, including smart mapping), pop-ups, extent, and graphics drawn (with or without custom symbols) as layers in your `web map`.\n","\n","To save the map, call the `save()` method. This method creates and returns a new `Web Map Item` object. As parameters, you can specify all valid Item properties as shown below:\n"]},{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[],"source":["from arcgis.gis import GIS\n","# gis = GIS(\"your_online_profile\")\n","gis = GIS()"]},{"cell_type":"code","execution_count":2,"metadata":{},"outputs":[{"data":{"text/plain":[""]},"execution_count":2,"metadata":{},"output_type":"execute_result"}],"source":["world_countries_item = gis.content.get('ac80670eb213440ea5899bbf92a04998')\n","world_countries_layer = world_countries_item.layers[0]\n","world_countries_layer"]},{"cell_type":"code","execution_count":5,"metadata":{},"outputs":[{"data":{"text/html":[""],"text/plain":[""]},"metadata":{},"output_type":"display_data"}],"source":["map1 = gis.map(\"USA\")\n","map1"]},{"cell_type":"code","execution_count":4,"metadata":{},"outputs":[],"source":["map1.add_layer(world_countries_layer)"]},{"cell_type":"code","execution_count":5,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["\n"]}],"source":["webmap_properties = {'title': 'World Countries 2022-01-06 16:33pm',\n"," 'snippet': 'Jupyter notebook widget saved as a web map',\n"," 'tags': ['automation', 'python']}\n","\n","webmap_item = map1.save(webmap_properties, thumbnail=r\".\\webmap_thumbnail.PNG\")\n","print(webmap_item)"]},{"cell_type":"markdown","metadata":{},"source":["## Updating a web map\n","\n","Similarly, if you created the `WebMap` object from an existing web map item, you can call the `update()` method to update it.\n","\n","Note, the `save()` method always creates a new item with an updated web map definition, so this is a great way to create a copy of an existing web map. For instance, the cell below calls the `save()` method and creates a new web map item with the new set of layers, without disturbing the original web map item.\n"]},{"cell_type":"markdown","metadata":{},"source":["We can search for web maps just like any other item:\n"]},{"cell_type":"code","execution_count":14,"metadata":{},"outputs":[],"source":["webmap_search = gis.content.search(\n"," \"COVID AND UK\", item_type=\"Web Map\", outside_org=True)\n","covid_map_item = webmap_search[0]"]},{"cell_type":"markdown","metadata":{},"source":["You can also create a WebMap object from an existing web map item by passing the web map item as the parameter to the constructor:\n"]},{"cell_type":"code","execution_count":13,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Coronavirus COVID-19 Cases V2 - Deaths\n","Coronavirus COVID-19 Cases V2 - Cases\n","Coronavirus COVID-19 Cases V2 - Cases_country\n","Coronavirus COVID-19 Cases V2 - Cases_state\n","\n"]}],"source":["from arcgis.mapping import WebMap\n","\n","covid_map = WebMap(covid_map_item)\n","for layer in covid_map.layers:\n"," print(layer.title)"]},{"cell_type":"markdown","metadata":{},"source":["You can update a web map by adding or removing layers. For instance, the cell below removes one of the layers from the web map and adds a different layer.\n"]},{"cell_type":"code","execution_count":50,"metadata":{},"outputs":[],"source":["# remove the first - unclassed layer from the map\n","covid_map.remove_layer(covid_map.layers[1])"]},{"cell_type":"code","execution_count":58,"metadata":{},"outputs":[{"data":{"text/plain":["True"]},"execution_count":58,"metadata":{},"output_type":"execute_result"}],"source":["covid_map.add_layer(world_countries_layer, options={\n"," 'title': 'World Countries'})"]},{"cell_type":"code","execution_count":59,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Coronavirus COVID-19 Cases V2 - Deaths\n","Coronavirus COVID-19 Cases V2 - Cases_country\n","Coronavirus COVID-19 Cases V2 - Cases_state\n","World Countries\n"]}],"source":["for layer in covid_map.layers:\n"," print(layer.title)"]},{"cell_type":"markdown","metadata":{},"source":["Next, let's add a layer to the existing `map1` and update the web map item:\n"]},{"cell_type":"code","execution_count":8,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["\n","\n"]}],"source":["from arcgis.features import FeatureLayer\n","# add layer via\n","# map1.add_layer(covid_map.layers[6])\n","# or alternatively\n","map1.add_layer(FeatureLayer(\n"," \"https://services.arcgis.com/qHLhLQrcvEnxjtPr/arcgis/rest/services/UK_COVID_19_Data/FeatureServer/1\"), )\n","\n","for layer in map1.layers:\n"," print(layer)"]},{"cell_type":"code","execution_count":46,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["\n","\n"]}],"source":["from arcgis.features import FeatureLayer\n","# add layer via\n","# map1.add_layer(covid_map.layers[6])\n","# or alternatively\n","map1.add_layer(FeatureLayer(\n"," \"https://services.arcgis.com/qHLhLQrcvEnxjtPr/arcgis/rest/services/UK_COVID_19_Data/FeatureServer/1\"), )\n","\n","for layer in map1.layers:\n"," print(layer)"]},{"cell_type":"markdown","metadata":{},"source":["Using the `save` method will create a new web map item, while using the `update` method will make updates to the previous web map item:\n","\n","```\n","update(mode=None, item_properties=None, thumbnail=None, metadata=None)\n","```\n","\n","The `update` method uses the following parameters:\n","\n"," --------------- --------------------------------------------------------------------\n"," item_properties Optional dictionary. See table below for the keys and values.\n"," --------------- --------------------------------------------------------------------\n"," mode Optional string. Whether to save this map instance as a 2D WebMap,\n"," or a 3D WebScene. Possible strings: ``2D``, ``webmap``, ``3D``, or\n"," ``webscene``.\n"," --------------- --------------------------------------------------------------------\n"," thumbnail Optional string. Either a path or URL to a thumbnail image.\n"," --------------- --------------------------------------------------------------------\n"," metadata Optional string. Either a path or URL to the metadata.\n"," --------------- --------------------------------------------------------------------\n","\n","\n","The `update` method will update the Web Map/Web Scene item that was used to create the `Map Widget` object. In addition, you are also able to update the item's other properties, thumbnail, and metadata.\n"]},{"cell_type":"code","execution_count":50,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["True\n"]}],"source":["webmap_item_properties = {'title': 'COVID-19 Cases in UK (Updated 2022-01-06)',\n"," 'snippet': 'Map created using Python API showing COVID cases in UK',\n"," 'tags': ['automation', 'world health', 'python']}\n","\n","new_map_item = map1.update(item_properties=webmap_item_properties,\n"," mode=\"2D\", thumbnail='./webmap_thumbnail.png')\n","print(new_map_item)"]},{"cell_type":"code","execution_count":52,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["\n"]}],"source":["# We can see from the output that the title is being updated:\n","print(webmap_item)"]},{"cell_type":"markdown","metadata":{},"source":["## Creating static maps via `take_screenshot`\n","\n","Since version 1.5.2, the ArcGIS API for Python supports the `take_screenshot()` method, which will create a screenshot of the current widget view. Note that the method only works in a Jupyter Notebook environment.\n","\n","```\n","take_screenshot(output_in_cell=True, set_as_preview=True, file_path='')\n","```\n","\n","The method takes the following arguments:\n","\n","- `output_in_cell`: Optional boolean that defaults to True. This will display the screenshot in the output area of the cell where this function is called.\n","- `set_as_preview`: Optional boolean that defaults to True. This will set the screenshot as the static image preview in the cell where the map widget is being displayed. Use this flag if you want the generated HTML previews of your notebook to have a map image visible.\n","- `file_path`: Optional String that defaults to “”. To output the screenshot to a .png file on disk, set this String to a writeable location file path (Ex. file_path = “/path/to/documents/my_screenshot.png”).\n","\n","In all notebook outputs, each image will be encoded to a base64 data URI and wrapped in an HTML tag. This means that the data for the image lives inside the notebook file itself, allowing for easy sharing of notebooks and generated HTML previews of notebooks.\n","\n","
\n"," Note: This function acts asynchronously, meaning that the Python function will return right away, with the notebook outputs/files being written after an indeterminate amount of time. Avoid calling this function multiple times in a row if the asynchronous portion of the function has not finished yet.\n","
\n","\n","
\n"," Another Note: Some limitations exist for taking screenshots in 2D mode, since the underlying screenshot mechanism is the ArcGIS JS API’s PrintTask object (https://bit.ly/2qRKGJG). If this function fails to create a screenshot, try a slightly different extent and try again. Non-published features with client-side graphics like Spatially Enabled DataFrames might not display in a 2D mode screenshot.\n","
\n"]},{"cell_type":"code","execution_count":6,"metadata":{},"outputs":[{"data":{"text/html":[""],"text/plain":[""]},"metadata":{},"output_type":"display_data"}],"source":["map1.take_screenshot()"]},{"cell_type":"markdown","metadata":{},"source":["## Export live map in a separate HTML file\n","\n","Another new feature in 1.5.0 is the ability to export the current widget as a stand-alone HTML file. This added functionality provides a new workflow for creating standalone HTML maps that can be shared with anyone. All they need is a web browser!\n","\n","The cell below will write the current widget to the file specified by a string path. Run the cell, find the file on your disk, and open it in your favorite web browser.\n","\n","
\n"," Note: By default, only publicly accessible layers will show on the exported HTML maps. You must specify that `credentials_prompt=True` to allow users to authenticate to the GIS to view private layers. See the API reference for more information.\n","
\n"]},{"cell_type":"code","execution_count":90,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["html saved as C:\\Users\\Username\\Documents\\home\\myWorldMap.html\n"]}],"source":["import os\n","\n","file_dir = os.path.join(os.getcwd(), 'home')\n","if not os.path.isdir(file_dir):\n"," os.mkdir(file_dir)\n","\n","file_path = os.path.join(file_dir, 'myWorldMap.html')\n","\n","map1.export_to_html(file_path)\n","print(\"html saved as \" + file_path)"]},{"cell_type":"markdown","metadata":{},"source":["## Embedding a live map\n","\n","The `embed` method embeds the current state of the map into the underlying notebook as an interactive HTML/JS/CSS element.\n","\n","```\n","embed(output_in_cell=True, set_as_preview=True)\n","```\n","\n","Its arguments include:\n","\n","- `output_in_cell`: Optional boolean that defaults to `True`. This will display the embedded HTML interactive map in the output area of the cell where this function is called.\n","- `set_as_preview`: Optional boolean that defaults to `True`. This will display the embedded HTML interactive map in the cell where the map widget is being displayed. Use this flag if you want the generated HTML previews of your notebook to have an interactive map displayed.\n","\n","
\n"," Note: This element will always display this `snapshot` state of the map, regardless of any future Python code ran.\n","
\n"]},{"cell_type":"markdown","metadata":{},"source":["In all notebook outputs, each embedded HTML element will contain the entire map state and all relevant HTML wrapped in an `