How to nest Arcade If statements

2815
14
Jump to solution
06-24-2019 08:33 PM
NaomiBegg2
Occasional Contributor III

I am working through a loop of intersecting layers, in which I want to choose the largest value.

I have tried to use create a list of numbers using += .  This worked fine when I was using a combo of text and numbers, but because I am now just looking at numbers it sums the values, == replaces the values.  I could not see how to make a list which I could then use the MAX function with.

My next attempt was to nest an if statement so that I am only getting the largest number out but I have an illegal return statement.

Below is an example of what I am currently doing.

var intersectLayer = Intersects(FeatureSetByName($map, "layer"), $feature)

var Zones = "";

var Zones1 = "";

for (var f in intersectLayer){

   if(Zones == ""){

      Zones = f.Shape__Area

   } else {

   Zones1 = f.Shape__Area

      if (Zones < Zones1){

      Zones == Zones1

      }else{

      Zones1 == ""

      }

}

}

Xander Bakker‌ Is this something you can help me with?  I'm expecting there to be a much easier way then I have thought of yet.

Tags (1)
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Hi Naomi Begg ,

According to what I see in your script you intersect with a single layer (is it really called "Layer" in your map?) and you loop through the intersected feature to determine the largest area (this is the largest area of the polygon, not the largest area of the overlap with the intersecting polygon). If that is the case, I assume you could do something like this:

var intersectLayer = Intersects(FeatureSetByName($map, "layer"), $feature);
var max_area = 0;

for (var f in intersectLayer) {
    if (f.Shape__Area > max_area) {
        max_area = f.Shape__Area;
   }
}

return max_area;

View solution in original post

14 Replies
XanderBakker
Esri Esteemed Contributor

Hi Naomi Begg ,

According to what I see in your script you intersect with a single layer (is it really called "Layer" in your map?) and you loop through the intersected feature to determine the largest area (this is the largest area of the polygon, not the largest area of the overlap with the intersecting polygon). If that is the case, I assume you could do something like this:

var intersectLayer = Intersects(FeatureSetByName($map, "layer"), $feature);
var max_area = 0;

for (var f in intersectLayer) {
    if (f.Shape__Area > max_area) {
        max_area = f.Shape__Area;
   }
}

return max_area;
NaomiBegg2
Occasional Contributor III

Thanks Xander

After spending most of a day looking at it I had definitely over complicated the solution!

"Layer" is actually called NZParcels_Enviornment_Identity4  A horrible name after a handful of attempts of figuring out the best way to look at our enviornmens layer in the context of parcels.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Naomi Begg ,

If you layer is really called "NZParcels_Enviornment_Identity4", it should be used in the Arcade expression. In that case line 1 would become:

var intersectLayer = Intersects(FeatureSetByName($map, "NZParcels_Enviornment_Identity4"), $feature);
0 Kudos
NaomiBegg2
Occasional Contributor III

I just wish that I could now use this as a query to symbolise a layer.  It is frustrating that the attribute expressions are viewable for popup information but you have to build it from scratch if you want to use that as a symbol field - plus it doesn't seem to allow for the intersect with another layer (featuresetbyname is not available).

As a side question, why does Arcade in popups not work when viewing in IE browsers?

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Naomi Begg ,

On one hand I totally agree. I would be very interesting to be able to use these functions in the symbology profile. On the other hand, I can imagine that when a complex expression has to be executed for each feature that is being drawn every time you zoom or pan your map, this would seriously affect the rendering time. 

I am not a fan of using IE. I prefer Chrome or Firefox or if nothing else is available, Edge. This might well be due to limitations of IE as a browser. In case you can, you should move away from IE, since it is the worst browser I have worked with.

0 Kudos
NaomiBegg2
Occasional Contributor III

Xander Bakkerhow do I populate a field with this query?  I am wanting to symbolise using it, but I also want to be able to view it in a table in Pro.  I've tried to build the query in the symbol query builder, and I have tried to use it in the Calculate Field tool in Pro.  Both seem to be upset with the use of $map,"layer" within the intersect query.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi njbegg ,

Strange, this should work in Pro in the Field Calculation too. I will do a simple test when I have a moment and post back my findings.

0 Kudos
XanderBakker
Esri Esteemed Contributor

HI Naomi Begg ,

So you are right the $map is not available in the field calculation.

However, $datastore is. Have a look below. 

Error when using $map in the field calculation:

But, using $datastore it will work (if your data reside in the same datastore😞

0 Kudos
NaomiBegg2
Occasional Contributor III

Hi Xander Bakker‌ sorry me again with another question.

Again I am trying to use arcade within the field calculator in Pro to do something that I can do within AGOL as a popup expression.

I'm using the following in AGOL:

var roads = FeatureSetByName($map,'TaupoProperty - Road')
var closestRoads = Intersects(roads,Buffer($feature, 100, "meters"))
var minDistance = Infinity
var closestRoad;
for (var l in closestRoads){
var roadDistance = Distance(l, $feature, 'meters')
if (roadDistance < minDistance){
minDistance = roadDistance
closestroad = l
}
}
closestroad.name

I change the $map to $datastore as figured in the last question.

However I am getting table not found when sing the $datastore.  I try and change the script in AGOL to $datastore but I also get an error there.  When you expand the $datastore within the right pane of the expression builder the layers I am looking for do not show. 

Is this maybe because the layer I am looking for is actually a ArcGIS Server layer rather than hosted on AGOL?

How do I get around this?

*I am booked on a Python/Arcade course in a few weeks so hopefully I wont have so many questions afterwards yet will know how to do a tonne more super cool stuff.*

0 Kudos