def test_point_parse(self): """ This test exercises the parsing logic our POINT WKT object @since 1.2 @jira_ticket PYTHON-641 @test_category dse geometric @expected_result We should be able to form POINT objects from properly formatted WKT strings """ # Test basic point ps = "POINT (1.0 2.0)" po = Point.from_wkt(ps) self.assertEqual(po.x, 1.0) self.assertEqual(po.y, 2.0) # Test bad point strings bps = "POIN (1.0 2.0)" with self.assertRaises(ValueError): bpo = Point.from_wkt(bps) bps = "POINT (1.0 2.0 3.0 4.0 5.0" with self.assertRaises(ValueError): bpo = Point.from_wkt(bps) # Points get truncated automatically tps = "POINT (9.0 2.0 3.0 4.0 5.0)" tpo = Point.from_wkt(tps) self.assertEqual(tpo.x, 9.0) self.assertEqual(tpo.y, 2.0) # Test point with NAN ps = "POINT (NAN NAN)" po = Point.from_wkt(ps) self.assertTrue(math.isnan(po.x)) self.assertTrue(math.isnan(po.y))
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_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_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")
class BasicGeometricPointTypeTest(AbstractGeometricTypeTest, BasicGeometricUnitTestCase): """ Runs all the geometric tests against PointType """ cql_type_name = "'{0}'".format(PointType.typename) original_value = Point(.5, .13) @unittest.skip("Empty String") def test_insert_empty_with_string(self): pass @unittest.skip("Empty String") def test_insert_empty_with_object(self): pass
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_geometric_graph_types(self, schema, graphson): """ Test to validate that geometric types function correctly Creates a very simple graph, and tries to insert a simple point type @since 1.0.0 @jira_ticket DSP-8087 @expected_result json types associated with insert is parsed correctly @test_category dse graph """ vertex_label = VertexLabel([('pointP', "Point()")]) ep = self.get_execution_profile(graphson) schema.create_vertex_label(self.session, vertex_label, ep) # import org.apache.cassandra.db.marshal.geometry.Point; rs = schema.add_vertex(self.session, vertex_label, 'pointP', Point(0, 1), ep) # if result set is not parsed correctly this will throw an exception self.assertIsNotNone(rs)
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 deserialize(cls, value, reader=None): return Point.from_wkt(value)
def _convert_point(val): coords = _get_coords(val, 'Point') _validate_point(coords, val) point = Point(*coords) return point
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())
# A map of common types and their corresponding groovy declaration for use in schema creation and insertion MAX_LONG = 9223372036854775807 MIN_LONG = -9223372036854775808 ZERO_LONG = 0 if sys.version_info < (3, 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)]]),