Beispiel #1
0
 def get_by_state(state_id, date, pixel_width):
     assert isinstance(state_id, int)
     assert isinstance(date, datetime)
     assert isinstance(pixel_width, float)
     with get_cursor() as cursor:
         bbox = StateCRUD.get_bbox(cursor, state_id, date).enlarge_to_aspect_ratio(16/9)
         precision = precision_from_bbox_and_px_width(bbox, pixel_width)
         territories = TerritoryCRUD.get_within_bbox_at_time(cursor, bbox, date, precision)
         state_ids = frozenset(t.state_id for t in territories)
         states = StateCRUD.get_many(cursor, state_ids)
         return {
             'states' : states,
             'territories' : territories,
             'bounding_box' : bbox,
             'date' : date
         }
Beispiel #2
0
 def delete(territory_id):
     with get_cursor() as cursor:
         territory = TerritoryCRUD.get(cursor, territory_id)
         state = StateCRUD.get(cursor, territory.state_id)
         if len(state.representations
                ) == 1 and not state.representations[0].name:
             its_territories = TerritoryCRUD.get_by_state(
                 cursor, territory.state_id)
             if len(its_territories) == 1:
                 StateCRUD.delete(cursor, state.state_id)
                 return {
                     "deleted_state": state.state_id,
                     "deleted_territory": territory_id
                 }
         TerritoryCRUD.delete(cursor, territory_id)
         return {"deleted_state": None, "deleted_territory": territory_id}
Beispiel #3
0
    def __merge_with_cursor(cursor, to_merge_id, sovereign_state_id):
        assert isinstance(to_merge_id, int)
        assert isinstance(sovereign_state_id, int)
        to_merge = StateCRUD.get(cursor, to_merge_id)
        sovereign = StateCRUD.get(cursor, sovereign_state_id)
        if to_merge.validity_start < sovereign.validity_start or to_merge.validity_end > sovereign.validity_end:
            raise MergeStateConflictException(
                f'The state to merge period {(to_merge.validity_start, to_merge.validity_end)} overflows the period of the sovereign state {(sovereign.validity_start, sovereign.validity_end)}',
                [
                    t.territory_id
                    for t in TerritoryCRUD.get_by_state(cursor, to_merge_id)
                ])
        territories_to_change = TerritoryCRUD.get_by_state(cursor, to_merge_id)
        for territory in territories_to_change:

            # Color integrity check
            its_color = territory.color
            if not its_color:
                candidate_colors = [
                    r.color
                    for r in to_merge.representations if r.period_intersects(
                        territory.validity_start, territory.validity_end)
                ]
                # All representation matching with the territory have similar color => territory color can be determined (else too complex nothing is done)
                if all(
                        colours_roughly_equal(candidate_colors[0], c)
                        for c in candidate_colors[1:]):
                    its_color = candidate_colors[0]
            if its_color:
                sovereign_color = [
                    r.color
                    for r in sovereign.representations if r.period_intersects(
                        territory.validity_start, territory.validity_end)
                ]
                if all(
                        colours_roughly_equal(c, its_color)
                        for c in sovereign_color):
                    territory.color = None
                # Destination color is different => set the territory color to preserve it
                else:
                    territory.color = its_color
                TerritoryCRUD.edit(cursor, territory, change_color=True)

            territory.state_id = sovereign_state_id
            TerritoryCRUD.edit(cursor, territory, change_state_id=True)
        StateCRUD.delete(cursor, to_merge_id)
        return sovereign_state_id
Beispiel #4
0
    def put(state, absorb_conflicts):
        assert isinstance(state, State)
        with get_cursor() as cursor:
            conflicts = StateTag.__get_name_conflicts(state, cursor)
            if len(conflicts):
                if absorb_conflicts:
                    start_conflicts = min(s.validity_start for s in conflicts)
                    end_conflicts = max(s.validity_end for s in conflicts)
                    StateCRUD.edit(cursor,
                                   state,
                                   change_validity_start=True,
                                   change_validity_end=True)
                    if start_conflicts < state.validity_start or end_conflicts > state.validity_end:
                        raise Conflict(
                            f'Cannot absorb conflicts : they go from {start_conflicts.isoformat()} to {end_conflicts.isoformat()}'
                        )
                    for c in conflicts:
                        StateTag.__merge_with_cursor(cursor, c.state_id,
                                                     state.state_id)
                else:
                    report = ', by '.join([
                        f'{c.state_id} from {c.validity_start.year} to {c.validity_end.year}'
                        for c in conflicts
                    ])
                    raise Conflict(f'names are already taken by {report} ')

            its_territories = TerritoryCRUD.get_by_state(
                cursor, state.state_id)
            conflicting_territories = [
                t for t in its_territories
                if t.validity_start < state.validity_start
                or t.validity_end > state.validity_end
            ]
            if len(conflicting_territories):
                raise Conflict(
                    f'Cannot update : territories {[t.territory_id for t in conflicting_territories]} have range from {min(t.validity_start for t  in conflicting_territories)} to {max(t.validity_end for t in conflicting_territories)}'
                )
            StateCRUD.edit(cursor,
                           state,
                           change_representations=True,
                           change_validity_start=True,
                           change_validity_end=True)
            return state.state_id
Beispiel #5
0
 def post(state):
     assert isinstance(state, State)
     with get_cursor() as cursor:
         conflicts = StateTag.__get_name_conflicts(state, cursor)
         if len(conflicts):
             report = ', by '.join([
                 f'{c.state_id} from {c.validity_start.year} to {c.validity_end.year}'
                 for c in conflicts
             ])
             raise Conflict(f'names are already taken by {report} ')
         return StateCRUD.add(cursor, state)
Beispiel #6
0
 def get(bbox, date, precision_level):
     assert isinstance(bbox, BoundingBox)
     assert isinstance(date, datetime)
     assert date.tzinfo is None
     with get_cursor() as cursor:
         territories = TerritoryCRUD.get_within_bbox_at_time(cursor, bbox, date, precision_level)
         state_ids = frozenset(t.state_id for t in territories)
         states = StateCRUD.get_many(cursor, state_ids)
         return {
             'states' : states,
             'territories' : territories
         }
Beispiel #7
0
 def _assign_to_state(cursor, territory, target_state_id):
     assert isinstance(territory, Territory)
     assert isinstance(target_state_id, int)
     created_territories_ids = []
     target_state = StateCRUD.get(cursor, target_state_id)
     if target_state.validity_end <= territory.validity_start or target_state.validity_start >= territory.validity_end:
         raise BadRequest(
             f'The territory goes from {territory.validity_start} to {territory.validity_end} and has no time in common with state from {target_state.validity_start} to {target_state.validity_end}'
         )
     if target_state.validity_end < territory.validity_end:
         to_add_at_end = Territory(None, territory.representations,
                                   territory.state_id,
                                   territory.bounding_box,
                                   target_state.validity_end,
                                   territory.validity_end, territory.color,
                                   territory.name)
         created_territories_ids.append(
             TerritoryCRUD.add(cursor, to_add_at_end))
         territory.validity_end = target_state.validity_end
         TerritoryCRUD.edit(cursor, territory, change_end=True)
     if target_state.validity_start > territory.validity_start:
         to_add_at_start = Territory(None, territory.representations,
                                     territory.state_id,
                                     territory.bounding_box,
                                     territory.validity_start,
                                     target_state.validity_start,
                                     territory.color, territory.name)
         created_territories_ids.append(
             TerritoryCRUD.add(cursor, to_add_at_start))
         territory.validity_start = target_state.validity_start
         TerritoryCRUD.edit(cursor, territory, change_start=True)
     new_color = TerritoryTag._get_color_for_territory(
         territory, StateCRUD.get(cursor, territory.state_id), target_state)
     if new_color != territory.color:
         territory.color = new_color
         TerritoryCRUD.edit(cursor, territory, change_color=True)
     territory.state_id = target_state.state_id
     TerritoryCRUD.edit(cursor, territory, change_state_id=True)
     return created_territories_ids
Beispiel #8
0
 def get_evolution_by_state(state_id, pixel_width):
     assert isinstance(state_id, int)
     assert isinstance(pixel_width, float)
     with get_cursor() as cursor:
         res = []
         for date in _determine_dates_to_show(cursor, state_id):
             try :
                 bbox = StateCRUD.get_bbox(cursor, state_id, date).enlarge_to_aspect_ratio(16/9)
                 logging.error('setting last bbox')
                 last_bbox = bbox
             except NotFound:
                 bbox = last_bbox 
             precision = precision_from_bbox_and_px_width(bbox, pixel_width)
             territories = TerritoryCRUD.get_within_bbox_at_time(cursor, bbox, date, precision)
             state_ids = frozenset(t.state_id for t in territories)
             states = StateCRUD.get_many(cursor, state_ids)
             res.append({
                 'states' : states,
                 'territories' : territories,
                 'bounding_box' : bbox,
                 'date' : date,
                 'lands': LandCRUD.get_lands(cursor, bbox, precision)
             })
         return res
Beispiel #9
0
 def __get_name_conflicts(state, cursor):
     assert isinstance(state, State)
     res = []
     res_ids = []
     for rpz in [r for r in state.representations if r.name]:
         conflicts = StateCRUD.get_by_name(cursor, rpz.name,
                                           rpz.validity_start,
                                           rpz.validity_end)
         if state.state_id:
             conflicts = [
                 c for c in conflicts if c.state_id != state.state_id
                 and c.state_id not in res_ids
             ]
         for c in conflicts:
             res.append(c)
             res_ids.append(c.state_id)
     return res
Beispiel #10
0
 def post(territory):
     assert isinstance(territory, Territory)
     with get_cursor() as cursor:
         state = StateCRUD.get(cursor, territory.state_id)
         if territory.validity_start < state.validity_start or territory.validity_end > state.validity_end:
             raise Conflict(
                 f"Cannot add territory ({territory.validity_start} , {territory.validity_end}) to state ({state.validity_start}, {state.validity_end}) : period overflows"
             )
         potentially_intersecting = TerritoryCRUD.get_within_bbox_in_period(
             cursor, territory.bounding_box, territory.validity_start,
             territory.validity_end, 0)
         t_conflict_ids = [
             other.territory_id for other in potentially_intersecting
             if territories_conflict(territory, other)
         ]
         if len(t_conflict_ids):
             raise Conflict(
                 f"Cannot add the territory : it conflicts with territories {t_conflict_ids}"
             )
         compressed_territory = compress_territory(territory)
         return TerritoryCRUD.add(cursor, compressed_territory)
Beispiel #11
0
 def search(pattern):
     assert isinstance(pattern, str)
     with get_cursor() as cursor:
         return StateCRUD.search(cursor, pattern)
Beispiel #12
0
 def delete(state_id):
     assert isinstance(state_id, int)
     with get_cursor() as cursor:
         return StateCRUD.delete(cursor, state_id)