Appending data to ESRI REST or AGOL Hosted Feature Layer

7999
9
03-09-2016 11:08 AM
RobertMueller_Jr1
New Contributor III

Does anyone know how I can append data to editable ESRI REST (Feature Service) or AGOL Hosted Feature Layer? I don't want a merge function as I want the Target data layer to be the same as the output data layer.

Tags (2)
0 Kudos
9 Replies
DavidChambers1
New Contributor III

Could you consider using the add features endpoint?

0 Kudos
RobertMueller_Jr1
New Contributor III

Thanks David - I'm looking into the possibility of programmatically incorporating this. I would like a user to add GPS information into my web app and append to an existing REST or Hosted Feature Layer. Given the novice nature of application users, I'm surprised I haven't found anything yet to guide users towards adding data and appending into existing. A merge (and output new) would destroy coded references to the primary dataset of interest.

0 Kudos
DavidChambers1
New Contributor III

No problem Robert.  You can definitely accomplish this tasking and we do have a suite of out of the box tools to allow users to edit data and have it auto-sync (add features) to existing hosted feature services on ArcGIS Online or ArcGIS Server services.  Check out the Edit Widget in Web AppBuilder or ArcGIS Online has baked in editing tools and allows you to publish data from XY information.   I think the answer to your question comes down to how your users will be adding data to the application. Will they be manually selecting points, adding a .csv of XY features from your GPS devices, etc.  Based on what you want to support you can use our APIs to extend existing functionality that you see in the app builders and work with services and the add features endpoint. 

RobertMueller_Jr1
New Contributor III

David-

Thanks for the reply. I am currently using the Edit Widget in great detail to allow users to edit features currently in the Hosted Feature Layer. I would like to do something like add a shapefile (which I have the Add SHP widget implemented) and now I would like to do something with those shapefile features, such as copy (append really) them into the Hosted Feature Layer. I could have users re-trace the shapefile features but with hundreds of polygon features. I've still been working with your "add features" suggestion as well as ArcRest https://github.com/Esri/ArcREST to figure out if a GP is require to navigate this.

0 Kudos
DavidChambers1
New Contributor III

What about if you allowed your users to browse to the shapefile on disk and used the Add shapefile JavaScript sample to generate a feature collection.  You could then make a request to add features or use ArcREST to add the feature collection to the existing hosted feature service.  The big issue I foresee is schema.  Is the schema between the shapefile and feature service going to be the same?

RobertMueller_Jr1
New Contributor III

David-

Yes, the Add Shapefile that you mentioned is the exact javascript example I used in my app. The schema coming in will (almost) always be the same, which I can either cross-walk to the Hosted Feature Layer or change the Feature Layer to match. Although, it isn't necessary for attributes (except for acres and name) to copy over - I am most concerned with the geometry relative to the ground.

0 Kudos
RobertMueller_Jr1
New Contributor III

David- Quick Update

I was able to figure out the ESRI REST add feature and add points or polygons into my data from the REST endpoint and view in ArcMap/AGOL, so I know that I can interact with the data. I've add the polygon and point samples for as samples for other users in the future.

Point:

[ { "geometry" : {"x" : -118.15, "y" : 33.80},"attributes" : {"NAme" : "Joe Smith", "VALUE" : 94820.37    }  }  ]

Polygon

[  {"geometry" : {"rings" : [  [ [-97.06138,32.837], [-97.06133,32.836], [-97.06124,32.834], [-97.06127,32.832], [-97.06138,32.837] ],  [ [-97.06326,32.759], [-97.06298,32.755], [-97.06153,32.749], [-97.06326,32.759] ] ],"spatialReference" : {"wkid" : 4326} }, "attributes" : {"NAme" : "Joe Smith","VALUE" : 94820.37

} }]

Now I'm attempting to modify the Add Shapefile widget to add the json information to the Hosted Feature Layer REST endpoint at the same time. I'm thinking by passing this request;

esriRequest({
  "url": "http://***/arcgis/rest/services/ServiceRequest/FeatureServer/0/AddFeatures",
  "content": {                     }
  }, {"usePost": true
  });

RobertMueller_Jr1
New Contributor III

I was able to figure this out - as an alternative to the REST endpoint "add features" you can use the "apply edits" as part of the JSAPI (FeatureLayer | API Reference | ArcGIS API for JavaScript ) and found in this example; Reshape polygons | ArcGIS API for JavaScript .Cannabolized this sample,

I added the feature layer reference;

        var firePerimeterFL = new esri.layers.FeatureLayer("https://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Fire/Sheep/FeatureServer/2", {

          mode: esri.layers.FeatureLayer.MODE_SNAPSHOT,

          outFields: ["*"],

          id: "firePerimeterFL"

        });

        map.addLayers([firePerimeterFL]);

Included a javascript function;

     dojo.connect(dijit.byId("reshape"), "onClick", function() {

            var targetGraphic = firePerimeterFL.getSelectedFeatures()[0].setGeometry(reshapedGeometry);

            firePerimeterFL.applyEdits([targetGraphic],null, null);

          });

And created a button;

<button id="reshape" data-dojo-type="dijit.form.Button">ApplyEdits</button>

But every time FireBug picks out this line; "var targetGraphic = firePerimeterFL.getSelectedFeatures()[0].setGeometry(reshapedGeometry)" and I haven't figured out what it should read instead. Anyone have any suggestions? I want the user to click the "Add Shapefile" button to add a shapefile to the map, and then select the loaded features from that shapefile and then the "ApplyEdits" button to add those features to the "firePerimeterFL".

0 Kudos
RobertMueller_Jr1
New Contributor III

So after a little work, I was able to get the code to work out for me. I plan on further extending the "Add Shapefile" from the ESRI JS API, but for now I have the code set to add a shapefile, and once added and the polygons appear, when you click on the added polygon (a user validation) that polygon is appended into the featurelayer of your hardcoded choice using the "ApplyEdits" method of the ESRI JS API. I didn't use a button as describe above, but added 1 line of code into the arrayUtils within Add Shapefile. Note that this is only copying the geometry and not the attributes.

Added these lines to to the Add Shapefile code;

var firePerimeterFL = new esri.layers.FeatureLayer("https://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Fire/Sheep/FeatureServer/2", {

          mode: esri.layers.FeatureLayer.MODE_SNAPSHOT,

          outFields: ["*"],

          id: "firePerimeterFL"

        });

        map.addLayers([firePerimeterFL]);

Add 1 line into this part of the Add Shapefile code;

arrayUtils.forEach(featureCollection.layers, function (layer) {

              var infoTemplate = new InfoTemplate("Details", "${*}");

              var featureLayer = new FeatureLayer(layer, {

                infoTemplate: infoTemplate

              });

              //associate the feature with the popup on click to enable highlight and zoom to

              featureLayer.on('click', function (event) {

                   map.infoWindow.setFeatures([event.graphic]);

                   firePerimeterFL.applyEdits([event.graphic], null, null);
              });

              //change default symbol if desired. Comment this out and the layer will draw with the default symbology

              changeRenderer(featureLayer);

              fullExtent = fullExtent ?

                fullExtent.union(featureLayer.fullExtent) : featureLayer.fullExtent;

              layers.push(featureLayer);

            });

            map.addLayers(layers);

            map.setExtent(fullExtent.expand(1.25), true);

            dom.byId('upload-status').innerHTML = "";

          }