Using Arcade to color code a polygon layer based on attribute values

4280
8
Jump to solution
10-22-2018 01:48 AM
HugoBouckaert1
New Contributor III

Hi 

In a map I use a "Custom Expression" to change the rendering style of a polygon layer, and use Arcade to make a custom expression. In Arcade, I combine all possible combinations of two fields. One field is named PIP and the other BIOSECURITY. Both use the same domain, namely a simple YES/NO choice where the code for "NO" = 0 and the code for YES = 1. However both fields can also be empty.

The logic is that I use decode to change the values of PIP differently from those of BIOSECURITY so they can never be the same. Once that is done, I add them up. Because the values are always different, each addition should be unique. I use a WHEN statement in which I add up the unique combinations of the values in the two fields. 

WHEN

(Decode($feature.PIP, 0, 2, 1, 4, 5) + Decode($feature.BIOSECURITY, 0, 22, 1, 32, 55) == 60,'Unique Color Code',

Decode($feature.PIP, 0, 2, 1, 4, 5) + Decode($feature.BIOSECURITY, 0, 22, 1, 32, 55) == 59, 'Another Unique Color Code',  'Default Value ')

But where I write 'Unique Color Code',  and ''Another Unique Color Code' I would actually like to instruct Arcade to render those layers with a particular color, e.g. green or red.  

I found some examples, but I think is for when you embed Arcade in JavaScript  

Decode($feature.PIP, 0, 2, 1, 4, 5) + Decode($feature.BIOSECURITY, 0, 22, 1, 32, 55) == 60, 'background-color:ff984f'‌

I also tried:  

Decode($feature.PIP, 0, 2, 1, 4, 5) + Decode($feature.BIOSECURITY, 0, 22, 1, 32, 55) == 60, '#ff984f'

Is there any way in Arcade to directly assign colors to a polygon layer, based on a WHEN statement, as in the code snippet above?

Thanks

Hugo 

 

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
HugoBouckaert1
New Contributor III

Thanks for your help, your code is much better (and elegant) than mine.

I am now formatting the colour schemes manually, seeing I have clear confirmation this cannot be done in Arcade. 

I have looked into using JSON for defining drawinginfo for webmaps before. So this is what I am doing now also for the new results from the categories resulting from the arcade code.   First I use  the arcgis.gis API to search for the webmap in question, and get the mapitem. Then I do this:  

mapitem.get_data().

The resultant JSON output is then put in a JSON formatter, after which I use everything that belongs to drawingInfo into a python progam as a variable. After that I search for layers in the other webmap, and copy the new JSON variable over by doing this:     

layer ['layerDefinition'] = mysavedJSON

Thanks again for your help. 

Cheers

Hugo 

View solution in original post

8 Replies
CarmelConnolly
Esri Regular Contributor

Hi Hugo, 

From the sounds of it, you're using the ArcGIS Online map viewer to render your layer's symbology?

When you've configured arcade expression, you can define the colours through 'Select a drawing style' instead of the expression yourself: 

--> 

If you've already tried this, did this not meet your needs?

Carmel

0 Kudos
HugoBouckaert1
New Contributor III

Hi Carmel 

Thanks for your reply. Yes this is what I have done i.e. I defined the colour manually, as you point out in your email. The issue is that I need to create 24 maps that look exactly the same, but have different content (different areas). I was hoping to set all this up with a script so I could just use the same Arcade script in all 24 maps. 

One question I have (and this may sound a little stupid): in the blogs there is continuous reference to JavaScript and embedding Arcade in JavaScript. So can you use JavaScript with (if needed) embedded Arcade in a custom expression, either for rendering a map in AGOL or customising pop-ups in AGOL? I thought the expression fields only take Arcade, but perhaps they also take JavaScript with Arcade in it? 

If this is the case I suppose it might be possible to define the colors programmatically? 

Thanks

Hugo     

0 Kudos
XanderBakker
Esri Esteemed Contributor

Arcade does not be allow you to define the fill colors of your polygons. The expression will only return in this case a text  that you can use to define the symbology for your polygons using standard smart mapping. You can define colors in pop-up windows or labels, using the expressions, but not for symbology. 

Arcade is based on JavaScript (it is an extract from JavaScript with some additional functions). An expression defined for one layer will not be available for the other layer and even when you copy the expression from another layer, you will still have to modify the symbology for each item.

You can however create the expression and symbology for a single layer and use ArcGIS Online Assistant  to apply it to the rest of the layers editing the json of the web map (this is a bit tricky!). Example expression:

var bio = $feature['BIOSECURITY'];
var pip = $feature['PIP'];

var result = "";
If (IsEmpty(pip)) {
    Console("PIP=?");
} else if (pip == 0) {
    result += "PIP:NO";
} else {
    result += "PIP:YES";
}

If (IsEmpty(bio)) {
    Console("BIO:?");
} else if (bio == 0) {
    result += "  BIO:NO";
} else {
    result += "  BIO:YES";
}

result = Trim(result);
if (result=="") {
    result = "No Data";
}

return result;

This will yield the following options, here shown in the legend with a color applied to each item:

In AGO assistant you will have to copy the entire layerDefinition section of the layer and insert it in the definition of the other layers. Be aware of creating an invalid json and do this preferably on a copy of the web map.

HugoBouckaert1
New Contributor III

Thanks for your help, your code is much better (and elegant) than mine.

I am now formatting the colour schemes manually, seeing I have clear confirmation this cannot be done in Arcade. 

I have looked into using JSON for defining drawinginfo for webmaps before. So this is what I am doing now also for the new results from the categories resulting from the arcade code.   First I use  the arcgis.gis API to search for the webmap in question, and get the mapitem. Then I do this:  

mapitem.get_data().

The resultant JSON output is then put in a JSON formatter, after which I use everything that belongs to drawingInfo into a python progam as a variable. After that I search for layers in the other webmap, and copy the new JSON variable over by doing this:     

layer ['layerDefinition'] = mysavedJSON

Thanks again for your help. 

Cheers

Hugo 

XanderBakker
Esri Esteemed Contributor

Sounds good! Glad you came up with a way to avoid doing all this manually. 

0 Kudos
RobertBorchert
Frequent Contributor III

Maybe you can help me in a different direction.

I have RGB values in three separate columns.  I could put them into a single column, not an issue.


I would like to designate the polygon fill based on the RGB value.  I am doing this in Pro. 

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi rborchert , 

As far as I understand that should be possible: Attribute-driven color in symbology—ArcGIS Pro | Documentation 

0 Kudos
RobertBorchert
Frequent Contributor III

Thanks I will dig into that

0 Kudos