Thursday, July 7, 2016

Using Python to access web data Week 6 Using the GeoJSON API

The problem:
n this assignment you will write a Python program somewhat similar to http://www.pythonlearn.com/code/geojson.py. The program will prompt for a location, contact a web service and retrieve JSON for the web service and parse that data, and retrieve the first place_id from the JSON. A place ID is a textual identifier that uniquely identifies a place as within Google Maps.
API End Points
To complete this assignment, you should use this API endpoint that has a static subset of the Google Data:
http://python-data.dr-chuck.net/geojson
This API uses the same parameters (sensor and address) as the Google API. This API also has no rate limit so you can test as often as you like. If you visit the URL with no parameters, you get a list of all of the address values which can be used with this API.
To call the API, you need to provide a sensor=false parameter and the address that you are requesting as the address= parameter that is properly URL encoded using theurllib.urlencode() fuction as shown in http://www.pythonlearn.com/code/geojson.py
Test Data / Sample Execution
You can test to see if your program is working with a location of "South Federal University" which will have a place_id of "ChIJJ8oO7_B_bIcR2AlhC8nKlok".
$ python solution.py
Enter location: South Federal University 
Retrieving http://...
Retrieved 2101 characters
Place id ChIJJ8oO7_B_bIcR2AlhC8nKlok 
Turn In
Please run your program to find the place_id for this location:
Moscow Engineering-Physics Institute
Make sure to enter the name and case exactly as above and enter the place_id and your Python code below. Hint: The first seven characters of the place_id are "ChIJybD ..."
Make sure to retreive the data from the URL specified above and not the normal Google API. Your program should work with the Google API - but the place_id may not match for this assignment.

My submission:

 import urllib  
 import json  

 serviceurl = 'http://python-data.dr-chuck.net/geojson?'  

 address = raw_input('Enter location: ')  
 url = serviceurl + urllib.urlencode({'sensor':'false', 'address': address})  
 print 'Retrieving', url  
 uh = urllib.urlopen(url)  
 data = uh.read()  
 print 'Retrieved',len(data),'characters'  
 try: js = json.loads(str(data))  
 except: js = None  
 if 'status' not in js or js['status'] != 'OK':  
   print '==== Failure To Retrieve ===='  
   print data  
 
 placeid = js["results"][0]["place_id"]  
 print 'Place ID ', placeid  

19 comments:

  1. what is the id of The University of Manchester

    ReplyDelete
  2. if 'status' not in js or js['status'] != 'OK':
    TypeError: argument of type 'NoneType' is not iterable
    how to solve this

    ReplyDelete
  3. import urllib.request, urllib.parse, urllib.error
    import json
    import ssl

    api_key = False
    # If you have a Google Places API key, enter it here
    # api_key = 'AIzaSy___IDByT70'
    # https://developers.google.com/maps/documentation/geocoding/intro

    if api_key is False:
    api_key = 42
    serviceurl = 'http://py4e-data.dr-chuck.net/json?'
    else:
    serviceurl = 'https://maps.googleapis.com/maps/api/geocode/json?'

    # Ignore SSL certificate errors
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE

    address = input('Enter location: ')

    parms = dict()
    parms['address'] = address
    if api_key is not False: parms['key'] = api_key
    url = serviceurl + urllib.parse.urlencode(parms)

    print('Retrieving', url)
    uh = urllib.request.urlopen(url, context=ctx)
    data = uh.read().decode()
    print('Retrieved', len(data), 'characters')

    js = json.loads(data)

    #print(json.dumps(js, indent=4))

    first_id = js['results'][0]['place_id']

    ReplyDelete
    Replies
    1. I also tried this same code. But my output for Bangalore University is not matching the desired output. Plz help

      Delete
    2. This comment has been removed by the author.

      Delete
    3. #The code is working right in me try again...
      import urllib.request, urllib.parse, urllib.error
      import json
      import ssl

      api_key = False
      # If you have a Google Places API key, enter it here
      # api_key = 'AIzaSy___IDByT70'
      # https://developers.google.com/maps/documentation/geocoding/intro

      if api_key is False:
      api_key = 42
      serviceurl = 'http://py4e-data.dr-chuck.net/json?'
      else:
      serviceurl = 'https://maps.googleapis.com/maps/api/geocode/json?'

      # Ignore SSL certificate errors
      ctx = ssl.create_default_context()
      ctx.check_hostname = False
      ctx.verify_mode = ssl.CERT_NONE

      address = input('Enter location: ')

      parms = dict()
      parms['address'] = address
      if api_key is not False: parms['key'] = api_key
      url = serviceurl + urllib.parse.urlencode(parms)

      print('Retrieving', url)
      uh = urllib.request.urlopen(url, context=ctx)
      data = uh.read().decode()
      print('Retrieved', len(data), 'characters')

      js = json.loads(data)

      #print(json.dumps(js, indent=4))

      first_id = js['results'][0]['place_id']
      print("Place id ", first_id)

      Delete
    4. what is place id of Bangalore University

      Delete
    5. Could you please send the output for Jadavpur University?

      Delete
  4. Desired output:ChIJ5yGKLV6dF4gRGUDjG8roZhQ[this is place-id of Kalamazoo college]
    The current code is as follows:
    import urllib.request, urllib.parse, urllib.error
    import json
    import ssl

    api_key = False
    # If you have a Google Places API key, enter it here
    # api_key = 'AIzaSy___IDByT70'
    # https://developers.google.com/maps/documentation/geocoding/intro

    if api_key is False:
    api_key = 42
    serviceurl = 'http://py4e-data.dr-chuck.net/json?'
    else :
    serviceurl = 'https://maps.googleapis.com/maps/api/geocode/json?'

    # Ignore SSL certificate errors
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE

    while True:
    address = input('Enter location: ')
    if len(address) < 1: break

    parms = dict()
    parms['address'] = address
    if api_key is not False: parms['key'] = api_key
    url = serviceurl + urllib.parse.urlencode(parms)

    print('Retrieving', url)
    uh = urllib.request.urlopen(url, context=ctx)
    data = uh.read().decode()
    print('Retrieved', len(data), 'characters')

    try:
    js = json.loads(data)
    except:
    js = None

    if not js or 'status' not in js or js['status'] != 'OK':
    print('==== Failure To Retrieve ====')
    print(data)
    continue

    print(json.dumps(js, indent=4))

    lat = js['results'][0]['geometry']['location']['lat']
    lng = js['results'][0]['geometry']['location']['lng']
    print('lat', lat, 'lng', lng)
    location = js['results'][0]['formatted_address']
    print(location)

    ReplyDelete
  5. This one by sarasij.majumdar@gmail.com is perfectly running. For any other queries, u can consult me.

    ReplyDelete
  6. for Virginia Tech,what is the place_id?

    ReplyDelete
  7. What is the place I'd of university of debercen

    ReplyDelete
  8. what is the Place id for Monash University Churchill Australia

    ReplyDelete
  9. what is the place id for Madras University

    ReplyDelete
  10. what is the place id for Saint Petersburg State University of Aerospace Instrumentation

    I did the assignment many times. but it says the place id does not match

    ReplyDelete
  11. what is the place id for Open University of Catalonia?

    ReplyDelete