Select to view content in your preferred language

Overwrite in find_centroids not working?

228
1
12-06-2024 03:37 PM
LDoty
by
Emerging Contributor

I have a feature layer with 163 line features.  I ran find_centroids() to generate a centroid feature layer (which appears to be working based on the credit usage.)  I'd like to overwrite that centroid layer each time the script runs but the resulting feature layer shows no features. (I previously removed the features in the centroid layer to test the overwrite function.)  What am I missing?

from arcgis import features
from arcgis.gis import GIS
from arcgis.features import FeatureLayer
from arcgis.features import FeatureLayerCollection
from arcgis.features.analysis import find_centroids

# VERIFY FEATURE COUNT IN Infrastructure Projects (Lines)
ipLayer_id = gis.content.get("67...5")
ipLines_id_count = ipLayer_id.layers[1].query()
print(
     f"Infrastructure Projects (Lines) has {len(ipLines_id_count.features)} features."
)
#RESULT: Infrastructure Projects (Lines) has 163 features.

# FIND CENTROIDS OF Infrastructure Projects (Lines)
ipLines_url = {"url": "https://services.../FeatureServer/2"}
centroid_results = find_centroids(
     input_layer=ipLines_url,
     point_location=False,
     output_name={
          "serviceProperties": {
               "name": "InfrastructureProjectCentroids"
          },
          "itemProperties": {
               "itemId": "11...2",
               "overwrite": True
          }
     }
)
#RESULT: {"cost": 0.163}

# VERIFY FEATURE COUNT IN InfrastructureProjectsCentroids
ipCentroids_id = gis.content.get("11...2")
ipCentroids_id_count = ipCentroids_id.layers[0].query()
print(
     f"InfrastructureProjectsCentroids has {len(ipCentroids_id_count.features)} features."
)
#RESULT: InfrastructureProjectsCentroids has 0 features.

0 Kudos
1 Reply
Clubdebambos
MVP Regular Contributor

Hi @LDoty ,

You just need to clean-up the syntax a bit. See below...

## the first run, creating the point feature layer with the centroids

centroid_results = find_centroids(
     input_layer=ipLines_url,
     point_location=False,
     output_name= "InfrastructureProjectCentroids"
)

 

Once you have your point feature layer created from the you can re-use it and overwrite.

## any run after the initial creation of the point feature layer

centroid_item = agol.content.get("FS_ITEM_ID")
centroid_fl = centroid_item.layers[0]

centroid_results = find_centroids(
     input_layer=ipLines_url,
     point_location=False,
     output_name= centroid_fl,
     context = {
         "overwrite": True
     }
)

I hope that helps,

Glen

~ learn.finaldraftmapping.com
0 Kudos