# Calculating distances between multiple Geo-Points in a survey with the Haversine Formula

3600
9
01-10-2018 04:44 AM
Esri Contributor
8 9 3,600

This blog post provides a solution for:

Capturing multiple locations per survey

Calculating straight-line distances between a base location point in the survey and other repeating locations         captured

Consider the scenario where your survey must assist in the administration of new liquor licence applications 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 (the base point or new_licence_location field in the survey below)

Step 3: Create a repeat section to capture the occurrences of nearby schools, places of worship etc. Each with a premise type and a geopoint (the nearby_location field in the survey below)

Your survey design should look similar to this:

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

Do the same for the nearby_location geopoint field:

Step 5: Use the X, Y (lat,long) 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 making some cosmetic enhancements (and adding the 500m stipulation) your survey should look like this:

The user can add multiple locations (Nearby Premises) at will in the repeat section and each will have it's own distance calculation to the base point (New Licence Location).

## Feel free to use and adapt as you require!

Tags (2)
New Contributor II

Thanks for posting this Deon!

Do you have any thoughts on using the Haversine calculation to determine the coordinate of a feature depending on the azimuth and distance from the location of the device collecting the information?

For example, locating the location of an animal as part of a wildlife survey. The field user is provides a geopoint with the location of the observation, an azimuth and a distance (meters) and Survey123 stores the derived geopoint? I cannot imagine why this wouldn't work but I'm not sure of the formula to use.

If you think this will work and can provide some guidance, we will put it to work right away. Apologies if this has already been posted somewhere else.

Esri Contributor

Hi Dorn

Thank you for reading the blog. What you need sounds do-able. I will have a look as soon as I get a chance and see if I can that working. This website seems to discuss exactly that: Calculate distance and bearing between two Latitude/Longitude points using haversine formula in Java...

Lower down in the article they discuss destination point given distance and bearing from start point...

New Contributor II

Thanks Deon.

I think the scripts you linked to will help. The real challenge will be 'adjusting' the resulting geopoint that is stored in the survey and not the observer point.

Esri Contributor

Hi Dorn

Only having one geopoint per "table" will cause a problem though. Do you foresee having this in a related table? So the user is capturing his "base" point and then all the observations from there are in the related table?

Occasional Contributor III

Thanks so much for posting this. I have a question related to this blog. And any help on it is greatly appreciated

I have lats and longs for 8 different facilities in our city. Dependant on where the survey is undertaken, is it possible to pre-poulate a select_one question to the nearest facility?

i.e. Which facility are you rating? (Pre-populate answer dependant on the nearest facility to user’s location)

Thanks,

Gee

Esri Contributor

Hi Gee

Will there always only be 8 facilities? If so, then you can calculate the distance of the user location to each one of the facilities and default the select_one to the correct one... But this will only work if you have a static number of facilities though. I have done something "similar" and published the Survey123 source files to AGOL:

AGOL Survey123 Solution Resource Item Zipfile

In my example you will see the following:

1) Let the user choose one of the facilities and see it on the map

2) Let the user retrieve the device's location from which the survey will then calculate the closest facility

3) Once a closest facility has been calculated then that is the only option to choose from in the select_one

I know you want to allow your user to still choose one of the other facilities - see if you can get that to work, I havent attempted that yet!

Occasional Contributor III

Thanks Deon, this is extremely helpful

Yes, the number of locations will always be a static number. And this workaround is PERFECT.

THANKS,

Gee

MVP Frequent Contributor

Been using this all the time. Thanks a lot!

by
New Contributor

This is amazing, thank you! I have a question - when using this to measure the distance from geopoints in one repeat to a geopoint in another repeat, does it measure to the last geopoint in that repeat?

My example is:

Repeat 1: points around the edge of a building (lets say 4 if its a rectangle).

Repeat 2: points located position of electricity poles (lets say there's two in the repeat, at opposite sides of the building).

I want to calculate the distance from each pole to the nearest point of the building. Ideally i'd measure from the repeating points (poles) to one geoshape (building) but i don't think this is possible, so trying a workaround to calculated from point to point.

Appreciate its a bit of a complex one, any help would be really appreciated!