Select to view content in your preferred language

Get Area value and update before apply Edits in Edit Widget

1393
11
Jump to solution
12-04-2017 09:03 PM
KrishV
by
Frequent Contributor

Hello All,

I'm trying to update one attribute value using Polygon Area in Edit Widget. I'm using before-apply-edits event to do so. For calculating area of polygon I'm using geometry service. Please find the code below:

onBeforeApplyEdits: function (evt) {
          if (evt.adds != null) {
              var feat = evt.adds[0];
              var geometry = feat.geometry
              var geometryService = new GeometryService("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/Geometry/GeometryServer");
              //geometryService.on("areas-and-lengths-complete", outputAreaAndLength);
              var areasAndLengthParams = new AreasAndLengthsParameters();
              areasAndLengthParams.areaUnit = GeometryService.UNIT_SQUARE_METERS;
              areasAndLengthParams.calculationType = "planar";

              geometryService.simplify([geometry], lang.hitch(this, function (simplifiedGeometries) {
                  areasAndLengthParams.polygons = simplifiedGeometries;
                  geometryService.areasAndLengths(areasAndLengthParams);
              }));

              geometryService.on("areas-and-lengths-complete", lang.hitch(this, function (evtObj) {
                  var result = evtObj.result;
                  console.log(result.areas[0]);
                  


                  var feat = evt.adds[0];
                  area = result.areas[0];
                 
                  feat.attributes.no_of_areas = Math.round(area / 25);

                  setTimeout(lang.hitch(this, function () {
                      evt.target.applyEdits(null, [feat], null);
                  }), 100);

                  debugger;

              }));
          }
      },

Attributes are getting updated but unable to save. Please suggest how to proceed.

Regards,

Krish

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Krish,

   This is what I have working (tested):

add new requires:

define([
    'dojo/_base/declare',
    ...
    'esri/tasks/AreasAndLengthsParameters',
    'esri/tasks/GeometryService'
  ],
  function(declare, 
    ...
    AreasAndLengthsParameters, GeometryService) {

Attach the before edit event lines (30 - 39) in _getTemplatePicker:

      _getTemplatePicker: function(layerInfos) {
        this._layerObjectsParaForTempaltePicker = [];

        array.forEach(layerInfos, function(layerInfo) {
          if(layerInfo.featureLayer &&
            layerInfo.featureLayer.getEditCapabilities &&
            layerInfo.featureLayer.getEditCapabilities().canCreate) {
            this._layerObjectsParaForTempaltePicker.push(layerInfo.featureLayer);
          }
        }, this);

        // change string of templatePicker is empty
        this._defaultTempaltePickerEmpeyStr =
          esriBundle.widgets.templatePicker.creationDisabled;
        if(this._canCreateLayersAreAllInvisibleFlag) {
          esriBundle.widgets.templatePicker.creationDisabled =
            this.nls.noCanCreateLayerAreCurrentlyVisible;
        }

        var bottomStyle = this._configEditor.toolbarVisible ? "" : "bottom: 0px";
        var topStyle = this._configEditor.useFilterEdit ? "top: 115px" : "top: 18px";
        var templatePicker = new TemplatePicker({
          featureLayers: this._layerObjectsParaForTempaltePicker,
          grouping: true,
          rows: "auto",
          columns: "auto",
          style: bottomStyle + ";" + topStyle
        }, html.create("div", {}, this.domNode));
        templatePicker.startup();
        templatePicker.on("selection-change", lang.hitch(this, function(evt){
          var selected = evt.target.getSelected();
          if (selected) {
            var featureLayer = selected.featureLayer;
            if(this.evtHandler){
              this.evtHandler.remove();
            }
            this.evtHandler = on(featureLayer, 'before-apply-edits', lang.hitch(this, this.onBeforeApplyEdits));
          }
        }));

        return templatePicker;
      },

onBeforeApplyEdits function (I did make a change line 5):

      onBeforeApplyEdits: function(evt) {
        if(evt.adds != null) {
          var feat = evt.adds[0];
          var geometry = feat.geometry
          var geometryService = jimuUtils.getArcGISDefaultGeometryService();
          var areasAndLengthParams = new AreasAndLengthsParameters();
          areasAndLengthParams.areaUnit = GeometryService.UNIT_SQUARE_METERS;
          areasAndLengthParams.calculationType = "planar";

          geometryService.simplify([geometry], lang.hitch(this, function(simplifiedGeometries) {
            areasAndLengthParams.polygons = simplifiedGeometries;
            geometryService.areasAndLengths(areasAndLengthParams);
          }));

          geometryService.on("areas-and-lengths-complete", lang.hitch(this, function(evtObj) {
            var result = evtObj.result;
            console.log(result.areas[0]);
            var feat = evt.adds[0];
            area = result.areas[0];
            feat.attributes.no_of_areas = Math.round(area / 25);
            setTimeout(lang.hitch(this, function() {
              evt.target.applyEdits(null, [feat], null);
            }), 100);
          }));
        }
      }

View solution in original post

11 Replies
RobertScheitlin__GISP
MVP Emeritus

Kirsh,

  So are you saying it is failing at line 28?

0 Kudos
KrishV
by
Frequent Contributor

Hi Robert,

Yes. applyEdits is not working and I don't see any errors in console. "areas-and-lengths-complete" is asynchronous and this fires after edits are completed.

Regards,

Krish

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Krish,

   Is it complaining that evt.target is undefined or null or that evt.target.applyEdits is not a function. What is the error?

0 Kudos
KrishV
by
Frequent Contributor

Robert,

I don't see any error message. This lines runs but the attributes are not getting updated in the feature layer.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Krish,

  Hmm.. Try adding this line 2 so we can be sure you still have a reference to a FeatureLayer:

setTimeout(lang.hitch(this, function () {
  console.info(evt.target);
  evt.target.applyEdits(null, [feat], null);
}), 100);
0 Kudos
KrishV
by
Frequent Contributor

Hi Robert Scheitlin, GISP,

I Added console.info and have a reference to Feature layer.

Regards,

Krish      

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Krish,

   So how are you testing that the edits are not getting applied?

0 Kudos
KrishV
by
Frequent Contributor

Hi rscheitlinGISP‌,

After capturing new feature, I'm refreshing the browser and clicking on captured feature to see updated attributes in pop up.

Thanks, 

Krish

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Krish,

   This is what I have working (tested):

add new requires:

define([
    'dojo/_base/declare',
    ...
    'esri/tasks/AreasAndLengthsParameters',
    'esri/tasks/GeometryService'
  ],
  function(declare, 
    ...
    AreasAndLengthsParameters, GeometryService) {

Attach the before edit event lines (30 - 39) in _getTemplatePicker:

      _getTemplatePicker: function(layerInfos) {
        this._layerObjectsParaForTempaltePicker = [];

        array.forEach(layerInfos, function(layerInfo) {
          if(layerInfo.featureLayer &&
            layerInfo.featureLayer.getEditCapabilities &&
            layerInfo.featureLayer.getEditCapabilities().canCreate) {
            this._layerObjectsParaForTempaltePicker.push(layerInfo.featureLayer);
          }
        }, this);

        // change string of templatePicker is empty
        this._defaultTempaltePickerEmpeyStr =
          esriBundle.widgets.templatePicker.creationDisabled;
        if(this._canCreateLayersAreAllInvisibleFlag) {
          esriBundle.widgets.templatePicker.creationDisabled =
            this.nls.noCanCreateLayerAreCurrentlyVisible;
        }

        var bottomStyle = this._configEditor.toolbarVisible ? "" : "bottom: 0px";
        var topStyle = this._configEditor.useFilterEdit ? "top: 115px" : "top: 18px";
        var templatePicker = new TemplatePicker({
          featureLayers: this._layerObjectsParaForTempaltePicker,
          grouping: true,
          rows: "auto",
          columns: "auto",
          style: bottomStyle + ";" + topStyle
        }, html.create("div", {}, this.domNode));
        templatePicker.startup();
        templatePicker.on("selection-change", lang.hitch(this, function(evt){
          var selected = evt.target.getSelected();
          if (selected) {
            var featureLayer = selected.featureLayer;
            if(this.evtHandler){
              this.evtHandler.remove();
            }
            this.evtHandler = on(featureLayer, 'before-apply-edits', lang.hitch(this, this.onBeforeApplyEdits));
          }
        }));

        return templatePicker;
      },

onBeforeApplyEdits function (I did make a change line 5):

      onBeforeApplyEdits: function(evt) {
        if(evt.adds != null) {
          var feat = evt.adds[0];
          var geometry = feat.geometry
          var geometryService = jimuUtils.getArcGISDefaultGeometryService();
          var areasAndLengthParams = new AreasAndLengthsParameters();
          areasAndLengthParams.areaUnit = GeometryService.UNIT_SQUARE_METERS;
          areasAndLengthParams.calculationType = "planar";

          geometryService.simplify([geometry], lang.hitch(this, function(simplifiedGeometries) {
            areasAndLengthParams.polygons = simplifiedGeometries;
            geometryService.areasAndLengths(areasAndLengthParams);
          }));

          geometryService.on("areas-and-lengths-complete", lang.hitch(this, function(evtObj) {
            var result = evtObj.result;
            console.log(result.areas[0]);
            var feat = evt.adds[0];
            area = result.areas[0];
            feat.attributes.no_of_areas = Math.round(area / 25);
            setTimeout(lang.hitch(this, function() {
              evt.target.applyEdits(null, [feat], null);
            }), 100);
          }));
        }
      }