blob 8b8eece1 (8399B) - Raw
1 \i wm.sql 2 3 --do $$ 4 --begin 5 -- SET AUTOCOMMIT TO ON; 6 --end $$ language plpgsql; 7 8 -- https://stackoverflow.com/questions/19982373/which-tools-libraries-do-you-use-to-unit-test-your-pl-pgsql 9 CREATE OR REPLACE FUNCTION assert_equals(expected anyelement, actual anyelement) RETURNS void AS $$ 10 begin 11 if expected = actual or (expected is null and actual is null) then 12 --do nothing 13 else 14 raise exception 'Assertion Error. Expected <%> but was <%>', expected, actual; 15 end if; 16 end $$ LANGUAGE plpgsql; 17 18 drop function if exists dbg_geomsummary; 19 create function dbg_geomsummary(geoms geometry[], OUT output text) as $$ 20 declare i int4; 21 begin 22 output = format('len: %s;', array_length(geoms, 1)); 23 for i in 1..array_length(geoms, 1) loop 24 output = output || format(' %s:%s;', i, st_astext(geoms[i])); 25 end loop; 26 end 27 $$ language plpgsql; 28 29 drop table if exists wm_figures; 30 create table wm_figures (name text, way geometry); 31 -- add fig8.gpkg to postgis: 32 -- ogr2ogr -update -f PostgreSQL PG:"host=127.0.0.1 user=osm password=osm dbname=osm" fig8.gpkg 33 -- to "normalize" a new line when it's in `f`: 34 -- select st_astext(st_snaptogrid(st_transscale(geometry, -19.5, .016, 4000, 4000), 1)) from f; 35 insert into wm_figures (name, way) values ('fig3',ST_GeomFromText('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)')); 36 insert into wm_figures (name, way) values ('fig3-1',ST_GeomFromText('LINESTRING(0 0,12 0,13 4,20 2,20 0,32 0,33 10,38 16,43 15,44 10,44 0)')); 37 insert into wm_figures (name, way) values ('fig5',ST_GeomFromText('LINESTRING(0 39,19 52,27 77,26 104,41 115,49 115,65 103,65 75,53 45,63 15,91 0)')); 38 insert into wm_figures (name, way) values ('fig6',ST_GeomFromText('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)')); 39 insert into wm_figures (name, way) values ('fig6-rev',ST_Reverse(ST_Translate((select way from wm_figures where name='fig6'), 60, 0))); 40 insert into wm_figures (name, way) values ('fig6-combi', 41 ST_Union( 42 ST_Translate((select way from wm_figures where name='fig6'), 0, 90), 43 ST_Translate((select way from wm_figures where name='fig6'), 80, 90) 44 ) 45 ); 46 insert into wm_figures (name, way) values('fig8', ST_GeomFromText('LINESTRING(173 12,174 10,180 8,186 8,186 13,191 11,189 6,201 5,203 11,216 16,216 6,222 7,229 3,236 2,239 6,243 8,248 6)')); 47 insert into wm_figures (name, way) values ('inflection-1',ST_GeomFromText('LINESTRING(110 24,114 20,133 20,145 15,145 0,136 8,123 10,114 10,111 2)')); 48 insert into wm_figures (name, way) values ('multi-island',ST_GeomFromText('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))')); 49 50 -- TODO: there is a bug and it does not go through `self_crossing` function. 51 insert into wm_figures (name, way) values ('selfcrossing-1',ST_GeomFromText('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)')); 52 insert into wm_figures (name, way) values ('selfcrossing-1-rev',ST_Reverse(ST_Translate((select way from wm_figures where name='selfcrossing-1'), 0, 60))); 53 54 delete from wm_debug where name in (select name from wm_figures); 55 56 insert into wm_demo (name, way) select name, ST_SimplifyWM(way, name) from wm_figures; 57 58 -- wm_visuals holds visual aids for the paper. 59 drop table if exists wm_visuals; 60 create table wm_visuals (name text, way geometry); 61 do $$ 62 declare fig6b1 geometry; 63 declare fig6b2 geometry; 64 65 declare scb1 geometry; 66 declare scb2 geometry; 67 begin 68 select way from wm_debug where name='fig6' and stage='bbends' and gen=1 into fig6b1 limit 1 offset 0; 69 select way from wm_debug where name='fig6' and stage='bbends' and gen=1 into fig6b2 limit 1 offset 2; 70 insert into wm_visuals (name, way) values('fig6-baseline', st_makeline(st_startpoint(fig6b2), st_endpoint(fig6b2))); 71 insert into wm_visuals (name, way) values('fig6-newline', st_makeline(st_endpoint(fig6b1), st_endpoint(fig6b2))); 72 73 select way from wm_debug where name='selfcrossing-1' and stage='bbends' and gen=1 into scb1 limit 1 offset 0; 74 select way from wm_debug where name='selfcrossing-1' and stage='bbends' and gen=1 into scb2 limit 1 offset 2; 75 insert into wm_visuals (name, way) values('selfcrossing-1-baseline', st_makeline(st_startpoint(scb2), st_endpoint(scb2))); 76 insert into wm_visuals (name, way) values('selfcrossing-1-newline', st_makeline(st_endpoint(scb1), st_endpoint(scb2))); 77 end $$ language plpgsql; 78 79 80 drop function if exists debug_wm_get; 81 create function debug_wm_get( 82 _stage text, 83 _name text, 84 OUT ways geometry[] 85 ) as $$ 86 declare 87 begin 88 ways = array((select way from wm_debug where stage=_stage and name=_name)); 89 end $$ language plpgsql; 90 91 do $$ 92 declare 93 vbends geometry[]; 94 begin 95 vbends = debug_wm_get('bbends', 'fig3'); 96 perform assert_equals(5, array_length(vbends, 1)); 97 perform assert_equals('LINESTRING(0 0,12 0,13 4)', st_astext(vbends[1])); 98 perform assert_equals('LINESTRING(12 0,13 4,20 2,20 0)', st_astext(vbends[2])); 99 perform assert_equals('LINESTRING(20 2,20 0,32 0,33 10)', st_astext(vbends[3])); 100 perform assert_equals('LINESTRING(32 0,33 10,38 16,43 15,44 10,44 0)', st_astext(vbends[4])); 101 perform assert_equals(4, array_length(detect_bends((select way from wm_figures where name='fig3-1')), 1)); 102 select detect_bends((select way from wm_figures where name='fig5')) into vbends; 103 perform assert_equals(3, array_length(vbends, 1)); 104 end $$ language plpgsql; 105 106 do $$ 107 declare 108 vbends geometry[]; 109 vinflections geometry[]; 110 begin 111 vinflections = debug_wm_get('cinflections', 'fig5'); 112 perform assert_equals('LINESTRING(0 39,19 52,27 77)', st_astext(vinflections[1])); 113 perform assert_equals('LINESTRING(19 52,27 77,26 104,41 115,49 115,65 103,65 75,53 45)', st_astext(vinflections[2])); 114 perform assert_equals('LINESTRING(65 75,53 45,63 15,91 0)', st_astext(vinflections[3])); 115 116 -- inflections-1, the example in fix_gentle_inflections docstring 117 select array((select way from wm_debug where name='inflection-1' and stage='bbends')) into vbends; 118 select array((select way from wm_debug where name='inflection-1' and stage='cinflections')) into vinflections; 119 perform assert_equals(vbends[1], vinflections[1]); -- unchanged 120 perform assert_equals('LINESTRING(114 20,133 20,145 15,145 0,136 8,123 10,114 10)', st_astext(vinflections[2])); 121 perform assert_equals('LINESTRING(123 10,114 10,111 2)', st_astext(vinflections[3])); 122 end $$ language plpgsql; 123 124 do $$ 125 declare 126 fig6 constant text default 'LINESTRING(84 47,91 59,114 64,120 45,125 39,141 39,147 32)'; 127 selfcrossing1 constant text default 'LINESTRING(-27 180,-20 166,-13 176,-18 184)'; 128 vcrossings geometry[]; 129 mutated boolean; 130 begin 131 select * from self_crossing(debug_wm_get('cinflections', 'fig6')) into vcrossings, mutated; 132 perform assert_equals(true, mutated); 133 perform assert_equals( 134 fig6, 135 (select st_astext( 136 st_linemerge(st_union(way)) 137 ) from (select unnest(vcrossings) way) a) 138 ); 139 140 select * from self_crossing(debug_wm_get('cinflections', 'fig6-rev')) into vcrossings, mutated; 141 perform assert_equals(true, mutated); 142 perform assert_equals( 143 fig6, 144 (select st_astext( 145 st_translate(st_reverse(st_linemerge(st_union(way))), -60, 0) 146 ) from (select unnest(vcrossings) way) a) 147 ); 148 149 select * from self_crossing(debug_wm_get('cinflections', 'fig6-combi')) into vcrossings, mutated; 150 perform assert_equals(true, mutated); 151 perform assert_equals( 152 '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))', 153 (select st_astext( 154 st_linemerge(st_union(way)) 155 ) from (select unnest(vcrossings) way) a) 156 ); 157 158 159 select * from self_crossing(debug_wm_get('cinflections', 'selfcrossing-1')) into vcrossings, mutated; 160 perform assert_equals(true, mutated); 161 perform assert_equals( 162 selfcrossing1, 163 (select st_astext( 164 st_linemerge(st_union(way)) 165 ) from (select unnest(vcrossings) way) a) 166 ); 167 168 select * from self_crossing(debug_wm_get('cinflections', 'selfcrossing-1-rev')) into vcrossings, mutated; 169 perform assert_equals(true, mutated); 170 perform assert_equals( 171 selfcrossing1, 172 (select st_astext( 173 st_translate(st_reverse(st_linemerge(st_union(way))), 0, -60) 174 ) from (select unnest(vcrossings) way) a) 175 ); 176 177 end $$ language plpgsql;