def add_geo_data(connection): pre = '{"type": "Point", "coordinates"' suf = ']}' for i in range(10): lng = 1220 - (2 * i) lat = 375 + (2 * i) key = ('test', 'demo', i) s = "{0}: [-{1}.{2}, {3}.{4}{5}".format(pre, (lng // 10), (lng % 10), (lat // 10), (lat % 10), suf) geo_object = aerospike.geojson(s) geo_list = [geo_object] geo_map_key = {geo_object: i} geo_map_val = {i: geo_object} connection.put( key, { "loc": geo_object, 'geo_list': geo_list, 'geo_map_keys': geo_map_key, 'geo_map_vals': geo_map_val }) key = ('test', 'demo', 'polygon') geo_object_polygon = aerospike.GeoJSON({ "type": "Polygon", "coordinates": [[[-122.500000, 37.000000], [-121.000000, 37.000000], [-121.000000, 38.080000], [-122.500000, 38.080000], [-122.500000, 37.000000]]] }) geo_loc_list = [geo_object_polygon] geo_loc_mk = {geo_object_polygon: 1} geo_loc_mv = {2: geo_object_polygon} connection.put( key, { "loc_polygon": geo_object_polygon, 'geo_loc_list': geo_loc_list, 'geo_loc_mk': geo_loc_mk, 'geo_loc_mv': geo_loc_mv }) key = ('test', 'demo', 'polygon2') geo_object_polygon = aerospike.GeoJSON({ "type": "Polygon", "coordinates": [[[-52.500000, 37.000000], [-51.000000, 37.000000], [-51.000000, 38.080000], [-52.500000, 38.080000], [-52.500000, 37.000000]]] }) geo_loc_list = [geo_object_polygon] geo_loc_mk = {geo_object_polygon: 1} geo_loc_mv = {2: geo_object_polygon} connection.put( key, { "loc_polygon": geo_object_polygon, 'geo_loc_list': geo_loc_list, 'geo_loc_mk': geo_loc_mk, 'geo_loc_mv': geo_loc_mv })
def test_geospatial_put_get_positive(self): """ Perform a get and put with multiple bins including geospatial bin """ key = ('test', 'demo', 'single_geo_put') geo_object_single = aerospike.GeoJSON( {"type": "Point", "coordinates": [42.34, 58.62]}) geo_object_dict = aerospike.GeoJSON( {"type": "Point", "coordinates": [56.34, 69.62]}) self.as_connection.put(key, {"loc": geo_object_single, "int_bin": 2, "string_bin": "str", "dict_bin": {"a": 1, "b": 2, "geo": geo_object_dict}}) key, _, bins = self.as_connection.get(key) expected = {'loc': {'coordinates': [42.34, 58.62], 'type': 'Point'}, "int_bin": 2, "string_bin": "str", "dict_bin": {"a": 1, "b": 2, "geo": {'coordinates': [56.34, 69.62], 'type': 'Point'}}} for b in bins: assert b in expected self.as_connection.remove(key)
def setup(self, request, as_connection): keys = [] for i in range(5): key = ('test', u'demo', i) rec = { 'name': 'name%s' % (str(i)), 'addr': 'name%s' % (str(i)), 'numeric_list': [1, 2, 3, 4], 'string_list': ["a", "b", "c", "d"], 'geojson_list': [aerospike.GeoJSON({"type": "Point", "coordinates": [-122.096449, 37.421868]}), aerospike.GeoJSON({"type": "Point", "coordinates": [-122.053321, 37.434212]})], 'age': i, 'no': i } as_connection.put(key, rec) keys.append(key) def teardown(): """ Teardown method. """ for key in keys: try: as_connection.remove(key) except e.RecordNotFound: pass request.addfinalizer(teardown)
def test_geospatial_positive_query(self): """ Perform a positive geospatial query for a polygon """ records = [] query = self.as_connection.query("test", "demo") geo_object2 = aerospike.GeoJSON({"type": "Polygon", "coordinates": [[ [-122.500000, 37.000000], [-121.000000, 37.000000], [-121.000000, 38.080000], [-122.500000, 38.080000], [-122.500000, 37.000000]]]}) query.where(p.geo_within_geojson_region("loc", geo_object2.dumps())) def callback(input_tuple): _, _, record = input_tuple records.append(record) query.foreach(callback) assert len(records) == 3 expected = [{"coordinates": [-122.0, 37.5], "type": "Point"}, {"coordinates": [-121.8, 37.7], "type": "Point"}, {"coordinates": [-121.6, 37.9], "type": "Point"}] for r in records: assert r['loc'].unwrap() in expected
def test_geospatial_positive_query_for_circle(self): """ Perform a positive geospatial query for a circle """ if TestGeospatial.skip_old_server is True: pytest.skip( "Server does not support apply on AeroCircle for GeoJSON") records = [] query = TestGeospatial.client.query("test", "demo") geo_object2 = aerospike.GeoJSON({ "type": "AeroCircle", "coordinates": [[-122.0, 37.5], 250.2] }) query.where(p.geo_within_geojson_region("loc", geo_object2.dumps())) def callback(input_tuple): _, _, record = input_tuple records.append(record) query.foreach(callback) assert len(records) == 1 expected = [{'coordinates': [-122.0, 37.5], 'type': 'Point'}] for r in records: assert r['loc'].unwrap() in expected
def test_pos_operate_incr_with_geospatial_new_record(self): """ Invoke operate() with incr command on a new record """ key = ('test', 'demo', 'geospatial_key') llist = [{ "op": aerospike.OPERATOR_INCR, "bin": "geospatial", "val": aerospike.GeoJSON({ "type": "Point", "coordinates": [42.34, 58.62] }) }, { "op": aerospike.OPERATOR_READ, "bin": "geospatial" }] (key, _, bins) = TestOperate.client_no_typechecks.operate(key, llist) assert bins['geospatial'].unwrap() == { 'coordinates': [42.34, 58.62], 'type': 'Point' } TestOperate.client_no_typechecks.remove(key)
def add_sign(self, info, features): location = aerospike.GeoJSON( { 'type': 'Point', 'coordinates': [info.longitude, info.latitude] } ) # Normailize features vector qsum = 0 for fea in features: qsum = qsum + fea * fea qsum = math.sqrt(qsum) for i in range(len(features)): features[i] = features[i] / qsum # aerospike udf cant handle int64 # so use strings instead # TODO try to split int64 into pair of int32 bins record = { 'user_id': str(info.user_id), 'sign_id': str(info.sign_id), 'location': location, 'features': features, 'is_private': int(info.is_private) } self._aerospike_connector.put_bins((self._namespace, self._global_set, str(info.sign_id)), record)
def test_geospatial_wrap_positive(self): """ Perform a positive wrap on geospatial data """ geo_object = aerospike.GeoJSON({ "type": "Polygon", "coordinates": [[[-124.500000, 37.000000], [-125.000000, 37.000000], [-121.000000, 38.080000], [-122.500000, 38.080000], [-124.500000, 37.000000]]] }) geo_object.wrap({ "type": "Polygon", "coordinates": [[[-122.500000, 37.000000], [-121.000000, 37.000000], [-121.000000, 38.080000], [-122.500000, 38.080000], [-122.500000, 37.000000]]] }) assert geo_object.unwrap() == { 'coordinates': [[[-122.5, 37.0], [-121.0, 37.0], [-121.0, 38.08], [-122.5, 38.08], [-122.5, 37.0]]], 'type': 'Polygon' }
def test_geospatial_positive_query_outside_shape(self): """ Perform a positive geospatial query for polygon where all points are outside polygon """ records = [] query = self.as_connection.query("test", "demo") geo_object2 = aerospike.GeoJSON({ "type": "Polygon", "coordinates": [[[-126.500000, 37.000000], [-124.000000, 37.000000], [-124.000000, 38.080000], [-126.500000, 38.080000], [-126.500000, 37.000000]]] }) query.where(p.geo_within_geojson_region("loc", geo_object2.dumps())) def callback(input_tuple): _, _, record = input_tuple records.append(record) query.foreach(callback) assert len(records) == 0
def test_geospatial_positive_query_with_point(self): """ Perform a positive geospatial query for a point """ records = [] query = self.as_connection.query("test", "demo") geo_object2 = aerospike.GeoJSON({ "type": "Point", "coordinates": [-121.700000, 37.200000] }) query.where( p.geo_contains_geojson_point("loc_polygon", geo_object2.dumps())) def callback(input_tuple): _, _, record = input_tuple records.append(record) query.foreach(callback) assert len(records) == 1 expected = [{ 'coordinates': [[[-122.500000, 37.000000], [-121.000000, 37.000000], [-121.000000, 38.080000], [-122.500000, 38.080000], [-122.500000, 37.000000]]], 'type': 'Polygon' }] for r in records: assert r['loc_polygon'].unwrap() in expected
def search_region(self, user_id, lat1, lon1, lat2, lon2, callback): user_id_str = str(user_id) region = aerospike.GeoJSON( { 'type': "Polygon", 'coordinates': [[ [lon1, lat1], [lon2, lat1], [lon2, lat2], [lon1, lat2], [lon1, lat1] ]] } ).dumps() # Public query = self._aerospike_connector._client.query(self._namespace, self._global_set) predicate = aerospike.predicates.geo_within_geojson_region('location', region) query.where(predicate) query.apply('search', 'apply_access_filter', [user_id_str]) query.foreach(callback) # Private query = self._aerospike_connector._client.query(self._namespace, self._private_set) predicate = aerospike.predicates.geo_within_geojson_region('location', region) query.where(predicate) query.apply('search', 'apply_access_filter', [user_id_str]) query.foreach(callback)
def test_geospatial_positive_query_with_point_outside_aerocircle(self): """ Perform a positive geospatial query for a point in aerocircle """ if TestGeospatial.skip_old_server is True: pytest.skip( "Server does not support apply on AeroCircle for GeoJSON") records = [] query = self.as_connection.query("test", "demo") geo_object2 = aerospike.GeoJSON({ "type": "Point", "coordinates": [-122.000000, 450.200] }) query.where( p.geo_contains_geojson_point("loc_circle", geo_object2.dumps())) def callback(input_tuple): _, _, record = input_tuple records.append(record) query.foreach(callback) assert len(records) == 0
def test_geospatial_positive_query_with_point_in_aerocircle_int(self): """ Perform a positive geospatial query for a point in aerocircle """ if TestGeospatial.skip_old_server is True: pytest.skip( "Server does not support apply on AeroCircle for GeoJSON") records = [] query = self.as_connection.query("test", "demo") geo_object2 = aerospike.GeoJSON({ "type": "Point", "coordinates": [-122, 37] }) query.where( p.geo_contains_geojson_point("loc_circle", geo_object2.dumps())) def callback(input_tuple): _, _, record = input_tuple records.append(record) query.foreach(callback) assert len(records) == 1 expected = [{ 'coordinates': [[-122.0, 37.0], 250.2], 'type': 'AeroCircle' }] for r in records: assert r['loc_circle'].unwrap() in expected
def test_pos_operate_ordered_incr_with_geospatial_new_record(self): """ Invoke operate_ordered() with incr command with geospatial data """ key = ('test', 'demo', 'geospatial_key') llist = [{ "op": aerospike.OPERATOR_INCR, "bin": "geospatial", "val": aerospike.GeoJSON({ "type": "Point", "coordinates": [42.34, 58.62] }) }, { "op": aerospike.OPERATOR_READ, "bin": "geospatial" }] (key, _, bins) = TestOperateOrdered.client_no_typechecks.\ operate_ordered(key, llist) assert bins[0] is None assert bins[1][1].unwrap() == { "type": "Point", "coordinates": [42.34, 58.62] } TestOperateOrdered.client_no_typechecks.remove(key)
def test_geospatial_operate_positive(self): """ Perform an operate operation with geospatial bin """ geo_object_operate = aerospike.GeoJSON({ "type": "Point", "coordinates": [43.45, 56.75] }) key = ('test', 'demo', 'single_geo_operate') llist = [{ "op": aerospike.OPERATOR_WRITE, "bin": "write_bin", "val": { "no": geo_object_operate } }, { "op": aerospike.OPERATOR_READ, "bin": "write_bin" }] key, _, bins = self.as_connection.operate(key, llist) self.keys.append(key) assert type(bins['write_bin']['no']) == aerospike.GeoJSON assert bins['write_bin']['no'].unwrap() == { 'coordinates': [43.45, 56.75], 'type': 'Point' }
def test_geospatial_object_non_json_serializable_string(self): """ The geospatial object is not a json serializable string """ with pytest.raises(e.ClientError) as err_info: aerospike.GeoJSON("abc") err_code = err_info.value.code assert err_code == AerospikeStatus.AEROSPIKE_ERR_CLIENT
def test_geospatial_object_not_dict_or_string(self): """ The geospatial object is not a dictionary or string """ with pytest.raises(e.ParamError) as err_info: aerospike.GeoJSON(1) err_code = err_info.value.code assert err_code == AerospikeStatus.AEROSPIKE_ERR_PARAM
def get_geo_object(): geo_object = aerospike.GeoJSON({ "type": "Polygon", "coordinates": [[[-124.500000, 37.000000], [-125.000000, 37.000000], [-121.000000, 38.080000], [-122.500000, 38.080000], [-124.500000, 37.000000]]] }) return geo_object
def test_geospatial_object_non_json_serializable_string(self): """ The geospatial object is not a json serializable string """ try: aerospike.GeoJSON("abc") except e.ClientError as exception: assert exception.code == -1 assert exception.msg == 'String is not GeoJSON serializable'
def test_geospatial_object_not_dict_or_string(self): """ The geospatial object is not a dictionary or string """ try: aerospike.GeoJSON(1) except e.ParamError as exception: assert exception.code == -2 assert exception.msg == 'Geospatial data should be a dictionary or raw GeoJSON string'
def test_geospatial_positive_query_without_set(self): """ Perform a positive geospatial query for a polygon without a set """ keys = [] pre = '{"type": "Point", "coordinates"' suf = ']}' for i in range(1, 10): lng = 1220 - (2 * i) lat = 375 + (2 * i) key = ('test', None, i) s = "{0}: [-{1}.{2}, {3}.{4}{5}".format(pre, (lng // 10), (lng % 10), (lat // 10), (lat % 10), suf) geo_object = aerospike.geojson(s) self.as_connection.put(key, {"loc": geo_object}) keys.append(key) self.as_connection.index_geo2dsphere_create("test", None, "loc", "loc_index_no_set") records = [] query = self.as_connection.query("test", None) geo_object2 = aerospike.GeoJSON({ "type": "Polygon", "coordinates": [[[-122.500000, 37.000000], [-121.000000, 37.000000], [-121.000000, 38.080000], [-122.500000, 38.080000], [-122.500000, 37.000000]]] }) query.where(p.geo_within_geojson_region("loc", geo_object2.dumps())) def callback(input_tuple): _, _, record = input_tuple records.append(record) query.foreach(callback) self.as_connection.index_remove('test', 'loc_index_no_set') for key in keys: self.as_connection.remove(key) assert len(records) == 2 expected = [{ 'coordinates': [-121.8, 37.7], 'type': 'Point' }, { 'coordinates': [-121.6, 37.9], 'type': 'Point' }] for r in records: assert r['loc'].unwrap() in expected
def test_geospatial_wrap_positive_with_query(self): """ Perform a positive wrap on geospatial data followed by a query """ geo_object_wrap = aerospike.GeoJSON({ "type": "Polygon", "coordinates": [[[-124.500000, 37.000000], [-125.000000, 37.000000], [-121.000000, 38.080000], [-122.500000, 38.080000], [-124.500000, 37.000000]]] }) geo_object_wrap.wrap({ "type": "Polygon", "coordinates": [[[-122.500000, 37.000000], [-121.000000, 37.000000], [-121.000000, 38.080000], [-122.500000, 38.080000], [-122.500000, 37.000000]]] }) assert geo_object_wrap.unwrap() == { 'coordinates': [[[-122.5, 37.0], [-121.0, 37.0], [-121.0, 38.08], [-122.5, 38.08], [-122.5, 37.0]]], 'type': 'Polygon' } records = [] query = TestGeospatial.client.query("test", "demo") query.where(p.geo_within_geojson_region("loc", geo_object_wrap.dumps())) def callback(input_tuple): _, _, record = input_tuple records.append(record) query.foreach(callback) assert len(records) == 3 expected = [{ 'coordinates': [-122.0, 37.5], 'type': 'Point' }, { 'coordinates': [-121.8, 37.7], 'type': 'Point' }, { 'coordinates': [-121.6, 37.9], 'type': 'Point' }] for r in records: assert r['loc'].unwrap() in expected
def setup_method(self, method): self.keys = [] pre = '{"type": "Point", "coordinates"' suf = ']}' for i in range(10): lng = 1220 - (2 * i) lat = 375 + (2 * i) key = ('test', 'demo', i) s = "{0}: [-{1}.{2}, {3}.{4}{5}".format(pre, (lng // 10), (lng % 10), (lat // 10), (lat % 10), suf) self.geo_object = aerospike.geojson(s) TestGeospatial.client.put(key, {"loc": self.geo_object}) self.keys.append(key) key = ('test', 'demo', 'polygon') self.geo_object_polygon = aerospike.GeoJSON({ "type": "Polygon", "coordinates": [[[-122.500000, 37.000000], [-121.000000, 37.000000], [-121.000000, 38.080000], [-122.500000, 38.080000], [-122.500000, 37.000000]]] }) TestGeospatial.client.put(key, {"loc_polygon": self.geo_object_polygon}) self.keys.append(key) if not TestGeospatial.skip_old_server: key = ('test', 'demo', 'circle') geo_circle = aerospike.GeoJSON({ "type": "AeroCircle", "coordinates": [[-122.0, 37.0], 250.2] }) TestGeospatial.client.put(key, {"loc_circle": geo_circle}) self.keys.append(key)
def setup(self, request, connection_with_config_funcs): as_connection = connection_with_config_funcs self.keys = [] if not self.skip_old_server: key = ('test', 'demo', 'circle') geo_circle = aerospike.GeoJSON( {"type": "AeroCircle", "coordinates": [[-122.0, 37.0], 250.2]}) as_connection.put(key, {"loc_circle": geo_circle}) self.keys.append(key) def teardown(): for key in self.keys: as_connection.remove(key) request.addfinalizer(teardown)
def test_geospatial_positive_query_with_point_outside_polygon(self): """ Perform a positive geospatial query for a point outside polygon """ records = [] query = self.as_connection.query("test", "demo") geo_object2 = aerospike.GeoJSON({"type": "Point", "coordinates": [-123.700000, 37.200000]}) query.where( p.geo_contains_geojson_point("loc_polygon", geo_object2.dumps())) def callback(input_tuple): _, _, record = input_tuple records.append(record) query.foreach(callback) assert len(records) == 0
def test_geospatial_contains_json_point_pred(self, bin_name, idx_type): records = [] query = self.as_connection.query("test", "demo") lat = -122.45 lon = 37.5 point_list = [lat, lon] point = aerospike.GeoJSON({'type': "Point", 'coordinates': point_list}) predicate = p.geo_contains_geojson_point(bin_name, point.dumps(), idx_type) def callback(input_tuple): _, _, record = input_tuple records.append(record) query.where(predicate) query.foreach(callback) assert len(records) == 1
def test_pos_operate_incr_with_geospatial_new_record(self): """ Invoke operate() with incr command on a new record """ key = ('test', 'demo', 'geospatial_key') llist = [ operations.increment( "geospatial", aerospike.GeoJSON({ "type": "Point", "coordinates": [42.34, 58.62] })), operations.read("geospatial") ] _, _, bins = TestOperate.client_no_typechecks.operate(key, llist) assert bins['geospatial'].unwrap() == { 'coordinates': [42.34, 58.62], 'type': 'Point' } TestOperate.client_no_typechecks.remove(key)
def test_store_multipolygon(self): polygons = [[[[-124.500000, 37.000000], [-125.000000, 37.000000], [-121.000000, 38.080000], [-122.500000, 38.080000], [-124.500000, 37.000000]]], [[[-24.500000, 37.000000], [-25.000000, 37.000000], [-21.000000, 38.080000], [-22.500000, 38.080000], [-24.500000, 37.000000]]]] geo_object = aerospike.GeoJSON({ "type": "MultiPolygon", "coordinates": polygons }) key = ('test', 'demo', 'multipoly') self.as_connection.put(key, {'multi': geo_object}) _, _, bins = self.as_connection.get(key) geo_returned = bins['multi'].unwrap() assert geo_returned['type'] == 'MultiPolygon' assert geo_returned['coordinates'] == polygons self.as_connection.remove(key)
from aerospike import exception as e from aerospike import predexp as as_predexp from aerospike_helpers import cdt_ctx from aerospike_helpers.operations import list_operations from aerospike_helpers.operations import map_operations from aerospike_helpers.operations import operations aerospike = pytest.importorskip("aerospike") try: import aerospike except: print("Please install aerospike python client.") sys.exit(1) geo_circle = aerospike.GeoJSON({ "type": "AeroCircle", "coordinates": [[-132.0, 37.5], 1000] }) geo_point = aerospike.GeoJSON({"type": "Point", "coordinates": [-132.0, 37.5]}) geo_point2 = aerospike.GeoJSON({"type": "Point", "coordinates": [-60.5, 20]}) class TestPredEveryWhere(object): @pytest.fixture(autouse=True) def setup(self, request, as_connection): """ Setup Method """ self.keys = [] self.test_data = [{
map_value = "map_value" ctx_ops = { list_index: cdt_ctx.cdt_ctx_list_index, list_rank: cdt_ctx.cdt_ctx_list_rank, list_value: cdt_ctx.cdt_ctx_list_value, map_index: cdt_ctx.cdt_ctx_map_index, map_key: cdt_ctx.cdt_ctx_map_key, map_rank: cdt_ctx.cdt_ctx_map_rank, map_value: cdt_ctx.cdt_ctx_map_value, } GEO_POLY = aerospike.GeoJSON({ "type": "Polygon", "coordinates": [[[-122.500000, 37.000000], [-121.000000, 37.000000], [-121.000000, 38.080000], [-122.500000, 38.080000], [-122.500000, 37.000000]]] }) def add_ctx_op(ctx_type, value): ctx_func = ctx_ops[ctx_type] return ctx_func(value) def verify_multiple_expression_result(client, test_ns, test_set, expr, op_bin, expected): keys = [(test_ns, test_set, i) for i in range(_NUM_RECORDS + 1)] # batch get