Calculating distances between locations (geopoints) in Survey123 with the Haversine Formula

Consider the scenario where a person is applying for a liquor licence and the law stipulates: New liquor premises must be located at least five hundred meters (500m) away from schools, places of worship; recreation facilities, rehabilitation or retreat centers, residential areas and public institutions.

For this requirement Survey123 can be a valuable tool and in this blog post I will show you how you can enable this functionality in a Survey123 form:

Step 1: Create a new survey with Survey123 Connect

Step 2: Create a geopoint field in the main survey which will be used to capture the location of the new licence premises

Step 3: Create a repeated section to capture the occurrences of nearby schools, places of worship etc. Each with a premise type and a geopoint

Your survey design should look similar to this:

type name label
geopoint new_licence_location New Licence Location
begin repeat nearby_places Nearby Permises
select_one category place_type Type of Premises
geopoint nearby_location Nearby Location
end repeat

Step 4: Now, split both the coordinates sets into their x,y pairs and convert them to radians:

calculate gps_lat gps lat pulldata(‘@geopoint’, ${new_licence_location}, ‘y’) * pi() div 180
calculate gps_long gps long pulldata(“@geopoint”, ${new_licence_location}, “x”) * pi() div 180

Do the same for the nearby_location geopoint field.
Step 5: Use the X, Y pairs to perform the distance calculation (in meters) with the Haversine formula:

acos(sin(${gps_lat_end})*sin(${gps_lat}) + cos(${gps_lat_end})*cos(${gps_lat})*cos(${gps_long}-${gps_long_end}) ) * 6371000

After having made some cosmetic enhancements and adding the 500m stipulation your survey should look like this:

DistanceCalcScreen

Feel free to use and adapt as you require. Here is a link to the Survey123 Excel design file: Licence Application

Leave a Reply