Select to view content in your preferred language

Use Feature Layer Attribute as GP Service Input

3333
29
Jump to solution
03-06-2017 11:15 AM
LloydBronn
Frequent Contributor

I'm trying to click on a country layer and use the country name as input for a GP tool. I've done this with XY inputs from map clicks, but I'm having a lot of trouble getting the attribute from a feature. Do I need a query task? Here is my code snippet. When I add content to the infoWindow, it displays "Country: ${NAME}" so it's not pulling that attribute from the feature layer correctly.

var country = {};

var countriesLayer = new FeatureLayer("http://<ourserver>/arcgis/rest/services/Countries_Outline/MapServer/1");
          
          map.addLayer(countriesLayer);
          
          var content = "<b>Country</b>: ${NAME}";
              
          map.on("click", function(evt){
                         
          map.graphics.clear();
          var graphic = new Graphic(evt.mapPoint);
            
          map.graphics.add(graphic);
          map.infoWindow.setFeatures([graphic]);
            
            map.infoWindow.resize(200,200);     
          map.infoWindow.setContent(content);
          map.infoWindow.show(evt.mapPoint)
            map.infoWindow.setTitle("Forecast Reports");
            
        }); 
          
            
            var today = new Date();
            var todayISO = today.toISOString().slice(0,10).replace(/-/g,"");
            
            var link = domConstruct.create("a",{
                "class": "action", 
                "id": "reportLink",
                "innerHTML": "Forecast Report", //text that appears in the popup for the link 
                "href": "javascript: void(0);"
              }, query(".actionList", map.infoWindow.domNode)[0]);
          on(link, "click", function(){
            
            domAttr.set(dom.byId("reportLink"), "innerHTML", "Generating Report...");
            
            window.gp_chart = new Geoprocessor("http://<ourserver>/arcgis/rest/services/Generate_Forecast_Report/GPServer/CreateReport");
               
               var country = window.map.infoWindow.getSelectedFeature();
                
              var taskParams = {
                "Country":country
              };
                 
              //window.gp_chart.execute(taskParams,gpChartResultAvailable,gpChartFailure);
                 window.gp_chart.execute(taskParams);
0 Kudos
29 Replies
LloydBronn
Frequent Contributor

I tried this and I'm getting the same error

var country = window.map.infoWindow.getSelectedFeature();
var countryName =  country.attributes["NAME"]; ‍‍
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lloyd,

  What does 

console.log(window.map.infoWindow.getSelectedFeature());

return?

0 Kudos
LloydBronn
Frequent Contributor

So I changed the code to use a query. This correctly displays the name of the country that I click on, in this case "Brazil." However, when I console.log(country) I get "3," and this is also what is passed in to the GP tool since I set country as a global variable. 

var country = {};
          var content = "<b>Location</b>: ${NAME}";
        var infoTemplate = new InfoTemplate("Forecast Reports", content);
          
          var countriesLayer = new FeatureLayer("http://gis.mymetcon.com/arcgis/rest/services/Countries_Outline/MapServer/1",{
            mode: FeatureLayer.MODE_ONDEMAND,
            infoTemplate: infoTemplate,
            outFields: ["*"]
          });
            
          map.addLayer(countriesLayer);
          
          
              
          map.on("click", function(evt){
               var country = FeatureLayer.SELECTION_NEW;
               console.log(country);
                var selectQuery = new Query();          
           selectQuery.geometry = geometry;
            featureLayer.selectFeatures(selectQuery,
             country);
          map.infoWindow.setContent(content);
            
        }); 
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lloyd,

???

var country = FeatureLayer.SELECTION_NEW;

That is because the constant for FeatureLayer.SELECTION_NEW is 3.

0 Kudos
LloydBronn
Frequent Contributor

I just found that out in another thread. How do I pass the result of that selection out as a string?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lloyd,

   The result of a FeatureLayer.selectFeatures returns an array of graphics (not a string). So you would have to have the callback set to a function and the response of that function would be that graphic array so you would have to get the first graphic in that array and the use its attributes property of that graphic to get the NAME attribute.

0 Kudos
LloydBronn
Frequent Contributor

OK. Could you point to an example of this? Thanks!

0 Kudos
RobertScheitlin__GISP
MVP Emeritus
0 Kudos
LloydBronn
Frequent Contributor

Robert, which part is the relevant code in that example? Sorry, I've never worked with a callback function. Would it go inside this?

map.on("click", function(evt){
               
                var selectQuery = new Query();     
                selectQuery.returnGeometry = true; 
           selectQuery.geometry = geometry;
            featureLayer.selectFeatures(selectQuery,
             FeatureLayer.SELECTION_NEW);
               map.infoWindow.setContent(content)
            
        }); 
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lloyd,

  The relevant part of that sample is the callback function for the FeatureLayer.selectFeatures:

featureLayer.selectFeatures(query, FeatureLayer.SELECTION_NEW, function(results){
  //now you work with the results object which is an array of graphics
});‍‍‍‍‍‍
0 Kudos