Skip to content

Commit

Permalink
removed unneeded functions, mypy and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardKoschicek committed Feb 19, 2025
1 parent 9d30ad5 commit 714ab3a
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 258 deletions.
40 changes: 14 additions & 26 deletions openatlas/api/endpoints/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
from openatlas.api.resources.templates import (
geojson_collection_template, geojson_pagination, linked_place_pagination,
linked_places_template, loud_pagination, loud_template)
from openatlas.api.resources.util import geometry_to_geojson, \
get_location_link
from openatlas.api.resources.util import get_location_link
from openatlas.models.entity import Entity, Link
from openatlas.models.gis import Gis

Expand Down Expand Up @@ -184,7 +183,7 @@ def get_entities_grouped_by_class(self) -> dict[str, Any]:

def link_parser_check(self, inverse: bool = False) -> list[Link]:
links = []
show_ = {'relations', 'types', 'depictions', 'links', 'geometry'}
show_ = {'relations', 'types', 'depictions', 'links'}
if set(self.parser.show) & show_:
links = Entity.get_links_of_entities(
[entity.id for entity in self.entities],
Expand Down Expand Up @@ -233,35 +232,24 @@ def get_entities_formatted(self) -> None:

def get_geojson(self) -> dict[str, Any]:
out = []
links = Entity.get_links_of_entities(
[e.id for e in self.entities],
'P53')
for e in self.entities:
if e.class_.view == 'place':
entity_links = [x for x in links if x.domain.id == e.id]
e.types.update(get_location_link(entity_links).range.types)
if geoms := [
self.parser.get_geojson_dict(e, geom)
for geom in self.parser.get_geom(e)]:
out.extend(geoms)
for e in self.entities_with_links.values():
if e['entity'].class_.view == 'place':
e['entity'].types.update(
get_location_link(e['links']).range.types)
if e['geometry']:
for geom in e['geometry']:
out.append(self.parser.get_geojson_dict(e, geom))
else:
out.append(self.parser.get_geojson_dict(e))
return {'type': 'FeatureCollection', 'features': out}

def get_geojson_v2(self) -> dict[str, Any]:
out = []
property_codes = ['P53', 'P74', 'OA8', 'OA9', 'P7', 'P26', 'P27']
link_parser = self.link_parser_check()
links = [x for x in link_parser if x.property.code in property_codes]
for e in self.entities:
entity_links = [
link_ for link_ in links if link_.domain.id == e.id]
if e.class_.view == 'place':
e.types.update(get_location_link(entity_links).range.types)
if geom := self.parser.get_geoms_as_collection(
e,
[x.range.id for x in entity_links]):
out.append(self.parser.get_geojson_dict(e, geom))
for e in self.entities_with_links.values():
if e['entity'].class_.view == 'place':
e['entity'].types.update(
get_location_link(e['links']).range.types)
out.append(self.parser.get_geojson_dict(e))
return {'type': 'FeatureCollection', 'features': out}

def get_entities_template(self, result: dict[str, Any]) -> dict[str, Any]:
Expand Down
43 changes: 6 additions & 37 deletions openatlas/api/endpoints/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@
check_if_date_search, validate_search_parameters)
from openatlas.api.resources.util import (
flatten_list_and_remove_duplicates, geometry_to_geojson,
get_geometric_collection,
get_geoms_dict, get_location_link, get_reference_systems,
get_location_link, get_reference_systems,
get_value_for_types, replace_empty_list_values_in_dict_with_none)
from openatlas.models.entity import Entity, Link
from openatlas.models.gis import Gis


class Parser:
Expand Down Expand Up @@ -179,21 +177,6 @@ def set_start_entity(self, total: list[int]) -> list[Any]:
return out
raise EntityDoesNotExistError

def get_geom(self, entity: Entity, ) -> list[Any]:
if entity.class_.view == 'place' or entity.class_.name == 'artifact':
id_ = entity.get_linked_entity_safe('P53').id
geoms = Gis.get_by_id(id_)
if self.centroid:
if centroid_result := Gis.get_centroids_by_id(id_):
geoms.extend(centroid_result)
return geoms
if entity.class_.name == 'object_location':
geoms = Gis.get_by_id(entity.id)
if self.centroid:
if centroid_result := Gis.get_centroids_by_id(entity.id):
geoms.extend(centroid_result)
return geoms
return []


def get_linked_places_entity(
Expand Down Expand Up @@ -233,11 +216,13 @@ def get_linked_places_entity(

def get_geojson_dict(
self,
entity: Entity,
geom: Optional[dict[str, Any]] = None) -> dict[str, Any]:
entity_dict: dict[str, Any],
geometry: Optional[dict[str, Any]] = None ) -> dict[str, Any]:
entity = entity_dict['entity']
geoms = geometry or geometry_to_geojson(entity_dict['geometry'])
return replace_empty_list_values_in_dict_with_none({
'type': 'Feature',
'geometry': geom,
'geometry': geoms,
'properties': {
'@id': entity.id,
'systemClass': entity.class_.name,
Expand Down Expand Up @@ -265,22 +250,6 @@ def get_geojson_dict(
for type_ in entity.types]
if 'types' in self.show else None}})

def get_geoms_as_collection(
self,
entity: Entity,
links: list[int]) -> Optional[dict[str, Any]]:
if entity.class_.name == 'object_location':
geoms: list[Any] = Gis.get_by_id(entity.id)
if self.centroid:
if centroid_result := Gis.get_centroids_by_id(entity.id):
geoms.extend(centroid_result)
return get_geoms_dict(geoms)
if links:
geoms = [Gis.get_by_id(id_) for id_ in links]
if self.centroid:
geoms.extend([Gis.get_centroids_by_id(id_) for id_ in links])
return get_geoms_dict(flatten_list_and_remove_duplicates(geoms))
return None

def rdf_output(
self,
Expand Down
13 changes: 3 additions & 10 deletions openatlas/api/formats/presentation_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from flask import g, url_for

from openatlas import app
from openatlas.api.endpoints.parser import Parser
from openatlas.api.formats.linked_places import get_lp_time
from openatlas.api.resources.util import (
get_crm_relation_x, geometry_to_geojson,
Expand All @@ -21,7 +20,7 @@ def get_presentation_types(
links: list[Link]) -> list[dict[str, Any]]:
types = []
location_types = {}
if entity.class_.view in ['place', 'artifact']:
if entity.class_.view == 'place':
location_types = get_location_link(links).range.types
for type_ in entity.types | location_types:
is_standard = False
Expand Down Expand Up @@ -121,29 +120,23 @@ def get_presentation_view(entity: Entity) -> dict[str, Any]:
standard_type = {
'id': rel_entity.standard_type.id,
'title': rel_entity.standard_type.name}
geometries = {}
if geoms.get(rel_entity.id):
geometries = geometry_to_geojson(geoms[rel_entity.id])
relations[rel_entity.class_.name].append({
'id': rel_entity.id,
'systemClass': rel_entity.class_.name,
'title': rel_entity.name,
'description': rel_entity.description,
'aliases': list(rel_entity.aliases.values()),
'geometries': geometries,
'geometries': geometry_to_geojson(geoms.get(rel_entity.id)),
'when': get_lp_time(rel_entity),
'standardType': standard_type,
'relationTypes': relation_types[rel_entity.id]})
geometries = {}
if geoms.get(entity.id):
geometries = geometry_to_geojson(geoms[entity.id])
data = {
'id': entity.id,
'systemClass': entity.class_.name,
'title': entity.name,
'description': entity.description,
'aliases': list(entity.aliases.values()),
'geometries': geometries,
'geometries': geometry_to_geojson(geoms.get(entity.id)),
'when': get_lp_time(entity),
'types': get_presentation_types(entity, links),
'externalReferenceSystems': get_reference_systems(links_inverse),
Expand Down
25 changes: 7 additions & 18 deletions openatlas/api/formats/subunits.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from collections import defaultdict
from operator import attrgetter
from typing import Any, Optional

from flask import g

from openatlas.api.resources.util import (
geometry_to_geojson, get_license_name, get_location_links,
geometry_to_geojson, get_license_name,
get_reference_systems, remove_duplicate_entities,
replace_empty_list_values_in_dict_with_none)
from openatlas.display.util import get_file_path
Expand Down Expand Up @@ -54,7 +53,7 @@ def get_geometries_thanados(
geometries = []
for item in geom['geometries']:
if not item:
continue
continue # pragma: no cover
item['coordinates'] = transform_geometries_for_xml(item)
geometries.append(item)
geom['geometries'] = [{'geom': item} for item in geometries]
Expand Down Expand Up @@ -221,23 +220,13 @@ def get_subunits_from_id(
link_dict[link_.domain.id]['links'].append(link_)
for link_ in links_inverse:
link_dict[link_.range.id]['links_inverse'].append(link_)
location_links = get_location_links(links)
location_ids = [l_.range.id for l_ in location_links]
location_geoms = Gis.get_by_ids(location_ids)

location_centroids = defaultdict(list)
for id_, geom in Gis.get_by_entities(entities).items():
link_dict[id_]['geoms'].extend(geom)
if parser['centroid']:
location_centroids = Gis.get_centroids_by_ids(location_ids)
for id_, geom in \
Gis.get_centroids_by_entities(entities).items():
link_dict[id_]['geoms'].extend(geom)

for entity_ in entities:
for link_ in location_links:
if entity_.id == link_.domain.id:
entity_.location = link_.range
link_dict[entity_.id]['geoms'].extend(
location_geoms[link_.range.id])
if parser['centroid'] and location_centroids:
link_dict[entity_.id]['geoms'].extend(
location_centroids[link_.range.id])
external_reference = get_type_links_inverse(entities)
entities_dict: dict[int, Any] = {}
for entity_ in entities:
Expand Down
82 changes: 4 additions & 78 deletions openatlas/api/resources/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ def get_reference_systems(links_inverse: list[Link]) -> list[dict[str, Any]]:
'referenceSystem': system.name})
return ref


def get_iiif_manifest_and_path(img_id: int) -> dict[str, str]:
iiif_manifest = ''
iiif_base_path= ''
iiif_base_path = ''
if check_iiif_activation() and check_iiif_file_exist(img_id):
iiif_manifest = url_for(
'api.iiif_manifest',
Expand All @@ -137,81 +138,14 @@ def get_iiif_manifest_and_path(img_id: int) -> dict[str, str]:
f"{g.settings['iiif_url']}{img_id}{g.files[img_id].suffix}"
return {'IIIFManifest': iiif_manifest, 'IIIFBasePath': iiif_base_path}

def get_geometric_collection(
entity: Entity,
links: list[Link],
parser: Any) -> Optional[dict[str, Any]]:
geometry = None
match entity.class_.view:
case 'place' | 'artifact':
geometry = get_geoms_by_entity(
get_location_id(links),
parser.centroid)
case 'actor':
geoms = [
Gis.get_by_id(link_.range.id) for link_ in links
if link_.property.code in ['P74', 'OA8', 'OA9']]
if parser.centroid:
centroids = []
for link_ in links: # pragma: no cover
if link_.property.code in ['P7', 'P26', 'P27']:
if centroid_result := (
Gis.get_centroids_by_id(link_.range.id)):
centroids.append(centroid_result)
if centroids:
geoms.extend(centroids) # pragma: no cover
geometry = {
'type': 'GeometryCollection',
'geometries': [geom for sublist in geoms for geom in sublist]}
case 'event':
geoms = [
Gis.get_by_id(link_.range.id) for link_ in links
if link_.property.code in ['P7', 'P26', 'P27']]
if parser.centroid:
centroids = []
for link_ in links:
if link_.property.code in ['P7', 'P26', 'P27']:
if centroid_result := (
Gis.get_centroids_by_id(link_.range.id)):
centroids.append(centroid_result)
if centroids:
geoms.extend(centroids)
geometry = {
'type': 'GeometryCollection',
'geometries': [geom for sublist in geoms for geom in sublist]}
case _ if entity.class_.name == 'object_location':
geometry = get_geoms_by_entity(entity.id, parser.centroid)
return geometry


def get_location_id(links: list[Link]) -> int:
return [l_.range.id for l_ in links if l_.property.code == 'P53'][0]


def get_location_links(links: list[Link]) -> list[Link]:
return [link_ for link_ in links if link_.property.code == 'P53']


def get_location_link(links: list[Link]) -> Link:
return [l_ for l_ in links if l_.property.code == 'P53'][0]


def get_geoms_by_entity(
location_id: int,
centroid: Optional[bool] = False) -> Optional[dict[str, Any]]:
geoms = Gis.get_by_id(location_id)
if not geoms:
return None
if centroid:
if centroid_result := Gis.get_centroids_by_id(location_id):
geoms.extend(centroid_result)
if len(geoms) == 1:
return geoms[0]
return {'type': 'GeometryCollection', 'geometries': geoms}


def geometry_to_geojson(
geoms: list[dict[str, Any]]) -> Optional[dict[str, Any]]:
geoms: Optional[list[dict[str, Any]]] = None) \
-> Optional[dict[str, Any]]:
if not geoms:
return None
if len(geoms) == 1:
Expand Down Expand Up @@ -270,14 +204,6 @@ def flatten_list_and_remove_duplicates(list_: list[Any]) -> list[Any]:
return [item for sublist in list_ for item in sublist if item not in list_]


def get_geoms_dict(geoms: list[dict[str, Any]]) -> Optional[dict[str, Any]]:
if len(geoms) == 0:
return None
if len(geoms) == 1:
return geoms[0]
return {'type': 'GeometryCollection', 'geometries': geoms}


def get_value_for_types(type_: Entity, links: list[Link]) -> dict[str, str]:
type_dict = {}
for link in links:
Expand Down
Loading

0 comments on commit 714ab3a

Please sign in to comment.