Changes between Initial Version and Version 1 of PerlMapScriptExamples35ex15


Ignore:
Timestamp:
Jan 29, 2009, 7:02:27 AM (15 years ago)
Author:
jmckenna
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PerlMapScriptExamples35ex15

    v1 v1  
     1= bound_add.pl =
     2The boundary.tar.gz is available at: http://www.highwayengineer.co.medina.oh.us/boundary.tar.gz
     3{{{
     4#!perl
     5#!/usr/bin/perl
     6#
     7# Copyright (C) 2002, Lowell Filak.
     8# You may distribute this file under the terms of the Artistic
     9# License.
     10use strict;
     11#
     12# Given the name of an existing polygon shapefile this routine will add
     13#   4 fields to the dbf file for the least bounding rectangle for each
     14#   polygon and populate those fields.
     15#
     16# Required modules are mapscript (installed as part of make install
     17#   http://mapserver.gis.umn.edu),
     18#   Getopt (normally included with Perl),
     19#   & XBase (cpan).
     20#   Please download boundary.tar.gz also, and:
     21#     tar -xf boundary.tar.gz --ungzip
     22#
     23# Suggested run line = ./bound_add.pl -pfile=boundary
     24#
     25# Include the mapscript module.
     26use mapscript;
     27#
     28# Include the xbase and dbi modules for searching and updating values.
     29use XBase;
     30#
     31# Include the getopt module to read input.
     32use Getopt::Long;
     33#
     34# Grab the file name from the input.
     35my $pfile='';
     36&GetOptions('pfile=s' => \$pfile);
     37if ( !$pfile ) {
     38  print "Syntax: bound_add.pl -pfile=[in_polygon_shapefile_name]";
     39  exit 0;
     40}
     41#
     42# Create a unique name for a new mapfile for querying the polygons.
     43#
     44# Grab the date.
     45my ($sec,$min,$hr,$mdy,$mnth,$yr,$wdy,$ydy,$isdst) = localtime;
     46#
     47# Create the name.
     48my $sfile = "T$hr$min$sec";
     49#
     50# Open the mapfile for writing.
     51open(MAPFILE, ">$sfile.map");
     52#
     53# Open the existing polygon shapefile.
     54
     55my $inshpf = new shapefileObj($pfile, -1) or die "Unable to open shapefile $pfile.";
     56#
     57# What are the extents.
     58my $inshpminx = $inshpf->{bounds}->{minx};
     59my $inshpminy = $inshpf->{bounds}->{miny};
     60my $inshpmaxx = $inshpf->{bounds}->{maxx};
     61my $inshpmaxy = $inshpf->{bounds}->{maxy};
     62#
     63# Open the existing dbf file for appending fields.
     64# Thanks to: Chris Stuber.
     65my $dbh = new XBase "$pfile.dbf" or die XBase->errstr;
     66my @fn = $dbh->field_names();
     67my @ft = $dbh->field_types();
     68my @fl = $dbh->field_lengths();
     69my @fd = $dbh->field_decimals();
     70#
     71# Push additional values into each
     72# of the arrays above. (ie new Columns)
     73push @fn, "recno","minx","miny","maxx","maxy";
     74push @ft, "N","N","N","N","N";
     75push @fl, "9","20","20","20","20";
     76push @fd, "0","8","8","8","8";
     77#
     78my $newtable = XBase->create("name" => "$sfile.dbf", "field_names" => [@fn], "field_types" => [@ft], "field_lengths" => [@fl], "field_decimals" => [@fd] ) or die XBase->errstr;
     79for my $x (0 .. $dbh->last_record) {
     80  my @rec = $dbh->get_record($x);
     81  my $deleted = shift @rec;
     82  next if ($deleted > 0);
     83  #
     84  # push new data onto @rec or leave empty
     85  print "Copying Record $x\n";
     86  push @rec,($x);
     87  $newtable->set_record($x,@rec) or die XBase->errstr;
     88}
     89#
     90# Close the dbf file, replace it, and reopen it for setting values.
     91undef $newtable;
     92undef $dbh;
     93unlink "$pfile.dbf";
     94rename "$sfile.dbf","$pfile.dbf";
     95$dbh = new XBase "$pfile.dbf" or die XBase->errstr;
     96#
     97# Create the contents of the mapfile.
     98print MAPFILE <<EOF;
     99#
     100NAME $sfile
     101STATUS ON
     102SIZE 600 600
     103SYMBOLSET "$sfile.sym"
     104EXTENT $inshpminx $inshpminy $inshpmaxx $inshpmaxy
     105UNITS FEET
     106SHAPEPATH ""
     107IMAGECOLOR 255 255 255
     108LAYER
     109  NAME bnd_qry
     110  TYPE POLYGON
     111  STATUS ON
     112  DATA "$pfile"
     113  TEMPLATE 'bogus.html'
     114  CLASS
     115    COLOR 255 0 0
     116    NAME "bnd_qry"
     117  END
     118END
     119END
     120EOF
     121#
     122# Close the mapfile.
     123close MAPFILE;
     124#
     125# Open the symbol file for writing.
     126open(SYMFILE, ">$sfile.sym");
     127#
     128# Create the contents of the symbol file.
     129print SYMFILE <<EOF;
     130SYMBOLSET
     131END
     132EOF
     133#
     134# Close the symbol file.
     135close SYMFILE;
     136#
     137# How many polygons are there.
     138my $innumshp = $inshpf->{numshapes};
     139#
     140# Create the mapobject.
     141my $map = new mapObj("$sfile.map") or die("Unable to Open Default MapFile $sfile.map!");
     142#
     143# Create the layer object for the queries.
     144my $lyr = $map->getLayerByName("bnd_qry") or die('Unable to Open Polygon Layer!');
     145#
     146# Set the default query result to blank.
     147my @row = ();
     148#
     149# Create the shape holder.
     150my $inpol = new shapeObj(-1);
     151#
     152# Loop through each polygon and record.
     153for (my $innum=0; $innum<$innumshp; $innum++ ) {
     154  print "Recording Polygon #$innum...\n";
     155  #
     156  # Grab the polygon by number.
     157  my $junk = $inshpf->get($innum, $inpol);
     158  #
     159  # Grab the bounds of the polygon.
     160  my $minx = $inpol->{bounds}->{minx};
     161  my $maxx = $inpol->{bounds}->{maxx};
     162  my $miny = $inpol->{bounds}->{miny};
     163  my $maxy = $inpol->{bounds}->{maxy};
     164  #
     165  # Grab the dbf record.
     166  my @recd = $dbh->get_record($innum);
     167  my $deleted = shift @recd;
     168  my $grbg = pop(@recd);
     169  my $grbg = pop(@recd);
     170  my $grbg = pop(@recd);
     171  my $grbg = pop(@recd);
     172  #
     173  # Push the values into place.
     174  push @recd,($minx,$miny,$maxx,$maxy);
     175  #
     176  # Record the new record.
     177  $dbh->set_record($innum,@recd) or die XBase->errstr;
     178}
     179#
     180# Get rid of the temporary mapfile & symbol file.
     181unlink "$sfile.map";
     182unlink "$sfile.sym";
     183}}}
     184----
     185back to PerlMapScript