예제 #1
0
class BasicGeometricLineStringTypeTest(AbstractGeometricTypeTest,
                                       BasicGeometricUnitTestCase):
    """
    Runs all the geometric tests against LineStringType
    """
    cql_type_name = cql_type_name = "'{0}'".format(LineStringType.typename)
    original_value = LineString(((1, 2), (3, 4), (9871234, 1235487215)))
    empty_statement = 'LINESTRING EMPTY'
    empty_value = LineString()
 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_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")
    def test_line_parse(self):
        """
        This test exercises the parsing logic our LINESTRING WKT object
        @since 1.2
        @jira_ticket PYTHON-641
        @test_category dse geometric
        @expected_result We should be able to form LINESTRINGS objects from properly formatted WKT strings
        """

        # Test simple line string
        ls = "LINESTRING (1.0 2.0, 3.0 4.0, 5.0 6.0)"
        lo = LineString.from_wkt(ls)
        lo_expected_cords = ((1.0, 2.0), (3.0, 4.0), (5.0, 6.0))
        self.assertEqual(lo.coords, lo_expected_cords)

        # Test very long line string
        long_ls = self._construct_line_string(10000)
        long_lo = LineString.from_wkt(long_ls)
        self.assertEqual(len(long_lo.coords), 10000)
        self.assertEqual(long_lo.coords,
                         self._construct_line_string_expected_cords(10000))

        # Test line string with negative numbers
        ls = "LINESTRING (-1.3 1.2, 3.23 -4.54, 1.34 -9.26)"
        lo = LineString.from_wkt(ls)
        lo_expected_cords = ((-1.3, 1.2), (3.23, -4.54), (1.34, -9.26))
        self.assertEqual(lo.coords, lo_expected_cords)

        # Test bad line strings
        bls = "LINESTRIN (1.0 2.0, 3.0 4.0, 5.0 6.0)"
        with self.assertRaises(ValueError):
            blo = LineString.from_wkt(bls)
        bls = "LINESTRING (1.0 2.0 3.0 4.0 5.0"
        with self.assertRaises(ValueError):
            blo = LineString.from_wkt(bls)

        # Test with NAN
        ls = "LINESTRING (NAN NAN, NAN NAN)"
        lo = LineString.from_wkt(ls)
        self.assertEqual(len(lo.coords), 2)
        for cords in lo.coords:
            for cord in cords:
                self.assertTrue(math.isnan(cord))
예제 #7
0
 def deserialize(cls, value, reader=None):
     return LineString.from_wkt(value)
예제 #8
0
def _convert_linestring(val):
    points = _get_coords(val, 'LineString')
    for xy in points:
        _validate_point(xy, val)
    linestring = LineString(points)
    return linestring
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())
예제 #10
0
    MAX_LONG = long(MAX_LONG)
    MIN_LONG = long(MIN_LONG)
    ZERO_LONG = long(ZERO_LONG)

deserializers = GraphSON1Deserializer()._deserializers

TYPE_MAP = {
    "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":