Create polygon from points / rings in 4.3 api

3286
6
Jump to solution
06-13-2017 10:47 AM
JeffSauder
Occasional Contributor

Hi all, I am trying to create a polygon on my view from click points, I take the point and add it to a point collection, then I calculate the rings for the polygon.  I am able to create the polygon with hard coded rings, like in the esri examples, but when I build the array of rings programmatically, it doesn't draw.  I don't get any errors, the polygon just doesn't draw.  I am building the array as an array of objects, I logged the hard coded array to the console to see what the exact structure was, then I created that programmatically.  If I am overlooking something, I can't think of what it could be, the functions are exactly the same, except for the rings assignment.

    function putIDBoxOnMap() {  //draws polygon
      var polyID = new Polygon({
        spatialReference: view.spatialReference,
        rings: [
          [750916.6634999937,856041.7037500741],
          [757750.010500021,856291.7042500751],
          [758416.6785000238,852375.0297500595],
          [751666.6649999968,851791.6952500571],
          [750916.6634999937,856041.7037500741]
        ]
      });
      console.log(polyID.rings);
      polygonGraphic.geometry=polyID;
      polygonGraphic.symbol=simpleFill;
      view.graphics.add(polygonGraphic);
    }

This is the logged array from this function:(I only expanded one of the vertices to save space)

750916.6634999937,856041.7037500741,757750.010500021,856291.7042500751,758416.6785000238,852375.0297500595,750916.6634999937,856041.7037500741
   [
      0: [
         0: [
            0: 750916.6634999937,
            1: 856041.7037500741,
            length: 2
         ],
         1: [ ],
         2: [ ],
         3: [ ],
         length: 4
      ],
      length: 1
   ]


    function putPolyOnMap() {  //does not draw polygon
      var pRings=[];
      var pPtXY=[];
      for (i=0;i<ptCount;i++) {
        pPtXY[0]=ptColl.x;
        pPtXY[1]=ptColl.y;
        pRings=pPtXY;
      }
      pPtXY[0]=ptColl[0].x;
      pPtXY[1]=ptColl[0].y;
      pRings[ptCount]=pPtXY; //closes the polygon
      var pRingsAll=[pRings];
      var idPolygon=new Polygon({
        rings: pRingsAll,
        spatialReference: view.spatialReference
      });
      console.log(pRingsAll);
      polygonGraphic.geometry=idPolygon;
      polygonGraphic.symbol=simpleFill;
      view.graphics.add(polygonGraphic);
    }
  });

Returns this array.

743749.9824999651,857375.0397500795,743749.9824999651,857375.0397500795,743749.9824999651,857375.0397500795,743749.9824999651,857375.0397500795
   [
      0: [
         0: [
            0: 743749.9824999651,
            1: 857375.0397500795,
            length: 2
         ],
         1: [ ],
         2: [ ],
         3: [ ],
         length: 4
      ],
      length: 1
   ]

I cannot see any difference in the structure of these arrays.

Thank you in advance for any help you could provide.

Jeff

0 Kudos
1 Solution

Accepted Solutions
JeffSauder
Occasional Contributor

I did get it to work, I was putting it on a layer that was apparently covered up by another layer, I was able to re order the layers and all works good now.

View solution in original post

0 Kudos
6 Replies
HemingZhu
Occasional Contributor III

please look at the json format of your rings: instead of 

rings: [
          [750916.6634999937,856041.7037500741],
          [757750.010500021,856291.7042500751],
          [758416.6785000238,852375.0297500595],
          [751666.6649999968,851791.6952500571],
          [750916.6634999937,856041.7037500741]
        ]

it should be

rings: [

               [
                   [750916.6634999937,856041.7037500741],
                   [757750.010500021,856291.7042500751],
                   [758416.6785000238,852375.0297500595],
                   [751666.6649999968,851791.6952500571],
                   [750916.6634999937,856041.7037500741]

            ]
        ]

rings are an array itself, each ring have four points. the points should be in rings[0]

0 Kudos
JeffSauder
Occasional Contributor

Thanks for the reply. I did try that, no difference, the polygon draws either way. When I compare the 2 array structures they are still exactly the same.

0 Kudos
ThomasSolow
Occasional Contributor III

Confusingly, it seems that polygon rings can be either of these.  But you'll have to use the second if you want a multi-ring polygon.

0 Kudos
HemingZhu
Occasional Contributor III

If you query a polygon layer from a map service in json format and study it, you will see geometry structure should something look like the following: 

   "geometry": {     "rings": [      [       [        -8586615.6500000004,        4692851.120099999       ],       [        -8586603.4973000009,        4692853.0230000019       ],       [        -8586586.5147999991,        4692856.2589000016       ],       [        -8586564.2438999992,        4692860.3178000003       ],       [        -8586538.3121000007,        4692865.0446000025       ],       [        -8586536.0107000005,        4692853.302199997       ],       [        -8586544.5018000007,        4692851.5055999979       ],       [        -8586578.3147999998,        4692845.3913000003       ],       [        -8586591.8905999996,        4692842.6696999967       ],       [        -8586613.5514000002,        4692838.9178000018       ],       [        -8586615.6500000004,        4692851.120099999       ]      ]     ]    }...
0 Kudos
JeffSauder
Occasional Contributor

I tried exporting the polygon rings from each function to json, and they are formatted that way, and exactly the same…

//From hard coded rings function:

[[[750333.3289999914,852958.3642500618],

,

,

]]

//From calculated rings function:

[[[750333.3289999914,852958.3642500618],

,

,

]]

0 Kudos
JeffSauder
Occasional Contributor

I did get it to work, I was putting it on a layer that was apparently covered up by another layer, I was able to re order the layers and all works good now.

0 Kudos