1

I am wondering if/how attributes of a Hosted Feature Layer can be read into a 2d Javascript Array using ArcGIS API for JavaScript 4.x?

I need a 2D Matrix of all attributes values to run some analysis.

2
  • what are i and j in your 2d matrix?, what type of geometry has the layer? – cabesuon Feb 7 '20 at 4:02
  • @cabesuon i would be the set of features and j attributes or a subset of attributes. – sMartines Feb 8 '20 at 23:07
1

I think this example that I made might help you, it retrieves the features and build a matrix with them. You have to consider that this is restricted by the maximum of features per request configure in the service. If this is a problem, then you will have to make several request.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="initial-scale=1,maximum-scale=1,user-scalable=no"
    />
    <title>Intro to FeatureLayer - 4.14</title>

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

    <style>
      html,
      body,
      #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
      }
    </style>

    <script>
      require([
        "dojo/on",
        "esri/Map",
        "esri/views/MapView",
        "esri/layers/FeatureLayer"
      ], function(on, Map, MapView, FeatureLayer) {
        var map = new Map({
          basemap: "hybrid"
        });

        var view = new MapView({
          container: "viewDiv",
          map: map,

          extent: {
            // autocasts as new Extent()
            xmin: -9177811,
            ymin: 4247000,
            xmax: -9176791,
            ymax: 4247784,
            spatialReference: 102100
          }
        });

        /********************
         * Add feature layer
         ********************/

        // Carbon storage of trees in Warren Wilson College.
        var featureLayer = new FeatureLayer({
          url:
            "https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/Landscape_Trees/FeatureServer/0"
        });

        var featureMatrix; // [...attributes, esrigeojson]

        featureLayer.queryFeatures(featureLayer.createQuery()).then(fs => {
          console.log(`Number of rows: ${fs.features.length}`);
          featureMatrix = fs.features.map(g => 
            [...Object.values(g.attributes), g.geometry.toJSON()]
          );
          console.log('Matrix:');
          console.log(featureMatrix);
        });

        map.add(featureLayer);
      });
    </script>
  </head>

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.