Update Point Geometry

1004
8
Jump to solution
03-14-2023 12:31 PM
Labels (1)
GenePotter
New Contributor

I am trying to update point geometry from an outside data set.  I can update all the other data and can successfully create new points with the correct geometry but when I try to update the geometry of an existing point the data layer does not update its location.  The pre and post geometry tags are updated correctly but when I requery the point it still has the old geometry.

        
gwp_content = gis.content.get('########################')
gwp_layer = gwp_content.layers[0]

assets_f = assets.query("AssetID == 'B1610'")
for a in assets_f.itertuples():    

# for a in assets.itertuples():    
    try:
        gwp_fset = gwp_layer.query(where="AssetID='" + a.Index + "'")
        f_count = len(gwp_fset.features)
        if f_count > 0:
            gwp_f = gwp_fset.features[0]

            # print("Pre", a)
            print("Pre gwp_f", gwp_f)
            # print(a.Index)
 
            gwp_f.set_value("End_Date",time_end)
            gwp_f.set_value("CreatedBy",a.CreatedBy)
            gwp_f.set_value("ModifiedBy",a.ModifiedBy)
            gwp_f.set_value("Title",a.title)
            gwp_f.set_value("AssetSubType",a.AssetSubType)
            gwp_f.set_value("RMID",a.RMID)
            gwp_f.set_value("Latitude",a.Latitude)
            gwp_f.set_value("Longitude",a.Longitude)
            gwp_f.set_value("POINT_X",a.POINT_X)
            gwp_f.set_value("POINT_Y",a.POINT_Y)
            gwp_f.set_value("UTMX",a.UTMX)
            gwp_f.set_value("UTMY",a.UTMY)
            gwp_f.set_value("UTMZone",a.UTMZone)
            gwp_f.set_value("UTMDatum",a.UTMDatum)
            new_geometry =  {"x":a.POINT_X, "y":a.POINT_Y}
            spatial_reference = gwp_fset.spatial_reference
            output_geometry = geometry.project(geometries=[new_geometry],in_sr=4326,out_sr=spatial_reference,gis=gis)
            gwp_f.geometry = output_geometry

            # gwp_f.set_value("geometry",{"x":a.POINT_X, "y":a.POINT_Y, "spatialReference" : {"wkid": 102100, "latestWkid": 3857}})

            update_result = gwp_layer.edit_features(updates=[gwp_f])
            print("Geo:", gwp_f.geometry)
            print("Post gwp_f", gwp_f)
            # print("Update Result:",update_result)
 
Any help would be appreciated.  I have spent way too many hours scouring the forums.
0 Kudos
1 Solution

Accepted Solutions
MobiusSnake
MVP

There's your problem, your geometry is nested in a list in the second case.

Edit:  geometry.project() is returning a list with a single geometry, and your code is handling it as if it were just a geometry.

View solution in original post

0 Kudos
8 Replies
MobiusSnake
MVP

Is this an AGOL hosted feature layer?  You very well may have already done this, but from the item page's Settings make sure to check two things:

  • Under Editing, make sure that it's set to allow both attribute and geometry updates, and not just attribute updates
  • Below that will be a link to "Manage geometry updates", click on that to open a pop-up and in it, make sure the layer you're updating is checked
0 Kudos
GenePotter
New Contributor

I appreciate the response.  I had already made sure those were checked and the layer is checked.  I had done that when I created the layer since we do allow our field staff to update the layer through the Web Map App.  Thanks again for responding I appreciate any help I can get.

0 Kudos
MobiusSnake
MVP

Can you print gwp_f to the console to see what it looks like immediately before you call edit_features()?  Not looking for anything in particular, but if it's structured in a weird way that might cause the update to be a no-op.

0 Kudos
GenePotter
New Contributor

Here is a console shot of the pre, the geo and the post.  It shows it changed but doesn't push the geo to AGOL.  If I update any of the other fields it will push those to AGOL.

 

Pre gwp_f {"geometry": {"x": -11734292.3359, "y": 4841408.441699997, "spatialReference": {"wkid": 102100, "latestWkid": 3857}}, "attributes": {"FID": 6762, "buildingID": 1575, "UTMX": 464835.62, "UTMY": 4409076.96, "UTMZone": "13", "UTMDatum": "NAD 83", "Status": "IN-SERVICE", "Record": "YES", "Method": "CONSULTANT", "CreatedBy": "PotterG", "CreatedDat": 1521612000000, "ModifiedBy": "PotterG", "ModifiedDa": 1678860000000, "Title": "Bandimere Cabin", "GUIDID": " ", "URL": "http:// ", "POINT_X": -105.410941666, "POINT_Y": 39.830963216, "GlobalID": "{9952BC6F-69D7-4FC0-8A07-432249E937EF}", "RMID": "NRPO4274", "GlobalID_2": "94584ada-21d5-499d-bda1-b78e0c185757", "Latitude": "39\u00b049'51.47\" N", "Longitude": "105\u00b024'39.39\" W", "End_Date": null, "Begin_Date": -2208988800000, "AssetID": "B1575", "F_": null, "AssetSubType": "Bunkhouse"}}

Geo: [{'x': -11734292.350298584, 'y': 4841408.633804429, 'spatialReference': {'wkid': 102100, 'latestWkid': 3857}}]

Post gwp_f {"geometry": [{"x": -11734292.350298584, "y": 4841408.633804429, "spatialReference": {"wkid": 102100, "latestWkid": 3857}}], "attributes": {"FID": 6762, "buildingID": 1575, "UTMX": 464835.62, "UTMY": 4409076.96, "UTMZone": 13.0, "UTMDatum": "NAD 83", "Status": "IN-SERVICE", "Record": "YES", "Method": "CONSULTANT", "CreatedBy": "PotterG", "CreatedDat": 1521612000000, "ModifiedBy": "PotterG", "ModifiedDa": 1678860000000, "Title": "Bandimere Cabin", "GUIDID": " ", "URL": "http:// ", "POINT_X": -105.410941666, "POINT_Y": 39.830963216, "GlobalID": "{9952BC6F-69D7-4FC0-8A07-432249E937EF}", "RMID": "NRPO4274", "GlobalID_2": "94584ada-21d5-499d-bda1-b78e0c185757", "Latitude": "39\u00b049'51.47\" N", "Longitude": "105\u00b024'39.39\" W", "End_Date": null, "Begin_Date": -2208988800000, "AssetID": "B1575", "F_": null, "AssetSubType": "Bunkhouse"}}

0 Kudos
MobiusSnake
MVP

There's your problem, your geometry is nested in a list in the second case.

Edit:  geometry.project() is returning a list with a single geometry, and your code is handling it as if it were just a geometry.

0 Kudos
GenePotter
New Contributor

Thanks, changed it to 

gwp_f.geometry = output_geometry[0]
 
and that fixed my problem.
0 Kudos
EarlMedina
Esri Regular Contributor

Hey,

Not sure if this will help you, but here's a simple example that works for me to update an existing point:

from arcgis import GIS
from arcgis.geometry import Point
from arcgis.features import FeatureLayer

gis = GIS("https://www.arcgis.com", "username", "password")
fl_url = url = "https://server.com/arcgis/rest/services/ex/FeatureServer/0"
fl = FeatureLayer(url, gis)
sdf = fl.query(as_df=True)
example_df = sdf.loc[sdf.OBJECTID==1].copy()[["OBJECTID", "SHAPE"]]
example_df.at[0, "SHAPE"] = Point({"x" : -118.15, "y" : 33.80, "spatialReference" : {"wkid" : 4326}})

update_fs = example_df.spatial.to_featureset()
fl.edit_features(updates=update_fs)
0 Kudos
GenePotter
New Contributor

Thanks for helping.   I was able to stay with my current code after MobiusSnake pointed out the error in my ways. 

0 Kudos