Select to view content in your preferred language

Find the length of sides of a polygon

1989
4
10-27-2020 02:02 AM
cadgism
Regular Contributor

Hi All Greetings

   How can I find the length of sides of a polygon ( Not Perimeter of the polygon).  Is the following right approach or is there any simpler approach please ?

1. find the number of rings in the polygon

2. loop through them

3. calculate length from the start point to end point

 

Thank you

0 Kudos
4 Replies
RobertScheitlin__GISP
MVP Emeritus

Almost. Each ring represents its own closed polygon, not a side of a polygon. So once you have the ring (by looping through the rings) you then get the rings points

Each ring is made up of three or more points.

You can use the getPoint method of the polygon class to get the specific point.

https://developers.arcgis.com/javascript/3/jsapi/polygon-amd.html#getpoint 

Then make a new Polyline from two points (i.e ring 0 point 0 and point 1) and use that new polyline in the geodesic or planar length method.

cadgism
Regular Contributor

Thanks. This is what I think I am doing. But keep getting error : "The operation cannot be performed on a non-simple geometry."

function graphicDimension(resultGraphic) {
    require(["esri/geometry/Polyline",
        "esri/tasks/support/LengthsParameters", "esri/tasks/GeometryService", "esri/geometry/Point"],
        function (Polyline, LengthsParameters, GeometryService, Point) {

          for (i = 0; i < resultGraphic.geometry.rings.length; i++) { // value 1 polygon

              for (j = 0; j < resultGraphic.geometry.rings[i].length; j++) { //value 6 points

                  fromPoint = new Point (resultGraphic.geometry.rings[i][j]);
                  toPoint = new Point( resultGraphic.geometry.rings[i][j + 1]);

                  //console.log(fromPoint, toPoint);

                 paths = [
                            [fromPoint.x, fromPoint.y],
                            [toPoint.x, toPoint.y]
                    ]

                polyLine = new Polyline({
                    paths: [paths],
                    spatialReference: app.view.spatialReference
                });

                lengthParams = new LengthsParameters();
                lengthParams.calculationType = "geodesic";
                lengthParams.geodesic = true;
                lengthParams.polylines = [polyLine];
                lengthParams.lengthUnit = "METER"; 

                geomSerVice = new GeometryService(geometryServiceUrl);
                geomSerVice.lengths(lengthParams, function (result) {
                    console.log(result.lengths[0]);
                    alert("length");
                });  

                // debugger; 
             }
        } 
            
           
    });
};‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

That tells me that you have to run the geometry through the simplify method first then.

https://developers.arcgis.com/javascript/3/jsapi/esri.geometry.geometryengine-amd.html#simplify 

cadgism
Regular Contributor

Thank you again, Still did not work . I used the traditional way to find out the length of a line 

√((x_2-x_1)²+(y_2-y_1)²)
0 Kudos