Arcade-get 4 nearest addresses from point feature

842
2
Jump to solution
05-22-2020 10:25 AM
MRReddy
Occasional Contributor

Arcade, Custom Attribute Expression, popup

I used the below code to get the nearest one location from $feature, but i'm looking now to get nearest 4 records from $feature.

I tried with json object, arrayList, but having syntax errors.

Not sure about the limitations of customization in Arcade

var searchDist = 6000;
var storeAddress = Intersects(FeatureSetById($map, "storeAddressFeaturelayer_2246"), Buffer( $feature, searchDist, "meters"));
var cnt = Count(storeAddress); 
var x=0;
var nearestaddress;
var neardistance=0;
var addressDist=0;

for (var f in storeAddress {
  addressDist = Round(Distance(f, $feature, "meters"),3);
 if(x==0){
     neardistance=addressDist;
     nearestaddress = f.Address+",  "+f.PhoneNumber;
  }
 if(neardistance>addressDist && x==1){
     neardistance=addressDist;
     nearestaddress = f.Address +",  "+f.PhoneNumber;
  }
  x=1;
 }

return nearestaddress;

Any Suggestions

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Hi MR Reddy ,

I just did a test using the data in the Arcade playground:ArcGIS Arcade | ArcGIS for Developers 

See the code below (it is based on the data and some settings for the data in the playground and needs to be adjusted to your data):

var parcel = $feature;
var searchDist = 200;
var storeAddress = Intersects(FeatureSetByName($map,"Parcels_And_Buildings - Tax Parcels"), Buffer( $feature, searchDist, "meters"));
var cnt = Count(storeAddress);
Console(cnt);

var dct = {};
for (var address in storeAddress) {
    var addressDist = Round(Distance(address, $feature, "meters"), 3);
    var key = Text(addressDist, "000.000");
    Console(key);
    //address.Address + ",  " + address.PhoneNumber;
    var val = address.SITEADDRESS + ",  " + address.PARCELID + " (" + addressDist + "m)";
    for (var i = 0; i < 10; i++) {
        if (HasKey(dct, key)) {
            key += Text(i);
        } else {
            // skip;
        }
    }
    dct[key] = val;
}

var result = "";
var mx = Min([cnt, 4]);
var i = 0;
for (var key in dct) {
    if (i < mx) {
        var val = dct[key];
        result += val + TextFormatting.NewLine;
    }
    i += 1;
}

return result;

What is does is:

  • It gets the intersecting features with the buffered parcel
  • it created a dictionary where the key is formed based upon the distance between the feature and the address
  • Then it loops through the dictionary and gets the first four addresses.

View solution in original post

0 Kudos
2 Replies
MRReddy
Occasional Contributor

Xander Bakker‌ Please, Any suggestions here

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi MR Reddy ,

I just did a test using the data in the Arcade playground:ArcGIS Arcade | ArcGIS for Developers 

See the code below (it is based on the data and some settings for the data in the playground and needs to be adjusted to your data):

var parcel = $feature;
var searchDist = 200;
var storeAddress = Intersects(FeatureSetByName($map,"Parcels_And_Buildings - Tax Parcels"), Buffer( $feature, searchDist, "meters"));
var cnt = Count(storeAddress);
Console(cnt);

var dct = {};
for (var address in storeAddress) {
    var addressDist = Round(Distance(address, $feature, "meters"), 3);
    var key = Text(addressDist, "000.000");
    Console(key);
    //address.Address + ",  " + address.PhoneNumber;
    var val = address.SITEADDRESS + ",  " + address.PARCELID + " (" + addressDist + "m)";
    for (var i = 0; i < 10; i++) {
        if (HasKey(dct, key)) {
            key += Text(i);
        } else {
            // skip;
        }
    }
    dct[key] = val;
}

var result = "";
var mx = Min([cnt, 4]);
var i = 0;
for (var key in dct) {
    if (i < mx) {
        var val = dct[key];
        result += val + TextFormatting.NewLine;
    }
    i += 1;
}

return result;

What is does is:

  • It gets the intersecting features with the buffered parcel
  • it created a dictionary where the key is formed based upon the distance between the feature and the address
  • Then it loops through the dictionary and gets the first four addresses.
0 Kudos