<?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 Adding data to the Feature Layer at the hosted in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/adding-data-to-the-feature-layer-at-the-hosted/m-p/1374886#M77958</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I am trying to update the data at the hosted layer. I am using Python programming language and in addition to that, I am also experimenting with different packages like arcgis, requests, json, etc. Below is the response that I get after adding a record at the hosted layer:&lt;/P&gt;&lt;PRE&gt;[{'success': True, 'globalId': '&amp;lt;globalid&amp;gt;', 'objectId': &amp;lt;objectId&amp;gt;}]&lt;/PRE&gt;&lt;P&gt;As it can be observed that the success is "True". However, even then, I don't see the data getting added at the hosted feature layer. Is there anything that I am missing? This is my first data engineering related project in ArcGIS. I am not able to figure out what am I missing. I utilized various methods, but I get the exact same result.&amp;nbsp; Please let me know what your thoughts are. I will also post my code below.&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;LI-CODE lang="python"&gt;fc = r'path_to_data\abc.sde\table'

fields = ['*']

data = [row for row in arcpy.da.SearchCursor(fc, fields)]
columns = [field.name for field in arcpy.ListFields(fc) if field.name in fields or fields == ['*']]
df_sde = pd.DataFrame(data, columns = columns)

# Creating a GIS instance
gis = GIS('&amp;lt;gis_portal&amp;gt;', username, password)

service = '&amp;lt;service_layer&amp;gt;'
hosted = '&amp;lt;hosted_layer&amp;gt;'

# Connecting to the services

fabric_service = FeatureLayerCollection(service, gis)
fabric_hosted = FeatureLayerCollection(hosted, gis)

service_layer = fabric_service.layers[num1] 
hosted_layer = fabric_hosted.layers[num2]

## Importing the records as dataframe
service_records = service_layer.query(where="&amp;lt;some condition&amp;gt;", as_df=True, return_geometry=True)

## Truncating all the data in the hosted layer
try:
    logging.info("Starting data truncation process for the hosted layer.")
    result = hosted_layer.delete_features(where="1=1")
    if result['deleteResults']:
        logging.info("Data truncated successfully.")
    else:
        logging.warning("No data was deleted.")
except Exception as e:
    logging.error("Error occurred while truncating data: " + str(e))

## Importing the hosted layer data just to get the column names and schema. It results in an empty dataframe with column names and corresponding to data types
hosted_records = hosted_layer.query(where="1=1", as_df=True, return_geometry=True)

# Function to modify geometries of the geometry column in sevice_records to match the geometry requirement in the hosted
def project_in_batches(geometries, in_sr, out_sr, batch_size=5000):
    projected_geometries = []
    for i in range(0, len(geometries), batch_size):
        batch = geometries[i:i + batch_size]
        projected_batch = project(geometries=batch, in_sr=in_sr, out_sr=out_sr)
        projected_geometries.extend(projected_batch)
    return projected_geometries

# Reproject the geometries from service layer to match the hosted layer's spatial reference in batches
projected_geometries = project_in_batches(geometries=service_records[&amp;lt;geometry_column&amp;gt;].tolist(),
                                          in_sr={'wkid':wkid1},
                                          out_sr={'wkid':wkid2})

# Replace the original geometries with the projected ones
service_records[&amp;lt;geometry_column&amp;gt;] = projected_geometries

def merging_service_sde():
    '''
    This function acts as introducing the columns that must be present in the hosted layer but don't have a corresponding column neither in service_records nor df_sde. After that, such columns are populatied by Null values for the time being.
    '''
    cols_service = [&amp;lt;columns in service_records&amp;gt;]
    
    cols_sde = [&amp;lt;columns in df_sde&amp;gt;]
    
    cols_ignore = [&amp;lt;columns in hosted_records that do not have a corresponding column neither in service_records nor in df_sde&amp;gt;]
    
    
    temp_df_record = pd.merge(service_records[cols_service], df_sde[cols_sde], 
                              how = 'left', left_on = col1, right_on = col2)

    # Setting columns in cols_ignore as Null
    for col in cols_ignore:
        temp_df_record[col] = pd.NA
    
    return temp_df_record
    
merged = merging_service_sde()

mapping = {&amp;lt;mapping dictionary that maps the column names in such a way that it is identical to the column names present in hosted_records&amp;gt;}

# Mapping the column names
merged = merged.rename(columns = mapping)

# Extra step to align the columna according to the order in hosted_records for better readability
merged = merged[hosted_records.columns]

# Aligning the data types
for column in merged.columns:
    if column in hosted_records.columns:
        desired_type = hosted_records[column].dtype
        merged[column] = merged[column].astype(desired_type)
        
## Handling the missing values

object_cols = merged.select_dtypes(include = ['object', 'string']).columns
num_cols = merged.select_dtypes(include = ['float64', 'Float64', 'Int32', 'Int64']).columns
date_cols = merged.select_dtypes(include = ['datetime64[ns]']).columns

merged[object_cols] = merged[object_cols].fillna('N')
merged[num_cols] = merged[num_cols].fillna(0)
merged[date_cols] = merged[date_cols].fillna('2000-01-01')

merged_spatial = merged.spatial.to_featureset()

batch_size = 100  
total_records = len(merged_spatial.features)
batches = [merged_spatial.features[i:i + batch_size] for i in range(0, total_records, batch_size)]

for i, batch in enumerate(batches):
    try:
        batch_feature_set = FeatureSet(features=batch, geometry_type=merged_spatial.geometry_type, 
                                       spatial_reference=merged_spatial.spatial_reference)
        update_result = hosted_layer.edit_features(adds=batch_feature_set)
        success = list(set([i['success'] for i in update_result['addResults']]))
        if len(success) == 1 and success[0] == True:
            logging.info(f"Batch {i+1}/{len(batches)} added successfully")
        else:
            logging.info(f"Batch {i+1}/{len(batches)} not added")
    except Exception as e:
        logging.error(f"Error adding batch {i+1}/{len(batches)}: {e}")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 26 Jan 2024 19:07:39 GMT</pubDate>
    <dc:creator>Murtaza_Mir</dc:creator>
    <dc:date>2024-01-26T19:07:39Z</dc:date>
    <item>
      <title>Adding data to the Feature Layer at the hosted</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/adding-data-to-the-feature-layer-at-the-hosted/m-p/1374886#M77958</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I am trying to update the data at the hosted layer. I am using Python programming language and in addition to that, I am also experimenting with different packages like arcgis, requests, json, etc. Below is the response that I get after adding a record at the hosted layer:&lt;/P&gt;&lt;PRE&gt;[{'success': True, 'globalId': '&amp;lt;globalid&amp;gt;', 'objectId': &amp;lt;objectId&amp;gt;}]&lt;/PRE&gt;&lt;P&gt;As it can be observed that the success is "True". However, even then, I don't see the data getting added at the hosted feature layer. Is there anything that I am missing? This is my first data engineering related project in ArcGIS. I am not able to figure out what am I missing. I utilized various methods, but I get the exact same result.&amp;nbsp; Please let me know what your thoughts are. I will also post my code below.&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;LI-CODE lang="python"&gt;fc = r'path_to_data\abc.sde\table'

fields = ['*']

data = [row for row in arcpy.da.SearchCursor(fc, fields)]
columns = [field.name for field in arcpy.ListFields(fc) if field.name in fields or fields == ['*']]
df_sde = pd.DataFrame(data, columns = columns)

# Creating a GIS instance
gis = GIS('&amp;lt;gis_portal&amp;gt;', username, password)

service = '&amp;lt;service_layer&amp;gt;'
hosted = '&amp;lt;hosted_layer&amp;gt;'

# Connecting to the services

fabric_service = FeatureLayerCollection(service, gis)
fabric_hosted = FeatureLayerCollection(hosted, gis)

service_layer = fabric_service.layers[num1] 
hosted_layer = fabric_hosted.layers[num2]

## Importing the records as dataframe
service_records = service_layer.query(where="&amp;lt;some condition&amp;gt;", as_df=True, return_geometry=True)

## Truncating all the data in the hosted layer
try:
    logging.info("Starting data truncation process for the hosted layer.")
    result = hosted_layer.delete_features(where="1=1")
    if result['deleteResults']:
        logging.info("Data truncated successfully.")
    else:
        logging.warning("No data was deleted.")
except Exception as e:
    logging.error("Error occurred while truncating data: " + str(e))

## Importing the hosted layer data just to get the column names and schema. It results in an empty dataframe with column names and corresponding to data types
hosted_records = hosted_layer.query(where="1=1", as_df=True, return_geometry=True)

# Function to modify geometries of the geometry column in sevice_records to match the geometry requirement in the hosted
def project_in_batches(geometries, in_sr, out_sr, batch_size=5000):
    projected_geometries = []
    for i in range(0, len(geometries), batch_size):
        batch = geometries[i:i + batch_size]
        projected_batch = project(geometries=batch, in_sr=in_sr, out_sr=out_sr)
        projected_geometries.extend(projected_batch)
    return projected_geometries

# Reproject the geometries from service layer to match the hosted layer's spatial reference in batches
projected_geometries = project_in_batches(geometries=service_records[&amp;lt;geometry_column&amp;gt;].tolist(),
                                          in_sr={'wkid':wkid1},
                                          out_sr={'wkid':wkid2})

# Replace the original geometries with the projected ones
service_records[&amp;lt;geometry_column&amp;gt;] = projected_geometries

def merging_service_sde():
    '''
    This function acts as introducing the columns that must be present in the hosted layer but don't have a corresponding column neither in service_records nor df_sde. After that, such columns are populatied by Null values for the time being.
    '''
    cols_service = [&amp;lt;columns in service_records&amp;gt;]
    
    cols_sde = [&amp;lt;columns in df_sde&amp;gt;]
    
    cols_ignore = [&amp;lt;columns in hosted_records that do not have a corresponding column neither in service_records nor in df_sde&amp;gt;]
    
    
    temp_df_record = pd.merge(service_records[cols_service], df_sde[cols_sde], 
                              how = 'left', left_on = col1, right_on = col2)

    # Setting columns in cols_ignore as Null
    for col in cols_ignore:
        temp_df_record[col] = pd.NA
    
    return temp_df_record
    
merged = merging_service_sde()

mapping = {&amp;lt;mapping dictionary that maps the column names in such a way that it is identical to the column names present in hosted_records&amp;gt;}

# Mapping the column names
merged = merged.rename(columns = mapping)

# Extra step to align the columna according to the order in hosted_records for better readability
merged = merged[hosted_records.columns]

# Aligning the data types
for column in merged.columns:
    if column in hosted_records.columns:
        desired_type = hosted_records[column].dtype
        merged[column] = merged[column].astype(desired_type)
        
## Handling the missing values

object_cols = merged.select_dtypes(include = ['object', 'string']).columns
num_cols = merged.select_dtypes(include = ['float64', 'Float64', 'Int32', 'Int64']).columns
date_cols = merged.select_dtypes(include = ['datetime64[ns]']).columns

merged[object_cols] = merged[object_cols].fillna('N')
merged[num_cols] = merged[num_cols].fillna(0)
merged[date_cols] = merged[date_cols].fillna('2000-01-01')

merged_spatial = merged.spatial.to_featureset()

batch_size = 100  
total_records = len(merged_spatial.features)
batches = [merged_spatial.features[i:i + batch_size] for i in range(0, total_records, batch_size)]

for i, batch in enumerate(batches):
    try:
        batch_feature_set = FeatureSet(features=batch, geometry_type=merged_spatial.geometry_type, 
                                       spatial_reference=merged_spatial.spatial_reference)
        update_result = hosted_layer.edit_features(adds=batch_feature_set)
        success = list(set([i['success'] for i in update_result['addResults']]))
        if len(success) == 1 and success[0] == True:
            logging.info(f"Batch {i+1}/{len(batches)} added successfully")
        else:
            logging.info(f"Batch {i+1}/{len(batches)} not added")
    except Exception as e:
        logging.error(f"Error adding batch {i+1}/{len(batches)}: {e}")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jan 2024 19:07:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/adding-data-to-the-feature-layer-at-the-hosted/m-p/1374886#M77958</guid>
      <dc:creator>Murtaza_Mir</dc:creator>
      <dc:date>2024-01-26T19:07:39Z</dc:date>
    </item>
    <item>
      <title>Re: Adding data to the Feature Layer at the hosted</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/adding-data-to-the-feature-layer-at-the-hosted/m-p/1398358#M80656</link>
      <description>&lt;P&gt;It finally worked. The issue was caused by the geometry column that was getting altered when I was merging two datasets in Pandas. This modification in the geometry column was preventing the data from getting uploaded at the hosted.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Mar 2024 13:02:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/adding-data-to-the-feature-layer-at-the-hosted/m-p/1398358#M80656</guid>
      <dc:creator>Murtaza_Mir</dc:creator>
      <dc:date>2024-03-20T13:02:13Z</dc:date>
    </item>
  </channel>
</rss>

