Select to view content in your preferred language

Web map created with python API and feature services not displaying features

6770
13
Jump to solution
04-18-2017 09:25 AM
CharlieWare
Deactivated User

I am using the Python API to publish a web map using a hosted feature service that I published from a file geodatabase in My Content. I am following the example in the Python API sample Jupyter Notebook 'Publishing Web Maps and Web Scenes' in the '05 Content Publishers' group of notebooks. The map publishes fine, the basemap displays correctly but the features in the feature service do not show up when I view the web map in the Map Viewer. They are listed in the Details window correctly but do not have any symbology. When I click on the  Legend button it says there is no legend. If I create a web map from with ArcGIS Online using the same feature service, the features show up correctly. 

0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

However, when I click on the hosted feature layer in My Content, it displays the features with some sort of default symbology.

Yes, the smart-mapping capability of the map viewer in ArcGIS Online would try to render your features even if you do not have a symbology set, that could be what you are seeing.

You can define a feature layer's symbology in at least 3 places.

  1. In the feature layer definition,
  2. In the feature layer's item
  3. In the web map (which you are publishing now)

We don't have a sample or guide that discusses symbology (yet). To keep things simple, I would recommend you set the symbology in your web map (option 3 above) using the workflow below:

  1. Style your feature layer with the necessary symbology in the map viewer and save that web map.
  2. Read that map using Python API into the WebMap class. View the definition, particularly the `drawingInfo` dictionary. You can access this like below:
    your_web_map_obj['operationalLayers'][0]['layerDefinition']['drawingInfo']['renderer']
    where the [0] refers to first layer.
  3. Modify and apply the `layerDefinition` dictionary of that web map to the script that you are publishing.

I see this is a popular workflow, we will work on getting this added to our SDK.

View solution in original post

13 Replies
AlexanderBrown5
Frequent Contributor

Have you shared the feature service publicly? 

0 Kudos
CharlieWare
Deactivated User

The feature service is shared to the organization. It's the same organization in which I created the Web Map. 

0 Kudos
by Anonymous User
Not applicable

Charlie, in the map viewer, can you view the attribute table (you can click on the table icon in table of contents for this) of the feature layer and ensure you see features in the table?

If that is the case, you might have dropped symbology for the features. By the way, are you running the exact sample or replacing with your content?

0 Kudos
CharlieWare
Deactivated User

I can see the attribute tables for the layers in the hosted feature layer. The source data for the layer is a hosted feature layer published from a file geodatabase that I uploaded to AGOL as a zip file. There never was any symbology for those features. However, when I click on the hosted feature layer in My Content, it displays the features with some sort of default symbology. I assumed that symbology would carry forward into the web map. Maybe I need to assign symbology to the features. Is there a sample for that?

I replaced the data in the sample with my own hosted feature layer. The sample used a Map Service. I did it both from within a Jupyter Notebook and as a standalone python script and had the same result with both.

0 Kudos
by Anonymous User
Not applicable

However, when I click on the hosted feature layer in My Content, it displays the features with some sort of default symbology.

Yes, the smart-mapping capability of the map viewer in ArcGIS Online would try to render your features even if you do not have a symbology set, that could be what you are seeing.

You can define a feature layer's symbology in at least 3 places.

  1. In the feature layer definition,
  2. In the feature layer's item
  3. In the web map (which you are publishing now)

We don't have a sample or guide that discusses symbology (yet). To keep things simple, I would recommend you set the symbology in your web map (option 3 above) using the workflow below:

  1. Style your feature layer with the necessary symbology in the map viewer and save that web map.
  2. Read that map using Python API into the WebMap class. View the definition, particularly the `drawingInfo` dictionary. You can access this like below:
    your_web_map_obj['operationalLayers'][0]['layerDefinition']['drawingInfo']['renderer']
    where the [0] refers to first layer.
  3. Modify and apply the `layerDefinition` dictionary of that web map to the script that you are publishing.

I see this is a popular workflow, we will work on getting this added to our SDK.

CharlieWare
Deactivated User

Thanks for getting back so quickly.

When I view my web map as a web map object, the operational layers are missing the entire 'layerDefinition' key (along with some others). I'm now looking into how to copy in a layerDefinition from one map into mine or create one from scratch and then publish that. 

0 Kudos
CharlieWare
Deactivated User

I got it to work but it's somewhat convoluted. I created a web map from the hosted feature layer, read that into a web map object and then output it's layerDefinitions to a text file. I then copy/pasted the layerDefinition key from the text file into the web_map_simple.json file from the sample in the example Jupyter notebook. I then read that into the web map object for the new web map and now the features show up. 

0 Kudos
by Anonymous User
Not applicable

Congrats! If you wish, you can avoid the step of saving to file by working with all the definitions, in-memory.

You can also look at the Web Map spec document which specifies the various parameters that define a web map json. For instance here is the layerDefinition dictionary.

DanielMast
Esri Contributor

Hello Atma, 

A year after solving this issue, I am in the situation where I need to update the urls in a WebMap and cannot find a good way to do it. I am using the Python API 1.4 but have not found any way to update an Item or a WebMap contents via Json, and do not understand what Charlie (user cwarewa) meant by "I then read that into the web map object for the new web map and now the features show up."

I tried getting the json contents from a template web map with 'get_data()' as you described here, and the used the updated json for the itemdict parameter on a new Item object in addition to the itemid of a newly created, empty WebMap but the map remained empty. As far as I can see, the update function on the WebMap object is only for updating the item info and metadata, is that correct?

I also tried using the rest endpoint "update" as explained in this GeoNet question and in the Docs but I get an error with the message "Item does not exist or is inaccessible" every time. Here's the section of code where I want to update the urls in the WebMap (token is created using same portal and credentials as the target_gis object):

# clone the template webmap
template_WebMap = source_gis.content.get(templateWebMapId)
result_WebMaps = target_gis.content.clone_items([template_WebMap], copy_data=False)

# get contents and update urls
result_WebMap = getResultWebMap(result_WebMaps)
webMap_content = result_WebMap.get_data()
updated_WebMap_content = updateWebMapContent(webMap_content, new_hosted_service_url)

# prepare call to upload rest endpoint
update_url = "{}/sharing/rest/content/users/{}/items/{}/update".format(
    portalUrl, target_gis.users.me.orgId, result_WebMap.id)
update_params = {
    'text': json.dumps(updated_WebMap_content),
    'token': token,
    'f': 'json'
}
update_response = urlopen(update_url, bytes(urlencode(update_params), 'utf-8'))
update_ouput = json.loads(update_response.read().decode("utf-8"))

# update_output = {'error': {'code': 400, 'message': 'Item does not exist or is inaccessible.', 
#   'messageCode': 'CONT_0001', 'details': []}}

So I am currently unable to update the map's contents in any way. Is there a preferred workflow for making updates to the Urls in the layers of a WebMap using the Python API? 

Thanks in advance,

Daniel

0 Kudos