I have the following line of code:
this.centerPoint = this.graphic.geometry.getCentroid();
When I run the program and step through the code, the value of geometry.rings is:
[-81.63820933449949,32.72321758156744,-81.6381280506363,32.72321758156744,-81.6381280506363,32.723341259885146,-81.63820933449949,32.723341259885146,-81.63820933449949,32.72321758156744]
But when I examine the X and Y values of the resultant centerpoint variable, they look opposite from what I would expect:
X = 81.6392691466801
Y = -32.7237205172898
Which would move the geometry from the southeastern U.S. to somewhere in the Indian ocean. Obviously I'm doing something wrong. Any guesses?
Solved! Go to Solution.
Dave,
It looks like you guys are using some custom library that parses shapefiles. The likely issue is that the portion of the custom code that is creating the polygon is not explicitly defining the SpatialReference for the geometry. You need to use the setspatialreference on the geometry and assign wkid 4326 like Panagiotis shows in his snippet above.
Polygon | API Reference | ArcGIS API for JavaScript | setSpatialReference
I burrowed into the shapeLoader function and this is the code that is used to create the polygon:
//parse as polygon
coords = shapeWkt.split(this.PolyRegex);
var ringData = [];
for (idxCoord = 3; idxCoord < coords.length - 2; idxCoord += 3) {
ringData[(idxCoord - 3) / 3] = [parseFloat(coords[idxCoord]), parseFloat(coords[idxCoord + 1])]
}
//build the shape
shape = new esri.geometry.Polygon(new esri.SpatialReference({ wkid: srid }));
shape.addRing(ringData);
return shape;
the value of the srid variable is 4326. The only difference I could see between our code and Panagiotis' example is that I didn't have wkid in quotes. I tried changing that, but it didn't help.
so far none of that accounts for the switching of the sign in the coordinates, I would look for a -1 or sign multiplier somewhere and whip in some print statements (dont know if it is possible) at various stages where the geometry is being handled.
Dave,
That looks good then. I am not sure what is going on with the centroid then. Does the polygon display is the correct location on the map?
It sure does - or so I assume it does. I use a separate tool to draw the polygon on the map and it draws right where it's supposed to draw. I haven't rigorously checked it, but the coordinates of the polygon at least smell right.
Hello Dave,
Your coordinates looks like is in counter-clockwise direction. The documentation in the addRing method says:
add points counter-clockwise to create a hole
May be thats what is causing the problem. At this point I am not sure if the Polygon constructor behaves the same way as the addRing does. as It seem you are getting the right centriod when creating the geometry as part of json parameter.
Hope this was helpful.
Thejus
That was it, Thejus. I reversed the array and now the centroid is working fine.
However... have I broken anything else in the app? That is the question. Gonna have to bounce this one off my dev lead and see what he says.
In any case - you get credit for the answer - and thanks to everyone else who kept pushing this until the answer was found.
That should not break the app. But to be on safer side. There is a method to check if it is clockwise or not before flipping the coords.
var polygon = new Polygon( ... ); var clockwise = polygon.isClockwise(ringData); if(clockwise){ polygon.addRing(ringData); } else { //flip the coords here polygon.addRing(ringData); }
So let me see if I have this correct... a incorrectly oriented polygon would produce a negative area, from which the centroid coordinates are calculated incorrectly, without consideration of the input coordinate space at all. If anybody knows, what geometry engine is being used to produce this affect?
Dan,
This is the JS API. I would have never guessed that a reversed poly out produce a bad centroid.