From aaa8afd59e58e2246c90bf0804d9998abdab7991 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20UTARD?= Date: Thu, 4 Apr 2019 19:17:43 +0200 Subject: [PATCH] Fix Postgis function case and underscores --- geotrek/altimetry/helpers.py | 2 +- geotrek/altimetry/sql/10_utilities.sql | 6 +-- geotrek/api/v2/functions.py | 20 ++++---- geotrek/common/utils/__init__.py | 2 +- .../commands/remove_duplicate_paths.py | 2 +- geotrek/core/sql/50_troncons_split.sql | 22 ++++---- geotrek/core/sql/70_troncons_merge.sql | 50 +++++++++---------- geotrek/maintenance/sql/20_views.sql | 2 +- 8 files changed, 53 insertions(+), 53 deletions(-) diff --git a/geotrek/altimetry/helpers.py b/geotrek/altimetry/helpers.py index 32e27f4fb0..c7b81308c3 100644 --- a/geotrek/altimetry/helpers.py +++ b/geotrek/altimetry/helpers.py @@ -34,7 +34,7 @@ def elevation_profile(cls, geometry3d, precision=None, offset=0): # Add measure to 2D version of geometry3d # Get distance from origin for each vertex sql = """ - WITH line2d AS (SELECT ST_force_2D('%(ewkt)s'::geometry) AS geom), + WITH line2d AS (SELECT ST_Force2D('%(ewkt)s'::geometry) AS geom), line_measure AS (SELECT ST_Addmeasure(geom, 0, ST_length(geom)) AS geom FROM line2d), points2dm AS (SELECT (ST_DumpPoints(geom)).geom AS point FROM line_measure) SELECT (%(offset)s + ST_M(point)) FROM points2dm; diff --git a/geotrek/altimetry/sql/10_utilities.sql b/geotrek/altimetry/sql/10_utilities.sql index f7372fb3d9..63b7a8aee7 100644 --- a/geotrek/altimetry/sql/10_utilities.sql +++ b/geotrek/altimetry/sql/10_utilities.sql @@ -82,11 +82,11 @@ BEGIN END IF; END LOOP; - points_output := array_append(points_output, ST_MAKEPOINT(ST_X(points[i]), ST_Y(points[i]), (current_values / count_values)::integer)); + points_output := array_append(points_output, ST_MakePoint(ST_X(points[i]), ST_Y(points[i]), (current_values / count_values)::integer)); END LOOP; - --RAISE EXCEPTION 'Nonexistent ID --> %', ST_ASEWKT(ST_SetSRID(ST_MakeLine(points_output), ST_SRID(linegeom))); + --RAISE EXCEPTION 'Nonexistent ID --> %', ST_AsEWKT(ST_SetSRID(ST_MakeLine(points_output), ST_SRID(linegeom))); RETURN QUERY SELECT (ST_DumpPoints(ST_SetSRID(ST_MakeLine(points_output), ST_SRID(linegeom)))).geom as geom; @@ -292,7 +292,7 @@ BEGIN -- Compute gain using simplification -- see http://www.postgis.org/docs/ST_Simplify.html -- https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm - FOR current IN SELECT (ST_DUMPPOINTS(ST_MAKELINE(points3d_smoothed))).geom + FOR current IN SELECT (ST_DumpPoints(ST_MakeLine(points3d_smoothed))).geom LOOP -- Add positive only if current - previous_geom > 0 result.positive_gain := result.positive_gain + greatest(ST_Z(current) - coalesce(ST_Z(previous_geom), diff --git a/geotrek/api/v2/functions.py b/geotrek/api/v2/functions.py index bb238567da..5f9c7cc742 100644 --- a/geotrek/api/v2/functions.py +++ b/geotrek/api/v2/functions.py @@ -7,9 +7,9 @@ def Transform(geom, srid): """ - ST_TRANSFORM postgis function + ST_Transform postgis function """ - return Func(geom, srid, function='ST_TRANSFORM') + return Func(geom, srid, function='ST_Transform') def Buffer(geom, radius, num_seg): @@ -28,9 +28,9 @@ def GeometryType(geom): class Length(Func): """ - ST_LENGTH postgis function + ST_Length postgis function """ - function = 'ST_LENGTH' + function = 'ST_Length' output_field = FloatField() @@ -44,23 +44,23 @@ class Length3D(Func): class Area(Func): """ - ST_AREA postgis function + ST_Area postgis function """ - function = 'ST_AREA' + function = 'ST_Area' output_field = FloatField() class StartPoint(Func): """ - ST_TRANSFORM postgis function + ST_StartPoint postgis function """ - function = 'ST_STARTPOINT' + function = 'ST_StartPoint' output_field = PointField() class EndPoint(Func): """ - ST_TRANSFORM postgis function + ST_EndPoint postgis function """ - function = 'ST_ENDPOINT' + function = 'ST_EndPoint' output_field = PointField() diff --git a/geotrek/common/utils/__init__.py b/geotrek/common/utils/__init__.py index db2d991c4f..6e112b075b 100644 --- a/geotrek/common/utils/__init__.py +++ b/geotrek/common/utils/__init__.py @@ -122,7 +122,7 @@ def intersecting(cls, obj, distance=None): if obj.geom.geom_type == 'LineString': # FIXME: move transform from DRF viewset to DRF itself and remove transform here ewkt = obj.geom.transform(settings.SRID, clone=True).ewkt - qs = qs.extra(select={'ordering': 'ST_Line_Locate_Point(ST_GeomFromEWKT(\'{ewkt}\'), ST_StartPoint((ST_Dump(ST_Intersection(ST_GeomFromEWKT(\'{ewkt}\'), geom))).geom))'.format(ewkt=ewkt)}) + qs = qs.extra(select={'ordering': 'ST_LineLocatePoint(ST_GeomFromEWKT(\'{ewkt}\'), ST_StartPoint((ST_Dump(ST_Intersection(ST_GeomFromEWKT(\'{ewkt}\'), geom))).geom))'.format(ewkt=ewkt)}) qs = qs.extra(order_by=['ordering']) if obj.__class__ == cls: # Prevent self intersection diff --git a/geotrek/core/management/commands/remove_duplicate_paths.py b/geotrek/core/management/commands/remove_duplicate_paths.py index cf0d2983d2..b171b2a8c0 100644 --- a/geotrek/core/management/commands/remove_duplicate_paths.py +++ b/geotrek/core/management/commands/remove_duplicate_paths.py @@ -16,7 +16,7 @@ def handle(self, *args, **options): query = """with relations as (SELECT t1.id as t1_id, t2.id as t2_id FROM l_t_troncon t1 - JOIN l_t_troncon t2 ON t1.id < t2.id AND ST_ORDERINGEQUALS(t1.geom, t2.geom) + JOIN l_t_troncon t2 ON t1.id < t2.id AND ST_OrderingEquals(t1.geom, t2.geom) ORDER BY t1.id, t2.id) SELECT * FROM relations WHERE t1_id NOT IN (SELECT t2_id FROM relations) """ cursor.execute(query) diff --git a/geotrek/core/sql/50_troncons_split.sql b/geotrek/core/sql/50_troncons_split.sql index b52b09a830..2e8ea6e140 100644 --- a/geotrek/core/sql/50_troncons_split.sql +++ b/geotrek/core/sql/50_troncons_split.sql @@ -119,19 +119,19 @@ BEGIN WHERE id != NEW.id AND brouillon = FALSE AND NEW.brouillon = FALSE - AND ST_DWITHIN(t.geom, NEW.geom, 0) + AND ST_DWithin(t.geom, NEW.geom, 0) AND GeometryType(ST_Intersection(geom, NEW.geom)) NOT IN ('LINESTRING', 'MULTILINESTRING') LOOP RAISE NOTICE '%-% (%) intersects %-% (%) : %', NEW.id, NEW.nom, ST_AsText(NEW.geom), troncon.id, troncon.nom, ST_AsText(troncon.geom), ST_AsText(ST_Intersection(troncon.geom, NEW.geom)); -- Locate intersecting point(s) on NEW, for later use - FOR fraction IN SELECT ST_Line_Locate_Point(NEW.geom, - (ST_Dump(ST_Intersection(troncon.geom, NEW.geom))).geom) - WHERE NOT ST_EQUALS(ST_STARTPOINT(NEW.geom), ST_STARTPOINT(troncon.geom)) - AND NOT ST_EQUALS(ST_STARTPOINT(NEW.geom), ST_ENDPOINT(troncon.geom)) - AND NOT ST_EQUALS(ST_ENDPOINT(NEW.geom), ST_STARTPOINT(troncon.geom)) - AND NOT ST_EQUALS(ST_ENDPOINT(NEW.geom), ST_ENDPOINT(troncon.geom)) + FOR fraction IN SELECT ST_LineLocatePoint(NEW.geom, + (ST_Dump(ST_Intersection(troncon.geom, NEW.geom))).geom) + WHERE NOT ST_Equals(ST_StartPoint(NEW.geom), ST_StartPoint(troncon.geom)) + AND NOT ST_Equals(ST_StartPoint(NEW.geom), ST_EndPoint(troncon.geom)) + AND NOT ST_Equals(ST_EndPoint(NEW.geom), ST_StartPoint(troncon.geom)) + AND NOT ST_Equals(ST_EndPoint(NEW.geom), ST_EndPoint(troncon.geom)) LOOP intersections_on_new := array_append(intersections_on_new, fraction); END LOOP; @@ -144,18 +144,18 @@ BEGIN -- Locate intersecting point(s) on current path (array of : {0, 0.32, 0.89, 1}) intersections_on_current := ARRAY[0::float]; - IF ST_DWITHIN(ST_STARTPOINT(NEW.geom), troncon.geom, 0) + IF ST_DWithin(ST_StartPoint(NEW.geom), troncon.geom, 0) THEN intersections_on_current := array_append(intersections_on_current, ST_LineLocatePoint(troncon.geom, - ST_CLOSESTPOINT(troncon.geom, ST_STARTPOINT(NEW.geom)))); + ST_ClosestPoint(troncon.geom, ST_StartPoint(NEW.geom)))); END IF; - IF ST_DWITHIN(ST_ENDPOINT(NEW.geom), troncon.geom, 0) + IF ST_DWithin(ST_EndPoint(NEW.geom), troncon.geom, 0) THEN intersections_on_current := array_append(intersections_on_current, ST_LineLocatePoint(troncon.geom, - ST_CLOSESTPOINT(troncon.geom, ST_ENDPOINT(NEW.geom)))); + ST_ClosestPoint(troncon.geom, ST_EndPoint(NEW.geom)))); END IF; RAISE NOTICE 'EEE : %', array_to_string(intersections_on_current, ', '); diff --git a/geotrek/core/sql/70_troncons_merge.sql b/geotrek/core/sql/70_troncons_merge.sql index bb3f2bf0c5..7410e15228 100644 --- a/geotrek/core/sql/70_troncons_merge.sql +++ b/geotrek/core/sql/70_troncons_merge.sql @@ -28,45 +28,45 @@ BEGIN merged_geom := (SELECT geom FROM l_t_troncon WHERE id = merged); -- DETECT matching point to rebuild path line - IF ST_EQUALS(ST_STARTPOINT(updated_geom), ST_STARTPOINT(merged_geom)) + IF ST_Equals(ST_StartPoint(updated_geom), ST_StartPoint(merged_geom)) THEN - rebuild_line := ST_MAKELINE(ST_REVERSE(updated_geom), merged_geom); + rebuild_line := ST_MakeLine(ST_Reverse(updated_geom), merged_geom); reverse_update := TRUE; - ELSIF ST_EQUALS(ST_STARTPOINT(updated_geom), ST_ENDPOINT(merged_geom)) + ELSIF ST_Equals(ST_StartPoint(updated_geom), ST_EndPoint(merged_geom)) THEN - rebuild_line := ST_MAKELINE(ST_REVERSE(updated_geom), ST_REVERSE(merged_geom)); + rebuild_line := ST_MakeLine(ST_Reverse(updated_geom), ST_Reverse(merged_geom)); reverse_update := TRUE; reverse_merged := TRUE; - ELSIF ST_EQUALS(ST_ENDPOINT(updated_geom), ST_ENDPOINT(merged_geom)) + ELSIF ST_Equals(ST_EndPoint(updated_geom), ST_EndPoint(merged_geom)) THEN - rebuild_line := ST_MAKELINE(updated_geom, ST_REVERSE(merged_geom)); + rebuild_line := ST_MakeLine(updated_geom, ST_Reverse(merged_geom)); reverse_merged := TRUE; - ELSIF ST_EQUALS(ST_ENDPOINT(updated_geom), ST_STARTPOINT(merged_geom)) + ELSIF ST_Equals(ST_EndPoint(updated_geom), ST_StartPoint(merged_geom)) THEN - rebuild_line := ST_MAKELINE(updated_geom, merged_geom); + rebuild_line := ST_MakeLine(updated_geom, merged_geom); - ELSIF (ST_DISTANCE(ST_STARTPOINT(updated_geom), ST_STARTPOINT(merged_geom))::float <= max_snap_distance) + ELSIF (ST_Distance(ST_StartPoint(updated_geom), ST_StartPoint(merged_geom))::float <= max_snap_distance) THEN - rebuild_line := ST_MAKELINE(ST_REVERSE(updated_geom), merged_geom); + rebuild_line := ST_MakeLine(ST_Reverse(updated_geom), merged_geom); reverse_update := TRUE; - ELSIF (ST_DISTANCE(ST_STARTPOINT(updated_geom), ST_ENDPOINT(merged_geom)) <= max_snap_distance) + ELSIF (ST_Distance(ST_StartPoint(updated_geom), ST_EndPoint(merged_geom)) <= max_snap_distance) THEN - rebuild_line := ST_MAKELINE(ST_REVERSE(updated_geom), ST_REVERSE(merged_geom)); + rebuild_line := ST_MakeLine(ST_Reverse(updated_geom), ST_Reverse(merged_geom)); reverse_update := TRUE; reverse_merged := TRUE; - ELSIF (ST_DISTANCE(ST_ENDPOINT(updated_geom), ST_ENDPOINT(merged_geom)) <= max_snap_distance) + ELSIF (ST_Distance(ST_EndPoint(updated_geom), ST_EndPoint(merged_geom)) <= max_snap_distance) THEN - rebuild_line := ST_MAKELINE(updated_geom, ST_REVERSE(merged_geom)); + rebuild_line := ST_MakeLine(updated_geom, ST_Reverse(merged_geom)); reverse_merged := TRUE; - ELSIF (ST_DISTANCE(ST_ENDPOINT(updated_geom), ST_STARTPOINT(merged_geom)) <= max_snap_distance) + ELSIF (ST_Distance(ST_EndPoint(updated_geom), ST_StartPoint(merged_geom)) <= max_snap_distance) THEN - rebuild_line := ST_MAKELINE(updated_geom, merged_geom); + rebuild_line := ST_MakeLine(updated_geom, merged_geom); ELSE -- no snapping -> END ! @@ -77,7 +77,7 @@ BEGIN FOR element IN SELECT * FROM l_t_troncon WHERE id != updated AND id != merged LOOP - IF ST_EQUALS(ST_STARTPOINT(updated_geom), ST_STARTPOINT(element.geom)) OR ST_EQUALS(ST_ENDPOINT(updated_geom), ST_STARTPOINT(element.geom)) OR ST_EQUALS(ST_STARTPOINT(updated_geom), ST_ENDPOINT(element.geom)) OR ST_EQUALS(ST_ENDPOINT(updated_geom), ST_ENDPOINT(element.geom)) + IF ST_Equals(ST_StartPoint(updated_geom), ST_StartPoint(element.geom)) OR ST_Equals(ST_EndPoint(updated_geom), ST_StartPoint(element.geom)) OR ST_Equals(ST_StartPoint(updated_geom), ST_EndPoint(element.geom)) OR ST_Equals(ST_EndPoint(updated_geom), ST_EndPoint(element.geom)) THEN RETURN 2; END IF; @@ -94,8 +94,8 @@ BEGIN THEN -- update reverse pk UPDATE e_r_evenement_troncon - SET pk_debut = (1- pk_debut) * ST_LENGTH(updated_geom) / (ST_LENGTH(updated_geom) + ST_LENGTH(merged_geom)), - pk_fin = (1- pk_fin) * ST_LENGTH(updated_geom) / (ST_LENGTH(updated_geom) + ST_LENGTH(merged_geom)) + SET pk_debut = (1- pk_debut) * ST_Length(updated_geom) / (ST_Length(updated_geom) + ST_Length(merged_geom)), + pk_fin = (1- pk_fin) * ST_Length(updated_geom) / (ST_Length(updated_geom) + ST_Length(merged_geom)) WHERE id = element.id; -- update reverse offset UPDATE e_t_evenement @@ -103,8 +103,8 @@ BEGIN WHERE id = element.evenement; ELSE UPDATE e_r_evenement_troncon - SET pk_debut = pk_debut * ST_LENGTH(updated_geom) / (ST_LENGTH(updated_geom) + ST_LENGTH(merged_geom)), - pk_fin = pk_fin * ST_LENGTH(updated_geom) / (ST_LENGTH(updated_geom) + ST_LENGTH(merged_geom)) + SET pk_debut = pk_debut * ST_Length(updated_geom) / (ST_Length(updated_geom) + ST_Length(merged_geom)), + pk_fin = pk_fin * ST_Length(updated_geom) / (ST_Length(updated_geom) + ST_Length(merged_geom)) WHERE id = element.id; END IF; END LOOP; @@ -119,8 +119,8 @@ BEGIN IF reverse_merged = TRUE THEN UPDATE e_r_evenement_troncon - SET pk_debut = ((1- pk_debut) * ST_LENGTH(merged_geom) / (ST_LENGTH(updated_geom) + ST_LENGTH(merged_geom))) + (ST_LENGTH(updated_geom) / (ST_LENGTH(updated_geom) + ST_LENGTH(merged_geom))), - pk_fin = ((1- pk_fin) * ST_LENGTH(merged_geom) / (ST_LENGTH(updated_geom) + ST_LENGTH(merged_geom))) + (ST_LENGTH(updated_geom) / (ST_LENGTH(updated_geom) + ST_LENGTH(merged_geom))) + SET pk_debut = ((1- pk_debut) * ST_Length(merged_geom) / (ST_Length(updated_geom) + ST_Length(merged_geom))) + (ST_Length(updated_geom) / (ST_Length(updated_geom) + ST_Length(merged_geom))), + pk_fin = ((1- pk_fin) * ST_Length(merged_geom) / (ST_Length(updated_geom) + ST_Length(merged_geom))) + (ST_Length(updated_geom) / (ST_Length(updated_geom) + ST_Length(merged_geom))) WHERE id = element.id; UPDATE e_t_evenement @@ -128,8 +128,8 @@ BEGIN WHERE id = element.evenement; ELSE UPDATE e_r_evenement_troncon - SET pk_debut = (pk_debut * ST_LENGTH(merged_geom) / (ST_LENGTH(updated_geom) + ST_LENGTH(merged_geom))) + (ST_LENGTH(updated_geom) / (ST_LENGTH(updated_geom) + ST_LENGTH(merged_geom))), - pk_fin = (pk_fin * ST_LENGTH(merged_geom) / (ST_LENGTH(updated_geom) + ST_LENGTH(merged_geom))) + (ST_LENGTH(updated_geom) / (ST_LENGTH(updated_geom) + ST_LENGTH(merged_geom))) + SET pk_debut = (pk_debut * ST_Length(merged_geom) / (ST_Length(updated_geom) + ST_Length(merged_geom))) + (ST_Length(updated_geom) / (ST_Length(updated_geom) + ST_Length(merged_geom))), + pk_fin = (pk_fin * ST_Length(merged_geom) / (ST_Length(updated_geom) + ST_Length(merged_geom))) + (ST_Length(updated_geom) / (ST_Length(updated_geom) + ST_Length(merged_geom))) WHERE id = element.id; END IF; END LOOP; diff --git a/geotrek/maintenance/sql/20_views.sql b/geotrek/maintenance/sql/20_views.sql index fd4be98442..1712f5209c 100644 --- a/geotrek/maintenance/sql/20_views.sql +++ b/geotrek/maintenance/sql/20_views.sql @@ -6,7 +6,7 @@ CREATE OR REPLACE VIEW gestion.m_v_intervention AS ( ); CREATE OR REPLACE VIEW gestion.m_v_chantier AS ( - SELECT ST_UNION(t.geom) AS geom_chantier, s.* + SELECT ST_Union(t.geom) AS geom_chantier, s.* FROM m_v_intervention AS t, m_t_chantier AS s WHERE t.chantier = s.id GROUP BY t.chantier, s.id