ReadFieldnames.pl

From Wildsong
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!