Select to view content in your preferred language

Update Hosted Feature Layer Using JSON from an API -- Not Working

463
2
07-02-2024 08:58 AM
Labels (1)
GIS_utahDEM
Frequent Contributor

Hello, I am trying to automate overwriting a polyline hosted feature layer using an API. I think I have the correct format for adding features via the json, but where I'm running into trouble is actually publishing/overwriting the features into the feature layer. 

This is my main code, I am thinking that the issue is occuring in line 39:

# Main function to fetch API, decode polylines, and publish to ArcGIS Online
def main():

    # Make request to API
    response = requests.get(api_url)
    if response.status_code == 200:
        # Assuming API response is JSON with 'features' containing polyline data
        features = response.json()
        

        # Extract features from the API data
        #features = api_data.get('features', [])

        #if not features:
        #    print("No features found in API response.")
        #    return

        try:
            # Truncate existing features in the feature layer
            feature_layer.manager.truncate()

            # Prepare new features to add
            geojson_features = []
            for feature in features:
                encoded_polyline = feature.get('EncodedPolyline')
                description = feature.get('Description')

                if encoded_polyline and description:
                    # Decode polyline from 'EncodedPolyline' field
                    decoded_polyline = decode_polyline(encoded_polyline)

                    # Convert to GeoJSON Feature format
                    geojson_feature = to_geojson_feature(decoded_polyline, description)
                    
                    geojson_features.append(geojson_feature)

            # Add new GeoJSON features to the feature layer
            print(json.dumps(geojson_features))
            result = feature_layer.edit_features(adds=json.dumps(geojson_features))

            print(f"Features updated successfully in ArcGIS Online hosted feature layer.")

        except Exception as e:
            print(f"Failed to update feature layer: {str(e)}")

    else:
        print("Failed to fetch data from API.")

 

The print(json.dumps(geojson_features) returns this: 

[{"type": "Feature", "geometry": {"type": "LineString", "coordinates": [[40.77248, -111.8377], [40.77254, -111.83776], [40.7728, -111.83804], [40.7728, -111.83804], [40.77282, -111.83803], [40.77285, -111.83802], [40.77286, -111.83802], [40.77288, -111.83802], [40.77248, -111.8377]]}, "properties": {"Description": "Blue Detour"}}, {"type": "Feature", "geometry": {"type":.................

***I have removed most of the coordinates for simplicity, and only show the first feature though there are more

 

The error I get, which I believe is in reference to result = feature_layer.edit_features(adds=json.dumps(geojson_features)) is:

pass in features as list of Features, dicts or PropertyMap
Parameters not valid for edit_features

 

0 Kudos
2 Replies
Clubdebambos
MVP Regular Contributor

Hi @GIS_utahDEM,

The edit_features is expecting a FeatureSet, see docs here.

You would need to parse your GeoJSON to create a list of dictionaries that represent a FeatureSet and each Feature would look similar to the below. You could then feed the list of dictionaries (features) into the edit_features() method.

new_features = [
    {
        "geometry": {
          # geometry details
        },
        "attributes": {
            "str_att_name" : "attribute_value",
            "num_att_name" : 0
        }
    }
]

 

Alternatively, take a look at the append() method which does allow GeoJSON to be appended.

~ learn.finaldraftmapping.com
0 Kudos
GIS_utahDEM
Frequent Contributor

@Clubdebambos Thank you! I eventually figured it out and posted about how to do it in case others were struggling with the same thing: https://community.esri.com/t5/arcgis-api-for-python-questions/a-timid-coder-s-guide-to-creating-a-po...