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: