Search code examples
google-mapspython-3.5geopy

geopy geocode does not map addr - browser googlemap does


I am trying to map a list of (~320+) Texas addresses to lat,lng.

I started using geopy (simple example) and it worked for some addresses but it failed on a set of addresses.

So I integrated a backup with googlemaps geocode... but it too failed. Below is the code... see address_to_geoPt.

Yet, when I submit the failed addresses via the browser, it finds the address... any tips on how to get more reliable hits? Which googleapi should I use (see address_to_geoPt_googlemaps())

class GeoMap(dbXLS):
    def __init__(self, **kwargs):
        super(umcGeoMap, self).__init__(**kwargs) 
        # Geo Locator
        self.gl = Nominatim()
        self.gmaps = googlemaps.Client(key='mykeyISworking')


        shName = self.xl.sheet_names[0] if 'sheet' not in kwargs else kwargs['sheet']
        self.df = self.xl.parse(shName)

    def address_to_geoPt(self, addr):
        l = self.geoLocation(addr)
        if l : return (l.latitude, l.longitude) 
        return (np.nan, np.nan)


    def address_to_geoPt_googlemaps(self, addr):
        geocode = self.gmaps.geocode(addr)
        if l == None : return (np.nan, np.nan)

        # Geocoding an address
        locDict = geocode[0]['geometry']['location']
        return(locDict['lat'], locDict['lng'])       


    def address(self, church):
        return (church.Address1 + " "
                                    + church.City + " " 
                                    + church.State + " " 
                                    + church.ZipCode + " "
                                    + church.Country)


    def church_to_geoPt(self, church):
        a = (church.Address1 + " "
             + church.City + " "
             + church.State)
        if pd.isnull(church.geoPt):
            (lat, lng) = self.address_to_geoPt(a)
        else: (lat, lng ) = church.geoPt
        if not pd.isnull(lat) : 
            print("DEBUG to_geoPt 1", lat, lng, a)
            return (lat,lng)
        (lat, lng) = self.address_to_geoPt_googlemaps(a)
        print("DEBUG to_geoPt 2", lat, lng, a)
        return (lat, lng)

The following shows a set of addresses that are not mapped by geocoders.

4     3000 Bee Creek Rd Spicewood TX 78669-5109 USA
6             P O BOX 197 BERTRAM TX 78605-0197 USA
10              2833 Petuma Dr Kempher TX 78639 USA

Solution

  • @geocodezip provided the answer... and the code worked the next day.