nzpostaddy

yay maps, something I haven't really used before. Good to try something new.

Using some demo examples from nz post. Examples are printed below.

script to search for addresses.

Gets address from nzpost then displays static map from mapquestapi


In [1]:
import json
import shutil
import requests

In [2]:
opjsres = open('/home/wcmckee/github/nzpostad.json', 'r')

In [3]:
opjrd = opjsres.read()

In [4]:
nzpod = json.loads(opjrd)

In [5]:
nzpod


Out[5]:
{'addresses': [{'address_id': '196111',
   'dpid': '754977',
   'full_address': '19 Queen Alexandra Street, Lansdowne, Masterton 5810'},
  {'address_id': '197147',
   'dpid': '750361',
   'full_address': '23 Queen Alexandra Street, Lansdowne, Masterton 5810'},
  {'address_id': '195925',
   'dpid': '751485',
   'full_address': '7A Queen Alexandra Street, Lansdowne, Masterton 5810'},
  {'address_id': '198893',
   'dpid': '747422',
   'full_address': '8 Queen Alexandra Street, Lansdowne, Masterton 5810'},
  {'address_id': '195986',
   'dpid': '754969',
   'full_address': '9A Queen Alexandra Street, Lansdowne, Masterton 5810'}],
 'success': True}

In [6]:
nzpaddy = nzpod['addresses']

In [7]:
nzplen = len(nzpod['addresses'])

In [8]:
nzplen


Out[8]:
5

In [9]:
for nzp in range(0, nzplen):
    nzitm = (nzpaddy[nzp])
    print(nzitm['full_address'])


19 Queen Alexandra Street, Lansdowne, Masterton 5810
23 Queen Alexandra Street, Lansdowne, Masterton 5810
7A Queen Alexandra Street, Lansdowne, Masterton 5810
8 Queen Alexandra Street, Lansdowne, Masterton 5810
9A Queen Alexandra Street, Lansdowne, Masterton 5810

In [10]:
opsuccessad = open('/home/wcmckee/github/nzpostsuccess.json', 'r')

In [11]:
opsucrd = opsuccessad.read()

In [12]:
opjslo = json.loads(opsucrd)

In [13]:
opjslo


Out[13]:
{'address': {'city': 'Dunedin',
  'country': 'New Zealand',
  'depot_name': 'Dunedin Depot',
  'is_rural_delivery': False,
  'latitude': -45.9041,
  'longitude': 170.552,
  'postcode': '9013',
  'run_number': 'DCP025  ',
  'street': 'Oregon',
  'street_number': 71,
  'street_type': 'Street',
  'suburb': 'Ocean Grove'},
 'message_id': '618a5799-85e3-4559-8a62-280f231a26b3',
 'success': True}

In [14]:
opja = opjslo['address']

In [15]:
opja


Out[15]:
{'city': 'Dunedin',
 'country': 'New Zealand',
 'depot_name': 'Dunedin Depot',
 'is_rural_delivery': False,
 'latitude': -45.9041,
 'longitude': 170.552,
 'postcode': '9013',
 'run_number': 'DCP025  ',
 'street': 'Oregon',
 'street_number': 71,
 'street_type': 'Street',
 'suburb': 'Ocean Grove'}

In [16]:
latad = opja['latitude']

In [17]:
longad = opja['longitude']

In [18]:
latad


Out[18]:
-45.9041

In [19]:
longad


Out[19]:
170.552

In [ ]:
mapk = open('/home/wcmckee/github/mapkey.txt', 'r')

In [ ]:
mapkrd = mapk.read()

In [ ]:
reqimg = 'http://www.mapquestapi.com/staticmap/v4/getmap?key=' + mapkrd + '&size=400,200&zoom=10&center=' + str(latad) + ',' + str(longad)

In [ ]:
rmnz = reqimg.replace('\n', '')

In [ ]:
response = requests.get(rmnz, stream=True)
with open('img.png', 'wb') as out_file:
    shutil.copyfileobj(response.raw, out_file)
del response