test.sql (9586B) - Raw
1 \i wm.sql 2 3 -- https://stackoverflow.com/questions/19982373/which-tools-libraries-do-you-use-to-unit-test-your-pl-pgsql 4 create or replace function assert_equals(expected anyelement, actual anyelement) returns void as $$ 5 begin 6 if expected = actual or (expected is null and actual is null) then 7 --do nothing 8 else 9 raise exception 'Assertion Error. Expected <%> but was <%>', expected, actual; 10 end if; 11 end $$ language plpgsql; 12 13 drop function if exists dbg_geomsummary; 14 create function dbg_geomsummary(geoms geometry[]) returns void as $$ 15 declare i int4; 16 begin 17 raise notice 'len: %', array_length(geoms, 1); 18 for i in 1..array_length(geoms, 1) loop 19 raise notice '% %: %', st_geometrytype(geoms[i]), lpad(i::text, 2, '0'), st_astext(geoms[i]); 20 end loop; 21 end 22 $$ language plpgsql; 23 24 drop table if exists wm_figures; 25 create table wm_figures (name text, way geometry); 26 -- add fig8.gpkg to postgis: 27 -- ogr2ogr -update -f PostgreSQL PG:"host=127.0.0.1 user=osm password=osm dbname=osm" fig8.gpkg 28 -- to "normalize" a new line when it's in `f`: 29 -- select st_astext(st_snaptogrid(st_transscale(geometry, -19.5, .016, 4000, 4000), 1)) from f; 30 insert into wm_figures (name, way) values 31 ('fig3', 'LINESTRING(0 0,12 0,13 4,20 2,20 0,32 0,33 10,38 16,43 15,44 10,44 0,60 0)'::geometry), 32 ('fig3-1', 'LINESTRING(0 0,12 0,13 4,20 2,20 0,32 0,33 10,38 16,43 15,44 10,44 0)'::geometry), 33 ('fig5', 'LINESTRING(0 39,19 52,27 77,26 104,41 115,49 115,65 103,65 75,53 45,63 15,91 0)'::geometry), 34 ('fig6', 'LINESTRING(84 47,91 59,114 64,122 80,116 92,110 93,106 106,117 118,136 107,135 76,120 45,125 39,141 39,147 32)'::geometry), 35 ('fig8', 'LINESTRING(173 12,174 10,180 8,186 8,186 13,191 11,189 6,201 5,203 11,216 16,216 6,222 6,229 3,236 2,239 6,243 8,248 6)'::geometry), 36 ('inflection-1', 'LINESTRING(110 24,114 20,133 20,145 15,145 0,136 8,123 10,114 10,111 2)'::geometry), 37 ('multi-island', 'MULTILINESTRING((-15 10,-10 10,-5 11,0 11,5 11,10 10,11 9,13 10,15 9),(-5 11,-2 15,0 16,2 15,5 11))'::geometry), 38 ('selfcrossing-1','LINESTRING(-27 180,-20 166,-21 142,-18 136,55 136,55 136,71 145,44 165,37 146,22 145,14 164,11 164,3 146,-12 146,-13 176,-18 184)'::geometry), 39 ('isolated-1', 'LINESTRING(-50 103,-48 102,-30 103,-31 105,-31 107,-27 107,-26 103,-6 103,-4 104)'::geometry), 40 ('isolated-2', 'LINESTRING(250 100,246 104,234 105,230 106,225 101,224 93,217 78,206 69)'::geometry); 41 42 insert into wm_figures (name, way) values ('fig6-rev',ST_Reverse( 43 ST_Translate((select way from wm_figures where name='fig6'), 60, 0))); 44 insert into wm_figures (name, way) values ('fig6-combi', ST_Union( 45 ST_Translate((select way from wm_figures where name='fig6'), 0, 90), 46 ST_Translate((select way from wm_figures where name='fig6'), 80, 90) 47 )); 48 49 insert into wm_figures (name, way) values ('selfcrossing-1-rev',ST_Reverse( 50 ST_Translate((select way from wm_figures where name='selfcrossing-1'), 0, 60))); 51 52 -- 3395 is now "reserved" for figures. 53 update wm_figures set way=st_setsrid(way, 3395); 54 55 delete from wm_debug where name in (select distinct name from wm_figures); 56 delete from wm_demo where name in (select distinct name from wm_figures); 57 insert into wm_demo (name, way) select name, ST_SimplifyWM(way, .1, null, name) from wm_figures where name not in ('fig8', 'isolated-1'); 58 insert into wm_demo (name, way) select name, ST_SimplifyWM(way, 14, null, name) from wm_figures where name in ('fig8', 'isolated-1', 'isolated-2'); 59 60 drop function if exists wm_debug_get; 61 create function wm_debug_get( _stage text, _name text, OUT ways geometry[]) as $$ 62 declare 63 begin 64 ways = array((select way from wm_debug where stage=_stage and name=_name order by id)); 65 end $$ language plpgsql; 66 67 do $$ 68 declare 69 vbends geometry[]; 70 begin 71 vbends = wm_debug_get('bbends', 'fig3'); 72 perform assert_equals(5, array_length(vbends, 1)); 73 perform assert_equals('LINESTRING(0 0,12 0,13 4)', st_astext(vbends[1])); 74 perform assert_equals('LINESTRING(12 0,13 4,20 2,20 0)', st_astext(vbends[2])); 75 perform assert_equals('LINESTRING(20 2,20 0,32 0,33 10)', st_astext(vbends[3])); 76 perform assert_equals('LINESTRING(32 0,33 10,38 16,43 15,44 10,44 0)', st_astext(vbends[4])); 77 perform assert_equals(4, array_length(wm_detect_bends((select way from wm_figures where name='fig3-1')), 1)); 78 select wm_detect_bends((select way from wm_figures where name='fig5')) into vbends; 79 perform assert_equals(3, array_length(vbends, 1)); 80 end $$ language plpgsql; 81 82 do $$ 83 declare 84 vbends geometry[]; 85 vinflections geometry[]; 86 begin 87 vinflections = wm_debug_get('cinflections', 'fig5'); 88 perform assert_equals('LINESTRING(0 39,19 52,27 77)', st_astext(vinflections[1])); 89 perform assert_equals('LINESTRING(19 52,27 77,26 104,41 115,49 115,65 103,65 75,53 45)', st_astext(vinflections[2])); 90 perform assert_equals('LINESTRING(65 75,53 45,63 15,91 0)', st_astext(vinflections[3])); 91 92 -- inflections-1, the example in fix_gentle_inflections docstring 93 select array((select way from wm_debug where name='inflection-1' and stage='bbends')) into vbends; 94 select array((select way from wm_debug where name='inflection-1' and stage='cinflections')) into vinflections; 95 perform assert_equals(vbends[1], vinflections[1]); -- unchanged 96 perform assert_equals('LINESTRING(114 20,133 20,145 15,145 0,136 8,123 10,114 10)', st_astext(vinflections[2])); 97 perform assert_equals('LINESTRING(123 10,114 10,111 2)', st_astext(vinflections[3])); 98 end $$ language plpgsql; 99 100 do $$ 101 declare 102 fig6 constant text default 'LINESTRING(84 47,91 59,114 64,120 45,125 39,141 39,147 32)'; 103 selfcrossing1 constant text default 'LINESTRING(-27 180,-20 166,-13 176,-18 184)'; 104 vcrossings geometry[]; 105 mutated boolean; 106 begin 107 select * from wm_self_crossing(wm_debug_get('cinflections', 'fig6')) into vcrossings, mutated; 108 perform assert_equals(true, mutated); 109 perform assert_equals( 110 fig6, 111 (select st_astext( 112 st_linemerge(st_union(way)) 113 ) from (select unnest(vcrossings) way) a) 114 ); 115 116 select * from wm_self_crossing(wm_debug_get('cinflections', 'fig6-rev')) into vcrossings, mutated; 117 perform assert_equals(true, mutated); 118 perform assert_equals( 119 fig6, 120 (select st_astext( 121 st_translate(st_reverse(st_linemerge(st_union(way))), -60, 0) 122 ) from (select unnest(vcrossings) way) a) 123 ); 124 125 select * from wm_self_crossing(wm_debug_get('cinflections', 'fig6-combi')) into vcrossings, mutated; 126 perform assert_equals(true, mutated); 127 perform assert_equals( 128 'MULTILINESTRING((84 137,91 149,114 154,120 135,125 129,141 129,147 122),(164 137,171 149,194 154,200 135,205 129,221 129,227 122))', 129 (select st_astext( 130 st_linemerge(st_union(way)) 131 ) from (select unnest(vcrossings) way) a) 132 ); 133 134 135 select * from wm_self_crossing(wm_debug_get('cinflections', 'selfcrossing-1')) into vcrossings, mutated; 136 perform assert_equals(true, mutated); 137 perform assert_equals( 138 selfcrossing1, 139 (select st_astext( 140 st_linemerge(st_union(way)) 141 ) from (select unnest(vcrossings) way) a) 142 ); 143 144 select * from wm_self_crossing(wm_debug_get('cinflections', 'selfcrossing-1-rev')) into vcrossings, mutated; 145 perform assert_equals(true, mutated); 146 perform assert_equals( 147 selfcrossing1, 148 (select st_astext( 149 st_translate(st_reverse(st_linemerge(st_union(way))), 0, -60) 150 ) from (select unnest(vcrossings) way) a) 151 ); 152 153 end $$ language plpgsql; 154 155 -- verifying bends in fig8 are eliminated like explained in the WM paper 156 do $$ 157 declare 158 fig8gen2 constant text default 'LINESTRING(173 12,174 10,180 8,186 8,189 6,201 5,203 11,216 16,216 6,229 3,236 2,239 6,243 8,248 6)'; 159 fig8gen3 constant text default 'LINESTRING(173 12,174 10,180 8,189 6,201 5,203 11,216 16,216 6,229 3,236 2,239 6,243 8,248 6)'; 160 eliminations geometry[]; 161 begin 162 eliminations = wm_debug_get('afigures', 'fig8'); 163 perform assert_equals(fig8gen2, st_astext(eliminations[2])); 164 perform assert_equals(fig8gen3, st_astext(eliminations[3])); 165 end $$ language plpgsql; 166 167 -- testing wm_exaggerate_bend2 in isolation 168 do $$ 169 declare 170 fig3b2 geometry; 171 bend geometry; 172 size float; 173 begin 174 select way from wm_debug where name='fig3' and stage='bbends' and gen=1 and nbend=2 into fig3b2; 175 size = wm_adjsize(fig3b2); 176 bend = wm_exaggerate_bend2(fig3b2, size, 50.); 177 perform assert_equals('ST_LineString', st_geometrytype(bend)); 178 insert into wm_debug(stage, name, gen, nbend, way) values('manual', 'fig3', 1, 1, bend); 179 end $$ language plpgsql; 180 181 -- misc visuals 182 do $$ 183 declare fig6b1 geometry; 184 declare fig6b2 geometry; 185 declare sclong geometry; 186 declare scshort geometry; 187 begin 188 select way from wm_debug where name='fig6' and stage='bbends' and gen=1 into fig6b1 limit 1 offset 0; 189 select way from wm_debug where name='fig6' and stage='bbends' and gen=1 into fig6b2 limit 1 offset 2; 190 insert into wm_visuals (name, way) values('fig6-baseline', st_makeline(st_startpoint(fig6b2), st_endpoint(fig6b2))); 191 insert into wm_visuals (name, way) values('fig6-newline', st_makeline(st_endpoint(fig6b1), st_endpoint(fig6b2))); 192 193 select way from wm_debug where name='selfcrossing-1' and stage='bbends' and gen=1 into sclong limit 1 offset 1; 194 select way from wm_debug where name='selfcrossing-1' and stage='bbends' and gen=1 into scshort limit 1 offset 4; 195 insert into wm_visuals (name, way) values('selfcrossing-1-baseline', st_makeline(st_startpoint(sclong), st_endpoint(sclong))); 196 insert into wm_visuals (name, way) values('selfcrossing-1-newline', st_makeline(st_startpoint(sclong), st_endpoint(scshort))); 197 end $$ language plpgsql; 198