Polygon.Centroid

968
8
10-24-2019 12:55 AM
DavidDoyle
New Contributor

Hi,

I have a polygon geometry and am trying to get the centroid.  im using "esri/geometry/Polygon" Property:centroid

When I run the code the centroid is located outside the polygon.

here is part of some test code I wrote to as a check:

 var polygon = new Polygon ({
          rings: [[[153.01608192200001,-27.474277019999988],[153.01597542299999,-27.47412221899998],       [153.01636757699998,-27.473908833999985],[153.01647393400003,-27.474062405999973],[153.01608192200001,-27.474277019999988]]],
          spatialReference: {wkid: 4283}
        });

var pointGraphic = new Graphic({
          geometry: polygon.centroid,
          symbol: markerSymbol,
          spatialReference: {wkid: 4283}
        });

var polygonGraphic = new Graphic({
          geometry: polygon,
          symbol: fillSymbol
        });

view.graphics.addMany([pointGraphic,  polygonGraphic]);

is there somthing im doing wrong.

attached is the point and the poly.

0 Kudos
8 Replies
KenBuja
MVP Honored Contributor

If you reverse the order of the vertices, the centroid shows up in the correct place.

var polygon = new Polygon ({
  rings: [
    [
      [153.01608192200001,-27.474277019999988],
      [153.01647393400003,-27.474062405999973],       
      [153.01636757699998,-27.473908833999985],
      [153.01597542299999,-27.47412221899998],
      [153.01608192200001,-27.474277019999988]
    ]
  ],
  spatialReference: {wkid: 4283}
});
0 Kudos
DavidDoyle
New Contributor

Wow thanks for that.  

Should I be worried about my data.  that ring was pulled from a geometry in a ArcGIS/rest service we have.

so even when I use the geometry straight from the service I get the wrong spot.

I also noticed that labels that are supposed to be centred are off.

Is there a reasons it works in reverse?

Ok I just read about the order of vertices within the rings.  It would seem ours is going in clockwise while the one that works is counter clockwise.  is that correct?  

I just went through and tested a lot of our data, half worked half I hae to reverse the vertices order.  All our data is clockwise.  I just cant see anything wrong with the data.

Could this be a  bug?  as for some it works others it doesn't.

eg this one works. is from the same datasource and is clockwise like the one that doesn't work:

var polygon = new Polygon({         
          rings: [

            [
               [152.97367960999998, -27.444320387999994],
               [152.97383328499996, -27.444315045999986],

               [152.97385016099997,  -27.444676890999972],
               [152.97369640299996, -27.444682263000004],
               [152.97367960999998, -27.444320387999994]
     ]
    ],
          spatialReference: { wkid: 4283 }
        });

0 Kudos
KenBuja
MVP Honored Contributor

Yes, both polygons are evaluating as clockwise using the isClockwise method. It might be a bug that one polygon works correctly while the other doesn't.

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor
0 Kudos
RobertBorchert
Frequent Contributor III

Why are you making this more complicated than it needs to be? Use the tool

Feature to Point

# Import arcpy module
import arcpy


# Local variables:
Mapsheets11x17 = "E:\\mapbase\\TheMaps.gdb\\Mapsheets\\Mapsheets11x17"
Center_shp = "C:\\02Tests\\Center.shp"

# Process: Feature To Point
arcpy.FeatureToPoint_management(Mapsheets11x17, Center_shp, "INSIDE")

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Not sure why you are sharing python code in the Javascript space.

0 Kudos
RobertBorchert
Frequent Contributor III

Even less sure why someone is trying to do this in Java script.  In the amount of time it took to create the post thousands of centroids could have been generated using an existing tool. But thank you for your output.

0 Kudos
DavidDoyle
New Contributor

Thanks for the code snippet,

I should have said I don't have ArcGIS desktop.  I am making a web application with the javascript api. 

One of the tools I have drops a  Point on the centroid of a polygon along with some other information and syncs to a arc web service.  Is why im using javascript.

0 Kudos