Ejemplo n.º 1
0
def _convert_polygon(val):
    rings = _get_coords(val, 'Polygon')
    if len(rings) == 0:
        return Polygon([])

    for ring in rings:
        for point in ring:
            _validate_point(point, val)

    polygon = Polygon(exterior=rings[0], interiors=rings[1:])
    return polygon
Ejemplo n.º 2
0
class BasicGeometricPolygonTypeTest(AbstractGeometricTypeTest,
                                    BasicGeometricUnitTestCase):
    """
    Runs all the geometric tests against PolygonType
    """
    cql_type_name = cql_type_name = "'{0}'".format(PolygonType.typename)
    original_value = Polygon([(10.0, 10.0), (110.0, 10.0), (110., 110.0),
                              (10., 110.0), (10., 10.0)], [[(20., 20.0),
                                                            (20., 30.0),
                                                            (30., 30.0),
                                                            (30., 20.0),
                                                            (20., 20.0)],
                                                           [(40., 20.0),
                                                            (40., 30.0),
                                                            (50., 30.0),
                                                            (50., 20.0),
                                                            (40., 20.0)]])
    empty_statement = 'POLYGON EMPTY'
    empty_value = Polygon()
 def test_both_endian(self):
     self._verify_both_endian(PointType, "dd",
                              (WKBGeometryType.POINT, 1, 2), Point(1, 2))
     self._verify_both_endian(
         LineStringType, "Idddddd",
         (WKBGeometryType.LINESTRING, 3, 1, 2, 3, 4, 5, 6),
         LineString(((1, 2), (3, 4), (5, 6))))
     self._verify_both_endian(
         PolygonType, "IIdddddd",
         (WKBGeometryType.POLYGON, 1, 3, 1, 2, 3, 4, 5, 6),
         Polygon(((1, 2), (3, 4), (5, 6))))
 def test_hash(self):
     for geo in (Point(1., 2.), LineString(((1., 2.), (3., 4.), (5., 6.))),
                 _LinearRing(((1., 2.), (3., 4.), (5., 6.))),
                 Polygon([(10.1, 10.0), (110.0, 10.0), (110., 110.0),
                          (10., 110.0), (10., 10.0)], [[(20., 20.0),
                                                        (20., 30.0),
                                                        (30., 30.0),
                                                        (30., 20.0),
                                                        (20., 20.0)],
                                                       [(40., 20.0),
                                                        (40., 30.0),
                                                        (50., 30.0),
                                                        (50., 20.0),
                                                        (40., 20.0)]])):
         self.assertEqual(len(set((geo, geo))), 1)
    def test_polygon_parse(self):
        """
        This test exercises the parsing logic our POLYGON WKT object
        @since 1.2
        @jira_ticket PYTHON-641
        @test_category dse geometric
        @expected_result We should be able to form POLYGON objects from properly formatted WKT strings
        """

        example_poly_string = 'POLYGON ((10.1 10.0, 110.0 10.0, 110.0 110.0, 10.0 110.0, 10.0 10.0), (20.0 20.0, 20.0 30.0, 30.0 30.0, 30.0 20.0, 20.0 20.0), (40.0 20.0, 40.0 30.0, 50.0 30.0, 50.0 20.0, 40.0 20.0))'
        poly_obj = Polygon.from_wkt(example_poly_string)
        expected_ex_coords = ((10.1, 10.0), (110.0, 10.0), (110.0, 110.0),
                              (10.0, 110.0), (10.0, 10.0))
        expected_in_coords_1 = ((20.0, 20.0), (20.0, 30.0), (30.0, 30.0),
                                (30.0, 20.0), (20.0, 20.0))
        expected_in_coords_2 = ((40.0, 20.0), (40.0, 30.0), (50.0, 30.0),
                                (50.0, 20.0), (40.0, 20.0))
        self.assertEqual(poly_obj.exterior.coords, expected_ex_coords)
        self.assertEqual(len(poly_obj.interiors), 2)
        self.assertEqual(poly_obj.interiors[0].coords, expected_in_coords_1)
        self.assertEqual(poly_obj.interiors[1].coords, expected_in_coords_2)

        # Test with very long polygon
        long_poly_string = self._construct_polygon_string(10000)
        long_poly_obj = Polygon.from_wkt(long_poly_string)
        self.assertEqual(len(long_poly_obj.exterior.coords), 10000)
        #for expected, recieved in zip(self._construct_line_string_expected_cords(10000), long_poly_obj.exterior.coords):
        #    self.assertEqual(expected, recieved)
        self.assertEqual(long_poly_obj.exterior.coords,
                         self._construct_line_string_expected_cords(10000))

        # Test bad polygon strings
        bps = "POLYGONE ((30 10, 40 40, 20 40, 10 20, 30 10))"
        with self.assertRaises(ValueError):
            bpo = Polygon.from_wkt(bps)
        bps = "POLYGON (30 10, 40 40, 20 40, 10 20, 30 10)"
        with self.assertRaises(ValueError):
            bpo = Polygon.from_wkt(bps)

        # Polygons get truncated automatically
        ps = "POLYGON ((30 10, 40 40, 20, 10 20, 30))"
        po = Polygon.from_wkt(ps)
        expected_ex_coords = ((30.0, 10.0), (40.0, 40.0), (20.0, ),
                              (10.0, 20.0), (30.0, ))
        self.assertEqual(po.exterior.coords, expected_ex_coords)

        # Test Polygon with NAN
        ps = "POLYGON ((NAN NAN, NAN NAN, NAN NAN, NAN, NAN))"
        po = Polygon.from_wkt(ps)
        for cords in po.exterior.coords:
            for cord in cords:
                self.assertTrue(math.isnan(cord))
    def test_eq(self):
        for geo in (Point(1., 2.), LineString(((1., 2.), (3., 4.), (5., 6.))),
                    _LinearRing(((1., 2.), (3., 4.), (5., 6.))),
                    Polygon([(10.1, 10.0), (110.0, 10.0), (110., 110.0),
                             (10., 110.0), (10., 10.0)], [[(20., 20.0),
                                                           (20., 30.0),
                                                           (30., 30.0),
                                                           (30., 20.0),
                                                           (20., 20.0)],
                                                          [(40., 20.0),
                                                           (40., 30.0),
                                                           (50., 30.0),
                                                           (50., 20.0),
                                                           (40., 20.0)]])):
            # same type
            self.assertEqual(geo, geo)

            # does not blow up on other types
            # specifically use assertFalse(eq) to make sure we're using the geo __eq__ operator
            self.assertFalse(geo == object())
    def test_str_wkt(self):
        self.assertEqual(str(Point(1., 2.)), 'POINT (1.0 2.0)')
        self.assertEqual(str(Point()), "POINT (nan nan)")
        self.assertEqual(str(LineString(((1., 2.), (3., 4.), (5., 6.)))),
                         'LINESTRING (1.0 2.0, 3.0 4.0, 5.0 6.0)')
        self.assertEqual(str(_LinearRing(((1., 2.), (3., 4.), (5., 6.)))),
                         'LINEARRING (1.0 2.0, 3.0 4.0, 5.0 6.0)')
        self.assertEqual(
            str(
                Polygon([(10.1, 10.0), (110.0, 10.0), (110., 110.0),
                         (10., 110.0), (10., 10.0)],
                        [[(20., 20.0), (20., 30.0), (30., 30.0), (30., 20.0),
                          (20., 20.0)],
                         [(40., 20.0), (40., 30.0), (50., 30.0), (50., 20.0),
                          (40., 20.0)]])),
            'POLYGON ((10.1 10.0, 110.0 10.0, 110.0 110.0, 10.0 110.0, 10.0 10.0), (20.0 20.0, 20.0 30.0, 30.0 30.0, 30.0 20.0, 20.0 20.0), (40.0 20.0, 40.0 30.0, 50.0 30.0, 50.0 20.0, 40.0 20.0))'
        )

        class LinearRing(_LinearRing):
            pass

        for cls in (LineString, LinearRing, Polygon):
            self.assertEqual(str(cls()), cls.__name__.upper() + " EMPTY")
Ejemplo n.º 8
0
 def deserialize(cls, value, reader=None):
     return Polygon.from_wkt(value)
class GeoTypes(unittest.TestCase):

    samples = (Point(1, 2), LineString(((1, 2), (3, 4), (5, 6))),
               Polygon([(10.1, 10.0), (110.0, 10.0), (110., 110.0),
                        (10., 110.0), (10., 10.0)], [[(20., 20.0), (20., 30.0),
                                                      (30., 30.0), (30., 20.0),
                                                      (20., 20.0)],
                                                     [(40., 20.0), (40., 30.0),
                                                      (50., 30.0), (50., 20.0),
                                                      (40., 20.0)]]))

    def test_marshal_platform(self):
        for proto_ver in protocol_versions:
            for geo in self.samples:
                cql_type = lookup_casstype(geo.__class__.__name__ + 'Type')
                self.assertEqual(
                    cql_type.from_binary(cql_type.to_binary(geo, proto_ver),
                                         proto_ver), geo)

    def _verify_both_endian(self, typ, body_fmt, params, expected):
        for proto_ver in protocol_versions:
            self.assertEqual(
                typ.from_binary(struct.pack(">BI" + body_fmt, wkb_be, *params),
                                proto_ver), expected)
            self.assertEqual(
                typ.from_binary(struct.pack("<BI" + body_fmt, wkb_le, *params),
                                proto_ver), expected)

    def test_both_endian(self):
        self._verify_both_endian(PointType, "dd",
                                 (WKBGeometryType.POINT, 1, 2), Point(1, 2))
        self._verify_both_endian(
            LineStringType, "Idddddd",
            (WKBGeometryType.LINESTRING, 3, 1, 2, 3, 4, 5, 6),
            LineString(((1, 2), (3, 4), (5, 6))))
        self._verify_both_endian(
            PolygonType, "IIdddddd",
            (WKBGeometryType.POLYGON, 1, 3, 1, 2, 3, 4, 5, 6),
            Polygon(((1, 2), (3, 4), (5, 6))))

    def test_empty_wkb(self):
        for cls in (LineString, Polygon):
            class_name = cls.__name__
            cql_type = lookup_casstype(class_name + 'Type')
            self.assertEqual(
                str(cql_type.from_binary(cql_type.to_binary(cls(), 0), 0)),
                class_name.upper() + " EMPTY")
        self.assertEqual(
            str(PointType.from_binary(PointType.to_binary(Point(), 0), 0)),
            "POINT (nan nan)")

    def test_str_wkt(self):
        self.assertEqual(str(Point(1., 2.)), 'POINT (1.0 2.0)')
        self.assertEqual(str(Point()), "POINT (nan nan)")
        self.assertEqual(str(LineString(((1., 2.), (3., 4.), (5., 6.)))),
                         'LINESTRING (1.0 2.0, 3.0 4.0, 5.0 6.0)')
        self.assertEqual(str(_LinearRing(((1., 2.), (3., 4.), (5., 6.)))),
                         'LINEARRING (1.0 2.0, 3.0 4.0, 5.0 6.0)')
        self.assertEqual(
            str(
                Polygon([(10.1, 10.0), (110.0, 10.0), (110., 110.0),
                         (10., 110.0), (10., 10.0)],
                        [[(20., 20.0), (20., 30.0), (30., 30.0), (30., 20.0),
                          (20., 20.0)],
                         [(40., 20.0), (40., 30.0), (50., 30.0), (50., 20.0),
                          (40., 20.0)]])),
            'POLYGON ((10.1 10.0, 110.0 10.0, 110.0 110.0, 10.0 110.0, 10.0 10.0), (20.0 20.0, 20.0 30.0, 30.0 30.0, 30.0 20.0, 20.0 20.0), (40.0 20.0, 40.0 30.0, 50.0 30.0, 50.0 20.0, 40.0 20.0))'
        )

        class LinearRing(_LinearRing):
            pass

        for cls in (LineString, LinearRing, Polygon):
            self.assertEqual(str(cls()), cls.__name__.upper() + " EMPTY")

    def test_repr(self):
        for geo in (Point(1., 2.), LineString(((1., 2.), (3., 4.), (5., 6.))),
                    _LinearRing(((1., 2.), (3., 4.), (5., 6.))),
                    Polygon([(10.1, 10.0), (110.0, 10.0), (110., 110.0),
                             (10., 110.0), (10., 10.0)], [[(20., 20.0),
                                                           (20., 30.0),
                                                           (30., 30.0),
                                                           (30., 20.0),
                                                           (20., 20.0)],
                                                          [(40., 20.0),
                                                           (40., 30.0),
                                                           (50., 30.0),
                                                           (50., 20.0),
                                                           (40., 20.0)]])):
            self.assertEqual(eval(repr(geo)), geo)

    def test_hash(self):
        for geo in (Point(1., 2.), LineString(((1., 2.), (3., 4.), (5., 6.))),
                    _LinearRing(((1., 2.), (3., 4.), (5., 6.))),
                    Polygon([(10.1, 10.0), (110.0, 10.0), (110., 110.0),
                             (10., 110.0), (10., 10.0)], [[(20., 20.0),
                                                           (20., 30.0),
                                                           (30., 30.0),
                                                           (30., 20.0),
                                                           (20., 20.0)],
                                                          [(40., 20.0),
                                                           (40., 30.0),
                                                           (50., 30.0),
                                                           (50., 20.0),
                                                           (40., 20.0)]])):
            self.assertEqual(len(set((geo, geo))), 1)

    def test_eq(self):
        for geo in (Point(1., 2.), LineString(((1., 2.), (3., 4.), (5., 6.))),
                    _LinearRing(((1., 2.), (3., 4.), (5., 6.))),
                    Polygon([(10.1, 10.0), (110.0, 10.0), (110., 110.0),
                             (10., 110.0), (10., 10.0)], [[(20., 20.0),
                                                           (20., 30.0),
                                                           (30., 30.0),
                                                           (30., 20.0),
                                                           (20., 20.0)],
                                                          [(40., 20.0),
                                                           (40., 30.0),
                                                           (50., 30.0),
                                                           (50., 20.0),
                                                           (40., 20.0)]])):
            # same type
            self.assertEqual(geo, geo)

            # does not blow up on other types
            # specifically use assertFalse(eq) to make sure we're using the geo __eq__ operator
            self.assertFalse(geo == object())
Ejemplo n.º 10
0
 "point1":
 ["Point()",
  Point(.5, .13), GraphSON1Deserializer.deserialize_point],
 "point2":
 ["Point()",
  Point(-5, .0), GraphSON1Deserializer.deserialize_point],
 "linestring1": [
     "Linestring()",
     LineString(((1.0, 2.0), (3.0, 4.0), (-89.0, 90.0))),
     GraphSON1Deserializer.deserialize_linestring
 ],
 "polygon1": [
     "Polygon()",
     Polygon([(10.0, 10.0), (80.0, 10.0), (80., 88.0), (10., 89.0),
              (10., 10.0)], [[(20., 20.0), (20., 30.0), (30., 30.0),
                              (30., 20.0), (20., 20.0)],
                             [(40., 20.0), (40., 30.0), (50., 30.0),
                              (50., 20.0), (40., 20.0)]]),
     GraphSON1Deserializer.deserialize_polygon
 ],
 "smallint1": ["Smallint()", 1, GraphSON1Deserializer.deserialize_smallint],
 "varint1":
 ["Varint()", 2147483647, GraphSON1Deserializer.deserialize_varint],
 "bigint1":
 ["Bigint()", MAX_LONG, GraphSON1Deserializer.deserialize_bigint],
 "bigint2":
 ["Bigint()", MIN_LONG, GraphSON1Deserializer.deserialize_bigint],
 "bigint3":
 ["Bigint()", ZERO_LONG, GraphSON1Deserializer.deserialize_bigint],
 "int1": ["Int()", 100, GraphSON1Deserializer.deserialize_int],
 "float1": ["Float()", .5, GraphSON1Deserializer.deserialize_float],