Part 1 - Setup

We will be using

  • PIL (Python Image Library) for getting the picture
  • pytesseract for the OCR (Object Character Recognition)
  • googlemaps and gmaps for mapping

In [ ]:
from PIL import Image 
import pytesseract 
import googlemaps
import gmaps as jupmap
import sys 
from datetime import datetime

# get my private keys for google maps and gmaps
f = open('private.key', 'r')
for line in f: 
    temp = line.rstrip('').replace(',','').replace('\n','').split(" ")
    exec(temp[0])
myMap = googlemaps.Client(key=googlemap_key)
jupmap.configure(api_key=jupmap_key)

Part 2 - Use OCR to read the address

On Android this can be done with Google Vision


In [ ]:
img = Image.open('mm_address.jpg')
label = pytesseract.image_to_string(img)
print(label)
clientLocation = label.splitlines()[2] + ', ' + label.splitlines()[3]
print(clientLocation)
testLocation = '2403 Englewood Ave, Durham, NC 27705'
print(testLocation)

Testing location

This might not be neccessary as google will take addresses


In [ ]:
testGeoCode = myMap.geocode(testLocation)[0]
lat = testGeoCode.get('geometry').get('location').get('lat')
lng = testGeoCode.get('geometry').get('location').get('lng')
print(lat, ' ', lng )
clientList = ['300 N Roxboro St, Durham, NC 27701','911 W Cornwallis Rd, Durham, NC 27707', '345 W Main Street, Durham, NC 27701' ]
wp=[]
for x in clientList:
    testGeoCode = myMap.geocode(x)[0]
    lat = testGeoCode.get('geometry').get('location').get('lat')
    lng = testGeoCode.get('geometry').get('location').get('lng')
    wp.append([lat,lng])
print(wp)

Google Maps

This api does not have a waypoint optimization Here are more details on how to do it: https://developers.google.com/maps/documentation/directions/intro


In [ ]:
m = jupmap.Map()
home = (36.0160282, -78.9321707)
foodLion = (36.0193147,-78.9603636)
church = (35.9969749, -78.9091543)
dl = jupmap.directions_layer(church, home, waypoints=wp)
#googlemaps has an optimize_waypoints=True but I can't find it in jupyter gmaps 
m.add_layer(dl)
m

In [ ]:
import gmaps
import gmaps.datasets
locations = gmaps.datasets.load_dataset("starbucks_uk")
print(locations)
fig = gmaps.Map()
starbucks_layer = gmaps.symbol_layer(locations, fill_color="green", stroke_color="green", scale=2)
fig.add_layer(starbucks_layer)
fig

In [ ]: