Changes between Initial Version and Version 1 of PerlMapScriptExamples35ex17


Ignore:
Timestamp:
Jan 29, 2009, 7:10:34 AM (13 years ago)
Author:
jmckenna
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PerlMapScriptExamples35ex17

    v1 v1  
     1= ref_to_map_ext.pl =
     2
     3A perl mapscript 3.6.4 sub-routine which converts a reference map mouse click into new map extents for main map. Really just a code snippet. Very little error checking.
     4
     5Author: Eric Bridger eric@gomoos.org, eric@maine.com
     6----
     7{{{
     8#!perl
     9# This is based entirely on the code in mapserver3.6.4 maptemplate.c::setExtent() case FROMREFPT
     10# calculate new map extents from mouse click in reference map.
     11# assumes CGI Form with:
     12# <input type="image" name="ref_map" src="mapserver_reference_map">
     13# <input type="hidden" name="imgext" value="MINX MINY MAXX MAXY"
     14
     15use mapscript;
     16use CGI ":cgi";
     17my $q = new CGI;
     18
     19$map = new mapscript::mapObj("mapfile.map");
     20
     21# This routine sets $map->{extent}{minx}, etc. and returns the new map extent as a string, which can be
     22# set in the html form as your current hidden imgext variable.
     23
     24sub ref_to_map_ext {
     25    my ($q, $map) = @_;
     26
     27    # as default just return current map extent
     28    my $mapext = join(' ', $map->{extent}->{minx},$map->{extent}->{miny},$map->{extent}->{maxx},$map->{extent}->{maxy});
     29    if(!$q->param('imgext')){
     30        return $mapext;
     31    }
     32    if(!$q->param('ref_map.x')){
     33        return $mapext;
     34    }
     35
     36    # get the current map extent from form imgext variable.
     37    my @imgext = split(' ', $q->param('imgext'));
     38
     39    # get mouse click x, y from ref_map image.
     40    my $refx = $q->param('ref_map.x');
     41    my $refy = $q->param('ref_map.y');
     42
     43 # reference map vars from  the map file
     44    # These never change unless the map file changes.
     45    my $ref_width = $map->{reference}{width};
     46    my $ref_height = $map->{reference}{height};
     47    my $ref_minx = $map->{reference}{extent}{minx};
     48    my $ref_maxx = $map->{reference}{extent}{maxx};
     49    my $ref_miny = $map->{reference}{extent}{miny};
     50    my $ref_maxy = $map->{reference}{extent}{maxy};
     51
     52    my $map_width = $map->{width};
     53    my $map_height = $map->{height};
     54
     55    my $cellx = ($ref_maxx - $ref_minx) / $ref_width;
     56    my $celly = ($ref_maxy - $ref_miny)/ $ref_height;
     57    my $map_x = $ref_minx + $cellx*$refx;
     58    my $map_y = $ref_maxy - $celly*$refy;
     59
     60    # set $map to new extent
     61    $map->{extent}->{minx} = $map_x - .5*($imgext[2] - $imgext[0]); # calculate new extent
     62    $map->{extent}->{miny} = $map_y - .5*($imgext[3] - $imgext[1]);
     63    $map->{extent}->{maxx} = $map_x + .5*($imgext[2] - $imgext[0]);
     64    $map->{extent}->{maxy} = $map_y + .5*($imgext[3] - $imgext[1]);
     65
     66    # reset $mapext to new value
     67    $mapext = join(' ', $map->{extent}->{minx},$map->{extent}->{miny},$map->{extent}->{maxx},$map->{extent}->{maxy});
     68
     69    return($mapext);
     70}
     71}}}
     72= Partial map file =
     73{{{
     74############################################################
     75# A patial map file.
     76#######################
     77MAP
     78  STATUS ON
     79  EXTENT -71.50 39.5 -63.0 46.0 # MINX MINY MAXX MAXY
     80  SIZE 504 385
     81  UNITS DD
     82  IMAGETYPE PNG
     83
     84# reference map
     85REFERENCE
     86  STATUS ON
     87  IMAGE "../../data/mapserver/raster/gom/ref_gom30b.png"
     88  SIZE 150 115
     89  EXTENT -71.5 39.5 -63.0 46.0 # MINX MINY MAXX MAXY
     90  COLOR -1 -1 -1
     91  OUTLINECOLOR 255 0 0
     92END
     93}}}
     94----
     95back to PerlMapScript