Select to view content in your preferred language

Can you help me with my script (Arcpy Script to extract feature(s) and attachment(s) from AGOL

3279
2
10-13-2015 07:28 AM
OLANIYANOLAKUNLE
Frequent Contributor

I have a script that is supposed to extract feature(s) from AGOL, the script runs without any error(s) but I don't get any output in the geodatabase I created specifically for this purpose as well as the attachment to the specified feature. I noticed that the script drops a .json file in my folder, when I tried to convert this .json file to a shapefile it throws an error - see error below;

>>> arcpy.JSONToFeatures_conversion("C:\Users\KUNLE\Desktop\New folder (2)\PropertyEnumerationForm.json","Sample")

Runtime error  Traceback (most recent call last):   File "<string>", line 1, in <module>   File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\conversion.py", line 434, in JSONToFeatures     raise e ExecuteError: ERROR 001558: Error parsing json file 'C:\Users\KUNLE\Desktop\New folder (2)\PropertyEnumerationForm.json'.  Failed to execute (JSONToFeatures).>>>

Below is my script

gtUrl = 'https://www.arcgis.com/sharing/rest/generateToken'
        gtValues = {'username' : 'XPARCELS',
        'password' : '@3$%678',
        'referer' : 'http://www.arcgis.com',
        'f' : 'json' }
        gtData = urllib.urlencode(gtValues)
        gtRequest = urllib2.Request(gtUrl, gtData)
        gtResponse = urllib2.urlopen(gtRequest)
        gtJson = json.load(gtResponse)
        token = gtJson['token']

        ### Create Replica ###
        ### Update service url HERE ###
        crUrl = 'http://services5.arcgis.com/b23aIR8OqGRlfJlE/arcgis/rest/services/PropertyEnumerationForm/FeatureSer...'
    

        crValues = {'f' : 'json',
        'layers' : '0',
        'returnAttachments' : 'true',
        'token' : token }
        crData = urllib.urlencode(crValues)
        crRequest = urllib2.Request(crUrl, crData)
        crResponse = urllib2.urlopen(crRequest)
        crJson = json.load(crResponse)
        replicaUrl = crJson['URL']
        urllib.urlretrieve(replicaUrl, 'PropertyEnumerationForm.json')

        ### Get Attachment ###
        cwd = os.getcwd()
        with open('PropertyEnumerationForm.json') as data_file:
            data = json.load(data_file)

        for x in data['layers'][0]['attachments']:
            gaUrl = x['url']
            gaFolder = cwd + '\\photos\\' + x['parentGlobalId']
        if not os.path.exists(gaFolder):
            os.makedirs(gaFolder)
            gaName = x['name']
            gaValues = {'token' : token }
            gaData = urllib.urlencode(gaValues)
            urllib.urlretrieve(url=gaUrl + '/' + gaName, filename=os.path.join(gaFolder, gaName),data=gaData)

        ### Create Features ###
        rows = arcpy.InsertCursor(cwd + '/data.gdb/PropertyEnumerationForm')
        fields = arcpy.ListFields(cwd + '/data.gdb/PropertyEnumerationForm')

        for cfX in data['layers'][0]['features']:
            pnt = arcpy.Point()
            pnt.X = cfX['geometry']['x']
            pnt.Y = cfX['geometry']['y']
            row = rows.newRow()
            row.shape = pnt

        ### Set Attribute columns HERE ###
        ## makes use of the "updatevalue function to deal with dates ##
        rows = arcpy.UpdateCursor("C:\Users\KUNLE\Desktop\TaxParcels\data.gdb\PropertyEnumerationForm")
        for row in rows:
            #row.setValue("BUFFER_DISTANCE", row.getValue("ROAD_TYPE") * 100)
            updateValue(row,'UNITNO', cfX['attributes']['UNITNO'])
            updateValue(row,'PROPERTYCLASSCODE', cfX['attributes']['PROPERTYCLASSCODE'])
            updateValue(row,'OWNERNAME1', cfX['attributes']['OWNERNAME1'])
            updateValue(row,'BUILDINGSTRUCTURETYPE', cfX['attributes']['BUILDINGSTRUCTURETYPE'])       
            updateValue(row,'BUILDINGARRANGEMENT', cfX['attributes']['BUILDINGARRANGEMENT'])
            updateValue(row,'BUILDINGGROUPING', cfX['attributes']['BUILDINGGROUPING'])
            updateValue(row,'PHONENUMBER1', cfX['attributes']['PHONENUMBER1'])
            updateValue(row,'EMAILADDRESS', cfX['attributes']['EMAILADDRESS'])
            updateValue(row,'SUBORCONDO', cfX['attributes']['SUBORCONDO'])
            updateValue(row,'NOOFBUILDINGS', cfX['attributes']['NOOFBUILDINGS'])
            updateValue(row,'BUILDINGNUMBER', cfX['attributes']['BUILDINGNUMBER'])
            updateValue(row,'NUMBEROFUNITS', cfX['attributes']['NUMBEROFUNITS'])
            updateValue(row,'BUILDINGUNITNUMBER', cfX['attributes']['BUILDINGUNITNUMBER'])
            updateValue(row,'NUMBEROFFLOORS', cfX['attributes']['NUMBEROFFLOORS'])
            updateValue(row,'FLOORNUMBER', cfX['attributes']['FLOORNUMBER'])
            updateValue(row,'OWNERTYPE', cfX['attributes']['OWNERTYPE'])
            updateValue(row,'BUILDINGSTATUS', cfX['attributes']['BUILDINGSTATUS'])
            updateValue(row,'HasChildren', cfX['attributes']['HasChildren'])
            updateValue(row,'HASWATERPROVIDER', cfX['attributes']['HASWATERPROVIDER'])
            updateValue(row,'HASSEWERPROVIDER', cfX['attributes']['HASSEWERPROVIDER'])
            updateValue(row,'DATECREATED', cfX['attributes']['DATECREATED'])   
            updateValue(row,'CreationDate', cfX['attributes']['CreationDate'])
            updateValue(row,'Creator', cfX['attributes']['Creator'])
            updateValue(row,'EditDate', cfX['attributes']['EditDate'])
            updateValue(row,'Editor', cfX['attributes']['Editor'])
            updateValue(row,'GlobalID_str', cfX['attributes']['GlobalID'])

            rows.insertRow(row)

        del row
        del rows

        ### Add Attachments ###
        ### Create Match Table ###
        rows = arcpy.InsertCursor(cwd + '/data.gdb/MatchTable')

        for cmtX in data['layers'][0]['attachments']:
            row = rows.newRow()

            row.setValue('GlobalID_Str', cmtX['parentGlobalId'])
            row.setValue('PhotoPath', cwd + '\\photos\\' + cmtX['parentGlobalId'] + '\\' + cmtX['name'])

            rows.insertRow(row)

        del row
        del rows

        ### Add Attachments ###
        arcpy.AddAttachments_management(cwd + '/data.gdb/PropertyEnumerationForm', 'GlobalID_Str', cwd + '/data.gdb/MatchTable', 'GlobalID_Str', 'PhotoPath')

Kindly assist me with the above a I've tried all possible combinations to ensure that the script above runs successfully. Waiting for your response. Thanks

Message was edited by: OLANIYAN OLAKUNLE

0 Kudos
2 Replies
DanPatterson_Retired
MVP Emeritus

The  JSON to Feature  requires a proper path and name, your source path is probably not acceptable.  Check to code sample in the link

0 Kudos
DanPatterson_Retired
MVP Emeritus

as a start... your updatecursor in "C.... is missing an r in front of it  to get it into raw format ie r"C....