1

I have a NumPy Array to FC script that parses a JSON web-service. The outer loop writes to my FC correctly, however my inner loop variables come back as not defined when appending the items. The outer loop variables include geographic data such as lon/lat, address and other ancillary information about a service request such as service request number, service request type, time created, etc.

The inner loop contains information relating to each service request number, address, etc.

I am using Python 2.6 and ArcGIS 10.2.1

import arcpy
import numpy
import requests
import json
import jsonpickle


fc = "myFC"
f2 =open('C:\Users\GeoffreyWest\Desktop\Request.json')
data2 = jsonpickle.encode(data2)

url2 = "myURL"
headers2 = {'Content-type': 'text/plain', 'Accept': '/'}

r2 = requests.post(url2, data=data2, headers=headers2)
decoded2 = json.loads(r2.text)
print json.dumps(decoded2, sort_keys=True, indent=4)
if arcpy.Exists(fc):
    arcpy.Delete_management(fc)
try:
    r2
except requests.exceptions.ConnectTimeout as e:
    print "Too slow Mojo!"


items = []
for sr in decoded2['Response']['ListOfServiceRequest']['ServiceRequest']:
    SRAddress = sr['SRAddress']
    Latitude = sr['Latitude']
    Longitude = sr['Longitude']
    ReasonCode = sr['ReasonCode']
    SRNumber = sr['SRNumber']
    FirstName = sr['FirstName']
    LastName = sr['LastName']
    ResolutionCode = sr['ResolutionCode']
    HomePhone = sr['HomePhone']
    CreatedDate = sr['CreatedDate']
    UpdatedDate = sr['UpdatedDate']
    BulkyItem = sr['ListOfLa311BulkyItem']
    ElectronicWaste = sr['ListOfLa311ElectronicWaste']
    MoveInMoveOut = sr['ListOfLa311MoveInMoveOut']
    IllegalDumping = sr['ListOfLa311IllegalDumpingPickup']
    ServiceNotComplete = sr['ListOfLa311ServiceNotComplete']
    BrushItems = sr['ListOfLa311BrushItemsPickup']
    Containers = sr['ListOfLa311Containers']
    MHA = sr['ListOfLa311MetalHouseholdAppliancesPickup']
    DeadAnimalRemoval = sr['ListOfLa311DeadAnimalRemoval']
    Manual = sr['ListOfLa311ManualPickup']
    CreatedDate = datetime.datetime.strptime(CreatedDate, "%m/%d/%Y %H:%M:%S")
    UpdatedDate = datetime.datetime.strptime(UpdatedDate, "%m/%d/%Y %H:%M:%S")


    for sr in ElectronicWaste:
            for ewastelocation in ElectronicWaste['La311ElectronicWaste']:
                                    locationewaste =  ewastelocation['CollectionLocation']
    for sr in ElectronicWaste:
            for ewastetype in ElectronicWaste['La311ElectronicWaste']:
                                    itemEwaste =  ewastetype['ElectronicWestType']

 dt = np.dtype([('Address', 'U40'),
        ('LatitudeShape', '<f8'),
        ('LongitudeShape', '<f8'),
        ('Latitude', '<f8'),
        ('Longitude', '<f8'),
        ('ReasonCode','U128'),
        ('SRNumber', 'U40'),
        ('ElectronicWaste', 'U128'), 
        ('FirstName', 'U40'),
       ('LastName', 'U40'),
        ('ResolutionCode','U128'),
       ('HomePhone', 'U40'),
        ('CreatedDate', 'U128'),
        ('UpdatedDate', 'U128'),

        ])


    items.append((SRAddress,
                  Latitude,
                 Longitude,
                  Latitude,
                  Longitude,
                  ReasonCode,
                  SRNumber,
                 locationewaste, 
                 FirstName,
                  LastName,
                  ResolutionCode,
                  HomePhone,
                  CreatedDate,
                  UpdatedDate,



    ))


sr = arcpy.SpatialReference(4326)

arr = np.array(items,dtype=dt)
NumPyArray = arcpy.da.NumPyArrayToFeatureClass(arr, fc, ['longitudeshape', 'latitudeshape'], sr)
print "success"

Output NameError: name 'locationewaste' is not defined

NOTE: My for loops for electronic waste are indented, I do not know if they are showing up correctly.

Output when inner loop variables aren't included...

2
  • You noted that your indentation may not be coming over properly...is the append supposed to be within one of the inner for loops? If not, you aren't accomplishing anything by assigning multiple values to those variables in that way, they should always have whatever the last value was. Commented May 11, 2015 at 14:08
  • There are multiple loops with different variables, ie location of commodity, type, items, count of items. The loop is coming over correctly after all and that append should take into account the inner loops. Commented May 11, 2015 at 14:18

1 Answer 1

3

This really doesn't have anything to do with numpy. If ElectronicWaste or ElectronicWaste['La311ElectronicWaste'] is empty, locationewaste won't get assigned. And then the items.append part of your code will fail with a NameError because locationewaste isn't there.

for sr in ElectronicWaste:
        for ewastelocation in ElectronicWaste['La311ElectronicWaste']:
                                locationewaste =  ewastelocation['CollectionLocation']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.