皆さん、こんにちは。
選択した地点(投票所)から特定の地点(小さな町)までの距離をマイル単位でポップアップに表示する式を書こうとしています。距離の式を何度か試してみましたが、うまく書けないようです。 Arcadeは初めてなので、何が足りないのか理解できていないのかもしれません。
var Geometry="住所_1"var Geometry="市"
Distance("住所_1", "市", 'マイル')
Hi Marvin Lopez ,
There is no function in Arcade that will get you the nearest feature in another layer. This does not mean that it is not possible. Let's assume you have two point layers, one holding the towns and the other the polling stations. When the user clicks on a town you will have access to the geometry. The town does not necessarily have to be a point, it could also be a polygon. If you have a huge amount of polling stations you will want to limit the search to a certain distance. This could be your search tolerance. The Arcade expression will take the town geometry (being a point or a polygon) and buffer it with the search tolerance. The resulting buffer polygon will be used to intersect the polling stations layer to obtain only the polling stations within the search distance. Next step would be to loop through each polling station and determine the distance and remember the minimum distance. In the resulting text that you can present to the end user you can include the name of the nearest polling station and the "as the crow flies" distance between the two. Remember, this will not be the actual distance someone will have to travel to get there.
See the expression below how to do this:
<SPAN class="comment token">// specify a search distance using the map units</SPAN> <SPAN class="keyword token">var</SPAN> searchtol <SPAN class="operator token">=</SPAN> <SPAN class="number token">10000</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// the town clicked on by the user</SPAN> <SPAN class="keyword token">var</SPAN> town <SPAN class="operator token">=</SPAN> $feature<SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// buffer the town with search distance and specify the units</SPAN> <SPAN class="keyword token">var</SPAN> searchbuffer <SPAN class="operator token">=</SPAN> <SPAN class="token function">Buffer</SPAN><SPAN class="punctuation token">(</SPAN>town<SPAN class="punctuation token">,</SPAN> searchtol<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'meters'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// access the poling stations layer (change according to your data)</SPAN> <SPAN class="keyword token">var</SPAN> pollingstations <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">'SpecifyTheNameOfThePollingStationsLayerInTheMap'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'FieldnamePolingStation'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// Find the nearpolling stations that are within the search distance</SPAN> <SPAN class="keyword token">var</SPAN> nearstations <SPAN class="operator token">=</SPAN> <SPAN class="token function">Intersects</SPAN><SPAN class="punctuation token">(</SPAN>pollingstations<SPAN class="punctuation token">,</SPAN> searchbuffer<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// count polling stations in buffer</SPAN> <SPAN class="keyword token">var</SPAN> cnt <SPAN class="operator token">=</SPAN> <SPAN class="token function">Count</SPAN><SPAN class="punctuation token">(</SPAN>nearstations<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// check if there are any stations</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="comment token">// there are polling stations</SPAN> <SPAN class="comment token">// initialize minimum distance, and polling station name</SPAN> <SPAN class="keyword token">var</SPAN> min_dist <SPAN class="operator token">=</SPAN> searchtol <SPAN class="operator token">+</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">var</SPAN> polstatname <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">null</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// loop through polling stations</SPAN> <SPAN class="keyword token">for</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">var</SPAN> polstat <SPAN class="keyword token">in</SPAN> nearstations<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="comment token">// calculate distance between town and polling station</SPAN> <SPAN class="keyword token">var</SPAN> dist <SPAN class="operator token">=</SPAN> <SPAN class="token function">Distance</SPAN><SPAN class="punctuation token">(</SPAN>polstat<SPAN class="punctuation token">,</SPAN> town<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'meters'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// if nearer than prevous nearest</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> <SPAN class="comment token">// update nearest</SPAN> min_dist <SPAN class="operator token">=</SPAN> dist<SPAN class="punctuation token">;</SPAN> polstatname <SPAN class="operator token">=</SPAN> polstat<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'FieldnamePolingStation'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="comment token">// return polling station name and distance to it</SPAN> <SPAN class="keyword token">return</SPAN> polstatname <SPAN class="operator token">+</SPAN> <SPAN class="string token">" ("</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="token function">Round</SPAN><SPAN class="punctuation token">(</SPAN>min_dist<SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"m.)"</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="keyword token">else</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="comment token">// there are NO polling stations (change units)</SPAN> <SPAN class="keyword token">return</SPAN> <SPAN class="string token">"No polling stations within "</SPAN> <SPAN class="operator token">+</SPAN> searchtol <SPAN class="operator token">+</SPAN> <SPAN class="string token">" m."</SPAN><SPAN class="punctuation token">;</SPAN> <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></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>
サインインすると、投稿したり、更新をフォローしたりできます。初めての方ですか?無料アカウントを登録しましょう。
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.