<?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: Selectively Update Data in a Hosted Feature Layer in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/selectively-update-data-in-a-hosted-feature-layer/m-p/1327042#M8991</link>
    <description>&lt;P&gt;Thanks Josh! It worked beautifully. All I had to do was to select the updated SHAPE column after the merge.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 08 Sep 2023 23:59:37 GMT</pubDate>
    <dc:creator>asmitashukla</dc:creator>
    <dc:date>2023-09-08T23:59:37Z</dc:date>
    <item>
      <title>Selectively Update Data in a Hosted Feature Layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/selectively-update-data-in-a-hosted-feature-layer/m-p/1326595#M8984</link>
      <description>&lt;P&gt;Hello - I have monitoring locations in a SQL Server database. I am creating a hosted feature layer by querying data and Python APIs using the process below-&lt;/P&gt;&lt;LI-CODE lang="python"&gt;cnxn_str = ("Driver={SQL Server Native Client 11.0};"
"Server=xxxx;"
"Database=xxxx;"
"UID=xxxx;"
"PWD=xxxx;")
cnxn = pyodbc.connect(cnxn_str)
Locationquery = """SELECT [X],[CollectionLatitude],[CollectionLongitude] FROM xxxx"""
Locations = pd.read_sql(Locationquery, cnxn)

Locations['SHAPE'] = '"spatialReference": {"wkid": 4326}, {"x":' + Locations['CollectionLongitude'].astype('str') + ', "y":' + Locations['CollectionLatitude'].astype('str') + '}'

sdf=GeoAccessor.from_xy(Locations,'CollectionLongitude','CollectionLatitude')

lyr1 = sdf.spatial.to_featurelayer("MonitoringLocations")&lt;/LI-CODE&gt;&lt;P&gt;Every week, some locations are updated in the database and I would like to update the features in the hosted feature layer using Python APIs. The truncate() option lets me delete all features but I want to overwrite/delete only the features that are updated in the database. Here is how I am pulling the updated locations-&lt;/P&gt;&lt;LI-CODE lang="python"&gt;UpdatedLocationQuery = """SELECT [X],[CollectionLatitude],[CollectionLongitude] FROM xxxx WHERE LastUpdated &amp;gt;= DATEADD(day, -7, GETDATE())

UpdatedLocations = pd.read_sql(UpdatedLocationQuery, cnxn)&lt;/LI-CODE&gt;&lt;P&gt;How can I selectively update features in the hosted feature layer? Any help is greatly appreciated.&lt;/P&gt;&lt;P&gt;Thank You.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Sep 2023 21:36:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/selectively-update-data-in-a-hosted-feature-layer/m-p/1326595#M8984</guid>
      <dc:creator>asmitashukla</dc:creator>
      <dc:date>2023-09-07T21:36:45Z</dc:date>
    </item>
    <item>
      <title>Re: Selectively Update Data in a Hosted Feature Layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/selectively-update-data-in-a-hosted-feature-layer/m-p/1326649#M8988</link>
      <description>&lt;P&gt;Hello.&lt;/P&gt;&lt;P&gt;What you are looking for is&amp;nbsp;&lt;STRONG&gt;edit_features&lt;/STRONG&gt;. This allows you to do adds, updates, and deletes. You can get a sense of how to use this method here:&amp;nbsp;&lt;A href="https://developers.arcgis.com/python/guide/editing-features/#:~:text=The%20edit_features%20%28%29%20method%20on%20FeatureLayer%20object%20can,display%20gis%20%3D%20GIS%20%28%22portal%20url%22%2C%20%27username%27%2C%20%27password%27%29" target="_blank"&gt;Editing Features | ArcGIS API for Python&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Sep 2023 00:55:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/selectively-update-data-in-a-hosted-feature-layer/m-p/1326649#M8988</guid>
      <dc:creator>EarlMedina</dc:creator>
      <dc:date>2023-09-08T00:55:45Z</dc:date>
    </item>
    <item>
      <title>Re: Selectively Update Data in a Hosted Feature Layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/selectively-update-data-in-a-hosted-feature-layer/m-p/1326765#M8989</link>
      <description>&lt;P&gt;This is a process we do regularly in my organization. In order to selectively &lt;EM&gt;update &lt;/EM&gt;features, you need to know the objectid from the hosted layer, so you need to do a couple steps before applying edits.&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Get your list of edited features&lt;/LI&gt;&lt;LI&gt;Query the hosted layer for matching features&lt;/LI&gt;&lt;LI&gt;Join the objectid&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;I detail the process in a Jupyter Notebook which you can find here: &lt;A href="https://github.com/jdcarls2/ilgisa-2022/blob/main/hosted-copy/hosted-copy.ipynb" target="_blank"&gt;https://github.com/jdcarls2/ilgisa-2022/blob/main/hosted-copy/hosted-copy.ipynb&lt;/A&gt;&lt;/P&gt;&lt;P&gt;But here's a short version of the code. This assumes the hosted layer is already published.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis import GIS
import pandas as pd

# portal connection and feature layer
gis = GIS('your portal url', 'user', 'password')
fl = gis.content.get('itemid of hosted layer').layers[0]

# database connection
cnxn_str = (
    "Driver={SQL Server Native Client 11.0};"
    "Server=xxxx;"
    "Database=xxxx;"
    "UID=xxxx;"
    "PWD=xxxx;"
)

cnxn = pyodbc.connect(cnxn_str)

Locationquery = """SELECT [X],[CollectionLatitude],[CollectionLongitude] FROM xxxx WHERE LastUpdated &amp;gt;= DATEADD(day, -7, GETDATE()"""

# query to dataframe
Locations = pd.read_sql(Locationquery, cnxn)

# reshape, convert to spatial df
Locations['SHAPE'] = '"spatialReference": {"wkid": 4326}, {"x":' + Locations['CollectionLongitude'].astype('str') + ', "y":' + Locations['CollectionLatitude'].astype('str') + '}'

sdf = GeoAccessor.from_xy(Locations,'CollectionLongitude','CollectionLatitude')

# query hosted layer
# NOTE: this assumes that your layer has some kind of unique ID in it present in both the hosted layer and the SDE table. if not, consider adding something like a GUID to both

oids = fl.query(out_fields=['objectid', 'shared_id_field'], as_df=True)

merged = sdf.merge(oids, how='inner', on='shared_id_field')

fl.edit_features(updates=merged.spatial.to_featureset())&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Sep 2023 13:55:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/selectively-update-data-in-a-hosted-feature-layer/m-p/1326765#M8989</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2023-09-08T13:55:05Z</dc:date>
    </item>
    <item>
      <title>Re: Selectively Update Data in a Hosted Feature Layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/selectively-update-data-in-a-hosted-feature-layer/m-p/1327042#M8991</link>
      <description>&lt;P&gt;Thanks Josh! It worked beautifully. All I had to do was to select the updated SHAPE column after the merge.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Sep 2023 23:59:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/selectively-update-data-in-a-hosted-feature-layer/m-p/1327042#M8991</guid>
      <dc:creator>asmitashukla</dc:creator>
      <dc:date>2023-09-08T23:59:37Z</dc:date>
    </item>
  </channel>
</rss>

