AnsweredAssumed Answered

Union geometry service

Question asked by alex.gole_ELDORADOCOUNTY on Nov 8, 2016
Latest reply on Nov 9, 2016 by alex.gole_ELDORADOCOUNTY

Hi all,

I am trying to make the geometry service work but I am unsuccessful. Any idea why?

Here is my code

 

 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",
          "dojo/dom",
          "dojo/on",
          "dojo/parser",
          "dojo/_base/array",
          "esri/Color",
          "dijit/form/Button",
          "dojo/domReady!"
        ],
          function (
            InfoTemplate, Map, FeatureLayer, SimpleFillSymbol, SimpleLineSymbol,
            Query, Draw, GeometryService, Union, dom, on, parser, arrayUtil, Color
          ) {

              parser.parse();


              var geometryService = new GeometryService("https://utility.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");

              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);
              });

              $("#clearSelectionButton").click(function () {
                  featureLayer.clearSelection();
              });

              function initSelectToolbar(event) {
                  selectionToolbar = new Draw(event.map);
                  var selectQuery = new Query();

                  on(selectionToolbar, "DrawEnd", function (geometry) {
                      selectQuery.geometry = geometry;
                      featureLayer.selectFeatures(selectQuery,
                        FeatureLayer.SELECTION_NEW);
                  });

                  on(featureLayer, "onSelectionComplete", function (features) {
                      if (features.length === 1) {
                          var targetGeometry = features[0].geometry;
                          geometryService.union(targetGeometry);
                      }
                  });
                  on(geometryService, "union-complete", function (uniongeometries) {
                      var targetGraphic = featureLayer.getSelectedFeatures()[0].setGeometry(uniongeometries);
                      featureLayer.applyEdits(null, [targetGraphic], null);
                  });
                
              }
          });

Outcomes