<jupyter_start><jupyter_text>Using and updating GIS contentThe GIS is a warehouse of geographic content and services. Arcgis includes several classes to make use of these content, publish new items and update the them when needed. This sample on updating the content of web maps and web scenes will demonstrate the following * **Replace web layers** of a web map. For instance, you can use this to update a web map when the services it points to were deleted. During the process, the sample shows how to read a web feature layer as a **FeatureService** object and inspect its properties. * **Drive the map widget by code**. In addition to displaying the interactive map widget, you can also set it to load at a particular extent. This is great for presentation purposes. During this process, the sample shows how to create and use a **MapView** object and a **Geocoder** object. * Make a **copy of a public web scene** item into your contents and then update it.     * Edit the list of layers to remove unnecessary ones.     * **Replace the basemap** of the web scene. In this step the sample shows how to search for **groups** and query the member items.     * Change visibility of layers.<jupyter_code>from arcgis.gis import GIS
from IPython.display import display

gis = GIS("https://python.playground.esri.com/portal", "arcgis_python", "amazing_arcgis_123")<jupyter_output><empty_output><jupyter_text>Data preparationTo run through the sample, we require some published web layers, web maps and web scene items. This section contains cells that show how to publish them. If you would like to work with your own datasets or have these items already published, you can skip this section. To understand the publishing process in detail, check out other samples within this samples directory.<jupyter_code>sd_file = "data/updating_gis_content/Ebola_Treatment_Units.sd"
# add the sd file as an item and publish it as a web layer
item = gis.content.add({},sd_file)
new_item = item.publish()<jupyter_output><empty_output><jupyter_text>Now that the web layers are published, run through this section to publish a web map. To understand how this part of the sample works, refer to the sample titled **Publishing web maps and web scenes**<jupyter_code>import json
web_map_json = str()

# read web map json from text file
with open("data/updating_gis_content/web_map_Ebola.json","r") as file_handle:
    web_map_json = json.load(file_handle)

# publish a web map
web_map_item_properties = {'title':'Ebola treatment locations',
                          'type':'Web Map',
                          'snippet':'This map shows locations of Ebola treatment centers in Africa',
                          'tags':'ArcGIS Python API',
                          'text':json.dumps(web_map_json)}

web_map_item = gis.content.add(web_map_item_properties)<jupyter_output><empty_output><jupyter_text>Using and updating a web mapIn the data preparation section above, we published a web map to play with. We will search for that web map, draw it and update it if necessary.<jupyter_code>search_result = gis.content.search("title:Ebola treatment locations", item_type = "Web Map")
display(search_result)
# display the first search result to confirm the item
web_map_item = search_result[0]
display(web_map_item)
import arcgis

# create a web map object out of the item
web_map_obj = arcgis.mapping.WebMap(web_map_item)

# display the web map obj in an interactive widget
web_map_obj<jupyter_output><empty_output><jupyter_text>![web_map_obj](http://esri.github.io/arcgis-python-api/notebooks/nbimages/05_Using_updating_GIS_01.PNG)  Fix errors in web mapThe widget loads an empty web map with just a basemap. Let us investigate the contents of the web map to determine the issue. Let us start with `operationalLayers` dictionary which contains the list of layers and inspect each layer.<jupyter_code>layer_list = web_map_obj['operationalLayers']
display(layer_list)<jupyter_output><empty_output><jupyter_text>The web map has only 1 layer and that points to a feature service title **Ebola_Facilities**. Let us verify if a feature service of that name exists on the server. If not, let us try to find the closest match.<jupyter_code>search_result = gis.content.search('title:Ebola_Facilities', item_type = 'Feature Service')
display(search_result)<jupyter_output><empty_output><jupyter_text>Let us change the search query leaving just the word `Ebola` in the title.<jupyter_code>search_result = gis.content.search('title:Ebola', item_type='Feature Layer')
search_result[0]<jupyter_output><empty_output><jupyter_text>It is likely the old service was deleted and a new one was with a different name was published. Let us update the web map dictionary with the correct url. But before that, we need to investigate if the new service also has layer with id `1` like the previous service.<jupyter_code>ebola = search_result[0]
ebola.layers<jupyter_output><empty_output><jupyter_text>The new feature service does have a layer with id `1`. Hence we can use the same layer id while switching the url. While updating the web map, it is important to not only update the **url** but also the **itemId** of the feature service item.<jupyter_code># set the url to feature service item's url
layer_list[0]['url'] = ebola.layers[1].url
layer_list[0]['itemId'] = search_result[0].id

# update the web map object's operationalLayers dictionary
web_map_obj['operationalLayers'] = layer_list<jupyter_output><empty_output><jupyter_text>Update the web mapNow the web map should be fixed as it points to a live service. To update the web map, we call the `update()` method.<jupyter_code># Let us print the opertationalLayers dictionary before calling the update()
web_map_obj['operationalLayers']
web_map_obj.update()<jupyter_output><empty_output><jupyter_text>Let us create a new web map object and try to draw it.<jupyter_code>search_result = gis.content.search('title: Ebola_treatment_locations', item_type = "Web Map")
display(search_result)
web_map_item = search_result[0]
web_map_obj = arcgis.mapping.WebMap(web_map_item)
web_map_obj<jupyter_output><empty_output><jupyter_text>![web_map_obj](http://esri.github.io/arcgis-python-api/notebooks/nbimages/05_Using_updating_GIS_02.PNG) The web map was sucessfully overwritten with correct operational layers. You can interact with the widget and zoom into the African coast to observe the locations of Ebola treatment centers.  Using and updating a web sceneIn the sample above we observed how to update a web map. Updating the web scene is similar, we use the `update()` method. Let us look at the example of a web scene that displays tropical cyclones over the Pacific ocean.<jupyter_code>search_result = gis.content.search('title:Western Pacific Typhoons (2005)', 
                                   item_type = 'Web Scene', outside_org = True)
search_result[0]<jupyter_output><empty_output><jupyter_text>Lets display the web scene in the notebook.<jupyter_code>web_scene_item = search_result[0]
web_scene_obj = arcgis.mapping.WebScene(web_scene_item)

# display web scene in the notebook
web_scene_obj<jupyter_output><empty_output><jupyter_text>![web_scene_obj](http://esri.github.io/arcgis-python-api/notebooks/nbimages/05_Using_updating_GIS_04.PNG) This is a great web scene and it displays a lot of hurricane tracks. However, we want to create a new one with only a particular subset of data and customize the basemaps. To modify this web scene, let us first make a copy of it and publish it into your portal.  Make a copy of the public web scene itemTo make a copy, we essentially download the content of the web scene JSON, remove the parts we don't want, add the layers that we want and publish a new item using that information. The publishing steps is similar to what is described earlier in the **data preparation** section and in detail in the sample titled **Publishing web maps and web scenes**.Let's say, we are only interested in the storms that occur in summer. Summer in tropical Asia is around April-June and that matches with a layer in the existing web scene. Let us query the `operationalLayers` section of the web scene to understand how the layers look like. Update operational layers of new web scene<jupyter_code>display(web_scene_obj['operationalLayers'])<jupyter_output><empty_output><jupyter_text>There is a lot of information displayed above. Let us drill into this and display only layer names and their urls. If you notice, some of the layers above are group layers, meaning, they contain sub layers. So let us write a loop like below and print some details.<jupyter_code>for layer in web_scene_obj['operationalLayers']:
    print(layer['title'] + " :: " + layer['layerType'])
    if layer['layerType'] == 'GroupLayer':
        for sub_layer in layer['layers']:
            print("\t" + sub_layer['title'] + " :: "+ sub_layer['url'])<jupyter_output>Typhoon Paths :: ArcGISFeatureLayer
October - December :: GroupLayer
	Labels Q4 :: http://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/PacificTyphoons2005_WFL/FeatureServer/17
	Typhoons Q4 :: http://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/PacificTyphoons2005_WFL/FeatureServer/16
September :: GroupLayer
	Labels Q3_3 :: http://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/PacificTyphoons2005_WFL/FeatureServer/14
	Typhoons Q3_3 :: http://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/PacificTyphoons2005_WFL/FeatureServer/13
August :: GroupLayer
	Labels Q3_2 :: http://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/PacificTyphoons2005_WFL/FeatureServer/11
	Typhoons Q3_2 :: http://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/PacificTyphoons2005_WFL/FeatureServer/10
July :: GroupLayer
	Labels Q3_1 :: http://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/PacificTyphoons2005_WFL/FeatureServer/8
	Typhoons Q3[...]<jupyter_text>We are only interested in the layers that correspond to cyclones in summer. From the above report, we understand that information is in a group layer with two sub layers. Let us extract just that dictionary and compose a new web scene data.<jupyter_code># Let us construct a list comprehension and mine out that group layer.
subset_op_layers = [subset for subset in web_scene_obj['operationalLayers'] if subset['title'] == 'April - June']
display(subset_op_layers)
# Let us apply the changes to a new web scene object.
new_web_scene_obj = web_scene_obj
new_web_scene_obj['operationalLayers'] = subset_op_layers<jupyter_output><empty_output><jupyter_text>Update basemap of new web sceneWe now have the necessary `operationalLayers` information. Let us also try to change the basemap to a darker shade. First let us search the basemaps available in the current portal. If no suitable one is found, we can widen the search outside the organization and use a basemap published by Esri.**Basemaps** are web maps that are stored in a **group** usually called **Basemaps**. Thus to get the list of basemaps available on a portal, we can find the basemaps group and list all web maps that are a part of it.To get the list of groups on the portal, we use `groups` property of the `GIS` class.<jupyter_code>basemap_search = gis.content.search('title:dark', 
                                    outside_org=True, item_type='web map')
for item in basemap_search:
    display(item)
    print(item.tags)<jupyter_output><empty_output><jupyter_text>We have found the basemap of our choice. Let us read it as a **`WebMap`** object and query the `baseMap` dictionary.<jupyter_code>dark_basemap_item = basemap_search[1]
dark_basemap_obj = arcgis.mapping.WebMap(dark_basemap_item)
dark_basemap_obj['baseMap']<jupyter_output><empty_output><jupyter_text>Now let us explore how the `baseMap` dictionary of the **web scene** looks like.<jupyter_code>new_web_scene_obj['baseMap']<jupyter_output><empty_output><jupyter_text>To get the desired basemap, we need to update the only `url` key-value pair of the web scene's `baseMap` dictionary. Here we will only pick the first layer of the dark basemap web map.<jupyter_code>new_web_scene_obj['baseMap']['baseMapLayers'][0]['url'] = \
dark_basemap_obj['baseMap']['baseMapLayers'][0]['url']<jupyter_output><empty_output><jupyter_text>Now that we have performed the necessary updates, we can go ahead and publish this as a new web scene item on our portal.<jupyter_code>new_web_scene_properties= {'title':'Toprical Cyclones - Summer',
                          'type' : 'Web Scene',
                          'tags' : 'ArcGIS Python API',
                          'snippet' : str.format('Subset of <a href={2}>{0}</a> published by {1}',
                                                 web_scene_item.title, web_scene_item.owner,
                                                "https://www.arcgis.com/home/item.html?id=" + web_scene_item.id),
                          'text' : json.dumps(new_web_scene_obj)}

new_item = gis.content.add(new_web_scene_properties)
new_item<jupyter_output><empty_output><jupyter_text>We have successfully published the new web scene. Now let us display in an interactive widget and observe if it has the necessary updates.<jupyter_code>new_item.share(True)
new_web_scene_obj = arcgis.mapping.WebScene(new_item)
new_web_scene_obj<jupyter_output><empty_output><jupyter_text>![new_web_scene_obj](http://esri.github.io/arcgis-python-api/notebooks/nbimages/05_Using_updating_GIS_05.PNG) Our required updates have been applied to the new web scene. However notice the **April - June** layer is **turned off** by default. Let us fix that and update the web scene.Let us query the `operationalLayer` dictionary of the new web scene and look for a key called `visibility`.<jupyter_code>for layer in new_web_scene_obj['operationalLayers']:
    print(layer['visibility'])<jupyter_output>False<jupyter_text>As we know, there is just 1 group layer and it is turned off. Let us change that and update the web scene.<jupyter_code>for layer in new_web_scene_obj['operationalLayers']:
    layer['visibility'] = True<jupyter_output><empty_output><jupyter_text>To update the web scene call the `update()` method on the web scene object.<jupyter_code>new_web_scene_obj.update()
new_web_scene_obj<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08021877848678213, 'fraction_numerical': 0.007783465395133581, 'mean_word_length': 5.017721518987342, 'pattern_counts': {'":': 0, '<': 101, '<?xml version=': 0, '>': 101, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '25', 'dir': 'jupyter-structured-clean-dedup', 'id': '132527', 'license': 'Apache-2.0', 'n_tokens_mistral': 4334, 'n_tokens_neox': 4198, 'n_words': 1544, 'path': 'samples/05_content_publishers/using_and_updating_GIS_content.ipynb', 'repo_name': 'Ham4chees/arcgis-python-api', 'size': '14261'}