Create a TileIndex shape file for raster data dynamically

Trond Michelsen trondmm-mapserver at CRUSADERS.NO
Wed Sep 26 08:35:53 EDT 2007


On Wed, Sep 26, 2007 at 10:35:36AM +0200, Zhonghai Wang wrote:
> thanks you for your clues for creating the index file, I am looking forword
> to sharing your experience of creating the utility programmatically.
> Actually, I create more often MapInfo seamless layer programmatically, but I
> am not very familar with the ESRI index file format, so I am just not sure
> how I should use the ShapeLib to create the index file.

My initial tests were successful, and it turned out to be very easy to
create a tileindex for rasterimages. It's basically exactly the same
as a tileindex for shapefiles.

I need to clean it up a bit, and make it possible to choose
outputfilename, but for now, here's a simple example:

--8<--
#!/usr/bin/perl

use strict;
use warnings;
use Geo::Shapelib ':constants';

my $tileindex = Geo::Shapelib->new();

$tileindex->{Shapetype}  = POLYGON;
$tileindex->{FieldNames} = ["Location"];
$tileindex->{FieldTypes} = ["String:255"];

for my $filename (@ARGV) {
    my ($xmin, $ymin, $xmax, $ymax) = get_coordinates($filename);
    push @{$tileindex->{Shapes}}, {
	SHPType  => POLYGON,
	Vertices => [[$xmin, $ymin],
		     [$xmax, $ymin],
		     [$xmax, $ymax],
		     [$xmin, $ymax],
		     [$xmin, $ymin]]
    };
    push @{$tileindex->{ShapeRecords}}, [$filename];
}

$tileindex->save('tileindex');

sub get_coordinates {
    my $filename = shift;
    my ($bbox) = $filename =~ /ec_world_(.*)\.png/;
    return split "," => $bbox;
}
__END__
--8<--

This program will take a list of files as arguments, and create a
tileindex, which is saved as tileindex.* in the current directory. The
subroutine get_coordinates() figures out what xmin, xmax, ymin and
ymax is for the current rasterimage. In my case, the bbox is embedded
in the filename, but you might have to read a worldfile or do
something else for your images. 

Speaking of worldfiles. The tileindex only tells mapserver which files
to load for a given view. Mapserver still need the worldfiles to
figure out where and how each of the pictures it opens should be
positioned.

Anyway - just like with a tileindex for shapefiles, mapserver
interprets the "Location" value as relative to shapepath if you use
relative paths. So I think it's better to use absolute paths when you
run the program.

To use this tileindex in a mapfile, you just include something like this:

LAYER
   NAME "tiled_raster"
   STATUS ON
   TILEINDEX "tileindex"
   TILEITEM "Location"
   TYPE RASTER
END


-- 
Trond Michelsen



More information about the mapserver-users mailing list