| 1 | = find.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 | {{{ |
| 5 | #!perl |
| 6 | #!/usr/bin/perl -w |
| 7 | # |
| 8 | # Copyright (C) 2002, Lowell Filak. |
| 9 | # You may distribute this file under the terms of the Artistic |
| 10 | # License. |
| 11 | # |
| 12 | # Given a shapefile name, item name, & item value this routine will dump out |
| 13 | # the matching records. |
| 14 | # |
| 15 | # Required modules are mapscript (installed as part of make install), |
| 16 | # Getopt (normally included with Perl), & XBase. |
| 17 | # Please download boundary.tar.gz also, and: |
| 18 | # tar -xf boundary.tar.gz --ungzip |
| 19 | # |
| 20 | # Suggested run line = ./find.pl -file=boundary -item=loc_name -value='Medina City' |
| 21 | use mapscript; |
| 22 | use Getopt::Long; |
| 23 | use XBase; |
| 24 | # |
| 25 | # Retrieve the input values. |
| 26 | GetOptions('file=s' => \$file, 'item=s' => \$item, 'value=s' => \$value); |
| 27 | # |
| 28 | # Check the input values. |
| 29 | if ( (!$file) || (!$item) || (!$value) ) { |
| 30 | print "Syntax: find.pl -file=[filename] -item=[item_name] -value=[item_value]\n"; |
| 31 | exit 0; |
| 32 | } |
| 33 | # |
| 34 | # Open the existing shapefile. |
| 35 | $shapefile = new shapefileObj($file, -1) or die "Unable to open shapefile $file."; |
| 36 | # |
| 37 | # Print out the entered values. |
| 38 | print "Shapefile: $file\nItem Name: $item\nItem Value: $value\n\n"; |
| 39 | # |
| 40 | # Open the db handle. |
| 41 | my $dbh = new XBase "$file" or die XBase->errstr; |
| 42 | # |
| 43 | # Grab and process the information. |
| 44 | my @row; |
| 45 | # |
| 46 | # Set the number of results to 0. |
| 47 | my $results = 0; |
| 48 | # |
| 49 | # Open a new shape object to hold the found shapes. |
| 50 | my $shape = new shapeObj(-1); |
| 51 | # |
| 52 | # Use xbase to query by attribute. |
| 53 | # |
| 54 | # Loop through each record (there are experimental modules for using indexes |
| 55 | # available according to xbase man page). |
| 56 | for ($record=0; $record<$dbh->last_record; $record++){ |
| 57 | # |
| 58 | # Grab the record. |
| 59 | my @row = $dbh->get_record($record, "$item") or die $dbh->errstr; |
| 60 | # |
| 61 | # Is the record marked for deletion. |
| 62 | my $deleted = $row[0]; |
| 63 | if ( $deleted == 1 ) { |
| 64 | # |
| 65 | # If so then skip it. |
| 66 | next; |
| 67 | } |
| 68 | else { |
| 69 | # |
| 70 | # Fall through. |
| 71 | } |
| 72 | # |
| 73 | # Set the value for the search field. |
| 74 | my $fndvalue = $row[1]; |
| 75 | # |
| 76 | # Does the value from the field match the query. |
| 77 | if ( "$fndvalue" ne "$value" ) { |
| 78 | # |
| 79 | # If not skip it. |
| 80 | next; |
| 81 | } |
| 82 | else { |
| 83 | # |
| 84 | # Fall through. |
| 85 | } |
| 86 | # |
| 87 | # Print the found record information. |
| 88 | print "The Row Found: $record\nThe Value Found: $fndvalue - go figure :-)\n"; |
| 89 | # |
| 90 | # Increment the results counter. |
| 91 | $results = $results + 1; |
| 92 | # |
| 93 | # Grab shape #$record and stick it into the shape holder. |
| 94 | $shapefile->get($record, $shape); |
| 95 | # |
| 96 | # Get the bounds of the shape. |
| 97 | my $minx = $shape->{bounds}->{minx}; |
| 98 | my $miny = $shape->{bounds}->{miny}; |
| 99 | my $maxx = $shape->{bounds}->{maxx}; |
| 100 | my $maxy = $shape->{bounds}->{maxy}; |
| 101 | # |
| 102 | # Print the bounds of the shape. |
| 103 | printf "bounds (%f,%f) (%f,%f)\n", $minx, $miny, $maxx, $maxy; |
| 104 | } |
| 105 | # |
| 106 | # Print the number of records found. |
| 107 | print "Number of Results: $results\n"; |
| 108 | }}} |
| 109 | |
| 110 | ---- |
| 111 | [wiki:PerlMapScript back to PerlMapScript] |