| 1 | = bound_add.pl = |
| 2 | The 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. |
| 10 | use 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. |
| 26 | use mapscript; |
| 27 | # |
| 28 | # Include the xbase and dbi modules for searching and updating values. |
| 29 | use XBase; |
| 30 | # |
| 31 | # Include the getopt module to read input. |
| 32 | use Getopt::Long; |
| 33 | # |
| 34 | # Grab the file name from the input. |
| 35 | my $pfile=''; |
| 36 | &GetOptions('pfile=s' => \$pfile); |
| 37 | if ( !$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. |
| 45 | my ($sec,$min,$hr,$mdy,$mnth,$yr,$wdy,$ydy,$isdst) = localtime; |
| 46 | # |
| 47 | # Create the name. |
| 48 | my $sfile = "T$hr$min$sec"; |
| 49 | # |
| 50 | # Open the mapfile for writing. |
| 51 | open(MAPFILE, ">$sfile.map"); |
| 52 | # |
| 53 | # Open the existing polygon shapefile. |
| 54 | |
| 55 | my $inshpf = new shapefileObj($pfile, -1) or die "Unable to open shapefile $pfile."; |
| 56 | # |
| 57 | # What are the extents. |
| 58 | my $inshpminx = $inshpf->{bounds}->{minx}; |
| 59 | my $inshpminy = $inshpf->{bounds}->{miny}; |
| 60 | my $inshpmaxx = $inshpf->{bounds}->{maxx}; |
| 61 | my $inshpmaxy = $inshpf->{bounds}->{maxy}; |
| 62 | # |
| 63 | # Open the existing dbf file for appending fields. |
| 64 | # Thanks to: Chris Stuber. |
| 65 | my $dbh = new XBase "$pfile.dbf" or die XBase->errstr; |
| 66 | my @fn = $dbh->field_names(); |
| 67 | my @ft = $dbh->field_types(); |
| 68 | my @fl = $dbh->field_lengths(); |
| 69 | my @fd = $dbh->field_decimals(); |
| 70 | # |
| 71 | # Push additional values into each |
| 72 | # of the arrays above. (ie new Columns) |
| 73 | push @fn, "recno","minx","miny","maxx","maxy"; |
| 74 | push @ft, "N","N","N","N","N"; |
| 75 | push @fl, "9","20","20","20","20"; |
| 76 | push @fd, "0","8","8","8","8"; |
| 77 | # |
| 78 | my $newtable = XBase->create("name" => "$sfile.dbf", "field_names" => [@fn], "field_types" => [@ft], "field_lengths" => [@fl], "field_decimals" => [@fd] ) or die XBase->errstr; |
| 79 | for 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. |
| 91 | undef $newtable; |
| 92 | undef $dbh; |
| 93 | unlink "$pfile.dbf"; |
| 94 | rename "$sfile.dbf","$pfile.dbf"; |
| 95 | $dbh = new XBase "$pfile.dbf" or die XBase->errstr; |
| 96 | # |
| 97 | # Create the contents of the mapfile. |
| 98 | print MAPFILE <<EOF; |
| 99 | # |
| 100 | NAME $sfile |
| 101 | STATUS ON |
| 102 | SIZE 600 600 |
| 103 | SYMBOLSET "$sfile.sym" |
| 104 | EXTENT $inshpminx $inshpminy $inshpmaxx $inshpmaxy |
| 105 | UNITS FEET |
| 106 | SHAPEPATH "" |
| 107 | IMAGECOLOR 255 255 255 |
| 108 | LAYER |
| 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 |
| 118 | END |
| 119 | END |
| 120 | EOF |
| 121 | # |
| 122 | # Close the mapfile. |
| 123 | close MAPFILE; |
| 124 | # |
| 125 | # Open the symbol file for writing. |
| 126 | open(SYMFILE, ">$sfile.sym"); |
| 127 | # |
| 128 | # Create the contents of the symbol file. |
| 129 | print SYMFILE <<EOF; |
| 130 | SYMBOLSET |
| 131 | END |
| 132 | EOF |
| 133 | # |
| 134 | # Close the symbol file. |
| 135 | close SYMFILE; |
| 136 | # |
| 137 | # How many polygons are there. |
| 138 | my $innumshp = $inshpf->{numshapes}; |
| 139 | # |
| 140 | # Create the mapobject. |
| 141 | my $map = new mapObj("$sfile.map") or die("Unable to Open Default MapFile $sfile.map!"); |
| 142 | # |
| 143 | # Create the layer object for the queries. |
| 144 | my $lyr = $map->getLayerByName("bnd_qry") or die('Unable to Open Polygon Layer!'); |
| 145 | # |
| 146 | # Set the default query result to blank. |
| 147 | my @row = (); |
| 148 | # |
| 149 | # Create the shape holder. |
| 150 | my $inpol = new shapeObj(-1); |
| 151 | # |
| 152 | # Loop through each polygon and record. |
| 153 | for (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. |
| 181 | unlink "$sfile.map"; |
| 182 | unlink "$sfile.sym"; |
| 183 | }}} |
| 184 | ---- |
| 185 | back to PerlMapScript |