#!/usr/bin/perl
# bnpcoa2shp -- 台灣地籍 .coa, .bnp 格式轉成 Shapefile
# Taiwan cadastral .coa, .bnp format to Shapefile
# Copyright       : http://www.fsf.org/copyleft/gpl.html
# Author          : 積丹尼 Dan Jacobson -- http://jidanni.org/
# Created On      : Mon Apr 30 01:42:26 2007
# Last Modified By: Dan Jacobson
# Last Modified On: Sat May 12 14:58:12 2007
# Update Count    : 55
#試以轉
#宗地資料檔 (.PAR) 格式
#地號界址檔 (.BNP) 格式
#界址坐標檔 (.COA) 格式
#成 .SHP
#全用猜的 I'm just guessing how to do this.
use warnings;
use strict;
##usage:
# $ ls
# le0714.bnp  le0714.coa  le0714.par
# $ ./THIS_PROGRAM le0714|sh -e
my ( %corners, %coordinates );
die unless my $arg = $ARGV[0];
@ARGV = ("$arg.coa");
##format: corner #, x, y
while (<>) {
    next unless /^ *\d/;
    @_ = split;
    @{ $coordinates{ $_[0] } } = ( $_[2], $_[1] );
}
@ARGV = ("$arg.bnp");
##format: parcel #, ..., corner numbers
while (<>) {
    next unless /^ *\d/;
    tr/+-/  /;    #clean up?
    @_ = split;
    my $parcelno = shift @_;
    my $subno    = shift @_;
    $parcelno .= "-" . $subno if $subno;
    shift @_;
    shift @_;
    push @{ $corners{$parcelno} }, @_;
}
print "shpcreate $arg polygon\n";
print "dbfcreate $arg -s NUMBER 9\n";
for ( keys %corners ) {
    print "shpadd $arg ";
    print "@{ $coordinates{$_} } "
      for @{ $corners{$_} }, $corners{$_}[0];
    print "\ndbfadd $arg $_\n";
  }
