| 1 | = shpinfo.pl = |
| 2 | The boundary.tar.gz is not available because I can't upload it. It is not required for the script to work, only to run the example. -sw http://www.highwayengineer.co.medina.oh.us/boundary.tar.gz |
| 3 | {{{ |
| 4 | #!perl |
| 5 | #!/usr/bin/perl -w |
| 6 | # |
| 7 | # Given a shapefile name this routine will dump out the attribute information. |
| 8 | # |
| 9 | # Required modules are mapscript (installed as part of make install) |
| 10 | # & Getopt (normally included with Perl). |
| 11 | # Note: It does not require XBase. In 3.5 there is a DBFInfo object |
| 12 | # available which makes READING basic dbf information much easier IMHO. |
| 13 | # Please download boundary.tar.gz also, and: |
| 14 | # tar -xf boundary.tar.gz --ungzip |
| 15 | # |
| 16 | # Suggested run line = ./shpinfo.pl -file=boundary |
| 17 | use XBase; |
| 18 | use mapscript; |
| 19 | use Getopt::Long; |
| 20 | # |
| 21 | # Declare a hash for easy access to shape types & file types. |
| 22 | %shptypes = ( '1' => 'point', |
| 23 | '3' => 'arc', |
| 24 | '5' => 'polygon', |
| 25 | '8' => 'multipoint' |
| 26 | ); |
| 27 | %typetypes = ( '0' => 'String', |
| 28 | '1' => 'Integer', |
| 29 | '2' => 'Double', |
| 30 | '3' => 'Invalid' |
| 31 | ); |
| 32 | # |
| 33 | # Grab the filename from the input. |
| 34 | &GetOptions("file=s", \$file); |
| 35 | # |
| 36 | # Check the input filename. |
| 37 | if(!$file) { |
| 38 | print "Syntax: shpinfo.pl -file=[filename]\n"; |
| 39 | exit 0; |
| 40 | } |
| 41 | # |
| 42 | # Open the shapefile. |
| 43 | $shapefile = new mapscript::shapefileObj($file, -1) or die "Unable to open shapefile $file."; |
| 44 | # |
| 45 | # Print the shapefile name, type, number of features, & bounds. |
| 46 | print "Shapefile $file:\n\n"; |
| 47 | print "\ttype: ". $shptypes{$shapefile->{type}} ."\n"; |
| 48 | print "\tnumber of features: ". $shapefile->{numshapes} ."\n"; |
| 49 | printf "\tbounds: (%f,%f) (%f,%f)\n", $shapefile->{bounds}->{minx}, $shapefile->{bounds}->{miny}, $shapefile->{bounds}->{maxx}, $shapefile->{bounds}->{maxy}; |
| 50 | # |
| 51 | # Create the XBase object. |
| 52 | $table = new XBase $file.'.dbf' or die XBase->errstr; |
| 53 | # |
| 54 | # Print the dbf file name & number of records.. |
| 55 | print "\nDBFInfo table $file.dbf:\n\n"; |
| 56 | print "\tnumber of records: ". ($table->last_record+1) ."\n"; |
| 57 | # |
| 58 | # Print the header for the field dump. |
| 59 | print "\tnumber of fields: ". ($table->last_field+1) ."\n\n"; |
| 60 | print "\tName Type Length Decimals\n"; |
| 61 | print "\t---------------- ------- ------ --------\n"; |
| 62 | # |
| 63 | # Loop through each field. |
| 64 | for ($table->field_names) { |
| 65 | # |
| 66 | # Print the field name, type, width, & # of decimal places. |
| 67 | printf "\t%-16s %-7s %6d %8d\n", $_, $table->field_type($_), $table->field_length($_); $table->field_decimal($_) |
| 68 | } |
| 69 | # |
| 70 | # Close the dbf file. |
| 71 | undef $table; |
| 72 | }}} |
| 73 | ---- |
| 74 | back to PerlMapScript |