Skip to content

Instantly share code, notes, and snippets.

@krogebry
Created May 12, 2021 21:08
Show Gist options
  • Save krogebry/e21cce012e73ee534238a906c401cebf to your computer and use it in GitHub Desktop.
Save krogebry/e21cce012e73ee534238a906c401cebf to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3.8
import re
import csv
import mpu
import json
import time
import pprint
import googlemaps
from datetime import datetime
pp = pprint.PrettyPrinter()
source = {
"lat": 43.534214,
"lng": -96.7133946
}
gmaps = googlemaps.Client(key='AIzaSyDIAsKzzuQlAyE92ldgSeHSMNcKcD3vzC8')
database = json.loads(open("/tmp/lori.json").read())
def write_db(data):
out = open("/tmp/lori.json", "w")
out.write(json.dumps(data))
out.close()
# Geocoding an address
# geocode_result = gmaps.geocode('1600 Amphitheatre Parkway, Mountain View, CA')
#
# # Look up an address with reverse geocoding
# # reverse_geocode_result = gmaps.reverse_geocode((40.714224, -73.961452))
# pp.pprint(geocode_result)
# Request directions via public transit
# now = datetime.now()
# directions_result = gmaps.directions("Sydney Town Hall",
# "Parramatta, NSW",
# mode="transit",
# departure_time=now)
# exit()
reader = csv.reader(open("/home/bkroger/dev/mpn.tsv"), delimiter="\t")
fields = ["MPN", "Hospital Name", "Address", "N", "N", "City", "State", "Zip", "GeoLookup", "Lat", "Long", "Distance"]
max = 0
# for row in reader:
# matched_city = False
#
# if row[0] == "MPN":
# continue
#
# data = {}
# for i, field in enumerate(row):
# print(f"{i} / {field}")
# if i == 1 and re.match(r".*\s\s\s.*", field):
# # (h_name, address, city) = re.split("\s\s+\W", field)
# split = re.split("\s\s+\W", field)
# data["Hospital Name"] = split[0]
# data["Address"] = split[1]
#
# if len(split) == 3:
# data["City"] = address
#
# if i == 2 and re.match(r".*\s\s\s.*", field):
# # print(f"Found tab in: {field}")
# (address, city) = re.split("\s\s+\W", field)
# # print(f"A: {address} / {city} ")
# data["Address"] = address
# data["City"] = city
# matched_city = True
#
# if i == 5 and matched_city:
# data["State"] = field
# # continue
#
# if i == 6 and matched_city:
# continue
#
# else:
# data[fields[i]] = field
#
# # 1600 Amphitheatre Parkway, Mountain View, CA
# if data["City"] == "" and data["N"] != "":
# data["City"] = data["N"]
#
# pp.pprint(data)
output = []
for mpn, data in database.items():
# if mpn != "450848":
# continue
# if data["Lat"] == "#ERROR!":
# print(mpn)
# pp.pprint(data)
# if data["Address"] == "":
# continue
#
# if data["GeoLookup"] == "#ERROR!":
# addy = f"{data['Address']}, {data['City']}, {data['State']}"
# print(f"Address: {addy}")
# geocode_result = gmaps.geocode(addy)
#
# if len(geocode_result) == 0:
# print("No geocode")
# continue
#
# pp.pprint(geocode_result)
# data["Lat"] = geocode_result[0]["geometry"]["location"]["lat"]
# data["Long"] = geocode_result[0]["geometry"]["location"]["lng"]
#
# data["GeoLookup"] = f"{data['Lat']}, {data['Long']}"
#
# database[data["MPN"]] = data
# write_db(database)
# time.sleep(1)
# # write_db(database)
#
if data["GeoLookup"] != "#ERROR!" and data["GeoLookup"] != "":
if data["Lat"] == "#ERROR!" and data["State"] == "#ERROR!":
zip = data["City"]
lat = data["Zip"]
long = data["GeoLookup"]
state = data["N"]
data["City"] = ""
data["GeoLookup"] = f"{lat}, {long}"
data["Lat"] = lat
data["Long"] = long
data["State"] = state
pp.pprint(data)
if data["Lat"] == "#ERROR!" and re.match(".*,.*", data["State"]):
geo = data["State"]
data["GeoLookup"] = geo
data["State"] = data["N"]
data["Zip"] = data["City"]
pp.pprint(data)
if data["Lat"] == "#ERROR!" or data["Long"] == "#ERROR!":
print(f"Geo: {data['GeoLookup']}")
pp.pprint(data)
(lat, long) = data["GeoLookup"].split(",")
data["Lat"] = lat
data["Long"] = long
d = mpu.haversine_distance((source["lat"], source["lng"]), (float(data["Lat"]), float(data["Long"])))
print(f"Distance: {d}")
data["Distance"] = d
database[data["MPN"]] = data
write_db(database)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment