def test_boundary_dataset_add_list(self): boundary1 = OptimalBoundary(cells=[ CellID('N'), CellID('O0'), CellID('P123'), CellID('S34567') ]) boundary2 = OptimalBoundary(cells=[CellID('O35'), CellID('P234')]) boundary3 = OptimalBoundary(cells=[CellID('S034'), CellID('S57')]) boundary4 = OptimalBoundary(cells=[CellID('P33'), CellID('P44')]) data = Data('') b_ds = { boundary1.boundary_ID.value: (boundary1, data), boundary2.boundary_ID.value: (boundary2, data), } b_dataset = BoundaryDataSet('id', b_ds) b_dataset.add_list([(boundary3, data), (boundary4, data)]) b_ds2 = { boundary1.boundary_ID.value: (boundary1, data), boundary2.boundary_ID.value: (boundary2, data), boundary3.boundary_ID.value: (boundary3, data), boundary4.boundary_ID.value: (boundary4, data) } self.assertEqual(b_dataset.boundary_data_set, b_ds2)
def test_update_cell_dataset(self): store.dropAll() cds = CellDataSet("id") cells = ['P0', 'P1', 'P2', 'P3', 'P4', 'P5'] for cell in cells: cds.add(CellID(cell), Data("")) store.insert(cds) cds2 = CellDataSet("id") cells2 = ['O0', 'O1', 'O2', 'O3', 'O4'] for cell in cells2: cds2.add(CellID(cell), Data("")) store.update_cell_dataset(cds2) stored_cds = store.query_by_cell_dataset_id("id") num_cds = 0 num_cells = 0 for cds in stored_cds: for cell in cds.get_cells(): assert cells2.__contains__(cell.value) num_cells = num_cells + 1 num_cds = num_cds + 1 self.assertEqual(num_cds, 1) self.assertEqual(num_cells, len(cells2)) store.dropAll()
def test_cell_ul_vertex(self): self.assertEqual(r.get_cell_ul_vertex(CellID('N0')), ((-pi + 0 * pi / 2) * 0.998882147091, (3 * pi / 4) * 0.998882147091)) self.assertEqual(r.get_cell_ul_vertex(CellID('S0')), ((-pi + 0 * pi / 2) * 0.998882147091, (-pi / 4) * 0.998882147091)) self.assertEqual(r.get_cell_ul_vertex(CellID('P')), ((-pi / 2) * 0.998882147091, (pi / 4) * 0.998882147091)) self.assertEqual(r.get_cell_ul_vertex(CellID('O123')), (-2.266391699796672, 0.726407596088677))
def optimize(self): """ :return: OptimalBoundary, that is, a boundary that is the smallest one that delimits exactly its area. """ maxrefinement = self.get_max_refinement() new_cells = self.cells for actual_refinement in range(maxrefinement, 0, -1): for cell_act_res in [ cell for cell in new_cells if cell.get_refinement() == actual_refinement ]: father_value = cell_act_res.value[actual_refinement - 1] cells_with_same_father = [ cell for cell in new_cells if (cell.get_refinement() == actual_refinement and cell.value[actual_refinement - 1] == father_value) ] if len(cells_with_same_father) == (self.dggs.N_side**2): new_cells = [ cell for cell in new_cells if cell not in cells_with_same_father ] copy_cell_value = cell_act_res.value new_cells.append( CellID(copy_cell_value[:actual_refinement])) cell_ids = sorted([cell.value for cell in new_cells]) new_cells = [CellID(ciud) for ciud in cell_ids] auid_bp, _, _, _, _ = cuids_to_bp_auid(cell_ids, pars="()", with_opening_par=False) return OptimalBoundary(boundary_ID=AUID(auid_bp), cells=new_cells)
def test_max_refinement(self): boundary = Boundary(cells=[ CellID('N'), CellID('O0'), CellID('P123'), CellID('S34567') ]) max_refinement = boundary.get_max_refinement() self.assertEqual(max_refinement, 5)
def __init__(self, boundary_ID=None, cells=None, dggs=rHEALPix(N_side=3, north_square=0, south_square=0)): """ :param boundary_ID: boundary identifier, of type BoundaryID :param cells: list of identifiers of the cells that make up the boundary, [ CellID, CellID ... ] :param dggs: Discrete Global Grid System, rHEALPix by default """ self.boundary_ID = boundary_ID self.cells = cells self.dggs = dggs assert boundary_ID is not None or cells is not None if cells is None: cells = [] cell_ID = '' first = True for char in boundary_ID.value: if char in self.dggs.cells_R0: if not first: cells.append(CellID(cell_ID)) else: first = False cell_ID = char else: cell_ID = cell_ID + char cells.append(CellID(cell_ID)) cell_ids = sorted([cell.value for cell in cells]) self.cells = [CellID(id) for id in cell_ids] if boundary_ID is None: cell_ids = sorted([cell.value for cell in cells]) self.cells = [CellID(id) for id in cell_ids] boundary_ID = '' for cell_ID in self.cells: boundary_ID = boundary_ID + cell_ID.value self.boundary_ID = BoundaryID(boundary_ID) refinement_set = {} for cell in cells: if cell.get_refinement() in refinement_set: cell_list = refinement_set[cell.get_refinement()] cell_list.append(cell) else: refinement_set[cell.get_refinement()] = [cell] grids_list = [] for refinement, cell_list in refinement_set.items(): grids_list.append(Grid(refinement, cells=cell_list)) def sort_by_refinement_level(grid): return grid.refinement_level grids_list.sort(key=sort_by_refinement_level) self.grids = grids_list
def test_cell_dataset(self): cell_data_set = { 'N01': (CellID('N01'), Data('test')), 'N02': (CellID('N02'), Data('test')), 'N03': (CellID('N03'), Data('test')) } c_dataset = CellDataSet('id', cell_data_set=cell_data_set) self.assertEqual(c_dataset.cell_data_set, cell_data_set)
def test_get_max_refinement(self): cells = [CellID('P0'), CellID('N02'), CellID('N033')] data = Data('test') cell_data_set = { cells[0].value: (cells[0], data), cells[1].value: (cells[1], data), cells[2].value: (cells[2], data) } c_dataset = CellDataSet('id', cell_data_set=cell_data_set) self.assertEqual(c_dataset.get_max_refinement(), 3)
def test_get_cells(self): cells = [CellID('N01'), CellID('N02'), CellID('N03')] data = Data('test') cell_data_set = { cells[0].value: (cells[0], data), cells[1].value: (cells[1], data), cells[2].value: (cells[2], data) } c_dataset = CellDataSet('id', cell_data_set=cell_data_set) self.assertEqual(c_dataset.get_cells(), cells)
def test_get_cell_data_list(self): cells = [CellID('P0'), CellID('N02'), CellID('N033')] data = Data('test') data2 = Data('test2') cell_data_set = { cells[0].value: (cells[0], data), cells[1].value: (cells[1], data), cells[2].value: (cells[2], data2) } c_dataset = CellDataSet('id', cell_data_set=cell_data_set) self.assertEqual(c_dataset.get_cell_data_list([cells[0], cells[2]]), [data, data2])
def test_delete_cell(self): store.dropAll() cds = CellDataSet("id") cells = ['P0', 'P1', 'P2', 'P3', 'P4', 'P5'] for cell in cells: cds.add(CellID(cell), Data("")) store.insert(cds) deleted_cells = store.delete_cell(CellID('P0')) self.assertEqual(deleted_cells, 1) stored_cells = store.query_by_cell(CellID('P0')) self.assertEqual(stored_cells.__len__(), 0) store.dropAll()
def get_optimal_boundary_from_shp_file(self, file, with_ids, refinement=None, unic_data=False): shapes = fiona.open(file) cells = [] data = {} first = True for polygon in shapes: if with_ids: cells.append(CellID(polygon['properties']['id'])) else: assert refinement is not None cells.append(self.get_cell_ID(polygon, int(refinement))) if not unic_data: if 'data' in polygon['properties']: data[polygon['properties'] ['id']] = polygon['properties']['data'] else: data[polygon['properties']['id']] = polygon['properties'] elif unic_data and first: if 'data' in polygon['properties']: data = polygon['properties']['data'] else: data = polygon['properties'] return OptimalBoundary(cells=cells), Data(data)
def destroy(self, request, cds=None, pk=None): if cds is not None: if not re.match(r'^[A-Za-z0-9]+$', pk): return Response({ 'status': 'Bad request', }, status=status.HTTP_400_BAD_REQUEST) try: result = self.store.delete_cell_in_cell_datasets( cds, CellID(pk)) if result == 0: return Response(status=status.HTTP_404_NOT_FOUND) else: return Response(status=status.HTTP_204_NO_CONTENT) except: return Response({ 'status': 'Bad request', }, status=status.HTTP_500_INTERNAL_SERVER_ERROR) else: try: result = self.store.delete_cell_dataset(pk) if result == 0: return Response(status=status.HTTP_404_NOT_FOUND) else: return Response(status=status.HTTP_204_NO_CONTENT) except: return Response({ 'status': 'Bad request', }, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def __init__(self, boundary_ID=None, cells=None, dggs=rHEALPix(N_side=3, north_square=0, south_square=0)): """ :param boundary_ID: boundary identifier, as defined in the AGILE19 paper, of type AUID :param cells: list of identifiers of the cells that make up the boundary, [ CellID, CellID ... ] :param dggs: Discrete Global Grid System, rHEALPix by default """ self.boundary_ID = boundary_ID self.cells = cells self.optimal = True self.dggs = dggs assert boundary_ID is not None or cells is not None if cells is None: cells = bp_auid_to_cuids(boundary_ID.value, pars="()", with_opening_par=False) self.cells = [] for cell in cells: self.cells.append(CellID(cell)) if boundary_ID is None: cuids = [cell.value for cell in self.cells] auid_bp, _, _, _, _ = cuids_to_bp_auid(cuids, pars="()", with_opening_par=False) self.boundary_ID = AUID(auid_bp)
def test_query_by_cell(self): store.dropAll() cds = CellDataSet("id") cells = ['P0', 'P1', 'P2', 'P3', 'P4', 'P5'] for cell in cells: cds.add(CellID(cell), Data("")) store.insert(cds) stored_cells = store.query_by_cell(CellID('P0')) num_cells = 0 for cell in stored_cells: assert cells.__contains__(cell[0].value) num_cells = num_cells + 1 self.assertEqual(num_cells, 1) store.dropAll()
def retrieve(self, request, cds=None, pk=None): if cds is not None: if not re.match(r'^[A-Za-z0-9]+$', pk): return Response({ 'status': 'Bad request', }, status=status.HTTP_400_BAD_REQUEST) try: cell_dataset = self.store.query_by_cell_in_cell_datasets( cds, CellID(pk)) except: return Response({ 'status': 'Bad request', }, status=status.HTTP_500_INTERNAL_SERVER_ERROR) serializer = self.serializer_class(instance=cell_dataset, many=True) else: try: cell_dataset = self.store.query_by_cell_dataset_id(pk) except: return Response({ 'status': 'Bad request', }, status=status.HTTP_500_INTERNAL_SERVER_ERROR) serializer = self.serializer_class(instance=cell_dataset, many=True) return Response(serializer.data)
def to_internal_value(self, data): cds = CellDataSet(id='') for item in data: if not re.match(r'^[A-Za-z0-9]+$', item['cellID']): raise ValidationError('Incorrect format: ' + item['cellID']) cds.add(CellID(item['cellID']), Data(item['data'])) return cds.cell_data_set
def test_cell_dataset_add_list(self): cells = [ CellID('N01'), CellID('N02'), CellID('N03'), CellID('N04'), CellID('N05') ] data = Data('test') cell_data_set = { cells[0].value: (cells[0], data), cells[1].value: (cells[1], data), cells[2].value: (cells[2], data) } c_dataset = CellDataSet('id', cell_data_set=cell_data_set) c_dataset.add_list([(cells[3], data), (cells[4], data)]) self.assertEqual(c_dataset.cells, cells)
def __init__(self, refinement_level, boundary_ID=None, cells=None, dggs=rHEALPix(N_side=3, north_square=0, south_square=0)): """ :param refinement_level :param boundary_ID: boundary identifier, of type BoundaryID :param cells: list of identifiers of the cells that make up the boundary, [ CellID, CellID ... ] :param dggs: Discrete Global Grid System, rHEALPix by default """ self.boundary_ID = boundary_ID self.cells = cells self.dggs = dggs self.refinement_level = refinement_level assert boundary_ID is not None or cells is not None if cells is None: cells = [] cell_ID = '' first = True for char in boundary_ID.value: if char in self.dggs.cells_R0: if not first: cells.append(CellID(cell_ID)) else: first = False cell_ID = char else: cell_ID = cell_ID + char cells.append(CellID(cell_ID)) cell_ids = sorted([cell.value for cell in cells]) self.cells = [CellID(id) for id in cell_ids] if boundary_ID is None: cell_ids = sorted([cell.value for cell in cells]) self.cells = [CellID(id) for id in cell_ids] boundary_ID = '' for cell_ID in self.cells: boundary_ID = boundary_ID + cell_ID.value self.boundary_ID = BoundaryID(boundary_ID)
def test_delete_cell_in_cell_datasets(self): store.dropAll() cds = CellDataSet("id") cells = ['P0', 'P1', 'P2', 'P3', 'P4', 'P5'] for cell in cells: cds.add(CellID(cell), Data("")) store.insert(cds) deleted_cells = store.delete_cell_in_cell_datasets("id", CellID('P0')) self.assertEqual(deleted_cells, 1) stored_cds = store.query_by_cell_in_cell_datasets("id", CellID('P0')) num_cds = 0 for cds in stored_cds: assert cds.get_cells().__len__() == 0 num_cds = num_cds + 1 self.assertEqual(num_cds, 1) store.dropAll()
def test_query_by_cell_in_cell_datasets(self): store.dropAll() cds = CellDataSet("id") cells = ['P0', 'P1', 'P2', 'P3', 'P4', 'P5'] for cell in cells: cds.add(CellID(cell), Data("")) store.insert(cds) stored_cds = store.query_by_cell_in_cell_datasets("id", CellID('P0')) num_cds = 0 num_cells = 0 for cds in stored_cds: for cell in cds.get_cells(): assert cells.__contains__(cell.value) num_cells = num_cells + 1 num_cds = num_cds + 1 self.assertEqual(num_cds, 1) self.assertEqual(num_cells, 1) store.dropAll()
def fromJSON(self, cds, file=False): if file: cds = json.load(cds) self.cell_data_set = {} self.id = cds['id'] cell_list = cds['cell_data_set'] for cell in cell_list: self.add(CellID(cell['cellID']), Data(cell['data'])) return self
def test_cell_from_point(self): p = (43.74999986695531, 47.57255097966877) cell = r.get_cell_from_point(4, p) self.assertEqual(cell, CellID('N1145')) p2 = (54.00000032594864, 54.91640765274602) cell = r.get_cell_from_point(3, p2) self.assertEqual(cell, CellID('N132')) p3 = (-133.74999985810513, -47.57255129550154) cell = r.get_cell_from_point(5, p3) self.assertEqual(cell, CellID('S11454')) p4 = (-134.38356121039018, -46.9523050079352) cell = r.get_cell_from_point(7, p4) self.assertEqual(cell, CellID('S1145000')) p5 = (-1.1179698216735403, 41.82074068655) cell = r.get_cell_from_point(2, p5) self.assertEqual(cell, CellID('P22'))
def get_cells_from_shp_file(self, file, with_ids, refinement=None, unic_data=False): shapes = fiona.open(file) cells = [] data = [] for polygon in shapes: if with_ids: cells.append(CellID(polygon['properties']['id'])) else: assert refinement is not None cells.append(self.get_cell_ID(polygon, refinement)) if 'data' in polygon['properties']: data.append(CellID(polygon['properties']['data'])) else: data.append(CellID(polygon['properties'])) return cells, data
def test_limit_cells(self): boundary = Boundary(cells=[ CellID('N1'), CellID('O0'), CellID('P123'), CellID('S34567') ]) top_cell, bottom_cell, left_cell, right_cell = boundary.get_limit_cells( ) self.assertEqual(top_cell, CellID('N1')) self.assertEqual(bottom_cell, CellID('S34567')) self.assertEqual(left_cell, CellID('O0')) self.assertEqual(right_cell, CellID('P123'))
def all_cell_datasets(self): """ :return: List of all stored CellDatasets """ cell_data_sets = [] cell_datasets_founded = self.db.c_data_sets.find() for cell_dataset in cell_datasets_founded: cds = CellDataSet(id=cell_dataset["_id"]) cells_in_cds_founded = self.db.cells.find({"cell_dataset_id": cell_dataset["_id"]}) for cell in cells_in_cds_founded: cds.add(CellID(cell['cellID']), Data(cell["data"])) cell_data_sets.append(cds) return cell_data_sets
def test_boundary_from_boundary_ID(self): boundary = Boundary(boundary_ID=BoundaryID('N11N12N13N88O0P123S34567')) self.assertEqual(boundary.cells, [ CellID('N11'), CellID('N12'), CellID('N13'), CellID('N88'), CellID('O0'), CellID('P123'), CellID('S34567') ])
def test_boundary_from_cells(self): boundary = Boundary(cells=[ CellID('N11'), CellID('N12'), CellID('N13'), CellID('N88'), CellID('O0'), CellID('P123'), CellID('S34567') ]) self.assertEqual(boundary.boundary_ID.value, 'N11N12N13N88O0P123S34567')
def test_optimal_boundary_from_AUID(self): optimal_boundary = OptimalBoundary(boundary_ID=AUID( 'RN11$))2$))3$)))88$))))O0$)))P123$)))))S34567$))))))))')) self.assertEqual(optimal_boundary.cells, [ CellID('N11'), CellID('N12'), CellID('N13'), CellID('N88'), CellID('O0'), CellID('P123'), CellID('S34567') ])
def query_by_cell_dataset_id(self, id): """ :param id: identifier of the CellDataset :return: List of tuples with stored cells and data associated stored in the CellDataset with that id. """ cell_datasets_founded = self.db.c_data_sets.find({"_id": id}) cell_data_sets = [] for cell_dataset in cell_datasets_founded: cds = CellDataSet(id=id) cells_in_cds_founded = self.db.cells.find({"cell_dataset_id": cell_dataset["_id"]}) for cell in cells_in_cds_founded: cds.add(CellID(cell['cellID']), Data(cell["data"])) cell_data_sets.append(cds) return cell_data_sets