Advertisement
wtgeographer

Untitled

Aug 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | None | 0 0
  1.  
  2. # coding: utf-8
  3.  
  4. import json
  5. import requests
  6. import re
  7. import arcpy
  8.  
  9. arcpy.env.overwriteOutput = True
  10.  
  11. jurisdiction_code = 'us_az'
  12. key = 'xxxxxxxxxxxxxxxxxxx' #key 2000/daily
  13. api_key = ''
  14.  
  15. if key:
  16.     api_key = '&api_token=' + key + '&per_page=100'
  17.  
  18.  
  19. fc = r'C:\Users\Noah\Desktop\gundog\chandler\test\test_parcels_codes_diss_cln.shp'
  20. arcpy.AddField_management(fc, 'r_address', "TEXT", "", "", "200")
  21. arcpy.AddField_management(fc, 'agent', "TEXT", "", "", "50")
  22. arcpy.AddField_management(fc, 'a_address', "TEXT", "", "", "200")
  23.  
  24. fields = ['OWNER','company_id','agent','a_address','r_address']
  25.  
  26.  
  27. def hasNumbers(inputString):
  28.     return any(char.isdigit() for char in inputString)
  29.  
  30.  
  31. def get_companies(company):
  32.     print company['agent_name']
  33.     a_company = {
  34.         'agent': company['agent_name'],
  35.         'agent_address': company['agent_address'],
  36.         'registered_address': company['registered_address_in_full']
  37.     }
  38.     return a_company
  39.  
  40.  
  41. def main():
  42.  
  43.     company_ids = [row[1] for row in arcpy.da.SearchCursor(fc, fields)]
  44.     uniqueValues = set(company_ids)
  45.  
  46.     fl = arcpy.MakeFeatureLayer_management(fc, "subs_fl")
  47.  
  48.     count = 1
  49.  
  50.     for cid in uniqueValues:
  51.         print 'Request No : ' + str(count)
  52.         obj = """"company_id"='%s' """ % (cid)
  53.         print obj
  54.        
  55.         arcpy.SelectLayerByAttribute_management (fl,'NEW_SELECTION', obj)
  56.         print arcpy.GetCount_management(fl)
  57.         URL = "https://api.opencorporates.com/v0.4/companies/us_az/{0}?sparse=true&api_token={1}" .format(cid, key)
  58.         r = requests.get(url=URL)
  59.         data = r.json()
  60.         try:
  61.             myDict = get_companies(data['results']['company'])
  62.             agent = myDict.get('agent')
  63.             a_add = myDict.get('agent_address')
  64.             r_add = myDict.get('registered_address')
  65.  
  66.             if a_add:
  67.                 a_add_list = [item for item in a_add.split('\n')]
  68.                 a_add_list_string = ' '.join(a_add_list)
  69.                 print a_add_list_string
  70.                 arcpy.CalculateField_management (fl, "a_address", '"{0}"'.format (a_add_list_string))
  71.  
  72.             if r_add:
  73.                 r_add_list = [item for item in r_add.split('\n')]
  74.                 r_add_list_string = ' '.join(r_add_list)
  75.                 print r_add_list_string
  76.                 arcpy.CalculateField_management (fl, "r_address", '"{0}"'.format(r_add_list_string))
  77.  
  78.             if agent:
  79.                 arcpy.CalculateField_management (fl, "agent", '"{0}"'.format (agent))
  80.         except:
  81.             pass
  82.            
  83.         count += 1
  84.        
  85.     print ""
  86.     print "Total matched: " + str(len(uniqueValues))
  87.  
  88. if __name__ == "__main__":
  89.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement