Skip to content

Instantly share code, notes, and snippets.

@pmacMaps
Created January 21, 2025 20:23
Show Gist options
  • Save pmacMaps/786195e017f8027e1c24775c7cda8b2c to your computer and use it in GitHub Desktop.
Save pmacMaps/786195e017f8027e1c24775c7cda8b2c to your computer and use it in GitHub Desktop.
Apply Layer Optimization for Feature Service Hosted in ArcGIS Online
from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
from print_errors import print_exception # helper module to printing error messages
from time import strftime as format_time
from time import sleep
def optimize(portal_url, username, password, item_id, lyr_idx, req_timeout=180):
"""
portal_url = URL for GIS portal; string
username: username of administrative user to portal; string
password: password for administrative user to portal; string
item_id: portal unique item identifier; string
lyr_idx: layer index of feature service; integar
req_timeout: number of seconds for timeout for results function call; numeric
"""
try:
# container for output messages
message = ''
# login to portal
gis = GIS(portal_url, username, password)
# add message
message += f"\n{format_time('%I:%M%p')} : signed into portal: {gis}\n"
# get feature service item to apply optimization to
item = gis.content.get(item_id)
# create feature layer collection from item
flc = FeatureLayerCollection.fromitem(item)
# add layer optimization to feature service
result = flc.layers[lyr_idx].manager.update_definition({"multiScaleGeometryInfo":{"levels":[]}}, future=True)
# message
message += f"\n{format_time('%I:%M%p')} : submitted layer optimization job for {item.title} ({item.id})\n"
# get job status
result_status = result.result(timeout=req_timeout)
# add delay
sleep(req_timeout + 30)
# add message
message += f"\n{format_time('%I:%M%p')} : results of layer optimization job: {result_status}\n"
except (Exception, EnvironmentError) as e:
message += print_exception(e)
finally:
return message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment