예제 #1
0
 def get_linked_entity(self,
                       code: str,
                       inverse: bool = False,
                       types: bool = False) -> Optional[Entity]:
     return Link.get_linked_entity(self.id,
                                   code,
                                   inverse=inverse,
                                   types=types)
예제 #2
0
 def get_geom(entity: Entity) -> Union[List[Dict[str, Any]], List[Any]]:
     if entity.class_.view == 'place' or entity.class_.name in [
             'find', 'artifact'
     ]:
         return Gis.get_by_id(Link.get_linked_entity(entity.id, 'P53').id)
     if entity.class_.name == 'object_location':
         print("here")
         return Gis.get_by_id(entity.id)
     return []
예제 #3
0
 def get_geom_entry(entity: Entity) -> Dict[str, None]:
     geom = {'type': None, 'coordinates': None}
     if entity.class_.view == 'place' \
             or entity.class_.name in ['find', 'artifact']:
         geom = ApiExportCSV.get_geometry(
             Link.get_linked_entity(entity.id, 'P53'))
     elif entity.class_.name == 'object_location':
         geom = ApiExportCSV.get_geometry(entity)
     return geom
예제 #4
0
def search(form: FlaskForm) -> ValuesView[Entity]:
    if not form.term.data:
        return {}.values()
    classes = form.classes.data
    if 'person' in classes:
        classes.append('actor_appellation')
    if 'place' in classes:
        classes.append('appellation')

    # Repopulate date fields with autocompleted values
    from_date = Date.form_to_datetime64(form.begin_year.data,
                                        form.begin_month.data,
                                        form.begin_day.data)
    to_date = Date.form_to_datetime64(form.end_year.data,
                                      form.end_month.data,
                                      form.end_day.data,
                                      to_date=True)
    if from_date:
        string = str(from_date)
        if string.startswith('-') or string.startswith('0000'):
            string = string[1:]
        parts = string.split('-')
        form.begin_month.raw_data = None
        form.begin_day.raw_data = None
        form.begin_month.data = int(parts[1])
        form.begin_day.data = int(parts[2])
    if to_date:
        string = str(to_date)
        if string.startswith('-') or string.startswith('0000'):
            string = string[1:]  # pragma: no cover
        parts = string.split('-')
        form.end_month.raw_data = None
        form.end_day.raw_data = None
        form.end_month.data = int(parts[1])
        form.end_day.data = int(parts[2])

    # Get search results
    entities = []
    for row in Db.search(form.term.data, tuple(form.classes.data),
                         form.desc.data, form.own.data, current_user.id):
        if row['system_class'] == 'actor_appellation':  # If found in actor alias
            entity = Link.get_linked_entity(row['id'], 'P131', True)
        elif row['system_class'] == 'appellation':  # If found in place alias
            entity = Link.get_linked_entity(row['id'], 'P1', True)
        else:
            entity = Entity(row)

        if not entity:  # pragma: no cover
            continue

        if not from_date and not to_date:
            entities.append(entity)
            continue

        # Date criteria present but entity has no dates
        if not entity.begin_from and not entity.begin_to and not entity.end_from \
                and not entity.end_to:
            if form.include_dateless.data:  # Include dateless entities
                entities.append(entity)
            continue

        # Check date criteria
        dates = [
            entity.begin_from, entity.begin_to, entity.end_from, entity.end_to
        ]
        begin_check_ok = False
        if not from_date:
            begin_check_ok = True  # pragma: no cover
        else:
            for date in dates:
                if date and date >= from_date:
                    begin_check_ok = True

        end_check_ok = False
        if not to_date:
            end_check_ok = True  # pragma: no cover
        else:
            for date in dates:
                if date and date <= to_date:
                    end_check_ok = True

        if begin_check_ok and end_check_ok:
            entities.append(entity)
    return {d.id: d
            for d in entities}.values()  # Remove duplicates before returning
예제 #5
0
    def get_entity(entity: Entity, parser: Dict[str, Any]) -> Dict[str, Any]:
        type_ = 'FeatureCollection'
        class_code = ''.join(entity.cidoc_class.code + " " +
                             entity.cidoc_class.i18n['en']).replace(" ", "_")
        features = {
            '@id': url_for('entity_view', id_=entity.id, _external=True),
            'type': 'Feature',
            'crmClass': "crm:" + class_code,
            'system_class': entity.class_.name,
            'properties': {
                'title': entity.name
            }
        }

        # Descriptions
        if entity.description:
            features['description'] = [{'value': entity.description}]

        # Alias
        if entity.aliases and 'names' in parser['show']:
            features['names'] = []
            for key, value in entity.aliases.items():
                features['names'].append({"alias": value})

        # Relations
        features['relations'] = GeoJsonEntity.get_links(
            entity) if 'relations' in parser['show'] else None

        # Types
        features['types'] = GeoJsonEntity.get_node(
            entity) if 'types' in parser['show'] else None

        # Depictions
        features['depictions'] = GeoJsonEntity.get_file(
            entity) if 'depictions' in parser['show'] else None

        # Time spans
        features['when'] = {
            'timespans': [GeoJsonEntity.get_time(entity)]
        } if 'when' in parser['show'] else None

        features['links'] = GeoJsonEntity.get_reference_systems(
            entity) if 'links' in parser['show'] else None

        # Geometry
        if 'geometry' in parser['show']:
            if entity.class_.view == 'place' or entity.class_.name in [
                    'find', 'artifact'
            ]:
                features['geometry'] = GeoJsonEntity.get_geoms_by_entity(
                    Link.get_linked_entity(entity.id, 'P53'))
            elif entity.class_.name == 'object_location':
                features['geometry'] = GeoJsonEntity.get_geoms_by_entity(
                    entity)

        data: Dict[str, Any] = {
            'type': type_,
            '@context': app.config['API_SCHEMA'],
            'features': [features]
        }
        return data