#! /bin/sh # # This AWK script converts an ARGIS GINA file to PostGres # # @(#)gtopg 1.6 7/28/92 # TMPFILE=`pwd`/topo$$ /bin/rm *.copy $TMPFILE gawk ' BEGIN { printf "create all_geo (" printf "feat_num=int4" printf ") \\g\n" printf "create all_points (" printf "geo_loc= point" printf ") inherits (all_geo) \\g\n" printf "create all_plines (" printf "geo_bbox= box, geo_pline= path" printf ") inherits (all_geo) \\g\n" } /^table/ { first_field= 1 printf "\ncreate " $2 "(" nrtables++ alltables[nrtables]= $2 if (!indirect) printf "feat_num=int4, " cur_table= $2 first_attr= 1 table_open= 1 } /^field/ { if (! first_field) { printf ", " cur_types= cur_types "," $3 } else { cur_types= $3 first_field= 0 } printf $2 "=" if ($3 == "CHAR") printf "text" else if ($3 == "NUMBER") { if ($5 == 0) { if ($4 <= 4) printf "int2" else printf "int4" } else printf "float4" } else if ($3 == "DATE") printf "abstime" } /^fc/ { fcode= substr($2, 2, length($2) - 2) fcodes[fcode]= cur_table tab_types[cur_table]= cur_types table_open= 0 printf ") \\g\n" } /^(feat|udb)/ { if (cur_coor != "") { outstr= last_fnum if (sat == "geo_loc") { # outstr= outstr "\t" "(" cur_coor ")" outstr= "(" cur_coor ")" "\t" outstr } else { nc= split(cur_coor, cca, ",") oldoutstr= outstr outstr= "(0," nc/2 "," cur_coor ")" 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] } outstr= outstr "\t(" xmin "," ymin "," xmax "," ymax ")" "\t" oldoutstr } # outstr= the_label "\t" outstr outstr= outstr "\t" the_label the_label= "" print outstr >> "f_" last_feat ".copy" cur_coor= "" } } /^feat/ { if ($6 == "l") { sat= "geo_pline" } else if ($6 == "p" || $6 == "pt" || $6 == "n") { sat= "geo_loc" } else sat= "unknown" if (! created_fc[$3]) { created_fc[$3]= 1; printf "\ncreate f_" $3 " (label= text) inherits (" nrtables++ alltables[nrtables]= "f_" $3 if ($6 == "l") { printf "all_plines" } else if ($6 == "p" || $6 == "pt" || $6 == "n") { printf "all_points" } else printf "unknown" printf ") \\g\n" } last_feat= $3 last_fnum= $2 cur_coor= "" } /^text/ { the_label= substr($0, index($0, " ")+1) the_label= substr(the_label, 2, length(the_label)-2) } /^coor / { if (cur_coor == "") { cur_coor= $2 ccstart= 3 } else ccstart= 2 for (i= ccstart; i <= NF; i++) cur_coor= cur_coor "," $i } /^attr/ { if (indirect) { tab_types[cur_table]= cur_types if (first_attr) { printf ") \\g\n\n" first_attr= 0 } outstr= "" outcopy= cur_table } else { outstr= last_fnum "\t" outcopy= fcodes[last_feat] } split(tab_types[cur_table], typea, ",") attrs_sub= substr($0, index($0, " ")+1) tna= split(attrs_sub, aa, ",") # Remove quotes: for (i= 1; i <= tna; i++) { if (substr(aa[i], 1, 1) == "\"") aa[i]= substr(aa[i], 2, length(aa[i])-2) } for (i= 1; i <= tna; i++) { if (typea[i] != "DATE") { outstr= outstr aa[i] } else { aa[i]= substr(aa[i], 2, length(aa[i])-2) split(aa[i], datea) if (datea[2] ~ /[a-zA-Z]/) # Month second field ? monthf= 2 else monthf= 1 monthval= datea[monthf] if (monthval ~ /[jJ][aA][nN]/) monthval= "Jan" else if (monthval ~ /[fF][eE][bB]/) monthval= "Feb" else if (monthval ~ /[mM][aA][rR]/) monthval= "Mar" else if (monthval ~ /[aA][pP][rR]/) monthval= "Apr" else if (monthval ~ /[mM][aA][yY]/) monthval= "May" else if (monthval ~ /[jJ][uU][nN]/) monthval= "Jun" else if (monthval ~ /[jJ][uU][lL]/) monthval= "Jul" else if (monthval ~ /[aA][uU][gG]/) monthval= "Aug" else if (monthval ~ /[sS][eE][pP]/) monthval= "Sep" else if (monthval ~ /[oO][cC][tT]/) monthval= "Oct" else if (monthval ~ /[nN][oO][vV]/) monthval= "Nov" else if (monthval ~ /[dD][eE][cC]/) monthval= "Dec" if (monthf == 2) { outstr= outstr monthval " " datea[1] } else { outstr= outstr datea[1] " " monthval } outstr= outstr " " datea[4] " " datea[3] } if (i < tna) { outstr= outstr "\t" } } print outstr >> outcopy ".copy" } /^udb-indirect/ { indirect= 1 } /^poly/ { has_poly= 1 printf "%d\t%d\t%s\t%s\n", $2, $3, $4, $5 >> "'$TMPFILE'" } /udb-indirect/ { if (has_poly) { printf ".\n" >> "'$TMPFILE'" printf "create poly_topology " printf "(edge_fn= int4, " printf "centroid_fn= int4, l_or_r= char, " printf "e_or_i= char) \\g\n" printf "copy poly_topology from \"'$TMPFILE'\" \\g\n" } } END { if (table_open) printf ") \\g\n" for (i= 1; i <= nrtables; i++) printf "copy " alltables[i] " from \"'`pwd`'/" alltables[i] ".copy\" \\g\n" } ' $2 | monitor $1 /bin/rm $TMPFILE *.copy