<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Correct workflow for publishing then updating hosted feature layer with spatially enabled dataframe in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/correct-workflow-for-publishing-then-updating/m-p/1007944#M5288</link>
    <description>&lt;P&gt;Hi &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/202029"&gt;@Jay_Gregory&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The script I've written reads a GDB feature class and updates a web feature layer i.e. appends new data to it.&lt;/P&gt;&lt;P&gt;You just need to change/update the fields of attribute table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis import GIS
import pandas as pd
from arcgis.features import GeoAccessor, GeoSeriesAccessor
from arcgis import geometry
from copy import deepcopy

gis = GIS('url', 'username', 'password')

sdf_to_append = pd.DataFrame.spatial.from_featureclass(r"path to gdb feature class")

#search for the hosted feature layer/service
featureLayer_item = gis.content.search('type: "Feature Service" AND title:"xxxxx"')

#access the item's feature layers
feature_layers = featureLayer_item[0].layers

#query all the features
fset = feature_layers[0].query()

features_to_be_added = []
template_hostedFeature = deepcopy(fset.features[0])

for index, row in sdf_to_append.iterrows():
    x = sdf_to_append.loc[index]['SHAPE']['x']
    y = sdf_to_append.loc[index]['SHAPE']['y']
    new_feature = deepcopy(template_hostedFeature)
    input_geometry = {'y':float(y), 'x':float(x)}
    output_geometry = geometry.project(geometries = [input_geometry],
                                       in_sr = 3857, 
                                       out_sr = 3857,
                                       gis = gis)

    # assign the updated values
    new_feature.geometry = output_geometry[0]
    new_feature.attributes['HoleID'] = row['HoleID']
    new_feature.attributes['Project'] = row['Project']
    new_feature.attributes['InterFrom'] = row['InterFrom']
    new_feature.attributes['InterTo'] = row['InterTo']
    new_feature.attributes['Grade'] = row['Grade']
    new_feature.attributes['GramMetre'] = row['GramMetre']
    new_feature.attributes['RegoProf'] = row['RegoProf']
    new_feature.attributes['MidPoint'] = row['MidPoint']
    new_feature.attributes['IntercptNo'] = row['IntercptNo']
    new_feature.attributes['Overlap'] = row['Overlap']
    new_feature.attributes['EndDate'] = row['EndDate']
    new_feature.attributes['EAST'] = row['EAST']
    new_feature.attributes['NORTH'] = row['NORTH']
    new_feature.attributes['RL'] = row['RL']
    new_feature.attributes['Width'] = row['Width']  
    
    features_to_be_added.append(new_feature)

feature_layers[0].edit_features(adds = features_to_be_added) 
print("Hosted feature layer is updated with gdb feature class!")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;However if you want to overwrite a feature layer, it's recommended to truncate it first then do an overwrite:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
gis = GIS("url", "username", "password")

# get the feature layer
flc_item = gis.content.get("item_id")
fLyr = flc_item.layers[0]
fLyr.manager.truncate()

# get the gdb item
gdb_item = gis.content.get("gdb_item_id")

feature_layer_collection = FeatureLayerCollection.fromitem(flc_item)
feature_layer_collection.manager.overwrite(gdb_item)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope that's helpful.&lt;/P&gt;&lt;P&gt;Cheers&lt;/P&gt;&lt;P&gt;Mehdi&lt;/P&gt;</description>
    <pubDate>Wed, 09 Dec 2020 01:23:54 GMT</pubDate>
    <dc:creator>MehdiPira1</dc:creator>
    <dc:date>2020-12-09T01:23:54Z</dc:date>
    <item>
      <title>Correct workflow for publishing then updating hosted feature layer with spatially enabled dataframe</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/correct-workflow-for-publishing-then-updating/m-p/1007794#M5284</link>
      <description>&lt;P&gt;I am curious as to the correct workflow to&amp;nbsp;&lt;/P&gt;&lt;P&gt;a) publishing a spatially enabled data frame containing polygon data as a hosted feature layer (one time only to create the feature layer)&lt;/P&gt;&lt;P&gt;b) overwrite and/or update the hosted feature layer with a new spatially enabled dataframe - same schema but different data.&amp;nbsp;&lt;/P&gt;&lt;P&gt;There are multiple ways to achieve a - sdf.spatial.to_featurelayer being the easier, but I have been unable to overwrite / update the resultant layer.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;From&amp;nbsp;&lt;A href="https://developers.arcgis.com/python/sample-notebooks/publishing-sd-shapefiles-and-csv/," target="_blank"&gt;https://developers.arcgis.com/python/sample-notebooks/publishing-sd-shapefiles-and-csv/,&lt;/A&gt;&amp;nbsp;it seems like my only option is to export the sdf to a shapefile, zip up the shapefile, publish from that, and then follow the same process (export to shapefile and zip) to update?&lt;/P&gt;&lt;P&gt;Is that my only option?&lt;/P&gt;&lt;P&gt;I hesitate to use shapefiles because of some of the nuances of their field names, date types, etc. etc.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Dec 2020 18:22:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/correct-workflow-for-publishing-then-updating/m-p/1007794#M5284</guid>
      <dc:creator>Jay_Gregory</dc:creator>
      <dc:date>2020-12-08T18:22:30Z</dc:date>
    </item>
    <item>
      <title>Re: Correct workflow for publishing then updating hosted feature layer with spatially enabled dataframe</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/correct-workflow-for-publishing-then-updating/m-p/1007808#M5285</link>
      <description>&lt;P&gt;Another issue is that I don't have access to Arcpy on this machine, so according to&amp;nbsp;&lt;A href="https://developers.arcgis.com/python/guide/introduction-to-the-spatially-enabled-dataframe" target="_blank" rel="noopener"&gt;https://developers.arcgis.com/python/guide/introduction-to-the-spatially-enabled-dataframe/&lt;/A&gt;&lt;/P&gt;&lt;P&gt;I can only export to shapefile.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 12:44:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/correct-workflow-for-publishing-then-updating/m-p/1007808#M5285</guid>
      <dc:creator>Jay_Gregory</dc:creator>
      <dc:date>2020-12-14T12:44:20Z</dc:date>
    </item>
    <item>
      <title>Re: Correct workflow for publishing then updating hosted feature layer with spatially enabled dataframe</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/correct-workflow-for-publishing-then-updating/m-p/1007944#M5288</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/202029"&gt;@Jay_Gregory&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The script I've written reads a GDB feature class and updates a web feature layer i.e. appends new data to it.&lt;/P&gt;&lt;P&gt;You just need to change/update the fields of attribute table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis import GIS
import pandas as pd
from arcgis.features import GeoAccessor, GeoSeriesAccessor
from arcgis import geometry
from copy import deepcopy

gis = GIS('url', 'username', 'password')

sdf_to_append = pd.DataFrame.spatial.from_featureclass(r"path to gdb feature class")

#search for the hosted feature layer/service
featureLayer_item = gis.content.search('type: "Feature Service" AND title:"xxxxx"')

#access the item's feature layers
feature_layers = featureLayer_item[0].layers

#query all the features
fset = feature_layers[0].query()

features_to_be_added = []
template_hostedFeature = deepcopy(fset.features[0])

for index, row in sdf_to_append.iterrows():
    x = sdf_to_append.loc[index]['SHAPE']['x']
    y = sdf_to_append.loc[index]['SHAPE']['y']
    new_feature = deepcopy(template_hostedFeature)
    input_geometry = {'y':float(y), 'x':float(x)}
    output_geometry = geometry.project(geometries = [input_geometry],
                                       in_sr = 3857, 
                                       out_sr = 3857,
                                       gis = gis)

    # assign the updated values
    new_feature.geometry = output_geometry[0]
    new_feature.attributes['HoleID'] = row['HoleID']
    new_feature.attributes['Project'] = row['Project']
    new_feature.attributes['InterFrom'] = row['InterFrom']
    new_feature.attributes['InterTo'] = row['InterTo']
    new_feature.attributes['Grade'] = row['Grade']
    new_feature.attributes['GramMetre'] = row['GramMetre']
    new_feature.attributes['RegoProf'] = row['RegoProf']
    new_feature.attributes['MidPoint'] = row['MidPoint']
    new_feature.attributes['IntercptNo'] = row['IntercptNo']
    new_feature.attributes['Overlap'] = row['Overlap']
    new_feature.attributes['EndDate'] = row['EndDate']
    new_feature.attributes['EAST'] = row['EAST']
    new_feature.attributes['NORTH'] = row['NORTH']
    new_feature.attributes['RL'] = row['RL']
    new_feature.attributes['Width'] = row['Width']  
    
    features_to_be_added.append(new_feature)

feature_layers[0].edit_features(adds = features_to_be_added) 
print("Hosted feature layer is updated with gdb feature class!")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;However if you want to overwrite a feature layer, it's recommended to truncate it first then do an overwrite:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
gis = GIS("url", "username", "password")

# get the feature layer
flc_item = gis.content.get("item_id")
fLyr = flc_item.layers[0]
fLyr.manager.truncate()

# get the gdb item
gdb_item = gis.content.get("gdb_item_id")

feature_layer_collection = FeatureLayerCollection.fromitem(flc_item)
feature_layer_collection.manager.overwrite(gdb_item)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope that's helpful.&lt;/P&gt;&lt;P&gt;Cheers&lt;/P&gt;&lt;P&gt;Mehdi&lt;/P&gt;</description>
      <pubDate>Wed, 09 Dec 2020 01:23:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/correct-workflow-for-publishing-then-updating/m-p/1007944#M5288</guid>
      <dc:creator>MehdiPira1</dc:creator>
      <dc:date>2020-12-09T01:23:54Z</dc:date>
    </item>
    <item>
      <title>Re: Correct workflow for publishing then updating hosted feature layer with spatially enabled dataframe</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/correct-workflow-for-publishing-then-updating/m-p/1009255#M5316</link>
      <description>&lt;P&gt;In your first solution, you read from a file geodatabase.&amp;nbsp; The machine I'm working on only has the Python API installed, not arcpy, and according to&amp;nbsp;&lt;A href="https://developers.arcgis.com/python/guide/introduction-to-the-spatially-enabled-dataframe/" target="_blank"&gt;https://developers.arcgis.com/python/guide/introduction-to-the-spatially-enabled-dataframe/&lt;/A&gt;&amp;nbsp;it seems like I'll be unable to work with gdb feature classes.&amp;nbsp; Regarding the second option, my source data is a spatially enabled dataframe, which I can't publish natively using the Python api.&amp;nbsp; So will I need to export it to a shapefile to do this?&amp;nbsp; Publish as a shapefile, then on each subsequent overwrite (I need to overwrite not update), export to a shapefile, zip up, then overwrite?&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 13:05:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/correct-workflow-for-publishing-then-updating/m-p/1009255#M5316</guid>
      <dc:creator>Jay_Gregory</dc:creator>
      <dc:date>2020-12-14T13:05:03Z</dc:date>
    </item>
    <item>
      <title>Re: Correct workflow for publishing then updating hosted feature layer with spatially enabled dataframe</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/correct-workflow-for-publishing-then-updating/m-p/1009622#M5317</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/202029"&gt;@Jay_Gregory&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can also input shapefiles in sdf = pd.DataFrame.spatial.from_featureclass("shapefiles or gdb fc").&lt;/P&gt;&lt;P&gt;If you use the following line, it publishes the spatially enabled dataframe to AGOL or Portal to a folder you specify.&lt;/P&gt;&lt;P&gt;&lt;SPAN class="n"&gt;hosted_feature_lyr&lt;/SPAN&gt; &lt;SPAN class="o"&gt;=&lt;/SPAN&gt; &lt;SPAN class="n"&gt;sdf&lt;/SPAN&gt;&lt;SPAN class="o"&gt;.&lt;/SPAN&gt;&lt;SPAN class="n"&gt;spatial&lt;/SPAN&gt;&lt;SPAN class="o"&gt;.&lt;/SPAN&gt;&lt;SPAN class="n"&gt;to_featurelayer&lt;/SPAN&gt;&lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="s1"&gt;'feature_layer_name'&lt;/SPAN&gt;&lt;SPAN class="p"&gt;,&lt;/SPAN&gt; &lt;SPAN class="n"&gt;folder&lt;/SPAN&gt;&lt;SPAN class="o"&gt;=&lt;/SPAN&gt;&lt;SPAN class="s1"&gt;'folder_name')&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Dec 2020 01:01:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/correct-workflow-for-publishing-then-updating/m-p/1009622#M5317</guid>
      <dc:creator>MehdiPira1</dc:creator>
      <dc:date>2020-12-15T01:01:40Z</dc:date>
    </item>
    <item>
      <title>Re: Correct workflow for publishing then updating hosted feature layer with spatially enabled dataframe</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/correct-workflow-for-publishing-then-updating/m-p/1041084#M5821</link>
      <description>&lt;P&gt;Have you tested your second method to see that it will actually overwrite the old content with the new gdb_item? The overwrite will return 'success' = True and the time stamp on the online feature will update but in actuality, the data seems to be restored to what was originally published and not the gdb_item data. As per:&amp;nbsp;&lt;A href="https://community.esri.com/t5/arcgis-api-for-python-questions/overwrite-not-working-wrong-number-of-records-returned/td-p/868821" target="_blank"&gt;https://community.esri.com/t5/arcgis-api-for-python-questions/overwrite-not-working-wrong-number-of-records-returned/td-p/868821&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Thoughts?&lt;/P&gt;&lt;P&gt;Brian&lt;/P&gt;</description>
      <pubDate>Fri, 26 Mar 2021 15:37:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/correct-workflow-for-publishing-then-updating/m-p/1041084#M5821</guid>
      <dc:creator>BrianWoodman</dc:creator>
      <dc:date>2021-03-26T15:37:16Z</dc:date>
    </item>
    <item>
      <title>Re: Correct workflow for publishing then updating hosted feature layer with spatially enabled dataframe</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/correct-workflow-for-publishing-then-updating/m-p/1095866#M6594</link>
      <description>&lt;P&gt;Thanks for the script.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Could you help me out with this bit...&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2021-09-07 1732031111.jpg" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/22501iE002A06D0D2BF493/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2021-09-07 1732031111.jpg" alt="Screenshot 2021-09-07 1732031111.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;KeyError 'x'.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Is that because your script is for point data?&amp;nbsp; I would like to append to a polygon dataset.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Sep 2021 09:33:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/correct-workflow-for-publishing-then-updating/m-p/1095866#M6594</guid>
      <dc:creator>NickShannon2</dc:creator>
      <dc:date>2021-09-07T09:33:57Z</dc:date>
    </item>
  </channel>
</rss>

