applyEdits on GeoJSON from fetch POST response (GeoJSON)

727
3
12-13-2021 08:57 AM
JanHellenberg
New Contributor II

Hi!

I have a GeoJSONlayer which I need to update occasionally with new features from a fetch-request, triggered by the user. Using version 4.19.

---------------
const response = await fetch(
'MyURL' +$.param(params_gj),
{
method: 'POST',
body: JSON.stringify(body),
headers: headers_gj,
})
const response_json = await response.json()

---------------

The fetch response is also a GeoJSON which I can make into a GEOJSONlayer with the following:

-------------
const blob = new Blob([JSON.stringify(response_json)], {type: 'application/json',})
const url = URL.createObjectURL(blob)
var MyNewGeoJSONlayer = new GeoJSONLayer({ url })

------------

However, I do not wish do make a new layer from the fetch response. I want to update my original GeoJSON layer with the features contained in the response. So I use applyEdits according to the following:

----------------
const promise = OriginalLayer.applyEdits({ addFeatures: response_json.features});
promise.then(console.log("Features added") )

----------------

However, this does not work and the javascript console gives no clues. To be honest, I am not sure whether to use response_json, response_json.features or some variation of my "blob". I tried a lot of variations but it seems that the geographic information is lost somehow.

I have a working workaround, which is to create the NewGeoJSONLayer (which I don't want), query all features from that layer (which seems unnecessary since I want all features in my response_json) and then do the applyEdits with featureSet.features.

So, how can I use my response_json with applyEdits on my original layer directly? Thanks in advance for any help!

 

Tags (2)
0 Kudos
3 Replies
RobertScheitlin__GISP
MVP Emeritus

@JanHellenberg 

Have you seen the new enhancement to geojson layer in 4.22? I think this will do what you are looking for.

https://developers.arcgis.com/javascript/latest/release-notes/#layer-updates

 

0 Kudos
UndralBatsukh
Esri Regular Contributor

@JanHellenberg , If you upgrade to 4.22, you will be able to do what you are asking with the use of GeoJSONLayer.customParameters and GeoJSONLayer.refresh. 

The section @RobertScheitlin__GISP referenced in his answer explains the updates we installed at 4.22 for CSV and GeoJSONLayers. The following video explains what this means. 
https://community.esri.com/t5/arcgis-api-for-javascript-blog/what-is-new-at-4-22-for-geojsonlayer-an...

 

This sample shows the updates in action.

0 Kudos
JanHellenberg
New Contributor II

Thanks and sorry for late reply, I had major issues updating from 4.19 to 4.22.

I have two questions: first, does the 4.22 GeoJSONLayer support POST method, which is required by the server in my case? I assumed that only GET is supported?

Secondly and related, in my example above I have parameters, headers and body which I send together with my Fetch POST request. How is the syntax for providing all these in the GeoJSONLayer properties, if possible at all?

I do something like this:

---------------------
var layer = new GeoJSONLayer({
  url'MyURL' + params
  customParameters: {
    body
 },
// set rest of the properties
})
---------------------
 
...but don't know where to put my header information.

 

 

0 Kudos