#! /bin/sh DB=${1:-geo} REL=${2:-top_polys} RESOL=${3:-1} TMPFILE=`pwd`/topo$$ # # We use gawk (gnu-awk) because we need the abs() function. # gawk ' { print } END { print "-1" } ' | gawk ' function abs(x) { if (x >= 0) return x; else return -x } function revcoor() { rsubcoor= xe "," ye for (rc= nc-1; rc >= 1; rc--) rsubcoor= rsubcoor "," coor[2*rc] "," coor[2*rc+1] } { if (old_fn == 0) old_fn= $1 if ($1 != old_fn) { while (pcnt > 1) { split(pla[pcnt], coor, ",") nc= coor[1] xs= coor[2]; ys= coor[3] xe= coor[2*nc]; ye= coor[2*nc+1] cind= index(pla[pcnt], ",") subcoor= substr(pla[pcnt], cind+1) for (i= 1; i < pcnt; i++) { split(pla[i], tcoor, ",") tnc= tcoor[1] txs= tcoor[2]; tys= tcoor[3] txe= tcoor[2*tnc]; tye= tcoor[2*tnc+1] cind= index(pla[i], ",") tsubcoor= substr(pla[i], cind+1) if (abs(xs-txe) <= '$RESOL' && abs(ys-tye) <= '$RESOL') { newcoor= (nc + tnc) "," tsubcoor "," subcoor } else if (abs(xs-txs) <= '$RESOL' && abs(ys-tys) <= '$RESOL') { revcoor() newcoor= (nc + tnc) "," rsubcoor "," tsubcoor } else if (abs(xe-txs) <= '$RESOL' && abs(ye-tys) <= '$RESOL') { newcoor= (nc + tnc) "," subcoor "," tsubcoor } else if (abs(xe-txe) <= '$RESOL' && abs(ye-tye) <= '$RESOL') { revcoor() newcoor= (nc + tnc) "," tsubcoor "," rsubcoor } else { continue; } pla[i]= newcoor pcnt-- i= 0 break; } if (i == pcnt) { printf "Inconsistent topology for feature: %d ! Exiting !\n", old_fn > "/dev/tty" exit } } nc= split(pla[1], cca, ",") xmin= 1e20; xmax= -1e20; ymin= xmin; ymax= xmax for (i= 2; i <= nc; i+= 2) { if (cca[i] < xmin) xmin= cca[i] if (cca[i] > xmax) xmax= cca[i] if (cca[i+1] < ymin) ymin= cca[i+1] if (cca[i+1] > ymax) ymax= cca[i+1] } #printf "append '$REL' (centroid_fn= %d, ", old_fn #printf "geo_bbox= \"(%f,%f,%f,%f)\", ", xmin, ymin, xmax, ymax #printf "geo_pline = \"(1," #printf "%s)\") \\g\n", pla[1] printf "%d\t(%f,%f,%f,%f)\t(1,%s)\n", old_fn, xmin, ymin, xmax, ymax, pla[1] >> "'$TMPFILE'" pcnt= 0 old_fn= $1 } if ($1 == -1) exit nopar= substr($2, 4, length($2) - 4) nc= split(nopar, coor, ",") if (nc <= 3 || (nc == 5 && (abs(coor[2]-coor[nc-1]) <= '$RESOL' \ && abs(coor[3]-coor[nc]) <= '$RESOL'))) { printf "Bad line! Feat_num: %d\n", old_fn > "/dev/tty" printf "Line: %s\n", $2 > "/dev/tty" next } pcnt++ pla[pcnt]= nopar } END { printf ".\n" >> "'$TMPFILE'" printf "copy '$REL' from \"'$TMPFILE'\" \\g\n" } ' | monitor $DB /bin/rm $TMPFILE