Advertisement
wtgeographer

Untitled

Aug 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. # coding: utf-8
  2.  
  3. import json
  4. import requests
  5. import re
  6. import arcpy
  7.  
  8.  
  9. fc = r'C:\Users\Noah\Desktop\gundog\chandler\test\test_parcels_codes_dissolve.shp'
  10. arcpy.AddField_management(fc, 'company_id', "TEXT", "", "", "50")
  11. fields = ['OWNER', 'company_id']
  12. results =[]
  13.  
  14.  
  15. def hasNumbers(inputString):
  16.     return any(char.isdigit() for char in inputString)
  17.  
  18.  
  19. def get_companies(companies):
  20.        
  21.     count = 0
  22.     company_no = None
  23.     company_id = ''
  24.     for item in companies:
  25.         company = item['company']
  26.         company_no = company['company_number']
  27.         count += 1
  28.  
  29.     if company_no is not None and count == 1:
  30.         company_id = company_no
  31.         results.append(company_no)
  32.     else:        
  33.         company_id = ''
  34.    
  35.     print company_id
  36.     return company_id
  37.  
  38.  
  39. stopwords = ['ASSOC', 'HOMEOWNER\'S', 'INC', 'IN', 'ASSN', 'HOA', 'ASSO', 'NO', 'ASS', "THE"]
  40.  
  41. jurisdiction_code = 'us_az'
  42. key = 'xxxxxxxxxxxxxxxxxxxxxxxxx'
  43. api_key = ''
  44.  
  45. if key:
  46.     api_key = '&api_token=' + key + '&per_page=100'
  47.  
  48. count = 1
  49. with arcpy.da.UpdateCursor(fc, fields) as cursor:
  50.     for row in cursor:
  51.         querywords = row[0].split()
  52.         resultwords  = [word for word in querywords if word not in stopwords]
  53.         result = ' '.join(resultwords)
  54.         if hasNumbers(result) == True:
  55.             parentsub = re.split(r'(^[^\d]+)', result)[1]
  56.             wildcard = '*' + parentsub.rstrip() + '*'
  57.         else:
  58.             parentsub = result
  59.             wildcard = '*' + parentsub.rstrip() + '*'
  60.  
  61.         print str(count) + ' ' + wildcard
  62.         PARAMS = {'q': wildcard, 'jurisdiction_code': 'us_az', 'api_token': key}
  63.         URL = "https://api.opencorporates.com/v0.4/companies/search?"
  64.         r = requests.get(url=URL, params=PARAMS)
  65.         print r.url
  66.         data = r.json()
  67.         row[1] = get_companies(data['results']['companies'])
  68.         cursor.updateRow(row)
  69.         count += 1
  70.  
  71. print ""
  72. print "Total matched: " + str(len(results))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement