Select to view content in your preferred language

Updating a GeoJSON layer that uses UniqueValueRenderer

425
3
Jump to solution
11-15-2023 12:26 PM
EdMayer
Emerging Contributor

Using version 4.28 of the maps SDK and looking for advice on best practice to update a GeoJSON layer that uses uniqueValueRenderer to create a custom simpleMarkerSymbol. Without providing the specific code, the program flow is as follows:

- load webmap
- send request for GeoJSON
- receive GeoJSON
- create content blob
- create url reference to blob
- create GeoJSON layer
- use query.queryFeatures() to fetch outfields
- create a uniqueValueRenderer instance
- iterate Features:
    - create a new simpleMarkerSymbol which is dependent on the Feature data
    - addUniqueValueInfo to the renderer
- add the renderer to the layer
- add the layer to the map

At this point the layer is created and shows on the map as expected. The problem I'm having is, how to apply the changes to the existing layer when a new request/response is generated/received? Currently I'm removing the layer and adding it back, but that causes the layer to flash when replaced. Any recommended approaches for this scenario?

I've experimented with applyEdits-updateFeatures-Collection but haven't been successful in getting the layer to update.

Additionally, I feel like I'm using the uniqueValueRenderer as a workaround, and there may be a better solution to accomplish the goal of creating data-driven marker symbols that depend on the feature content (including an SVG path) which is contained in the feature data. Any comments are appreciated.

-Ed

1 Solution

Accepted Solutions
EdMayer
Emerging Contributor

Update:

I've learned a bit since my post above and have found an acceptable solution. Usign applyEdits is the way to go, but I had to make some changes. I'm no longer using Query:

Here's the solution that appears to be working:

- load webmap
- send request for GeoJSON
- receive GeoJSON

on first data:
- create content blob
- create url reference to blob
- create GeoJSON layer
- layer.when():
    - create a uniqueValueRenderer instance
    - iterate Features (of GeoJSON)
        - create a new simpleMarkerSymbol which is dependent on the Feature data
        - addUniqueValueInfo to the renderer
        - add renderer to the layer
- add the layer to the map

on new data / refresh:
- create a new uniqueValueRenderer instance, graphics array
- iterate Features (of GeoJSON):
    - create a new simpleMarkerSymbol which is dependent on the Feature data
    - addUniqueValueInfo to the renderer
    - add new Graphic to graphics array
- add renderer to the layer
- layer.applyEdits({updateFeatures:graphics});

This sample was helpful in getting applyEdits to work:
https://developers.arcgis.com/javascript/latest/sample-code/sandbox/?sample=layers-featurelayer-coll... 

View solution in original post

0 Kudos
3 Replies
KristianEkenes
Esri Regular Contributor

I'm not sure I follow the scenario 100%, but it sounds like you could just use the smart mapping functions for this: https://developers.arcgis.com/javascript/latest/api-reference/esri-smartMapping-renderers-type.html#...

The smart mapping functions generate renderers based on the underlying data. The createRenderer function for unique values works for all "feature layer-like" types, including GeoJSON. Here's an example: https://codepen.io/kekenes/pen/wvNPGXJ?editors=100 

0 Kudos
EdMayer
Emerging Contributor

Thanks Kristian.  I had been looking at the smartMapping renderers just prior to posting.  It does appear to offer similar capability to what I'm doing with using the UniqueValueRenderer.  I hadn't seen that codepen, so thanks for that link also.

0 Kudos
EdMayer
Emerging Contributor

Update:

I've learned a bit since my post above and have found an acceptable solution. Usign applyEdits is the way to go, but I had to make some changes. I'm no longer using Query:

Here's the solution that appears to be working:

- load webmap
- send request for GeoJSON
- receive GeoJSON

on first data:
- create content blob
- create url reference to blob
- create GeoJSON layer
- layer.when():
    - create a uniqueValueRenderer instance
    - iterate Features (of GeoJSON)
        - create a new simpleMarkerSymbol which is dependent on the Feature data
        - addUniqueValueInfo to the renderer
        - add renderer to the layer
- add the layer to the map

on new data / refresh:
- create a new uniqueValueRenderer instance, graphics array
- iterate Features (of GeoJSON):
    - create a new simpleMarkerSymbol which is dependent on the Feature data
    - addUniqueValueInfo to the renderer
    - add new Graphic to graphics array
- add renderer to the layer
- layer.applyEdits({updateFeatures:graphics});

This sample was helpful in getting applyEdits to work:
https://developers.arcgis.com/javascript/latest/sample-code/sandbox/?sample=layers-featurelayer-coll... 

0 Kudos