# Calculate distance between geopoints

**URL:** <https://forum.getodk.org/t/calculate-distance-between-geopoints/19846>\
**Category:** Support\
**Tags:** odk-xlsform, odk-collect\
**Created:** [May 15, 2019, 10:11pm UTC](https://forum.getodk.org/t/calculate-distance-between-geopoints/19846 "2019-05-15T22:11:14Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![andhulthen](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/andhulthen/32/7057_2.png) [@andhulthen](https://forum.getodk.org/u/andhulthen)\
**Post date:** [May 15, 2019, 10:11pm UTC](https://forum.getodk.org/t/calculate-distance-between-geopoints/19846/1 "2019-05-15T22:11:14Z")

</div>

Hi all,

I am using ODK Collect (1.21.2) and XLS form, and I am designing a form to record locations for field collected insect samples. I'd like each new sampling location (in the same form) to be over a minimum distance from the one before it. Is it possible in the form to 1.Calculate the linear distance between sample geopoints, and 2. Have a constraint based on this minimum distance for new geopoints?

Any advice/assistance would be greatly appreciated...

---

<div class="post-metadata">

**Author:** ![Xiphware](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/xiphware/32/2587_2.png) [@Xiphware](https://forum.getodk.org/u/Xiphware)\
**Post date:** [May 15, 2019, 11:17pm UTC](https://forum.getodk.org/t/calculate-distance-between-geopoints/19846/2 "2019-05-15T23:17:12Z")

</div>

> [@andhulthen](#):
>
> 1.Calculate the linear distance between sample geopoints

If you put your two geopoints in a geotrace, you can measure the distance of the trace - ie the distance between the two locations - using the ODK [distance()](https://docs.opendatakit.org/form-operators-functions/#distance) function.

After which you can use this value in a constraint no different than a regular constraint calculation; eg ${distance} \< 100

---

<div class="post-metadata">

**Author:** ![andhulthen](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/andhulthen/32/7057_2.png) [@andhulthen](https://forum.getodk.org/u/andhulthen)\
**Post date:** [May 16, 2019, 12:02am UTC](https://forum.getodk.org/t/calculate-distance-between-geopoints/19846/3 "2019-05-16T00:02:48Z")

</div>

@Xiphware I had considered this, but the sample points need to selected independently, and there are several questions in the form related to sample collection information following the first geopoint selection, and before the selection (and geopoint) of the next sample collection location. Does that make sense?

---

<div class="post-metadata">

**Author:** ![Xiphware](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/xiphware/32/2587_2.png) [@Xiphware](https://forum.getodk.org/u/Xiphware)\
**Post date:** [May 16, 2019, 12:22am UTC](https://forum.getodk.org/t/calculate-distance-between-geopoints/19846/4 "2019-05-16T00:22:16Z")

</div>

You do not need to capture an actual geotrace per se - you can in fact construct a geotrace by simply concat'ing two (independently acquired) geopoints together, separated by a '`;`', eg

`concat(${location1}, ';', ${location2})`

Try this:

[geopointdistance.xls](https://getodk.b-cdn.net/uploads/default/original/2X/0/07a55d5fd5f8a8810c2630f73802c3afe51e20c7.xls) (5.5 KB)

 ![33%20PM](https://getodk.b-cdn.net/uploads/default/original/2X/7/7df553afd8e0b885e0a7de7feaa3edafdd98d23a.png)

---

<div class="post-metadata">

**Author:** ![andhulthen](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/andhulthen/32/7057_2.png) [@andhulthen](https://forum.getodk.org/u/andhulthen)\
**Post date:** [May 16, 2019, 1:16am UTC](https://forum.getodk.org/t/calculate-distance-between-geopoints/19846/5 "2019-05-16T01:16:03Z")

</div>

Works perfectly. Thanks @Xiphware!

---

<div class="post-metadata">

**Author:** ![famador](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/famador/32/8770_2.png) [@famador](https://forum.getodk.org/u/famador)\
**Post date:** [May 27, 2019, 1:43pm UTC](https://forum.getodk.org/t/calculate-distance-between-geopoints/19846/6 "2019-05-27T13:43:10Z")

</div>

Hello, i try to download the example but appear a message that the site do not exist.

---

<div class="post-metadata">

**Author:** ![arazak584](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/arazak584/32/14785_2.png) [@arazak584](https://forum.getodk.org/u/arazak584)\
**Post date:** [October 21, 2020, 3:16pm UTC](https://forum.getodk.org/t/calculate-distance-between-geopoints/19846/7 "2020-10-21T15:16:01Z")

</div>

Please is the output in metres?

---

<div class="post-metadata">

**Author:** ![Xiphware](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/xiphware/32/2587_2.png) [@Xiphware](https://forum.getodk.org/u/Xiphware)\
**Post date:** [October 21, 2020, 5:46pm UTC](https://forum.getodk.org/t/calculate-distance-between-geopoints/19846/8 "2020-10-21T17:46:44Z")

</div>

per [distance()](https://docs.getodk.org/form-operators-functions/#distance) :

> **distance(nodeset | geoshape | geotrace)**
> 
> Returns the distance, in _meters_, of either:
> 
> - a nodeset of geopoints
> - the perimeter of a geoshape
> - the length of a geotrace value  
> ...

---

<div class="post-metadata">

**Author:** ![Beryl\_Makori1](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/beryl_makori1/32/15867_2.png) [@Beryl\_Makori1](https://forum.getodk.org/u/Beryl_Makori1)\
**Post date:** [July 14, 2021, 6:12am UTC](https://forum.getodk.org/t/calculate-distance-between-geopoints/19846/9 "2021-07-14T06:12:30Z")

</div>

Hi @Xiphware thank you this is good. But is there a way I can make the location 1 a constant in all the forms. ie. the enumerator will be coming from the same location(their house) each day to carry out activities, then location 2 is taken at the point of activity.  
This is so that they do not have to keep taking the coordinates of their homes each time before they leave.

---

<div class="post-metadata">

**Author:** ![danbjoseph](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/danbjoseph/32/21901_2.png) [@danbjoseph](https://forum.getodk.org/u/danbjoseph)\
**Post date:** [July 22, 2024, 6:43pm UTC](https://forum.getodk.org/t/calculate-distance-between-geopoints/19846/11 "2024-07-22T18:43:19Z")

</div>

Split this topic on July 22, 2024, 6:43pm UTC

A post was split to a new topic: [Distance between geopoint from csv and newly collected geo point](https://forum.getodk.org/t/distance-between-geopoint-from-csv-and-newly-collected-geo-point/48277)
