'Testing Polygon objects.'

def test_polygons(self):

    prev = fromstr(u'POINT(0    0)')
    for p in self.geometries.polygons:
        poly = fromstr(p.wkt)
        self.assertEqual(poly.geom_type, u'Polygon')
        self.assertEqual(poly.geom_typeid, 3)
        self.assertEqual(poly.empty, False)
        self.assertEqual(poly.ring, False)
        self.assertEqual(p.n_i, poly.num_interior_rings)
        self.assertEqual((p.n_i + 1), len(poly))
        self.assertEqual(p.n_p, poly.num_points)
        self.assertAlmostEqual(p.area, poly.area, 9)
        self.assertAlmostEqual(p.centroid[0], poly.centroid.tuple[0], 9)
        self.assertAlmostEqual(p.centroid[1], poly.centroid.tuple[1], 9)
        self.assertEqual(True, (poly == fromstr(p.wkt)))
        self.assertEqual(False, (poly == prev))
        self.assertEqual(True, (poly != prev))
        ring = poly.exterior_ring
        self.assertEqual(ring.geom_type, u'LinearRing')
        self.assertEqual(ring.geom_typeid, 2)
        if p.ext_ring_cs:
            self.assertEqual(p.ext_ring_cs, ring.tuple)
            self.assertEqual(p.ext_ring_cs, poly[0].tuple)
        self.assertRaises(GEOSIndexError, poly.__getitem__, len(poly))
        self.assertRaises(GEOSIndexError, poly.__setitem__, len(poly), False)
        self.assertRaises(GEOSIndexError, poly.__getitem__, (((-1) * len(poly)) - 1))
        for r in poly:
            self.assertEqual(r.geom_type, u'LinearRing')
            self.assertEqual(r.geom_typeid, 2)
        self.assertRaises(TypeError, Polygon, 0, [1, 2, 3])
        self.assertRaises(TypeError, Polygon, u'foo')
        rings = tuple((r for r in poly))
        self.assertEqual(poly, Polygon(rings[0], rings[1:]))
        ring_tuples = tuple((r.tuple for r in poly))
        self.assertEqual(poly, Polygon(*ring_tuples))
        self.assertEqual(poly.wkt, Polygon(*tuple((r for r in poly))).wkt)
        self.assertEqual(poly.wkt, Polygon(*tuple((LinearRing(r.tuple) for r in poly))).wkt)
