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_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_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_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_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())
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 objectify(cls, v, _): return Point.from_wkt(v)
# 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 = 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)]]),
MAKE_STRICT = "schema.config().option('graph.schema_mode').set('production')" MAKE_NON_STRICT = "schema.config().option('graph.schema_mode').set('development')" ALLOW_SCANS = "schema.config().option('graph.allow_scan').set('true')" # 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) TYPE_MAP = { "point1": ["Point()", Point(.5, .13)], "point2": ["Point()", Point(-5, .0)], "linestring1": ["Linestring()", LineString(((1.0, 2.0), (3.0, 4.0), (-89.0, 90.0)))], "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)]]) ], "smallint1": ["Smallint()", 1], "varint1": ["Varint()", 2147483647], "bigint1": ["Bigint()", MAX_LONG],
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 deserialize(byts, protocol_version): is_little_endian = bool(_ord(byts[0])) point = point_le if is_little_endian else point_be return Point(*point.unpack_from(byts, 5)) # ofs = endian byte + int type
def _convert_point(val): coords = _get_coords(val, 'Point') _validate_point(coords, val) point = Point(*coords) return point
def deserialize(cls, value): return Point.from_wkt(value)