Why is the creation of the arcpy.Polygon object failing for this point array?

3667
12
Jump to solution
01-27-2016 05:41 AM
BKuiper
Occasional Contributor III

Hi,

I have developed two tests to demonstrate the problem

    def test_centroidTestFails(self):

        polygonpoints = [

         [37.310437, -122.244706],

         [37.320437, -122.244706],

         [37.320437, -122.242706],

         [37.310437, -122.242706]]

        points = arcpy.Array([arcpy.Point(*coords) for coords in polygonpoints])

        polygon = arcpy.Polygon(points)

        with self.assertRaises(Exception) as context:

            centroid = polygon.centroid

    def test_centroidTestSucceeds(self):

        polygonpoints = [

         [37.310437, -122.244706],

         [37.320437, -122.244706],

         [37.320437, -122.241706],

         [37.310437, -122.241706]]

        points = arcpy.Array([arcpy.Point(*coords) for coords in polygonpoints])

        polygon = arcpy.Polygon(points)

        centroid = polygon.centroid

Why is the first test failing? For some reason the generated points array can't be used as input for the arcpy.Polygon() constructor. polygon.pointCount will return 0 for the test_centroidTestFails(). What is going wrong? Am i doing something wrong or is this a framework problem?

EDIT:  Note that there is a small difference in the data points that are used for the two tests. The first test fails for no apparent reason and polygon.pointCount will return 0 on this test. The second test works successfully. For some reason the Array of points that is created is not accepted by the polygon constructor in the first test. Adding an additional point to close the polygon is not necessary and doesn't resolve the issue. The polygon will still return 0 points when using the data points from the first test.

EDIT 2: the minor difference is -122.242706 vs. -122.241706 for the last two data points. ( .242 vs .241)

Tags (2)
0 Kudos
12 Replies
BKuiper
Occasional Contributor III

Thank you. That thread was really helpful. It is interesting to see that it actually works when you specify an empty SpatialReference object (thus: polygon = arcpy.Polygon(points, arcpy.SpatialReference()))

At least there is some workaround.

0 Kudos
NeilAyres
MVP Alum

But it is always better to define the coordinate system when creating geometry.

0 Kudos
NeilAyres
MVP Alum

Why would you want to create features with an unknown spatial reference?