From 6d755ee8dcff5ea0eb189b835f29baf5af0d7db0 Mon Sep 17 00:00:00 2001 From: Tykayn Date: Thu, 18 Sep 2025 23:43:06 +0200 Subject: [PATCH] up demo --- doc/{SEARCH_ENDPOINT.md => api_search.md} | 0 .../extractor_sncf.md | 0 extractors/osm_cal.py | 62 +- oedb/resources/demo.py | 766 ++++++++++++++++++ oedb/utils/db.py | 14 +- ...ION.md => systemd_service_installation.md} | 0 test_env_path.py | 153 ++++ test_osm_cal.py | 80 ++ 8 files changed, 1058 insertions(+), 17 deletions(-) rename doc/{SEARCH_ENDPOINT.md => api_search.md} (100%) rename extractors/SNCF_TRAVAUX_IMPLEMENTATION.md => doc/extractor_sncf.md (100%) rename server_config/{SYSTEMD_SERVICE_INSTALLATION.md => systemd_service_installation.md} (100%) create mode 100755 test_env_path.py create mode 100755 test_osm_cal.py diff --git a/doc/SEARCH_ENDPOINT.md b/doc/api_search.md similarity index 100% rename from doc/SEARCH_ENDPOINT.md rename to doc/api_search.md diff --git a/extractors/SNCF_TRAVAUX_IMPLEMENTATION.md b/doc/extractor_sncf.md similarity index 100% rename from extractors/SNCF_TRAVAUX_IMPLEMENTATION.md rename to doc/extractor_sncf.md diff --git a/extractors/osm_cal.py b/extractors/osm_cal.py index 83bce59..2bd0d3a 100755 --- a/extractors/osm_cal.py +++ b/extractors/osm_cal.py @@ -284,6 +284,7 @@ def event_exists(db, properties): Returns: bool: True if the event exists, False otherwise. """ + print('event: ', properties) try: cur = db.cursor() @@ -348,6 +349,7 @@ def submit_event(event): cur = db.cursor() geometry = json.dumps(event['geometry']) + print('event: ', event) # Insert the geometry into the geo table cur.execute(""" INSERT INTO geo @@ -361,20 +363,56 @@ def submit_event(event): hash_result = cur.fetchone() if hash_result is None: - # If the hash is None, get it from the database + # If the hash is None, check if the geometry already exists in the database cur.execute(""" - SELECT md5(st_asewkt(geom)), - ST_IsValid(geom), - ST_IsValidReason(geom) from (SELECT st_geomfromgeojson(%s) as geom) as g; + SELECT hash FROM geo + WHERE hash = md5(st_astext(st_setsrid(st_geomfromgeojson(%s),4326))); """, (geometry,)) - hash_result = cur.fetchone() - - if hash_result is None or (len(hash_result) > 1 and not hash_result[1]): - logger.error(f"Invalid geometry for event: {properties.get('label')}") - db.close() - return False - - geo_hash = hash_result[0] + existing_hash = cur.fetchone() + + if existing_hash: + # Geometry already exists in the database, use its hash + geo_hash = existing_hash[0] + logger.info(f"Using existing geometry with hash: {geo_hash}") + else: + # Geometry doesn't exist, try to insert it directly + cur.execute(""" + SELECT md5(st_astext(geom)) as hash, + ST_IsValid(geom), + ST_IsValidReason(geom) from (SELECT st_setsrid(st_geomfromgeojson(%s),4326) as geom) as g; + """, (geometry,)) + hash_result = cur.fetchone() + + if hash_result is None or not hash_result[1]: + logger.error(f"Invalid geometry for event: {properties.get('label')}") + if hash_result and len(hash_result) > 2: + logger.error(f"Reason: {hash_result[2]}") + db.close() + return False + + geo_hash = hash_result[0] + + # Now insert the geometry explicitly + cur.execute(""" + INSERT INTO geo (geom, hash, geom_center) + VALUES ( + st_setsrid(st_geomfromgeojson(%s),4326), + %s, + st_centroid(st_setsrid(st_geomfromgeojson(%s),4326)) + ) + ON CONFLICT (hash) DO NOTHING; + """, (geometry, geo_hash, geometry)) + + # Verify the geometry was inserted + cur.execute("SELECT 1 FROM geo WHERE hash = %s", (geo_hash,)) + if cur.fetchone() is None: + logger.error(f"Failed to insert geometry with hash: {geo_hash}") + db.close() + return False + + logger.info(f"Inserted new geometry with hash: {geo_hash}") + else: + geo_hash = hash_result[0] # Determine the bounds for the time range bounds = '[]' if properties['start'] == properties['stop'] else '[)' diff --git a/oedb/resources/demo.py b/oedb/resources/demo.py index 82c6bba..159b321 100644 --- a/oedb/resources/demo.py +++ b/oedb/resources/demo.py @@ -505,6 +505,7 @@ class DemoResource:

Demo Pages: