-
Notifications
You must be signed in to change notification settings - Fork 2
PerlMapScriptExamples35ex7
{{{
#!perl
#!/usr/bin/perl -w
use mapscript;
use Getopt::Long;
use XBase;
GetOptions('file=s' => $file, 'coorx=s' => $coorx, 'coory=s' => $coory, 'item=s' => $item);
if ( (!$file) || (!$coorx) || (!$coory) || (!$item) ) {
print "Syntax: find.pl -file=[filename] -coorx=[x_coordinate] -coory=[y_coordinate] -item=[item_name]\n";
exit 0;
}
$item = uc $item;
my $mapfile = $file . '.map';
my $map = new mapObj("$mapfile") or die('Unable to Open Default MapFile!');
$imgx = $map->{width} - 1;
$imgy = $map->{height} - 1;
$minx = $map->{extent}->{minx};
$miny = $map->{extent}->{miny};
$maxx = $map->{extent}->{maxx};
$maxy = $map->{extent}->{maxy};
$dx = $maxx - $minx;
$dy = $maxy - $miny;
$fctrx = $dx / $imgx;
$fctry = $dy / $imgy;
$coorx = $coorx * $fctrx;
$coory = $coory * $fctry;
$coorx = $coorx + $minx;
$coory = $maxy - $coory;
$pnt = new pointObj();
$pnt->{x} = $coorx;
$pnt->{y} = $coory;
print "Selecting Using Point Coordinates: x=$coorx y=$coory\n";
my $lyr = $map->getLayerByName("$file") or die('Unable to Open Boundary Layer!');
$lyr->{status} = $mapscript::MS_ON;
$lyr->{type} = $mapscript::MS_LAYER_POLYGON;
$lyr->{data} = "$file";
$lyr->queryByPoint($map,$pnt,$mapscript::MS_SINGLE,0);
my $rsltcache = $lyr->{resultcache};
print "Found $rsltcache->{numresults} Result.\n";
my $rslt = $lyr->getResult(0);
my $record = $rslt->{shapeindex};
print "The Query Found Shape #$record.\n";
my $dbh = new XBase "$file" or die XBase->errstr;
my @names = $dbh->field_names;
my $fldcnt = $dbh->last_field;
my $fieldnum = 0;
for ($field=0; $field<=$fldcnt; $field++){
if ( $names[$field] eq $item ) {
#
# If so then exit loop.
$fieldnum = $field;
#
# Print the field number.
print "The Key Item is Field #$fieldnum.\n";
last;
}
else {
#
# Fall through.
}
}
my @row = $dbh->get_record_nf($record, $fieldnum) or die $dbh->errstr;
my $value = $row[1];
print "The Value of $item for Shape #$record = $value.\n";
my $results = 0;
my $shapesel = new shapefileObj('selected',$mapscript::MS_SHAPEFILE_POLYGON);
my $shapefile = new shapefileObj("$file",-1);
for ($record=0; $record<$dbh->last_record; $record++){
my @row = $dbh->get_record($record, "$item") or die $dbh->errstr;
my $deleted = $row[0];
if ( $deleted == 1 ) {
#
# If so then skip it.
next;
}
else {
#
# Fall through.
}
my $fndvalue = $row[1];
if ( "$fndvalue" ne "$value" ) {
#
# If not skip it.
next;
}
else {
#
# Fall through.
}
print "Record #$record Matches with a Value of $fndvalue - good thing :-)\n";
$results = $results + 1;
my $shape = new shapeObj(-1);
$shapefile->get($record - 1, $shape);
$shapesel->add($shape);
}
undef $shapesel;
my $newdbh = $dbh->create("name" => "selected.dbf");
$shapesel = new shapefileObj("selected", -1);
$newrect = $shapesel->{bounds};
$newminx = $newrect->{minx};
$newmaxx = $newrect->{maxx};
$newminy = $newrect->{miny};
$newmaxy = $newrect->{maxy};
$numseld = $shapesel->{numshapes};
undef $shapesel;
print "The Extents of the Selected Set: minx=$newminx miny=$newminy maxx=$newmaxx maxy=$newmaxy.\n";
back to PerlMapScrip