Changes between Initial Version and Version 1 of PerlMapScriptExamples35ex3


Ignore:
Timestamp:
Jan 29, 2009, 6:36:10 AM (15 years ago)
Author:
jmckenna
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PerlMapScriptExamples35ex3

    v1 v1  
     1= dump.pl =
     2The boundary.tar.gz file referenced below is a test file to run the example. It has not been uploaded to wiki, because I can't do that. -sw http://www.highwayengineer.co.medina.oh.us/boundary.tar.gz
     3
     4{{{
     5#!perl
     6#!/usr/bin/perl -w
     7#
     8# Given a shapefile name this routine will dump out the shapefile type,
     9#   members, & applicable coordinate information.
     10#
     11# Required modules are mapscript (installed as part of make install)
     12#    & Getopt (normally included with Perl).
     13#   Please download boundary.tar.gz also, and:
     14#     tar -xf boundary.tar.gz --ungzip
     15#
     16# Suggested run method = ./dump.pl -file=boundary > /tmp/boundary.dump
     17use mapscript;
     18use Getopt::Long;
     19#
     20# Declare a hash for easy access to the shape types.
     21%types = ( '1' => 'point',
     22           '3' => 'arc',
     23           '5' => 'polygon',
     24           '8' => 'multipoint'
     25         );
     26#
     27# Declare an initial value for the shapefile extents.
     28my $shapeminx = 0,
     29my $shapeminy = 0;
     30my $shapemaxx = 1;
     31my $shapemaxy = 1;
     32#
     33# Check input for a specified file name.
     34&GetOptions("file=s", \$file);
     35if(!$file) {
     36  print "Syntax: dump.pl -file=[filename]\n";
     37  exit 0;
     38}
     39#
     40# Open the specified shapefile.
     41$shapefile = new shapefileObj($file, -1) or die "Unable to open shapefile $file";
     42#
     43# Print shapefile type & number of shapes.
     44print "Shapefile opened (type=". $types{$shapefile->{type}} .") with ".
     45$shapefile->{numshapes} ." shape(s)\n";
     46#
     47# Open a shape object for pulling the shapefile shapes into.
     48$shape = new shapeObj(-1);
     49#
     50# Loop through each shape in the shapefile.
     51for($i=0; $i<$shapefile->{numshapes}; $i++) {
     52    #
     53    # Pull the nth shape into the shape object for analysis.
     54    $shapefile->get($i, $shape);
     55    #
     56    # Print the shapes number of lines.
     57    print "Shape $i has ". $shape->{numlines} ." part(s) - ";
     58    #
     59    # Get the bounds.
     60    my $minx = $shape->{bounds}->{minx};
     61    my $miny = $shape->{bounds}->{miny};
     62    my $maxx = $shape->{bounds}->{maxx};
     63    my $maxy = $shape->{bounds}->{maxy};   
     64    #
     65    # Print the bounds for the shape.
     66    printf "bounds (%f,%f) (%f,%f)\n", $minx, $miny, $maxx, $maxy;
     67    #
     68    # Is this the first shape.
     69    if ( $i == 0 ) {
     70      #
     71      # Set the initial bounds to an actual shape.
     72      $shapeminx = $minx;
     73      $shapeminy = $miny;
     74      $shapemaxx = $maxx;
     75      $shapemaxy = $maxy;
     76    }
     77     else {
     78      #
     79      # Create compounded shapefile extent.
     80      #   Note: This is just an example, there is a better way to do this.
     81      #         See shpinfo.pl .
     82      if ($minx < $shapeminx) {
     83        $shapeminx = $minx;
     84      }
     85      if ($miny < $shapeminy) {
     86        $shapeminy = $miny;
     87      }
     88      if ($maxx > $shapemaxx) {
     89        $shapemaxx = $maxx;
     90      }
     91      if ($maxy > $shapemaxy) {
     92        $shapemaxy = $maxy;
     93      }
     94     }
     95    #
     96    # Loop through each line in the shape.
     97    for($j=0; $j<$shape->{numlines}; $j++) {
     98        #
     99        # Create the line object.
     100        $part = $shape->get($j);
     101        #
     102        # Print the number of points in the line.
     103        print "Part $j has ". $part->{numpoints} ." point(s)\n";
     104        #
     105        # Loop through each point.
     106        for($k=0; $k<$part->{numpoints}; $k++) {
     107            #
     108            # Create the point object.
     109            $point = $part->get($k);
     110            #
     111            # Print the x & y coordinate for the point.
     112            print "$k: ". $point->{x} .", ". $point->{y} ."\n";
     113        }
     114    }
     115}
     116#
     117# Print the shapefiles bounding rectangle.
     118print "$file\'s Bounding Rectangle is $shapeminx, $shapeminy, $shapemaxx, $shapemaxy\n";
     119}}}