# DBSCAN clustering with Haversine metric

**URL:** https://discourse.julialang.org/t/dbscan-clustering-with-haversine-metric/121685
**Category:** Geo
**Tags:** clustering
**Created:** [October 24, 2024, 8:57am UTC](https://discourse.julialang.org/t/dbscan-clustering-with-haversine-metric/121685 "2024-10-24T08:57:49Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![miguelborrero](https://avatars.discourse-cdn.com/v4/letter/m/eb9ed0/32.png) [@miguelborrero](https://discourse.julialang.org/u/miguelborrero)
#### Post date: [October 24, 2024, 8:57am UTC](https://discourse.julialang.org/t/dbscan-clustering-with-haversine-metric/121685/1 "2024-10-24T08:57:50Z")

</div>

Hi there,

I have an array of points (latitude, longitude) in radians and I am trying to use the package `Clustering.jl` to cluster my points. In the documentation it seems like a metric can be given, the default being: `Euclidean()` however I need to use the Haversine metric and I dont seem to find the way of using such metric. Any ideas?

Thanks a lot!

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [October 24, 2024, 9:50am UTC](https://discourse.julialang.org/t/dbscan-clustering-with-haversine-metric/121685/2 "2024-10-24T09:50:33Z")

</div>

Perhaps you can try the clustering API in GeoStats.jl. I believe we have all Clustering.jl models over there too. The latlon will be taken care of for you internally. But it is been a while since the last time we tested DBSCAN

---

<div class="post-metadata">

### Author: ![evetion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evetion/32/22679_2.png) [@evetion](https://discourse.julialang.org/u/evetion)
#### Post date: [October 24, 2024, 11:02am UTC](https://discourse.julialang.org/t/dbscan-clustering-with-haversine-metric/121685/3 "2024-10-24T11:02:09Z")

</div>

Looking at the documentation it seems that only DBScan takes a metric keyword argument:

[DBSCAN · Clustering.jl](https://juliastats.org/Clustering.jl/stable/dbscan.html#Clustering.dbscan).

You should be able to pass `Haversine()` to it instead of `Euclidean()`.

Some other (not all) methods (e.g. hierarchical, k-mediods), take a distance matrix, instead of a data matrix, so you could compute all `pairwise` distances yourself with `Haversine()` first.

---

<div class="post-metadata">

### Author: ![miguelborrero](https://avatars.discourse-cdn.com/v4/letter/m/eb9ed0/32.png) [@miguelborrero](https://discourse.julialang.org/u/miguelborrero)
#### Post date: [October 24, 2024, 11:10am UTC](https://discourse.julialang.org/t/dbscan-clustering-with-haversine-metric/121685/4 "2024-10-24T11:10:58Z")

</div>

I tried and passing `Haversine()` did not work (or other transformations of the same command). Yep, I could do the pairwise distance but as the documentation says, that method results in efficiency losses so I was wondering whether it could be avoided. After all, passing some haversine version of `Euclidean()` seems like it should be there.

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [October 24, 2024, 12:53pm UTC](https://discourse.julialang.org/t/dbscan-clustering-with-haversine-metric/121685/5 "2024-10-24T12:53:46Z")

</div>

Alternative: project the geogs into a Cartesian system and use euclidean distances. And at the end convert back to geogs.
