[mapserver-users] RE: Mapserver getExtent

Puneet Kishor pkishor at eidesis.org
Tue Nov 13 10:01:54 EST 2001


Erwin,

Look at my response below (I am cc-ing this to the entire list so others 
may benefit) --

On Tuesday, November 13, 2001, at 12:01  AM, Azimuth, Erwin Perik wrote:

> Hi Puneet,
>
> thanks for the answer. As you perhaps may have thought i was hoping for 
> a small example, but the textual explanation is good for exploring also.
> Index +1 could be my problem. Will sort that out.

be very sure of that, otherwise you will be getting a map that is off

>
> Delft was a good example ;-)
>
> I'll mail (not at the PC where i have setup mapserver) my code which 
> get's the row information for the City.
>
> What you said under 2: "merely a matter of creating a new shapeobject 
> with that shapeindex, and finding its mapextent." is my actual problem.
> I do have the index, but that's the part on where i got stuck. Have to 
> read the mapscript info again i think ;-)
> This should be real easy, but somehow i cannot make it happen.
>

here's the script --

<?php

/*================================================================
begin: function for calculating new map extent
This function takes two params, name of a shapefile with its fully
qualified path, and an integer representing the index of the shape
presumably derived from some other operation such as a query.
*/================================================================
function calcNewMapExtent( $shpFileName, $shpIndex ) {

/*================================================================
two global variables created elsewhere. mapObj, without
which nothing would work, and rootdir representing the dir
path to the application.
*/================================================================
	global $mapObj, $rootdir;
	
// border around the selected feature so zoom may not be too tight
	$border = 0.0001;
	
// create a new shapefileobject
	$shapeFileObj = ms_newShapeFileObj( "$rootdir/data/$shpFileName", -1 );
// create a new shapeext object using the shapeindex
	$shapeExtObj = $shapeFileObj->getExtent( $shpIndex );
	
// calc new map extent using the border
	$minx = ( $shapeExtObj->minx ) * ( 1 - $border );
	$miny = ( $shapeExtObj->miny ) * ( 1 - $border );
	$maxx = ( $shapeExtObj->maxx ) * ( 1 + $border );
	$maxy = ( $shapeExtObj->maxy ) * ( 1 + $border );

// set extent of the map object
	$mapObj->setextent( $minx, $miny, $maxx, $maxy );
	
}
		
?>



Now remember, all you have done till now is created a new mapextent to 
zoom to. The next step is to actually show the City eq "Delft" (or shall 
we make it Ootmarsum, a place I have fond memories of) highlighted in 
some bright color. To do that make sure you have an additional layer for 
cities in your map file with the appropriate highlighted color. This 
highlight layer (as I call it) should be evaluated in the map file with 
an expression (see docs for this), and should be set to "off" so it 
doesn't display all the time.

Once you have calculated the map extent using the script above, make 
sure you turn on the highlight layer using another script. That way when 
you draw your map, it will draw zoomed in to a rectangle around City eq 
"Delft," and Delft itself will be turned on in the highlight color.

Enjoy.



> At 17:32 12-11-01 -0600, you wrote:
>> Erwin,
>>
>> Please see my answer below. I am cc-ing this to the entire list so 
>> others
>> may benefit from it also.
>>
>> > -----Original Message-----
>> > From: Consult IT!, E. Perik [mailto:pxp at xs4all.nl]
>> > Sent: Monday, November 12, 2001 5:13 PM
>> > To: Puneet Kishor
>> > Subject: Mapserver getExtent
>> >
>> >
>> > Hello Puneet,
>> >
>> > i saw your question regarding getting the extents of a queried shape.
>> >
>> > Have you solved this problem? hop you did, because i could
>> > youse some help
>> > on that.
>> > I have a form-list with all cities of a dbase database. When
>> > user selects a
>> > citie, the queried citie is highlighted.
>> >
>> > What i want to do (with PHP and mapscript) is to zoom in on
>> > that city. Have
>> > tried for ages, but never succeeded.
>>
>>
>>
>> Here's the general concept --
>>
>> 1. You have to find the index of the shape that belongs to the feature 
>> you
>> have selected. For example, if you have selected City eq "Delft", then 
>> you
>> have to find the index of the shape in the cities layer that represents
>> "Delft".
>>
>> 2. Once you have found the index then it is merely a matter of 
>> creating a
>> new shapeobject with that shapeindex, and finding its mapextent. The 
>> problem
>> is getting the index of the shape.
>>
>> 3. The index of the shape is the same as the row number of the city 
>> record
>> in the corresponding .dbf file (remember, all shape files are made up 
>> of
>> .shp, .shx, .dbf and whatnot... the .shp stores the actual geometry 
>> while
>> the .dbf stores the attributes... they are linked by the shapeindex 
>> which is
>> the same as the row number of the corresponding record in the .dbf 
>> file.)
>>
>> 4. .dbf files are really primitive. They have an absolute sort order 
>> that is
>> hard-coded in the way the file is written. You have to somehow query 
>> the
>> .dbf file for City eq "Delft" and get the row number of the that 
>> record. You
>> can use the Xbase module in PHP to do that. I have not done that. 
>> Instead, I
>> imported the .dbf file into a MySQL table and created a new field 
>> called
>> row_id. I populated the row_id with the corresponding row number in 
>> the .dbf
>> file. Keep in mind, the .dbf file is indexed from 0, however, the 
>> first row
>> contains the row headers (the field names). Now I simply query the 
>> MySQL
>> table for City eq "Delft" and get its row_id.
>>
>> 5. The advantage of doing it the way I do it is that it is really 
>> fast... no
>> mucking around with .dbf and Xbase. The downside is that if my 
>> shapefile
>> were to ever change then I would have to purge my MySQL table and 
>> re-import.
>> Not a problem for me at this time.
>>
>> 6. If you are using the Xbase module then your Xbase query will return 
>> the
>> row number somehow or the other... I haven't done this but others have
>> assured that it is indeed so.
>>
>> >
>> > Do you have an example of your version. It would really help.
>> >
>>
>>
>> I do have, and I have been promising the list that I am going to make 
>> it all
>> available. Except, I have been so madly beseiged with another client 
>> project
>> that I haven't had the time to package the scripts and the data in a 
>> way
>> that not only does it make sense to other coders/users but it also 
>> doesn't
>> make the client (for whom I did the PHP/Mapserver project) unhappy by
>> violating their secrecy, privacy, etc.
>>
>> In other words, I will have all the scripts and sample data available 
>> for
>> everyone's perusing pleasure sooner or later, but I don't right now.
>>
>> Otoh, if any of the above is confusing, just let me know I will be 
>> happy to
>> guide you further.
>>
>> Good luck,
>>
>> pk/
>>
--
Puneet Kishor
pkishor at eidesis.org




More information about the mapserver-users mailing list