ReadFieldnames.pl

From Wildsong
Revision as of 22:08, 12 April 2006 by Brian Wilson (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
#
#  Read and display the fieldnames and fieldtypes for a shapefile.
#
use strict;
use Geo::Shapelib qw/:all/;
use Data::Dumper;

my $file = shift; # Get the shapefile name from the command line

# ------------------------------------------------------------------------
# Initialization steps
#

my $shape      = new Geo::Shapelib $file, {
    Load => 0,        # don't read the data, just the header
};

my $fieldnames = $$shape{FieldNames};
my $fieldtypes = $$shape{FieldTypes};

# Load the atrribute field names into a hash for convenience access later.
my %fieldnames;

# print "Number of attribute fieldnames = $#$fieldnames\n";
for my $f (0..$#$fieldnames) {
    my $fieldname = $$fieldnames[$f];
    print "$f $fieldname $$fieldtypes[$f]\n";
    $fieldnames{$fieldname} = $f;  # allows reverse lookup: find index using na\
me
}

# That's all!