Query in SubLayer of MapImageLayer API 4.8

2474
4
08-17-2018 01:07 PM
AndresDiaz
New Contributor

Hi, I would like to use MapImageLayer and his method SubLayer to do a query; layerimage is a MapImageLayer, I extract sublayer using findSublayerById, next I done filter sublayer  using a definitionExpression. But when I do a Query in this SubLayer dont work, nothing happen. After resolve promise of queryFeatures this result would be FeatureSet but nothing happen have not created object FeatureSet

What I can do?

Thanks for yours help

         
this.layerimage.when(()=>{
this.coberture=this.layerimage.findSublayerById(0);
this.coberture.definitionExpression="COBERT IN( 'forest' )"
this.coberture.popupTemplate=this.template
this.queryCober=this.coberturas.createQuery()
this.queryCober.returnGeometry = true
this.queryCober.outFields=["COBER"]
this.queryCober.geometry=this.mapView.extent


this.coberturas.queryFeatures().then((response=>{
console.log(response)
}))
})
0 Kudos
4 Replies
RobertScheitlin__GISP
MVP Emeritus

Andres,

   Here is a modified sample that adds the query featureset results to the console.

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
  <title>MapImageLayer - Set definition expressions on sublayers - 4.8</title>

  <link rel="stylesheet" href="https://js.arcgis.com/4.8/esri/css/main.css">
  <script src="https://js.arcgis.com/4.8/"></script>

  <script>
    require([
        "esri/Map",
        "esri/views/SceneView",
        "esri/layers/MapImageLayer",
        "dojo/on",
        "dojo/number",
        "dojo/domReady!"
      ],
      function(
        Map, SceneView, MapImageLayer, on, number
      ) {

        /*****************************************************************
         * Create a MapImageLayer instance pointing to a Map Service
         * containing data about US Cities, Counties, States and Highways.
         * Define sublayers with visibility for each layer in Map Service.
         *****************************************************************/
        var layer = new MapImageLayer({
          url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer",
          sublayers: [
          {
            id: 3,
            visible: false
          },
          {
            id: 2,
            visible: true
          },
          {
            id: 1,
            visible: true
          },
          {
            id: 0,
            visible: true
          }]
        });

        /*****************************************************************
         * Add the layer to a map
         *****************************************************************/
        var map = new Map({
          basemap: "dark-gray",
          layers: [layer]
        });

        var view = new SceneView({
          container: "viewDiv",
          map: map,
          zoom: 4,
          center: [-99, 39]
        });

        /*****************************************************************
         * Listen for events on when the slider values have changed.
         * When the slider value changes, apply the new value to the
         * MapImageLayer definitionExpression.
         *****************************************************************/
        layer.when(function() {
          var cities = layer.findSublayerById(0);
          var citiesQuery = cities.createQuery();
          citiesQuery.where = "areaname LIKE 'A%'";
          cities.queryFeatures(citiesQuery)
          .then(function(response){
            console.info(response.features);
          });
        });
      });
  </script>

  <style>
    html,
    body {
      font-family: sans-serif;
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }

    #viewDiv {
      position: absolute;
      right: 0;
      left: 0;
      top: 0;
      bottom: 0;
    }

  </style>

</head>

<body>
  <div id="viewDiv"></div>
</body>

</html>
AndresDiaz
New Contributor

Thanks works very well

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Great, don’t forget to mark this as answered.

0 Kudos
GeoJason
New Contributor III
//layer.findSublayerById(0);

This just saved me from hours of frustration today. I should always remember check the community first. 

0 Kudos