<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Find the Distance Between Two or More Geolocation Coordinates in Reporting</title>
    <link>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/57658#M10291</link>
    <description>&lt;P&gt;No, I don't see why you'd need to do the distance calculation &lt;EM&gt;within&lt;/EM&gt; the stats clause.  That would be a little crazy.   Do it before and use some form of &lt;CODE&gt;last(distance) as distance by username&lt;/CODE&gt;,   or &lt;CODE&gt;by username distance&lt;/CODE&gt; in your stats, and then filter afterwards.    Or use some form of &lt;CODE&gt;last(src_ip_latitude) as src_ip_latitude last(src_ip_longitude) as src_ip_longitude&lt;/CODE&gt; in stats and then do the distance calculation after.&lt;/P&gt;</description>
    <pubDate>Thu, 13 Jun 2013 23:17:51 GMT</pubDate>
    <dc:creator>sideview</dc:creator>
    <dc:date>2013-06-13T23:17:51Z</dc:date>
    <item>
      <title>Find the Distance Between Two or More Geolocation Coordinates</title>
      <link>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/57653#M10286</link>
      <description>&lt;P&gt;I am trying to find the distance between two or more IP geolocations without the use of an external script (not an admin). Here is my base search:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;tag=login | geoip src_ip | stats distinct_count(src_ip_country_name) AS count_country, values(src_ip_country_name) AS country by username | where count_country &amp;gt; 1
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I know I can find the difference in the latitude and longitude fields. Something like the following:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sqrt(pow(src_ip_latidude1-src_ip_latidude2,2)+pow(src_ip_longitude1-src_ip_logitude2,2))
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But how do I incorporate that into my base search? Would I be able to build a table with the geolocations and the distance &lt;STRONG&gt;grouped by username&lt;/STRONG&gt;?&lt;/P&gt;

&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jun 2013 23:50:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/57653#M10286</guid>
      <dc:creator>martinaire</dc:creator>
      <dc:date>2013-06-06T23:50:53Z</dc:date>
    </item>
    <item>
      <title>Re: Find the Distance Between Two or More Geolocation Coordinates</title>
      <link>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/57654#M10287</link>
      <description>&lt;P&gt;The pythagorean theorem is a good approximation only for shorter distances.   If you're actually dealing with pretty big distances you have to break out some trig functions and calculate great circle distance.  &lt;A href="http://en.wikipedia.org/wiki/Great-circle_distance"&gt;http://en.wikipedia.org/wiki/Great-circle_distance&lt;/A&gt; &lt;/P&gt;

&lt;P&gt;And since eval can't do trig functions ( see &lt;A href="http://splunk-base.splunk.com/answers/26399/can-eval-evaluate-cosines"&gt;http://splunk-base.splunk.com/answers/26399/can-eval-evaluate-cosines&lt;/A&gt; ) that would lead you back to a custom search command again. &lt;/P&gt;

&lt;P&gt;However, if your distances are all short enough,  then what you propose just needs to be plugged into eval. &lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;| eval distance=sqrt(pow(src_ip_latidude1-src_ip_latidude2,2)+pow(src_ip_longitude1-src_ip_logitude2,2))&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Once that eval clause gives you that field called distance on your rows, you can do whatever you want with it. &lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2013 06:57:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/57654#M10287</guid>
      <dc:creator>sideview</dc:creator>
      <dc:date>2013-06-11T06:57:26Z</dc:date>
    </item>
    <item>
      <title>Re: Find the Distance Between Two or More Geolocation Coordinates</title>
      <link>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/57655#M10288</link>
      <description>&lt;P&gt;I completely forgot about the fact that that the Earth is round. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Too bad I can't use the great-circle formula.&lt;/P&gt;

&lt;P&gt;How can I pull out the latitude and longitude field by username and plug it into the eval? In other words, how can I incorporate the eval into the base search?&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2013 14:47:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/57655#M10288</guid>
      <dc:creator>martinaire</dc:creator>
      <dc:date>2013-06-11T14:47:13Z</dc:date>
    </item>
    <item>
      <title>Re: Find the Distance Between Two or More Geolocation Coordinates</title>
      <link>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/57656#M10289</link>
      <description>&lt;P&gt;Assuming you have those other four fields in your events,   just tack the &lt;CODE&gt;| eval&lt;/CODE&gt; onto the end of the search.   Just by that eval will add an additional field to all rows called "distance".   Again you have to have all four of those fields by those exact case sensitive names,  on all events.  More generally on all incoming rows, whether they're events or whether they've already been transformed or altered by other search language commands.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2013 17:11:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/57656#M10289</guid>
      <dc:creator>sideview</dc:creator>
      <dc:date>2013-06-11T17:11:34Z</dc:date>
    </item>
    <item>
      <title>Re: Find the Distance Between Two or More Geolocation Coordinates</title>
      <link>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/57657#M10290</link>
      <description>&lt;P&gt;I think my question is a little more complex than I initially thought. My current base search only has the src_ip_latitude and src_ip_longitude fields. I want break it up (e.g. latitude1, latitude2, etc.) grouped by the username. I'm thinking I would need alter the end of my search to something like "where (count_country &amp;gt; 1) AND (distance &amp;gt; 100)". That means I likely need to do the distance calculation it within my stats clause. Because after my stats clause, I no longer have access to the latitude and longitude fields.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 14:05:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/57657#M10290</guid>
      <dc:creator>martinaire</dc:creator>
      <dc:date>2020-09-28T14:05:30Z</dc:date>
    </item>
    <item>
      <title>Re: Find the Distance Between Two or More Geolocation Coordinates</title>
      <link>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/57658#M10291</link>
      <description>&lt;P&gt;No, I don't see why you'd need to do the distance calculation &lt;EM&gt;within&lt;/EM&gt; the stats clause.  That would be a little crazy.   Do it before and use some form of &lt;CODE&gt;last(distance) as distance by username&lt;/CODE&gt;,   or &lt;CODE&gt;by username distance&lt;/CODE&gt; in your stats, and then filter afterwards.    Or use some form of &lt;CODE&gt;last(src_ip_latitude) as src_ip_latitude last(src_ip_longitude) as src_ip_longitude&lt;/CODE&gt; in stats and then do the distance calculation after.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2013 23:17:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/57658#M10291</guid>
      <dc:creator>sideview</dc:creator>
      <dc:date>2013-06-13T23:17:51Z</dc:date>
    </item>
    <item>
      <title>Re: Find the Distance Between Two or More Geolocation Coordinates</title>
      <link>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/57659#M10292</link>
      <description>&lt;P&gt;I'm working on a similar query and I much appreciate what you've both done here.  I've worked up this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| lookup geoip clientip |dedup userID, client_city| eval location=clientip."- ".client_city.", ".client_region.", ".client_country| stats last(client_lat) as Lat1, last(client_lon) as Lon1, first(client_lat) as Lat2, first(client_lon) as Lon2, values(location) dc(client_city) as distinctCount by userID| where distinctCount = 2 | eval distance=sqrt(pow(Lat1-Lat2,2)+pow(Lon1-Lon2,2))|sort distance desc
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I've gotten it to work when a user has had 2 different IPs.  using first &amp;amp; last precludes more though.  Still trying to work on that.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Aug 2013 20:13:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/57659#M10292</guid>
      <dc:creator>rgonzale6</dc:creator>
      <dc:date>2013-08-22T20:13:27Z</dc:date>
    </item>
    <item>
      <title>Re: Find the Distance Between Two or More Geolocation Coordinates</title>
      <link>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/57660#M10293</link>
      <description>&lt;P&gt;There is a &lt;A href="http://apps.splunk.com/app/936"&gt;Haversine add-on on Splunkbase&lt;/A&gt; that should do the trick for you.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2013 07:17:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/57660#M10293</guid>
      <dc:creator>Damien_Dallimor</dc:creator>
      <dc:date>2013-08-23T07:17:57Z</dc:date>
    </item>
    <item>
      <title>Re: Find the Distance Between Two or More Geolocation Coordinates</title>
      <link>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/57661#M10294</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;

&lt;P&gt;fast forward into the future, we can do the &lt;EM&gt;great circle formula&lt;/EM&gt; in Splunk now. &lt;BR /&gt;
This example will provide the expected result:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval lat1=1, lon1=1, lat2=2, lon2=2 
| eval rlat1 = pi()*lat1/180, rlat2=pi()*lat2/180, rlat = pi()*(lat2-lat1)/180, rlon= pi()*(lon2-lon1)/180
| eval a = sin(rlat/2) * sin(rlat/2) + cos(rlat1) * cos(rlat2) * sin(rlon/2) * sin(rlon/2) 
| eval c = 2 * atan2(sqrt(a), sqrt(1-a)) 
| eval distance = 6371 * c
| table lat1 lon1 lat2 lon2 distance
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;CODE&gt;distance&lt;/CODE&gt; will be the distance in &lt;CODE&gt;km&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;Hope this helps ...&lt;/P&gt;

&lt;P&gt;cheers, MuS&lt;/P&gt;</description>
      <pubDate>Mon, 04 Sep 2017 21:14:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/57661#M10294</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2017-09-04T21:14:30Z</dc:date>
    </item>
    <item>
      <title>Re: Find the Distance Between Two or More Geolocation Coordinates</title>
      <link>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/57662#M10295</link>
      <description>&lt;P&gt;&lt;A href="https://splunkbase.splunk.com/apps/#/search/haversine/"&gt;https://splunkbase.splunk.com/apps/#/search/haversine/&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Mar 2019 19:20:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/57662#M10295</guid>
      <dc:creator>aworkman</dc:creator>
      <dc:date>2019-03-27T19:20:14Z</dc:date>
    </item>
    <item>
      <title>Re: Find the Distance Between Two or More Geolocation Coordinates</title>
      <link>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/531126#M10296</link>
      <description>&lt;P&gt;The three macros below calculate the haversine formula that&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/2012"&gt;@MuS&lt;/a&gt;&amp;nbsp;provided.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;[haversine(5)]
# Calculate the great circle distance for a sphere with an arbitrary radius
args = input_lat1, input_lon1, input_lat2, input_lon2, hav_radius
definition = "eval hav_lat1_radians = pi()*$input_lat1$/180, hav_lat2_radians=pi()*$input_lat2$/180, hav_delta_lat_radians = pi()* ($input_lat2$-$input_lat1$)/180, hav_delta_lon_radians= pi()*($input_lon2$-$input_lon1$)/180 | eval hav_intermediate = pow(sin(hav_delta_lat_radians/2), 2) + cos(hav_lat1_radians) * cos(hav_lat2_radians) * pow(sin(hav_delta_lon_radians/2), 2) | eval hav_distance = 2 * $hav_radius$ * atan2(sqrt(hav_intermediate), sqrt(1-hav_intermediate)) | fields - hav_*_radians, hav_intermediate "

[haversine(4)]
# Calculate the great circle distance for the earth (in kilometers)
args = input_lat1, input_lon1, input_lat2, input_lon2
definition = "`haversine($input_lat1$, $input_lon1$, $input_lat2$, $input_lon2$, 6371)` "

[haversine(2)]
# Calculate the great circle distance between two IPs (in kilometers)
args = input_ip1, input_ip2
definition = "iplocation $input_ip1$ prefix=$input_ip1$_ | iplocation $input_ip2$ prefix=$input_ip2$_ | `haversine($input_ip1$_lat, $input_ip1$_lon, $input_ip2$_lat, $input_ip2$_lon)` "&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;Using streamstats, you can calculate IP location distances between events. With eventstats, you can calculate IP location distances between a common IP location and an events IP location.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The calculated value is returned as &lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;hav_distance&lt;/STRONG&gt;&lt;/FONT&gt;, to decrease the chances of a field name collision.&lt;/P&gt;&lt;P&gt;The haversine formula is not as accurate as&amp;nbsp;&lt;A href="https://en.wikipedia.org/wiki/Vincenty%27s_formulae" target="_blank" rel="noopener"&gt;Vincenty's formulae&lt;/A&gt;, but is much more accurate than a simple chord length calculation.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults 
| eval usual_src_ip="8.8.8.8", src_ip="9.9.9.9"
| `haversine(usual_src_ip, src_ip)`
| where hav_distance &amp;gt; 500 &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Nov 2020 10:29:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/531126#M10296</guid>
      <dc:creator>malvidin</dc:creator>
      <dc:date>2020-11-27T10:29:15Z</dc:date>
    </item>
    <item>
      <title>Re: Find the Distance Between Two or More Geolocation Coordinates</title>
      <link>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/549513#M10297</link>
      <description>Just wanted to post a quick thanks for these macros. They came in handy to replace a custom command |distance that was included with the Okta app which did not pass the Cloud app vetting process. Thanks for posting!</description>
      <pubDate>Tue, 27 Apr 2021 18:40:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Find-the-Distance-Between-Two-or-More-Geolocation-Coordinates/m-p/549513#M10297</guid>
      <dc:creator>_smp_</dc:creator>
      <dc:date>2021-04-27T18:40:49Z</dc:date>
    </item>
  </channel>
</rss>

