I was wondering if it was possible to calculate the distance from one point feature to the nearest point (a separate feature service), and having this distance as a popup, using Arcade?
TIA
Hi Josh Harris ,
Here is an example expression that you can use:
<SPAN class="keyword token">var</SPAN> fs <SPAN class="operator token">=</SPAN> <SPAN class="token function">FeatureSetByName</SPAN><SPAN class="punctuation token">(</SPAN>$map<SPAN class="punctuation token">,</SPAN><SPAN class="string token">"The Name of the other point feature service"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">var</SPAN> buf <SPAN class="operator token">=</SPAN> <SPAN class="token function">Buffer</SPAN><SPAN class="punctuation token">(</SPAN>$feature<SPAN class="punctuation token">,</SPAN> <SPAN class="number token">500</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"meter"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">var</SPAN> fs_filtered <SPAN class="operator token">=</SPAN> <SPAN class="token function">Intersects</SPAN><SPAN class="punctuation token">(</SPAN>fs<SPAN class="punctuation token">,</SPAN> buf<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">var</SPAN> cnt <SPAN class="operator token">=</SPAN> <SPAN class="token function">Count</SPAN><SPAN class="punctuation token">(</SPAN>fs_filtered<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">var</SPAN> min_dist <SPAN class="operator token">=</SPAN> <SPAN class="number token">999</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>cnt <SPAN class="operator token">></SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">for</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">var</SPAN> f <SPAN class="keyword token">in</SPAN> fs_filtered<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">var</SPAN> dist <SPAN class="operator token">=</SPAN> <SPAN class="token function">Distance</SPAN><SPAN class="punctuation token">(</SPAN>$feature<SPAN class="punctuation token">,</SPAN> f<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>dist <SPAN class="operator token"><</SPAN> min_dist<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> min_dist <SPAN class="operator token">=</SPAN> dist<SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// if you want to know which feature is nearest, </SPAN> <SPAN class="comment token">// you should extract that here</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="keyword token">return</SPAN> min_dist<SPAN class="punctuation token">;</SPAN> <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
The short answer is yes you can. However, you will need to consider performance and be smart on determining the nearest point. Als the other service should be in the same map or in the same datastore to have access.
You can access the other service and start processing each feature one by one to determine the nearest point. If the service has a lot of points, this will come with a cost (performance wise). You can reduce the number of features to process if you have some idea about the maximum distance in which you are sure to find a point. This would allow you to create buffer around the input feature, and use the Intersects function to query the other layer and after that loop through the subset of feature to get to the nearest.
So to provide you with some more advise, it would be necessary to share how many feature you have in the other feature service and if there is a distance you can define to avoid processing a large quantity of features.
Hi Xander Bakker,
Thanks for the prompt response. That's great. So the scenario is that I have a webmap with two hosted feature services (Point 1, Point 2). Both feature services are being updated on a daily basis, and will hold at maximum ~30,000 records. I am only interested in distances <= 500m. Initially I thought the Distance or DistanceGeodetic would give me what I was after, but I kept getting "Illegal Argument" error. Am I on the right path with this?
Spot on Xander Bakker, that works perfectly thank you
Hi @XanderBakker
I'm digging this old post up. I'm looking to retrieve a specific attribute from the nearest feature (e.g I want to grab the asset ID from the nearest feature. I've tried tweaking the above expression, but it doesn't seem to be reliable in what it's retrieving. Any help would be greatly appreciated.
var fs = FeatureSetByPortalItem(Portal('https://www.arcgis.com/'), '[item_id]', 0); var buf = Buffer($feature, 500, "meter"); var fs_filtered = Intersects(fs, buf); var cnt = Count(fs_filtered); var min_dist = 999; if (cnt > 0) { for (var f in fs_filtered) { var dist = DistanceGeodetic($feature, f); if (dist < min_dist) { min_dist = dist; } } } return f.[field_name];
@XanderBakker Hello Is there anyway I can filter the latest point I created based on the date of creation field and use that point tocalculate the distance to the new point?Basically I need to calculate the distance between the previous point and the newly created point each time.
Hello @XanderBakker
I am wondering if we can calculate distance of two points from the mid point and auto populates it using attribute rule.
I am having feature class which has electric poles and I want to calculate distance between each poles and calculates it in second feature class.
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.