Created
June 3, 2020 01:24
-
-
Save mostafam/7e04a31b506916ddafd3733f36997685 to your computer and use it in GitHub Desktop.
Final Take!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
from uszipcode import SearchEngine | |
import sqlite3 | |
search = SearchEngine(db_file_dir="/tmp/db") | |
conn = sqlite3.connect("/tmp/db/simple_db.sqlite") | |
pdf = pd.read_sql_query("select zipcode, lat, lng, radius_in_miles, | |
bounds_west, bounds_east, bounds_north, bounds_south from | |
simple_zipcode",conn) | |
brd_pdf = sc.broadcast(pdf) | |
@udf('string') | |
def get_zip_b(lat, lng): | |
pdf = brd_pdf.value | |
try: | |
out = pdf[(pdf['bounds_north']>=lat) & | |
(pdf['bounds_south']<=lat) & | |
(pdf['bounds_west']<=lng) & | |
(pdf['bounds_east']>=lng) ] | |
dist = [None]*len(out) | |
for i in range(len(out)): | |
dist[i] = (out['lat'].iloc[i]-lat)**2 + (out['lng'].iloc[i]-lng)**2 | |
zip = out['zipcode'].iloc[dist.index(min(dist))] | |
except: | |
zip = 'bad' | |
return zip | |
output_df = df.withColumn('zip', get_zip_b(col("latitude"),col("longitude"))).cache() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment