Select to view content in your preferred language

ArcGIS API for Python FeaturesLayer.append() error

3518
4
Jump to solution
12-13-2019 08:21 AM
deleted-user-NvcfpBOWaKwr
Deactivated User

I'm having an issue with appending to a hosted feature service with a shapefile using the .append() method. I keep getting a very non-descriptive error which is:

ERROR:arcgis._impl.connection:Object reference not set to an instance of an object.
Traceback (most recent call last):
File "C:/Users/c-jermoore/Desktop/Farmland_Preservation_Scripts/DO_NOT_DELETE/AGOL_Clip_Soils_Layer_No_GUI/AGOL_Clip_Soils_Layer.py", line 460, in <module>
upload_format="shapefile")
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\layer.py", line 1134, in append
sres = self._con.get(path=surl, params={'f' : 'json'})
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\_impl\connection.py", line 898, in get
self._handle_json_error(resp_json['error'], errorcode)
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\_impl\connection.py", line 1198, in _handle_json_error
raise RuntimeError(errormessage)
RuntimeError: Object reference not set to an instance of an object.
(Error Code: 400)

I'm not exactly sure what this means. I had a suspicion that it might be due to fields not matching, which some don't but I'm mapping them in the parameters for the method. So now I'm wondering if maybe I'm not calling the FeatureLayer item correctly. 

Has anyone ever run into this before?

Here is the block of code giving me issues:

getclippedSoilsSHPItemID = agolLogin.content.get(findClippedSoilsSHPList[0])

print(getclippedSoilsSHPItemID.name)

layerToAppend = arcgis.gis.Item(gis=agolLogin, itemid="60820364b71743c49822a60da1e85492")

testAppendSoilsLayer = layerToAppend.layers[0]

print(testAppendSoilsLayer)

testAppendSoilsLayer.append(item_id=getclippedSoilsSHPItemID,
                            field_mappings=[{"name": "AREASYMBOL",
                                             "source": "AREASYMBOL"},
                                            {"name": "SPATIALVER",
                                             "source": "SPATIALVER"},
                                            {"name": "MUSYM",
                                             "source": "MUSYM"},
                                            {"name": "MUKEY",
                                             "source": "MUKEY"},
                                            {"name": "Mapunit_Name",
                                             "source": "Mapunit_Na"},
                                            {"name": "Non_Irrigated_Capability_Class___Dominant_Condition",
                                             "source": "Non_Irriga"}],
                            upload_format="shapefile")

logging.info("DONE!")

exit()

Any help would be greatly appreciated

0 Kudos
1 Solution

Accepted Solutions
deleted-user-NvcfpBOWaKwr
Deactivated User

I figured it out. The .append method needs an Item ID for the item that will be used to append to the working layer. I was inserting an "Item" using the content.get without specifying what information I wanted from that item. So I had to change line 11 from:

testAppendSoilsLayer.append(item_id=getclippedSoilsSHPItemID,

to

testAppendSoilsLayer.append(item_id=getclippedSoilsSHPItemID.id,

and it worked as intended. 

Easy mistake to make and over look.

View solution in original post

0 Kudos
4 Replies
deleted-user-NvcfpBOWaKwr
Deactivated User

I figured it out. The .append method needs an Item ID for the item that will be used to append to the working layer. I was inserting an "Item" using the content.get without specifying what information I wanted from that item. So I had to change line 11 from:

testAppendSoilsLayer.append(item_id=getclippedSoilsSHPItemID,

to

testAppendSoilsLayer.append(item_id=getclippedSoilsSHPItemID.id,

and it worked as intended. 

Easy mistake to make and over look.

0 Kudos
JamesKwong1
Occasional Contributor

Hi, can you help me understand if it's possible to append data without creating new item?

In your example you are appending getclippedSoilsSHPItemID to item with ID:"60820364b71743c49822a60da1e85492".

Do you think it's possible to append CSV that I have directly on my hdd? As far as I can see I need to create this interim item and upload CSV there, and only then I can use that item to append to another item.

0 Kudos
deleted-user-NvcfpBOWaKwr
Deactivated User

You can append to a CSV file using python. There are quite a few tutorials you can find on google that will walk you through how to work with CSV files.

 

https://realpython.com/python-csv/ 

Not sure if that answers your question. 

0 Kudos
DarrylKlassen1
Frequent Contributor

I am having similar issues - I want to append a features from a SDE feature class to my AGOL Item.  I am doing the following but getting an error:

from arcgis.gis import GIS

import arcpy,sys,string,os

agol_item_url = arcpy.GetParameter(0)
sde_fc = arcpy.GetParameter(1)

gis = GIS(portal=cokportal, username=u, password=p)

from arcgis.features import FeatureLayer
fl = FeatureLayer(agol_item_url)

geojsonfile = r"c:\temp\myjsonfeatures.json"

arcpy.FeaturesToJSON_conversion(sde_fc , geojsonfile)

fl.append(item_id=None, upload_format='geojson', source_table_name=geojsonfile)

Then I get this error:

Object reference not set to an instance of an object. (Error Code: 400)

Is it possible to append these features??