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 deserialize(byts, protocol_version): is_little_endian = bool(_ord(byts[0])) point = point_le if is_little_endian else point_be coords = ((point.unpack_from(byts, offset) for offset in range(1 + 4 + 4, len(byts), point.size)) ) # start = endian + int type + int count return LineString(coords)
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_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_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))
def objectify(cls, v, _): return LineString.from_wkt(v)
MAX_LONG = long(MAX_LONG) MIN_LONG = long(MIN_LONG) ZERO_LONG = long(ZERO_LONG) deserializers = GraphSON1TypeDeserializer()._deserializers TYPE_MAP = { "point1": ["Point()", Point(.5, .13), GraphSON1TypeDeserializer.deserialize_point], "point2": ["Point()", Point(-5, .0), GraphSON1TypeDeserializer.deserialize_point], "linestring1": [ "Linestring()", LineString(((1.0, 2.0), (3.0, 4.0), (-89.0, 90.0))), GraphSON1TypeDeserializer.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)]]), GraphSON1TypeDeserializer.deserialize_polygon ], "smallint1": ["Smallint()", 1, GraphSON1TypeDeserializer.deserialize_smallint], "varint1": ["Varint()", 2147483647, GraphSON1TypeDeserializer.deserialize_varint],
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())
def _convert_linestring(val): points = _get_coords(val, 'LineString') for xy in points: _validate_point(xy, val) linestring = LineString(points) return linestring
def deserialize(cls, value): return LineString.from_wkt(value)