Set Visibility Range and/or Add Filter using ArcGIS Python API

490
1
03-30-2017 08:21 AM
Highlighted
New Contributor II

Is a way to set the Visibility Range and/or add a filter based on attribute values with a Feature Layer in ArcGIS Online using the ArcGIS Python API?

1 Reply
Highlighted
New Contributor III

Here is one way how you can do that for one service and it could give you idea how to implement the full process.

EDIT: Note that I understood that you want to change the range in the Feature Service level (and not override it later on the configuration stack ie. WebMap level). So this will change the values directly to the service endpoint level.

from arcgis.gis import GIS
import getpass

password = getpass.getpass("Enter password: ")

gis = GIS('https://cgisuomi.maps.arcgis.com/home', 'antti.kajanus_CGISuomi', password)
print("Connected to: {}".format(gis.properties.urlKey + "." + gis.properties.customBaseUrl))
print("Connected as: {}".format(gis.users.me.username))

from arcgis.features import FeatureLayer

feature_layer = feature_layer_item.layers[0]
feature_layer
#<FeatureLayer url:"https://services1.arcgis.com/ut5Ig3pyAO7DhCxU/arcgis/rest/services/HSLCityBikes/FeatureServer/0">

import requests
import json
# Get service content info
request = requests.get(feature_layer.url + '?f=pjson&token={}'.format(gis._con.token))
service_layer_json = request.json()

#Check current scale range
print('MinScale = {}'.format(service_layer_json['minScale']))
print('MaxScale = {}'.format(service_layer_json['maxScale']))
# MinScale = 1155582
# MaxScale = 0

#Update scale range
service_layer_json['minScale'] = 15000
service_layer_json['maxScale'] = 150

#Check new scale range
print('MinScale = {}'.format(service_layer_json['minScale']))
print('MaxScale = {}'.format(service_layer_json['maxScale']))
# MinScale = 15000
# MaxScale = 150

# Apply edits to the service
# Make sure that url is not in the properties
if 'editingInfo' in service_layer_json:
del service_layer_json['editingInfo']

result = feature_layer.manager.update_definition(service_layer_json)
result
# {'success': True}

# Testing that changes have been applied
feature_layer_item = gis.content.get('96d90824c0b94025abefeb7fe0ef60a0')
# Get service content info
request = requests.get(feature_layer.url + '?f=pjson&token={}'.format(gis._con.token))
service_layer_json = request.json()
#Check current scale range
print('MinScale = {}'.format(service_layer_json['minScale']))
print('MaxScale = {}'.format(service_layer_json['maxScale']))
# MinScale = 15000
# MaxScale = 150‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍