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 get_Boundary_Data(self, boundary): """ :param boundary: document stored that contains the boundary_id and data associated :return: OptimalBoundary and Data tuple """ return OptimalBoundary(boundary_ID=AUID(boundary["auid"])), Data( boundary["data"])
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 query_by_boundary_in_boundary_datasets(self, id, boundary): """ :param id: identifier of the BoundaryDataset :param boundary: Boundary or OptimalBoundary. If it is not optimal, it is optimized before making the query. :return: Boundary and data associated stored in the BoundaryDataset with that id. """ if boundary.is_optimal(): optimal_boundary = boundary else: optimal_boundary = boundary.optimize() boundaries_datasets_founded = self.db.b_data_sets.find({"_id": id}) boundary_data_sets = [] for boundary_dataset in boundaries_datasets_founded: bds = BoundaryDataSet(id=id) boundaries_in_bds_founded = self.db.boundaries.find({ "boundary_dataset_id": boundary_dataset["_id"], "auid": optimal_boundary.boundary_ID.value }) for boundary in boundaries_in_bds_founded: bds.add(OptimalBoundary(boundary_ID=AUID(boundary["auid"])), Data(boundary["data"])) boundary_data_sets.append(bds) return boundary_data_sets
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 fromJSON(self, bds, file=False): if file: bds = json.load(bds) self.boundary_data_set = {} self.id = bds['id'] boundary_list = bds['boundary_data_set'] for boundary in boundary_list: self.add(OptimalBoundary(boundary_ID=AUID(boundary['AUID'])), Data(boundary['data'])) return self
def all_boundary_datasets(self): """ :return: List of all stored BoundaryDatasets """ boundary_data_sets = [] boundaries_datasets_founded = self.db.b_data_sets.find() for boundary_dataset in boundaries_datasets_founded: bds = BoundaryDataSet(id=boundary_dataset["_id"]) boundaries_in_bds_founded = self.db.boundaries.find( {"boundary_dataset_id": boundary_dataset["_id"]}) for boundary in boundaries_in_bds_founded: bds.add(OptimalBoundary(boundary_ID=AUID(boundary["auid"])), Data(boundary["data"])) boundary_data_sets.append(bds) return boundary_data_sets
def query_by_boundary_dataset_id(self, id): """ :param id: identifier of the BoundaryDataset :return: BoundaryDataset stored with that id. """ boundaries_datasets_founded = self.db.b_data_sets.find({"_id": id}) boundary_data_sets = [] for boundary_dataset in boundaries_datasets_founded: bds = BoundaryDataSet(id=id) boundaries_in_bds_founded = self.db.boundaries.find( {"boundary_dataset_id": boundary_dataset["_id"]}) for boundary in boundaries_in_bds_founded: bds.add(OptimalBoundary(boundary_ID=AUID(boundary["auid"])), Data(boundary["data"])) boundary_data_sets.append(bds) return boundary_data_sets