January 12, 2023

Helping Patients Find Care: Calculating for "Near Me"


Our CarePrecise Gold™ and CarePrecise Platinum™ customers have been wild about the geographic lookup of healthcare providers since we first introduced geographic radius search more than ten years ago in our CP ListMaker™ software. It's not a hard thing to do, and I'll show you how it works so you can use it in your own applications!

 All you need is a Zip Code® table that incudes longitude and latitude for the center of each zip if you want a rough estimation of distance (included in CarePrecise Gold and Platinum), or you can purchase our SelectGeo database that has the rooftop-level latitude/longitude pair ("geocode") for every U.S. healthcare provider's address.

The code we offer here is also included in CP ListMaker as open source, and it relies on some of the built-in functions available in VBA: the Sin() and Cos() math functions (sine and cosine), and a few others like the Abs() function (returns the absolute of a number).  All of the build-in functions can be found fully described at support.microsoft.com.

You can use the first function, CalcDistance(), to just return the distance between two points, or along with the others to list all of the spots in a miles radius from a central point. If you want to see only all of the physicians located within a radius, you would first create a query that filters for just physician locations. 

The Calculations

The magic happens with just three little Visual Basic for Application functions, shown below, which are pretty easily converted to VB or other code. After you create the CalcDistance() function, you send it the latitude and longitude of both locations, and it calculates the distance between them. 

Here's the VB/VBA code (shown as used in a Microsoft Access module)...

CalcDistance

This function calculates the distance between latitude/longitude pairs, and returns it in miles.

Public Function CalcDistance(lat1, lon1, lat2, lon2) As Variant

'This function calculates the distance between two latitude/longitude pairs.

Dim theta

Dim dist As Double

theta = lon1 - lon2

dist = Sin(deg2rad(lat1)) * Sin(deg2rad(lat2)) + Cos(deg2rad(lat1)) * Cos(deg2rad(lat2)) * Cos(deg2rad(theta))

If dist > 0 Then

    dist = dist - 0.00000000000001

End If

dist = acos(dist)

dist = rad2deg(dist)

dist = dist * 69.09

CalcDistance = dist

End Function

The Radius Functions

Use these functions to return the points of interest within a radius from a central point:

Public Function acos(Rad)

  If CDbl(Abs(Rad)) <> 1 Then

  acos = (pi / 2) - Atn(Rad / Sqr(1 - Rad * Rad))

  ElseIf Rad = -1 Then

    acos = pi

  End If

End Function

Public Function deg2rad(Deg)

    deg2rad = CDbl(Deg * pi / 180)

End Function

Public Function rad2deg(Rad)

    rad2deg = CDbl(Rad * 180 / pi)

End Function

Geocoding Applications

One of the most common applications of this code, used in tandem with our healthcare provider databases, is in web apps that help patients find nearby providers. CarePrecise physician, hospital and clinic data powers many online applications and services – some may be familiar to you (we maintain NDAs with our clients so we won't list them here).

Once a patient has chosen a nearby provider, the CalcDistance() function is used to say just how far away it is. Integration with Google Maps or another mapping API can then map the route using the coordinates from our SelectGeo database. 

This is some of the hardest working code out there, and the uses abound. Play with it!

Note: CarePrecise does not offer technical support on the open source code we publish. We put all of our efforts into supporting our data products! To get a copy of CP ListMaker, complete with center-zip geocodes for all U.S. zips, you can either purchase CarePrecise Gold, Platinum, which have include data on all HIPAA-covered U.S. healthcare providers, or just the CP ListMaker add-on (with no provider data).
- Michael

No comments:

Post a Comment