Can the results of a query be placed in a FeatureLayer instead of a GraphicsLayer?

1043
21
Jump to solution
10-12-2018 12:35 PM
JohnRichardson
New Contributor III

I currently have a featurelayer being queried, and the results placed in a graphicslayer ... I am hoping that the results can be placed in a new featurelayer instead?

Here is current code:

$("#radio-4").click(function() {                       //shows only January crimes
      var sr = new SpatialReference(102100);

      janResults = new GraphicsLayer();
      janResults.title = "January";
      map.add(janResults);
      var janQuery = featurelayer.createQuery(); 
                                                                                    
      janQuery.where = "Month = 'January'";
      janQuery.outFields = ["Offense" , "Location"];
      janQuery.returnGeometry = true;
      janQuery.outSpatialReference = sr;

      featurelayer.queryFeatures(janQuery).then(displayGraphics);
      
      function displayGraphics(results) {

            janResults.removeAll();

            for (feature of results.features) {
                  var janGraphic = new Graphic ({
                        geometry: feature.geometry,
                        symbol: {
                              type: "simple-marker",
                              color: "blue",
                              size: "20px",
                              outline: {
                                    color:"black",
                                    width: 2,
                              },
                        },
                  });
                  janResults.add(janGraphic);
            }

      }
     
});  //end of January query
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Esteemed Contributor

John,

   Sure you just need to create the FL from the graphics array.

      var sRenderer = {
        type: "simple", // autocasts as new SimpleRenderer()
        symbol: {
          type: "simple-marker", // autocasts as new SimpleMarkerSymbol()
          size: "20px",
          color: "blue,
          outline: {
            width: 2,
            color: "black"
          }
        }
      };
      var graArr = [];
      var janQuery = featurelayer.createQuery(); 
                                                                                    
      janQuery.where = "Month = 'January'";
      janQuery.outFields = ["Offense" , "Location"];
      janQuery.returnGeometry = true;
      janQuery.outSpatialReference = sr;

      featurelayer.queryFeatures(janQuery).then(displayGraphics);

      function displayGraphics(results) {
        for (feature of results.features) {
          var janGraphic = new Graphic ({
            geometry: feature.geometry,
            attributes: {
              Offense: feature.attributes.Offense,
              Location: feature.attributes.Location
            }
          });
          graArr.push(janGraphic);
        }
        createLayer(graArr);
      }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
      function createLayer(graphics) {
        var fields = [{
          name: "ObjectID",
          alias: "ObjectID",
          type: "oid"
        }, {
          name: "Offense",
          alias: "Offense",
          type: "string"
        }, {
          name: "Location",
          alias: "Location",
          type: "string"
        }];
        layer = new FeatureLayer({
          title: "January",
          source: graphics,
          // create an instance of esri/layers/support/Field for each field object
          fields: fields, // This is required when creating a layer from Graphics
          objectIdField: "ObjectID", // This must be defined when creating a layer from Graphics
          renderer: sRenderer, // set the visualization on the layer
        });
        map.add(layer);
      }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

21 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

John,

   Sure you just need to create the FL from the graphics array.

      var sRenderer = {
        type: "simple", // autocasts as new SimpleRenderer()
        symbol: {
          type: "simple-marker", // autocasts as new SimpleMarkerSymbol()
          size: "20px",
          color: "blue,
          outline: {
            width: 2,
            color: "black"
          }
        }
      };
      var graArr = [];
      var janQuery = featurelayer.createQuery(); 
                                                                                    
      janQuery.where = "Month = 'January'";
      janQuery.outFields = ["Offense" , "Location"];
      janQuery.returnGeometry = true;
      janQuery.outSpatialReference = sr;

      featurelayer.queryFeatures(janQuery).then(displayGraphics);

      function displayGraphics(results) {
        for (feature of results.features) {
          var janGraphic = new Graphic ({
            geometry: feature.geometry,
            attributes: {
              Offense: feature.attributes.Offense,
              Location: feature.attributes.Location
            }
          });
          graArr.push(janGraphic);
        }
        createLayer(graArr);
      }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
      function createLayer(graphics) {
        var fields = [{
          name: "ObjectID",
          alias: "ObjectID",
          type: "oid"
        }, {
          name: "Offense",
          alias: "Offense",
          type: "string"
        }, {
          name: "Location",
          alias: "Location",
          type: "string"
        }];
        layer = new FeatureLayer({
          title: "January",
          source: graphics,
          // create an instance of esri/layers/support/Field for each field object
          fields: fields, // This is required when creating a layer from Graphics
          objectIdField: "ObjectID", // This must be defined when creating a layer from Graphics
          renderer: sRenderer, // set the visualization on the layer
        });
        map.add(layer);
      }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
JohnRichardson
New Contributor III

Thank you Robert ... I will give this a shot!

0 Kudos
JohnRichardson
New Contributor III

dojo.js:251 [esri.layers.FeatureLayer] #load() Failed to load layer (title: 'January', id: '166785b54b9-layer-0')

  1. {name: "feature-layer:missing-property", message: "FeatureLayer created as feature collection requires properties: geometryType,fields,objectIdField", details: {…}}
    1. details:
      1. requiredProperties: (3) ["geometryType", "fields", "objectIdField"]
      2. __proto__: Object
    2. message: "FeatureLayer created as feature collection requires properties: geometryType,fields,objectIdField"
    3. name: "feature-layer:missing-property"
    4. __proto__: b

I get the above error message in console ...

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

John,

  make this change:

        layer = new FeatureLayer({
          geometryType: "point",
          title: "January",
          source: graphics,
          // create an instance of esri/layers/support/Field for each field object
          fields: fields, // This is required when creating a layer from Graphics
          objectIdField: "ObjectID", // This must be defined when creating a layer from Graphics
          renderer: sRenderer, // set the visualization on the layer
        });
        map.add(layer);
JohnRichardson
New Contributor III

Interestingly, no errors are thrown, the symbol for January shows up in the legend, the layer shows up as turned on in the layer list, but  nothing is shown on the map!

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

And when you were adding this as a graphics layer there where results right?

0 Kudos
JohnRichardson
New Contributor III

Yes, many

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

John,

  I was just guessing at the geometry type when I choose point. Is the data actually point data or some other geometry type?

0 Kudos
JohnRichardson
New Contributor III

It is point data, yes

0 Kudos