Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardKoschicek committed Dec 10, 2024
1 parent f2a0353 commit 31af09f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 34 deletions.
29 changes: 21 additions & 8 deletions openatlas/api/endpoints/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
from openatlas import app
from openatlas.api.endpoints.parser import Parser
from openatlas.api.formats.csv import (
build_dataframe, build_dataframe_network, build_link_dataframe)
build_dataframe_with_relations, build_dataframe, build_link_dataframe)
from openatlas.api.formats.loud import get_loud_entities
from openatlas.api.resources.resolve_endpoints import (
download, parse_loud_context)
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 (
get_linked_entities_api, get_location_link)
from openatlas.api.resources.util import get_location_link
from openatlas.models.entity import Entity, Link


Expand Down Expand Up @@ -84,7 +83,15 @@ def resolve_entities(self) -> Response | dict[str, Any]:
if self.parser.search:
self.entities = [
e for e in self.entities if self.parser.search_filter(e)]
self.remove_duplicate_entities()
before = len(self.entities)
# self.remove_duplicate_entities()
self.entities = set(self.entities)
after = len(self.entities)
if before != after:
print(before)
print(after)
self.entities = list(self.entities)

if self.parser.count == 'true':
return jsonify(len(self.entities))
self.sort_entities()
Expand Down Expand Up @@ -132,7 +139,7 @@ def filter_by_type(self) -> list[Entity]:

def export_entities_csv(self) -> Response:
frames = [
build_dataframe(e, relations=True)
build_dataframe_with_relations(e)
for e in self.entities_with_links.values()]
return Response(
pd.DataFrame(data=frames).to_csv(),
Expand Down Expand Up @@ -177,7 +184,7 @@ def get_entities_grouped_by_class(self) -> dict[str, Any]:
sorted(self.entities, key=lambda entity: entity.class_.name),
key=lambda entity: entity.class_.name):
grouped_entities[class_] = \
[build_dataframe_network(entity) for entity in entities_]
[build_dataframe(entity) for entity in entities_]
return grouped_entities

def link_parser_check(self, inverse: bool = False) -> list[Link]:
Expand All @@ -202,8 +209,14 @@ def sort_entities(self) -> None:
def remove_duplicate_entities(self) -> None:
seen: set[int] = set()
seen_add = seen.add # Faster than always call seen.add()
self.entities = \
[e for e in self.entities if not (e.id in seen or seen_add(e.id))]
entities = []
for e in self.entities:
if e.id not in seen:
seen_add(e.id)
entities.append(e)
self.entities = entities
# self.entities = \
# [e for e in self.entities if not (e.id in seen or seen_add(e.id))]

def get_entities_formatted(self) -> None:
entities = []
Expand Down
35 changes: 9 additions & 26 deletions openatlas/api/formats/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,20 @@
from openatlas.models.gis import Gis


def build_dataframe(
entity_dict: dict[str, Any],
relations: bool = False) -> dict[str, Any]:
def build_dataframe_with_relations(
entity_dict: dict[str, Any]) -> dict[str, Any]:
entity = entity_dict['entity']
geom = get_csv_geom_entry(entity)
data = {
'id': str(entity.id),
'name': entity.name,
'description': entity.description,
'begin_from': entity.begin_from,
'begin_to': entity.begin_to,
'begin_comment': entity.begin_comment,
'end_from': entity.end_from,
'end_to': entity.end_to,
'end_comment': entity.end_comment,
'cidoc_class': entity.cidoc_class.name,
'system_class': entity.class_.name,
'geom_type': geom['type'],
'coordinates': geom['coordinates']}
if relations:
for key, value in get_csv_links(entity_dict).items():
data[key] = ' | '.join(list(map(str, value)))
for key, value in get_csv_types(entity_dict).items():
data[key] = ' | '.join(list(map(str, value)))
data = build_dataframe(entity)
for key, value in get_csv_links(entity_dict).items():
data[key] = ' | '.join(list(map(str, value)))
for key, value in get_csv_types(entity_dict).items():
data[key] = ' | '.join(list(map(str, value)))
return data


def build_dataframe_network(entity: Entity) -> dict[str, Any]:
def build_dataframe(entity: Entity) -> dict[str, Any]:
geom = get_csv_geom_entry(entity)
data = {
return {
'id': str(entity.id),
'name': entity.name,
'description': entity.description,
Expand All @@ -54,7 +38,6 @@ def build_dataframe_network(entity: Entity) -> dict[str, Any]:
'system_class': entity.class_.name,
'geom_type': geom['type'],
'coordinates': geom['coordinates']}
return data


def build_link_dataframe(link: Link) -> dict[str, Any]:
Expand Down
7 changes: 7 additions & 0 deletions openatlas/models/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ def __init__(self, data: dict[str, Any]) -> None:
self.creator = g.file_info[self.id]['creator']
self.license_holder = g.file_info[self.id]['license_holder']

def __eq__(self, other):
return self.id == other.id

def __hash__(self):
return hash(('id', self.id))

def get_linked_entity(
self,
code: str,
Expand Down Expand Up @@ -586,6 +592,7 @@ def __init__(
self.last = format_date_part(self.end_to, 'year') \
if self.end_to else self.last


def update(self) -> None:
db_link.update({
'id': self.id,
Expand Down

0 comments on commit 31af09f

Please sign in to comment.