Union only working on second draw extent

826
8
Jump to solution
11-10-2016 09:58 AM
by Anonymous User
Not applicable

Hi all,

I am using geometry service's union to dissolve polygons. It works great. However, I cannot figure out why on my first draw-end my featurelayer selection is done but not the union. If I draw a second extent then it works. not too sure what I am missing here. Thanks,

Alex

here is my code:

 <script>
        var map;

        require([
          "esri/InfoTemplate",
          "esri/map",
          "esri/layers/FeatureLayer",
          "esri/symbols/SimpleFillSymbol",
          "esri/symbols/SimpleLineSymbol",
          "esri/tasks/query",
          "esri/toolbars/draw",
          "esri/tasks/GeometryService",
          "esri/dijit/editing/Union",
          "esri/graphic",
          "esri/graphicsUtils",
          "dojo/dom",
          "dojo/on",
          "dojo/parser",
          "dojo/_base/array",
          "esri/Color",
          "dojo/domReady!"
        ],
          function (
            InfoTemplate, Map, FeatureLayer, SimpleFillSymbol, SimpleLineSymbol,
            Query, Draw, GeometryService, Union, Graphic, graphicsUtils, dom, on, parser, arrayUtil, Color
          ) {

              parser.parse();


              

              var selectionToolbar, featureLayer;


              map = new Map("map", {
                  basemap: "streets",
                  center: [-97.395, 37.537],
                  zoom: 11
              });

              map.on("load", initSelectToolbar);

              var fieldsSelectionSymbol =
                new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,
                  new SimpleLineSymbol(SimpleLineSymbol.STYLE_DASHDOT,
                new Color([255, 0, 0]), 2), new Color([255, 255, 0, 0.5]));


              featureLayer = new FeatureLayer("https://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Petroleum/KSPetro/MapServer/1",
                {
                    mode: FeatureLayer.MODE_SNAPSHOT
                });
              featureLayer.setSelectionSymbol(fieldsSelectionSymbol);
              map.addLayer(featureLayer);

              $("#selectFieldsButton").click(function () {
                  selectionToolbar.activate(Draw.EXTENT);
              });

              function initSelectToolbar(event) {
                  console.log("start edits");
                  selectionToolbar = new Draw(event.map);
                  var selectQuery = new Query();
                  
                  on(selectionToolbar, "DrawEnd", function (geometry) {
                      var geometryService = new GeometryService("http://itas46:6080/arcgis/rest/services/Utilities/Geometry/GeometryServer");
                      selectQuery.geometry = geometry;
                      featureLayer.selectFeatures(selectQuery, FeatureLayer.SELECTION_NEW);
                      var targetGeometry = graphicsUtils.getGeometries(featureLayer.getSelectedFeatures());
                      console.log(targetGeometry);
                      geometryService.union(targetGeometry, function (geometry) {
                          featureLayer.clearSelection();
                          var symbol = new SimpleFillSymbol("none", new SimpleLineSymbol("solid", new Color([255, 255, 255]), 2), new Color([255, 255, 255, 0.25]));
                          var graphic = new Graphic(geometry, symbol);
                          map.graphics.add(graphic);
                          console.log(targetGeometry);
                      });

                  });

              }
          });
    </script>

 Thanks

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Alex,

  The FeatureLayer.selectFeatures method returns a promise and it apears that you are not waiting for it to return in your code:

featureLayer.selectFeatures(selectQuery, FeatureLayer.SELECTION_NEW);

it should be something like:

featureLayer.selectFeatures(selectQuery, FeatureLayer.SELECTION_NEW, lang.hitch(this, function(){
   //Now do something with the newly selected features.
}));

View solution in original post

0 Kudos
8 Replies
RobertScheitlin__GISP
MVP Emeritus

Alex,

  The FeatureLayer.selectFeatures method returns a promise and it apears that you are not waiting for it to return in your code:

featureLayer.selectFeatures(selectQuery, FeatureLayer.SELECTION_NEW);

it should be something like:

featureLayer.selectFeatures(selectQuery, FeatureLayer.SELECTION_NEW, lang.hitch(this, function(){
   //Now do something with the newly selected features.
}));
0 Kudos
by Anonymous User
Not applicable

Just tried but the call back does not trigger my geometry service the first time around.

on(selectionToolbar, "DrawEnd", function (geometry) {
                      var geometryService = new GeometryService("http://itas46:6080/arcgis/rest/services/Utilities/Geometry/GeometryServer");
                      selectQuery.geometry = geometry;
                      featureLayer.selectFeatures(selectQuery, FeatureLayer.SELECTION_NEW, lang.hitch(this, function (targetGeometry) {
                          targetGeometry = graphicsUtils.getGeometries(featureLayer.getSelectedFeatures());
                          console.log(targetGeometry);
                          geometryService.union(targetGeometry, function (geometry) {
                              featureLayer.clearSelection();
                              var symbol = new SimpleFillSymbol("none", new SimpleLineSymbol("solid", new Color([255, 255, 255]), 2), new Color([255, 255, 255, 0.25]));
                              var graphic = new Graphic(geometry, symbol);
                              map.graphics.add(graphic);
                              console.log(targetGeometry);
                          });

                      }));
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Alex,

  Does the console.log show more than one geometry the first time through?

0 Kudos
by Anonymous User
Not applicable

Yes. It returns the number of polygons selected that are in my "targetGeometry" array as expected.

0 Kudos
by Anonymous User
Not applicable

To add to this it seems like

This part is executed correctly, I get my selected fatures and array:

var geometryService = new GeometryService("http://itas46:6080/arcgis/rest/services/Utilities/Geometry/GeometryServer");
                      selectQuery.geometry = geometry;
                      featureLayer.selectFeatures(selectQuery, FeatureLayer.SELECTION_NEW, lang.hitch(this, function (targetGeometry) {
                          targetGeometry = graphicsUtils.getGeometries(featureLayer.getSelectedFeatures());
                          console.log(targetGeometry);

This part does not trigger until the second draw (i dont get any console.log stuff the 1st time i draw and nothing shows up):

geometryService.union(targetGeometry, function (geometry) {
                              featureLayer.clearSelection();
                              var symbol = new SimpleFillSymbol("none", new SimpleLineSymbol("solid", new Color([255, 255, 255]), 2), new Color([255, 255, 255, 0.25]));
                              var graphic = new Graphic(geometry, symbol);
                              map.graphics.add(graphic);
                              console.log(targetGeometry);
                          });

 the first time it looks like this:

The second time it looks like this (FYI you can see on the right window that the union has been successful):

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Alex,

  try this (added a GeometryService error function):

                    on(selectionToolbar, "DrawEnd", function (geometry) {
                      var geometryService = new GeometryService("http://itas46:6080/arcgis/rest/services/Utilities/Geometry/GeometryServer");
                      selectQuery.geometry = geometry;
                      featureLayer.selectFeatures(selectQuery, FeatureLayer.SELECTION_NEW, lang.hitch(this, function (targetGeometry) {
                          targetGeometry = graphicsUtils.getGeometries(featureLayer.getSelectedFeatures());
                          console.log(targetGeometry);
                          geometryService.union(targetGeometry, function (geometry) {
                              featureLayer.clearSelection();
                              var symbol = new SimpleFillSymbol("none", new SimpleLineSymbol("solid", new Color([255, 255, 255]), 2), new Color([255, 255, 255, 0.25]));
                              var graphic = new Graphic(geometry, symbol);
                              map.graphics.add(graphic);
                              console.log("unioned features");
                          }, function(err){
                              console.log(err);
                          });
                      }));
0 Kudos
by Anonymous User
Not applicable

Still No errors (except the XMLHTTPRequest, is that the problem maybe?)

0 Kudos
by Anonymous User
Not applicable

That was the issue. Proxy was.

0 Kudos