Appendix: census.ire.org


In [1]:
# can we use http://census.ire.org/docs/boundary.html to locate Cafe Milano
# http://geocoder.us/

import requests

try:
    from urllib.parse import urlparse, urlencode, parse_qs, urlunparse
except ImportError:
    from urlparse import urlparse, parse_qs, urlunparse
    from urllib import urlencode
    
    
def geocoder_us(address="1600 Pennsylvania Ave, Washington, DC"):
    """takes an adddress and returns the the result of a geocoding call to geocoder.us"""
    base_url = "http://rpc.geocoder.us/service/json"
    url = base_url + "?" + urlencode({'address':address})
    return requests.get(url).json()

r = geocoder_us('2522 Bancroft Way, BERKELEY, CA, 94704')
r


Out[1]:
[{'city': 'Berkeley',
  'hash': 1440113044,
  'lat': '37.868918',
  'long': '-122.259245',
  'number': '2522',
  'prefix': '',
  'state': 'CA',
  'street': 'Bancroft',
  'suffix': '',
  'type': 'Way',
  'zip': '94704'}]

In [2]:
# let's try the census.ire.org service...which we can host ourselves soon
# http://census.ire.org/docs/boundary.html
# at least down to the tract
# http://census.ire.org/geo/1.0/boundary/?contains=38.948337,-92.328032&sets=tracts,counties



def ire_contains(lat,lng,sets=None):

    if sets is None:
        sets1 = ["tracts","counties"]
    else:
        sets1 = sets
    base_url = "http://census.ire.org/geo/1.0/boundary/"
    url = base_url + "?" + urlencode(
        {
          'contains': "{lat},{lng}".format(lat=lat, lng=lng),
          'sets': sets1
        },
        True
     )

    return requests.get(url).json()
    

response = ire_contains(lat=r[0]['lat'],lng=r[0]['long'])
response


Out[2]:
{'meta': {'limit': 20,
  'next': None,
  'offset': 0,
  'previous': None,
  'total_count': 1},
 'objects': [{'centroid': {'coordinates': [-121.917886, 37.65055],
    'type': 'Point'},
   'external_id': '06001',
   'kind': 'County',
   'metadata': {'ALAND10': 1914046110,
    'AWATER10': 213184643,
    'CBSAFP10': '41860',
    'CLASSFP10': 'H1',
    'COUNTYFP10': '001',
    'COUNTYNS10': '01675839',
    'CSAFP10': '488',
    'FUNCSTAT10': 'A',
    'GEOID10': '06001',
    'INTPTLAT10': '+37.6480811',
    'INTPTLON10': '-121.9133039',
    'LSAD10': '06',
    'METDIVFP10': '36084',
    'MTFCC10': 'G4020',
    'NAME10': 'Alameda',
    'NAMELSAD10': 'Alameda County',
    'STATEFP10': '06'},
   'name': 'Alameda',
   'resource_uri': '/geo/1.0/boundary/alameda-county/',
   'set': '/geo/1.0/boundary-set/counties/',
   'simple_shape': {'coordinates': [[[[-122.340281, 37.800628],
       [-122.346881, 37.811427],
       [-122.355881, 37.835727],
       [-122.367781, 37.866726],
       [-122.373782, 37.883725],
       [-122.358981, 37.887225],
       [-122.310881, 37.897825],
       [-122.309481, 37.897825],
       [-122.30878, 37.898025],
       [-122.30358, 37.898525],
       [-122.301479, 37.898525],
       [-122.30028, 37.898225],
       [-122.29698, 37.898925],
       [-122.293579, 37.898625],
       [-122.291079, 37.897926],
       [-122.290063, 37.897825],
       [-122.28848, 37.897925],
       [-122.28718, 37.898625],
       [-122.28498, 37.899225],
       [-122.28298, 37.900325],
       [-122.28138, 37.900625],
       [-122.27988, 37.900625],
       [-122.27798, 37.901626],
       [-122.27838, 37.903225],
       [-122.27618, 37.903826],
       [-122.27418, 37.905025],
       [-122.27108, 37.905824],
       [-122.27178, 37.904925],
       [-122.271679, 37.904724],
       [-122.268217, 37.903975],
       [-122.267244, 37.904448],
       [-122.265901, 37.904386],
       [-122.265221, 37.904201],
       [-122.2647, 37.90381],
       [-122.263946, 37.903734],
       [-122.262367, 37.901696],
       [-122.26129, 37.901392],
       [-122.261117, 37.89998],
       [-122.260776, 37.899658],
       [-122.259685, 37.899876],
       [-122.258689, 37.899032],
       [-122.258419, 37.898981],
       [-122.257113, 37.899856],
       [-122.256906, 37.899823],
       [-122.256063, 37.898421],
       [-122.256325, 37.897751],
       [-122.256274, 37.89697],
       [-122.255796, 37.89645],
       [-122.255156, 37.896258],
       [-122.255533, 37.895966],
       [-122.254981, 37.896341],
       [-122.253702, 37.896193],
       [-122.252685, 37.895015],
       [-122.252668, 37.894569],
       [-122.253101, 37.894067],
       [-122.25294, 37.893764],
       [-122.252125, 37.893542],
       [-122.251154, 37.89384],
       [-122.249477, 37.893086],
       [-122.248977, 37.891222],
       [-122.248691, 37.89083],
       [-122.249779, 37.889625],
       [-122.249479, 37.889225],
       [-122.249493, 37.888251],
       [-122.248979, 37.887325],
       [-122.248914, 37.886867],
       [-122.244079, 37.883224],
       [-122.241979, 37.881924],
       [-122.239879, 37.882925],
       [-122.238679, 37.883325],
       [-122.230979, 37.881325],
       [-122.223878, 37.878326],
       [-122.221376, 37.875325],
       [-122.217376, 37.871724],
       [-122.216276, 37.868822],
       [-122.219181, 37.867226],
       [-122.221488, 37.865026],
       [-122.220389, 37.864427],
       [-122.214736, 37.85951],
       [-122.213196, 37.857601],
       [-122.211933, 37.856946],
       [-122.210778, 37.855026],
       [-122.210978, 37.854526],
       [-122.208486, 37.85169],
       [-122.207861, 37.85175],
       [-122.207232, 37.851511],
       [-122.204094, 37.851387],
       [-122.200678, 37.847589],
       [-122.195274, 37.843587],
       [-122.195293, 37.842799],
       [-122.196101, 37.842005],
       [-122.194304, 37.841366],
       [-122.193947, 37.841028],
       [-122.193923, 37.840758],
       [-122.191778, 37.839826],
       [-122.189679, 37.838027],
       [-122.188886, 37.83763],
       [-122.185277, 37.837026],
       [-122.184674, 37.835828],
       [-122.184177, 37.833926],
       [-122.186577, 37.830426],
       [-122.187077, 37.829526],
       [-122.187177, 37.828726],
       [-122.187058, 37.827199],
       [-122.186784, 37.827043],
       [-122.185877, 37.825526],
       [-122.186677, 37.823526],
       [-122.185977, 37.820726],
       [-122.181477, 37.819526],
       [-122.176977, 37.816227],
       [-122.172775, 37.815826],
       [-122.166873, 37.813424],
       [-122.157392, 37.817952],
       [-122.156683, 37.817563],
       [-122.155717, 37.816546],
       [-122.15436, 37.816084],
       [-122.15295, 37.815034],
       [-122.148808, 37.81376],
       [-122.145946, 37.810723],
       [-122.144268, 37.807693],
       [-122.140551, 37.804594],
       [-122.138376, 37.804326],
       [-122.135076, 37.804227],
       [-122.092175, 37.801126],
       [-122.045473, 37.798126],
       [-122.030472, 37.787727],
       [-122.021972, 37.784227],
       [-122.017772, 37.781227],
       [-122.014072, 37.780227],
       [-121.997771, 37.763227],
       [-122.010771, 37.755127],
       [-122.011371, 37.754028],
       [-122.011771, 37.747428],
       [-122.001071, 37.739028],
       [-121.993571, 37.738128],
       [-121.989971, 37.733628],
       [-121.983468, 37.730864],
       [-121.982254, 37.730713],
       [-121.977603, 37.729497],
       [-121.97407, 37.729428],
       [-121.97247, 37.728528],
       [-121.96077, 37.718629],
       [-121.94997, 37.721029],
       [-121.943695, 37.722722],
       [-121.939207, 37.723606],
       [-121.939191, 37.72373],
       [-121.939068, 37.723638],
       [-121.937068, 37.724347],
       [-121.937003, 37.724247],
       [-121.932825, 37.725436],
       [-121.929544, 37.726166],
       [-121.929372, 37.726078],
       [-121.929269, 37.726228],
       [-121.914507, 37.729623],
       [-121.910138, 37.73046],
       [-121.812589, 37.754225],
       [-121.780364, 37.761827],
       [-121.666262, 37.790427],
       [-121.624161, 37.799127],
       [-121.55916, 37.818927],
       [-121.558905, 37.818432],
       [-121.557367, 37.81777],
       [-121.556936, 37.817218],
       [-121.556997, 37.816488],
       [-121.556761, 37.816087],
       [-121.557038, 37.815988],
       [-121.557064, 37.815753],
       [-121.557002, 37.77346],
       [-121.556658, 37.756028],
       [-121.557159, 37.746329],
       [-121.556959, 37.743329],
       [-121.557158, 37.714328],
       [-121.556958, 37.703629],
       [-121.556857, 37.635531],
       [-121.557257, 37.616631],
       [-121.556655, 37.542732],
       [-121.550454, 37.539332],
       [-121.548355, 37.535932],
       [-121.546854, 37.535032],
       [-121.545754, 37.532532],
       [-121.542855, 37.531432],
       [-121.539654, 37.529831],
       [-121.529554, 37.527031],
       [-121.528553, 37.527831],
       [-121.522154, 37.524832],
       [-121.516654, 37.525531],
       [-121.508553, 37.524832],
       [-121.504153, 37.525932],
       [-121.498653, 37.522632],
       [-121.499653, 37.520632],
       [-121.502053, 37.518532],
       [-121.497553, 37.513232],
       [-121.495552, 37.508432],
       [-121.496052, 37.504932],
       [-121.493352, 37.502432],
       [-121.485452, 37.502532],
       [-121.481763, 37.499932],
       [-121.48017, 37.497774],
       [-121.47962, 37.496584],
       [-121.477484, 37.49563],
       [-121.476386, 37.495577],
       [-121.476035, 37.495424],
       [-121.475485, 37.494532],
       [-121.47486, 37.494051],
       [-121.473761, 37.492777],
       [-121.472372, 37.492229],
       [-121.47196, 37.491733],
       [-121.470938, 37.491183],
       [-121.47074, 37.490794],
       [-121.469275, 37.489688],
       [-121.469214, 37.48936],
       [-121.469824, 37.48833],
       [-121.470038, 37.487109],
       [-121.470724, 37.486125],
       [-121.470511, 37.485461],
       [-121.471198, 37.485034],
       [-121.47106, 37.483699],
       [-121.471198, 37.482829],
       [-121.471925, 37.481783],
       [-121.472952, 37.482333],
       [-121.559755, 37.482533],
       [-121.564355, 37.482433],
       [-121.689079, 37.482828],
       [-121.714961, 37.483635],
       [-121.741281, 37.48394],
       [-121.765458, 37.484034],
       [-121.786346, 37.48433],
       [-121.795427, 37.483996],
       [-121.800732, 37.483991],
       [-121.865267, 37.484637],
       [-121.869934, 37.48168],
       [-121.870076, 37.480808],
       [-121.870489, 37.480329],
       [-121.870785, 37.47945],
       [-121.872742, 37.478252],
       [-121.873693, 37.477845],
       [-121.874009, 37.477407],
       [-121.874612, 37.475767],
       [-121.875205, 37.475215],
       [-121.875783, 37.474998],
       [-121.876262, 37.474924],
       [-121.876504, 37.475121],
       [-121.87745, 37.475183],
       [-121.878112, 37.474046],
       [-121.878919, 37.473332],
       [-121.879465, 37.472168],
       [-121.8799, 37.471756],
       [-121.881155, 37.471076],
       [-121.882603, 37.470554],
       [-121.883891, 37.470769],
       [-121.884415, 37.471204],
       [-121.885159, 37.471413],
       [-121.885966, 37.471375],
       [-121.887738, 37.470936],
       [-121.889352, 37.470102],
       [-121.890407, 37.468322],
       [-121.890856, 37.467982],
       [-121.894992, 37.467544],
       [-121.897653, 37.466973],
       [-121.898306, 37.466661],
       [-121.898973, 37.465845],
       [-121.899893, 37.465642],
       [-121.901307, 37.464958],
       [-121.90524, 37.464227],
       [-121.906835, 37.46372],
       [-121.907291, 37.463292],
       [-121.907388, 37.46347],
       [-121.909129, 37.462995],
       [-121.911135, 37.461822],
       [-121.911059, 37.461671],
       [-121.9139, 37.460304],
       [-121.915996, 37.458983],
       [-121.922569, 37.455538],
       [-121.922859, 37.455683],
       [-121.925548, 37.454389],
       [-121.926294, 37.454573],
       [-121.926755, 37.455241],
       [-121.927363, 37.455029],
       [-121.92785, 37.455262],
       [-121.927441, 37.456279],
       [-121.927461, 37.456705],
       [-121.927793, 37.457124],
       [-121.928444, 37.457394],
       [-121.929207, 37.456853],
       [-121.930531, 37.456489],
       [-121.932069, 37.455277],
       [-121.932626, 37.455147],
       [-121.932899, 37.455447],
       [-121.932712, 37.456842],
       [-121.931128, 37.45739],
       [-121.931521, 37.458831],
       [-121.931128, 37.460158],
       [-121.931199, 37.460644],
       [-121.931471, 37.460911],
       [-121.932085, 37.460999],
       [-121.933378, 37.460762],
       [-121.932374, 37.460352],
       [-121.933578, 37.460271],
       [-121.934732, 37.460524],
       [-121.935137, 37.46084],
       [-121.935296, 37.461421],
       [-121.934947, 37.46212],
       [-121.934987, 37.462464],
       [-121.936025, 37.462929],
       [-121.938903, 37.462394],
       [-121.939241, 37.462487],
       [-121.940023, 37.463125],
       [-121.940515, 37.464346],
       [-121.941085, 37.464948],
       [-121.943101, 37.465116],
       [-121.944226, 37.465822],
       [-121.944317, 37.4674],
       [-121.944144, 37.467623],
       [-121.944137, 37.468119],
       [-121.944244, 37.468526],
       [-121.944914, 37.469163],
       [-121.945479, 37.469168],
       [-121.946449, 37.46859],
       [-121.947087, 37.467424],
       [-121.94699, 37.466252],
       [-121.946167, 37.464487],
       [-121.946174, 37.463946],
       [-121.946409, 37.463362],
       [-121.947041, 37.462602],
       [-121.947842, 37.461978],
       [-121.949602, 37.461408],
       [-121.951921, 37.461461],
       [-121.955889, 37.46273],
       [-121.959313, 37.463515],
       [-121.964157, 37.463151],
       [-121.965951, 37.462813],
       [-121.968524, 37.461953],
       [-121.975071, 37.460639],
       [-121.980014, 37.460892],
       [-121.992669, 37.464639],
       [-121.994311, 37.466049],
       [-121.996671, 37.467239],
       [-122.021153, 37.465073],
       [-122.022472, 37.465139],
       [-122.025425, 37.46563],
       [-122.036182, 37.464966],
       [-122.037266, 37.464699],
       [-122.040698, 37.462975],
       [-122.046574, 37.459526],
       [-122.047825, 37.459007],
       [-122.050313, 37.4589],
       [-122.051931, 37.459251],
       [-122.081473, 37.477838],
       [-122.109615, 37.497685],
       [-122.117393, 37.506894],
       [-122.149175, 37.545835],
       [-122.167276, 37.568735],
       [-122.186676, 37.592734],
       [-122.246679, 37.665731],
       [-122.251979, 37.672431],
       [-122.28178, 37.70823],
       [-122.29448, 37.72803],
       [-122.31048, 37.753829],
       [-122.336581, 37.795028],
       [-122.340281, 37.800628]]]],
    'type': 'MultiPolygon'},
   'slug': 'alameda-county'}]}

In [ ]: