AnsweredAssumed Answered

NumPy Array to Feature Class: NameError from Inner Loop Variables

Question asked by Geoffrey.West on May 11, 2015
Latest reply on May 11, 2015 by dkwiens

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

Outcomes