#! /bin/sh DB=${1:-geo} REL=${2:-top_polys} RESOL=${3:-1} # # We use nawk because we need the abs() function # Use gawk (gnu-awk) if you have version 7 awk # nawk ' { print } END { print "-1" } ' | nawk ' function abs(x) { if (x >= 0) return x; else return -x } { 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) rsubcoor= xe "," ye for (i= nc-1; i >= 1; i--) rsubcoor= rsubcoor "," coor[2*i] "," coor[2*i+1] for (i= 1; i < pcnt; i++) { #for (j= 1; j <= pcnt; j++) { #print pla[j] #} 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) #print "fn: " old_fn #print "xs: " xs " ys: " ys #print "xe: " xe " ye: " ye #print "txs: " txs " tys: " tys #print "txe: " txe " tye: " tye #print "subcoor: " subcoor " rsubcoor: " rsubcoor " tsubcoor:" tsubcoor if (abs(xs-txe) <= '$RESOL' && abs(ys-tye) <= '$RESOL') { newcoor= (nc + tnc) "," tsubcoor "," subcoor } else if (abs(xs-txs) <= '$RESOL' && abs(ys-tys) <= '$RESOL') { 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') { newcoor= (nc + tnc) "," tsubcoor "," rsubcoor } else { #printf "Not found\n" continue; } pla[i]= newcoor pcnt-- i= 0 #printf "Found\n" 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] 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 } ' | monitor $DB