How to convert/query SHAPE.STArea() field value and display in info/pop up window

4393
6
Jump to solution
01-27-2016 02:56 PM
KushendraShah1
New Contributor III

Here is my code:

<!DOCTYPE html>

<html>

  <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>

    <title>Map and Layer</title>

    <link rel="stylesheet" href="https://js.arcgis.com/3.15/esri/css/esri.css">

    <style>

      html, body, #map {

        height: 100%;

        margin: 0;

      }

      #info {

        position: absolute;

        right: 0;

        top: 0;

        padding: 10px;

        background-color: #999;

        font: 14px Segoe UI;

        width: 200px;

        text-align: center;

        border-radius: 0 0 0 10px;

      }

    </style>

    <script src="https://js.arcgis.com/3.15/"></script>

    <script>

    var map, calculateAcreage;

      require([

        "esri/map", "esri/layers/FeatureLayer", "esri/InfoTemplate",

        "esri/dijit/Legend",

        "esri/Color", "dojo/number", "dojo/domReady!"

      ], function(

        Map, FeatureLayer, InfoTemplate,

        Legend,

        Color, number

      ) {

        map = new Map("map", {

          basemap: "gray",

          center: [-97.144, 34.174],

          zoom: 6

        });

        map.on("layers-add-result", createLegend);

        var infoTemplate = new InfoTemplate(

          "${Name},

          "${SHAPE.STArea()} sq.ft is equivalent to ${SHAPE.STArea():calculateAcreage} acres."

        );

        var layer = new FeatureLayer("http://url", {

          infoTemplate: infoTemplate,

          outFields: ["*"],

          opacity: 0.5

        });

       

        // convert squarefeet to acre

       

        calculateAcreage = function(value) {

          var squarefeet = (value.hasOwnProperty("attributes")) ? value.attributes.SHAPE.STArea() : value;

          return number.format(squarefeet / 43560, { places: 2 });

        };    

   

        map.addLayers([layer]);

        function createLegend(results) {

          var legend = new Legend({

            layerInfos: [{

              layer: results.layers[0].layer,

              title: " "

            }],

            map: map

          }, "legend");

          legend.startup();

        }

      });

    </script>

  </head>

  <body>

    <div id="map"></div>

    <div id="info">

      <div style="font-size: 36px;">Acreage of Cooperator Land.</div>

      <div id="legend"></div>

    </div>

  </body>

</html>

0 Kudos
1 Solution

Accepted Solutions
TomSellsted
MVP Regular Contributor

Kushendra,

I couldn't figure out a simple way to access the Shape.STArea() value.  It does seem like you should be able to use it directly.  Maybe someone else will have a solution.

I do have a workaround for you using the geometryEngine.  Call your function from the infoTemplate something like this:

var infoTemplate = new InfoTemplate(
    "${Name}",
    calculateAcreage
);

Add the function:

function calculateAcreage(value) {
    var sqft = geometryEngine.geodesicArea(value.geometry, "square-feet");
    var squarefeet = number.format(sqft, { places: 1 });
    var acres = number.format(sqft / 43560, { places: 2 });
    return squarefeet + " sq.ft is equivalent to " + acres + " acres."
}

I hope this helps!

Regards,

Tom

View solution in original post

0 Kudos
6 Replies
TomSellsted
MVP Regular Contributor

Kushendra,

I couldn't figure out a simple way to access the Shape.STArea() value.  It does seem like you should be able to use it directly.  Maybe someone else will have a solution.

I do have a workaround for you using the geometryEngine.  Call your function from the infoTemplate something like this:

var infoTemplate = new InfoTemplate(
    "${Name}",
    calculateAcreage
);

Add the function:

function calculateAcreage(value) {
    var sqft = geometryEngine.geodesicArea(value.geometry, "square-feet");
    var squarefeet = number.format(sqft, { places: 1 });
    var acres = number.format(sqft / 43560, { places: 2 });
    return squarefeet + " sq.ft is equivalent to " + acres + " acres."
}

I hope this helps!

Regards,

Tom

0 Kudos
KushendraShah1
New Contributor III

Awesome Tom!. That works great. I didn't realize geometry engine is powerful one. I have been digging out all this morning to fix this issue. Thank you so much for the help!. Truly appreciated!!

0 Kudos
TomSellsted
MVP Regular Contributor

Kushendra,

You are welcome! 

Also note that if you are using a typical ArcGIS basemaps, then geodesicArea is correct. If the basemap you are using is for instance in stateplane coordinates, you should use planarArea.

esri/geometry/geometryEngine | API Reference | ArcGIS API for JavaScript

The first paragraph in this link gives a good explanation of which to use.

Best Regards,

Tom

KushendraShah1
New Contributor III

Thanks for the insights Tom.

0 Kudos
JordanBaumgardner
Occasional Contributor III

MS SQL has Geometry functions that might be helpful if you didn't want to preform the calc on the client side.

Select shape.STArea() as Area from sde.COUNTIES

KushendraShah1
New Contributor III

Thanks Jordan.

0 Kudos