#! /bin/sh

set -v

DB=${1:-geo}
REL=${2:-top_polys}
RESOL=${3:-1}

monitor -c "destroy pol_tmp" $DB
monitor -c "retrieve into pol_tmp (ap.all) from ap in all_plines*" $DB

monitor -c "define index pol_tmp_index on pol_tmp using btree (feat_num int4_ops)" $DB

monitor -c "destroy $REL" $DB
monitor -c "create $REL (centroid_fn= int4, geo_bbox= box, geo_pline= path)" $DB

monitor -TN -c "retrieve (poly_topology.centroid_fn, pol_tmp.geo_pline) \
  where poly_topology.e_or_i != 'i'\
  and poly_topology.edge_fn = pol_tmp.feat_num sort by centroid_fn" $DB \
| $GEOHOME/conversion/topo.awk.copy $DB $REL $RESOL

monitor -c "destroy pol_tmp" $DB
