#! /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 ' NF == 2 { 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-1] "," coor[2*rc] } { if (old_fn == 0) old_fn= $1 if ($1 != old_fn) { if (allcnt < 500) { print "Composing: " old_fn > "/dev/tty" while (pcnt > 1) { #print pla[pcnt] > "/dev/tty" split(pla[pcnt], ncstr, ":") split(ncstr[2], coor, ",") nc= ncstr[1] #print nc > "/dev/tty" xs= coor[1]; ys= coor[2] xe= coor[2*nc-1]; ye= coor[2*nc] # cind= index(pla[pcnt], ":") # subcoor= substr(pla[pcnt], cind+1) subcoor= ncstr[2] for (i= 1; i < pcnt; i++) { split(pla[i], ncstr, ":") tnc= ncstr[1] split(ncstr[2], tcoor, ",") txs= tcoor[1]; tys= tcoor[2] txe= tcoor[2*tnc-1]; tye= tcoor[2*tnc] #cind= index(pla[i], ":") #tsubcoor= substr(pla[i], cind+1) tsubcoor= ncstr[2] 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 ! Continueing !\n", old_fn > "/dev/tty" # exit break } } split(pla[1], ncstr, ":") nc= split(ncstr[2], cca, ",") xmin= 1e20; xmax= -1e20; ymin= xmin; ymax= xmax for (i= 1; 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 "%d\t(%f,%f,%f,%f)\t(%s)\n", old_fn, xmin, ymin, xmax, ymax, pla[1] >> "'$TMPFILE'" } else { printf "Skipping polygon: " old_fn " (" allcnt " points)\n" > "/dev/tty" } pcnt= 0 allcnt= 0 old_fn= $1 } if ($1 == -1) exit nopar= substr($2, 2, length($2) - 2) #print nopar > "/dev/tty" split(nopar, nohead, ":") #print nohead[2] > "/dev/tty" nc= split(nohead[2], coor, ",") #print nc > "/dev/tty" if (nc <= 2 || (nc == 4 && (abs(coor[1]-coor[nc-1]) <= '$RESOL' \ && abs(coor[2]-coor[nc]) <= '$RESOL'))) { printf "Bad line! Feat_num: %d\n", old_fn > "/dev/tty" printf "Line: %s\n", $2 > "/dev/tty" next } pcnt++ allcnt+= nc / 2 pla[pcnt]= nopar } END { printf ".\n" >> "'$TMPFILE'" printf "copy '$REL' from \"'$TMPFILE'\" \\g\n" } ' | monitor $DB /bin/rm $TMPFILE