diff --git a/api/migrations/versions/587e69ecb466_transform_overtaking_event_geometry_to_.py b/api/migrations/versions/587e69ecb466_transform_overtaking_event_geometry_to_.py new file mode 100644 index 0000000..3cc6373 --- /dev/null +++ b/api/migrations/versions/587e69ecb466_transform_overtaking_event_geometry_to_.py @@ -0,0 +1,30 @@ +"""transform overtaking_event geometry to 3857 + +Revision ID: 587e69ecb466 +Revises: f4b0f460254d +Create Date: 2023-04-01 14:30:49.927505 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "587e69ecb466" +down_revision = "f4b0f460254d" +branch_labels = None +depends_on = None + + +def upgrade(): + op.execute("UPDATE overtaking_event SET geometry = ST_Transform(geometry, 3857);") + op.execute( + "ALTER TABLE overtaking_event ALTER COLUMN eometry TYPE geometry(POINT, 3857);" + ) + + +def downgrade(): + op.execute( + "ALTER TABLE overtaking_event ALTER COLUMN overtaking_event.geometry TYPE geometry;" + ) + op.execute("UPDATE overtaking_event SET geometry = ST_Transform(geometry, 4326);") diff --git a/api/obs/api/process.py b/api/obs/api/process.py index 01a601d..354bed3 100644 --- a/api/obs/api/process.py +++ b/api/obs/api/process.py @@ -8,7 +8,7 @@ import pytz from os.path import join from datetime import datetime -from sqlalchemy import delete, select, and_ +from sqlalchemy import delete, func, select, and_ from sqlalchemy.orm import joinedload from obs.face.importer import ImportMeasurementsCsv @@ -306,11 +306,16 @@ async def import_overtaking_events(session, track, overtaking_events): hex_hash=hex_hash, way_id=m.get("OSM_way_id"), direction_reversed=m.get("OSM_way_orientation", 0) < 0, - geometry=json.dumps( - { - "type": "Point", - "coordinates": [m["longitude"], m["latitude"]], - } + geometry=func.ST_Transform( + func.ST_GeomFromGeoJSON( + json.dumps( + { + "type": "Point", + "coordinates": [m["longitude"], m["latitude"]], + } + ) + ), + 3857, ), latitude=m["latitude"], longitude=m["longitude"], diff --git a/api/obs/api/routes/stats.py b/api/obs/api/routes/stats.py index 6d65a65..54bc82a 100644 --- a/api/obs/api/routes/stats.py +++ b/api/obs/api/routes/stats.py @@ -191,9 +191,7 @@ async def stats(req): .select_from(Region) .join( OvertakingEvent, - func.ST_Within( - func.ST_Transform(OvertakingEvent.geometry, 3857), Region.geometry - ), + func.ST_Within(OvertakingEvent.geometry, Region.geometry), ) .group_by( Region.id, diff --git a/tile-generator/layers/obs_events/layer.sql b/tile-generator/layers/obs_events/layer.sql index b4448a0..99e81e2 100644 --- a/tile-generator/layers/obs_events/layer.sql +++ b/tile-generator/layers/obs_events/layer.sql @@ -3,7 +3,7 @@ RETURNS TABLE(event_id bigint, geometry geometry, distance_overtaker float, dist SELECT overtaking_event.id::bigint as event_id, - ST_Transform(overtaking_event.geometry, 3857) as geometry, + overtaking_event.geometry as geometry, distance_overtaker, distance_stationary, (case when direction_reversed then -1 else 1 end)::int as direction, @@ -14,7 +14,7 @@ RETURNS TABLE(event_id bigint, geometry geometry, distance_overtaker float, dist FROM overtaking_event FULL OUTER JOIN road ON road.way_id = overtaking_event.way_id JOIN track on track.id = overtaking_event.track_id - WHERE ST_Transform(overtaking_event.geometry, 3857) && bbox + WHERE overtaking_event.geometry && bbox AND zoom_level >= 8 AND (user_id is NULL OR user_id = track.author_id) AND time BETWEEN COALESCE(min_time, '1900-01-01'::timestamp) AND COALESCE(max_time, '2100-01-01'::timestamp); diff --git a/tile-generator/layers/obs_events/obs_events.yaml b/tile-generator/layers/obs_events/obs_events.yaml index aa03e5c..9cfb2f2 100644 --- a/tile-generator/layers/obs_events/obs_events.yaml +++ b/tile-generator/layers/obs_events/obs_events.yaml @@ -19,7 +19,7 @@ layer: zone: | rural or urban defaults: - srs: EPSG:3785 + srs: EPSG:3857 datasource: srid: 3857 geometry_field: geometry diff --git a/tile-generator/layers/obs_regions/layer.sql b/tile-generator/layers/obs_regions/layer.sql index 3581b69..4bfe412 100644 --- a/tile-generator/layers/obs_regions/layer.sql +++ b/tile-generator/layers/obs_regions/layer.sql @@ -16,7 +16,7 @@ RETURNS TABLE( region.name as name, count(overtaking_event.id)::int as overtaking_event_count FROM region - LEFT OUTER JOIN overtaking_event on ST_Within(ST_Transform(overtaking_event.geometry, 3857), region.geometry) + LEFT OUTER JOIN overtaking_event on ST_Within(overtaking_event.geometry, region.geometry) WHERE zoom_level >= 3 AND zoom_level <= 12 AND diff --git a/tile-generator/layers/obs_regions/obs_regions.yaml b/tile-generator/layers/obs_regions/obs_regions.yaml index 2401923..d995e56 100644 --- a/tile-generator/layers/obs_regions/obs_regions.yaml +++ b/tile-generator/layers/obs_regions/obs_regions.yaml @@ -9,7 +9,7 @@ layer: name: | Name of the region defaults: - srs: EPSG:3785 + srs: EPSG:3857 datasource: srid: 3857 geometry_field: geometry diff --git a/tile-generator/layers/obs_roads/obs_roads.yaml b/tile-generator/layers/obs_roads/obs_roads.yaml index 2f1f926..b1e7d97 100644 --- a/tile-generator/layers/obs_roads/obs_roads.yaml +++ b/tile-generator/layers/obs_roads/obs_roads.yaml @@ -27,7 +27,7 @@ layer: offset_direction: | Factor for offset to shift the line to the driving side. One of -1, 0, 1. defaults: - srs: EPSG:3785 + srs: EPSG:3857 datasource: srid: 3857 geometry_field: geometry