I am new to python and I had the task to find the US zip code based on latitude and longitude. After messing with arcgis I realized that this was giving me empty values for certain locations. I ended up coding something that accomplishes my task by taking a dataset containing all US codes and using Euclidean distance to determine the closest zip code based on their lat/lon. However, this takes approximately 1.3 seconds on average to compute which for my nearly million records will take a while as a need a zip code for each entry. I looked that vectorization is a thing on python to speed up tasks. But, I cannot find a way to apply it to my code. Here is my code and any feedback would be appreciated:
for j in range(len(myFile)): p1=0 p1=0 point1 = np.array((myFile["Latitude"][j], myFile["Longitude"][j])) # This is the reference point i = 0 resultZip = str(usZips["Zip"]) dist = np.linalg.norm(point1 - np.array((float(usZips["Latitude"]), float(usZips["Longitude"])))) for i in range(0, len(usZips)): lat = float(usZips["Latitude"][i]) lon = float(usZips["Longitude"][i]) point2 = np.array((lat, lon)) # This will serve as the comparison from the dataset temp = np.linalg.norm(point1 - point2) if (temp <= dist): # IF the temp euclidean distance is lower than the alread set it will: dist = temp # set the new distance to temp and... resultZip = str(usZips["Zip"][i]) # will save the zip that has the same index as the new temp # p1=float(myFile["Latitude"]) # p2=float(myFile["Longitude"]) i += 1
I am aware Google also has a reverse geocoder API but it has a request limit per day.
The file called
myFile is a csv file with the attributes userId, latitude, longitude, timestamp with about a million entries. The file usZips is public dataset with information about the city, lat, lon, zip and timezone with about 43k records of zips across the US.
resultZipthe result? Why is it not saved/printed? Please include the rest of the code if possible. \$\endgroup\$