Hopeing that someone can offer advice here. I need some API access to python support for distance calculation between two locatons (not the HubDB filter).
I have seen the sophisticated and well supported GeoPy package referenced to offer such functionality, but it seems that it it is not possible to import this API into python code in a custom workflow step ( module not found errors when using import ). So two questions arise:
- Is there a generic access mechanism to install python packages not available in the standard custom code environment to get access to available powerful APIs?
- Is there any alternative funtionality for geographic distance calculation etc. available within the standard python custom code run-time?
Grateful for all pointers here.
Hi, @SteveHTM 
Have you thought about using the Haversine formula? This would let us stick to only using Python’s built-in maths library.
Here’s an example:
def calculate_distance(lat1, lon1, lat2, lon2):
R = 6371.0 # radius of Earth in kilometers. Use 3956 for miles
lat1, lon1, lat2, lon2 = map(radians, [lat1, lon1, lat2, lon2])
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
sqrt_a = sqrt(a)
c = 2 * atan2(sqrt_a, hypot(1, -sqrt_a))
distance = R * c
return distance
Just remember to pass latitude and longitude values as decimal degrees into the function. The function will return the distance in kilometers due to the Earth’s radius (R) being set to 6371.0 kilometers.
Please let us know if this worked or if you found another way to solve this challenge.
Have fun building! — Jaycee
PS, In full disclosure, I did ask GPT-4 for help in converting the formula into Python. What’s not to like about a little spherical trigonometry amongst friends? 
Thanks @Jaycee_Lewis (and ChatGPT) for this formula - very interesting! Getting lat and long (from city/zip for example) still seems to be an problem without GeoPy. I realize that there is a HubL function, but functionality does not seem to be found elsewhere.
Hey, Steve! You are 100% correct. Without a way to convert zip codes to lat/long, I can steal all the trig I want from 19th century ship captains, but it won’t make a lick of difference.
Let’s tag some of our community members into the converstaion — hey @tominal @JBeatty @Teun @ChrisoKlepke, do you have any additional suggestions for our friend @SteveHTM?
I did have another thought if we’re committed to having to use a workaround of some type. Care to go down another rabbit hole?
What if we have two workflows? Both with a custom coded action. One to handle the zip to lat/long conversion for your Contacts. And one to do the measuring.
Workflow A:
- Enroll contacts based on the condition that a ZIP code is known
- Use a custom coded action to send a request to your external server with the ZIP code
- The server performs the ZIP code to latitude/longitude conversion
- Google’s Geocoding API is an option
- Your server sends the latitude and longitude back to the Contacts API as values for custom properties
Workflow B:
- Enroll contacts based on the condition that both latitude and longitude properties are known
- Use a Python action to calculate the distance between the two points using the latitude and longitude properties
While it’s enjoyable for me to brainstorm creative solutions, I acknowledge that there are likely real-world limitations that I am unaware of or not seeing.
For example, setting up an external server and using Google’s Geocoding API (or any other geocoding service) may involve additional costs and technical expertise. It’s also important to make sure your server is secure and that it can handle the volume of requests that may be sent to it.
Talk soon! — Jaycee
Thanks again for community advice!
I’m looking into the use of ZipCodeAPI - Location Data Documentation -Zipcodedownload as a commercial service to do city/zip translation into lat/lon - at least for US/CA.
I’ll then be in a position to use your spendid Haversine code to generate distances! I’ll update here when I have all the parts working smoothly.
Steve
That’s wonderful! Glad we could talk it through together 
I look forward to hearing how you get it all set up 
Have fun building! —Jaycee