Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I'm giving my hands-on reverse geocoding with the python and the module geocoder 

I've created this script:

#!/Users/admin/anaconda/bin/python

import geocoder

import unicodecsv

import logging

with open('locs2.csv', 'rb') as f:

    reader = unicodecsv.DictReader(f, encoding='iso-8859-1')

    for line in reader:

        lat = line['lat']

        lon = line['lon']

        g = geocoder.google(['lat','lon'], method=reverse)

        if g.ok:

          g.postal

          logging.info('Geocoding SUCCESS: ' + address)

        else:

          logging.warning('Geocoding ERROR: ' + address)

As indicated by the doc here, we can do reverse. In any case, when I'm running the script , I have this mistake 

NameError: name 'reverse' is not defined

Why?

This is one of the sample from my file

lat, lon

48.7082,2.2797

48.7577,2.2188

47.8333,2.2500

48.9833,1.7333

47.9333,1.9181

46.2735,4.2586

I also have this error

WARNING:root:Geocoding ERROR:

The amended script

#!/Users/admin/anaconda/bin/python

import geocoder

import unicodecsv

import logging

pcode=[] 

lat=[]

lon=[]

with open('locs2.csv', 'rb') as f:

    reader = unicodecsv.DictReader(f, encoding='iso-8859-1')

    for line in reader:

        lat = line['lat']

        lon = line['lon']

        g = geocoder.google([lat,lon], method='reverse')

        if g.ok:

          pcode.extend(g.postal)

          logging.info('Geocoding SUCCESS: '+

str(lat)+','+str(lon)+','+str(pcode))

        else:

          logging.warning('Geocoding ERROR: ' + str(lat)+','+str(lon))

fields= 'lat', 'lon', 'pcode'

rows=zip(lat,lon,pcode) 

with open('/Users/admin/python/myfile.csv', 'wb') as outfile: 

    w =  unicodecsv.writer(outfile, encoding='iso-8859-1') 

    w.writerow(fields) 

    for i in rows: 

        w.writerow(i)

1 Answer

0 votes
by (26.4k points)

The issue is that you neglected to add quotes around your choice reverse: 

g = geocoder.google(['lat','lon'], method='reverse')

I think about that as a misstep in their documentation. You could make an message on the creator to advise him/her of this, he/she should refresh their docs. 

Besides, you'll need to call it with the real parameters lat and lon, not the strings. Else you'll get a error. Along these lines,

g = geocoder.google([lat, lon], method='reverse')  # lat and lon you've extracted already, but maybe you'll still need to cast them to float.

Are you looking for a good python tutorial? Join the python course fast and gain more knowledge in python.

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
0 votes
4 answers
asked Apr 4, 2021 in Python by laddulakshana (16.4k points)
0 votes
4 answers
0 votes
4 answers

Browse Categories

...