|
This article shows how to access the MapPoint API from Perl using the Win32::OLE module. Topics include basic Perl syntax, how to call MapPoint methods, and how to set MapPoint API properties.
I've been a fan of using Perl for manipulating text files for some years having
previously used AWK. Both are free, but the main advantage of using Perl is the
enormous variety of modules available ranging from HTML and XML parsers,
image manipulation libraries, communications and networking, mathematics libraries
and so on.
One the the modules is a library for calling programs via Windows OLE. Windows
applications including Microsoft Office and MapPoint can be accessed programmatically
and controlled from Perl.

ActiveState provides Perl for Windows in an MSI package which is freely
downloadable. After it is installed, all that is required to begin executing
Perl scripts is a text file which the extension .pl containing the code. There
is no "project" file or compiling that needs to occur, Perl scripts are interpreted
and executed on the fly making for a very easy learning curve.
The script below defines an array with the U.S. State names, instantiates MapPoint,
and loops through the array saving a map for each state. This is very similar to a previous
article
Automating MapPoint with the .NET SDK published on MP2K Magazine last year which
basically accomplishes the same thing with the free VB .NET compiler found in
the .NET SDK.
Both approaches allow you to program MapPoint for free, but in comparison
automating MapPoint with Perl is much easier, not least because some
de-bugging output is returned, aiding greatly in tracking down syntax errors.
Here is the script, you simply need to put this code in a text file and rename
it with a .pl extension and just that simply you are able to automate MapPoint
without needing an expensive development tool.
use Win32::OLE qw(in with);
use Win32::OLE::Const;
use Win32::OLE::Const 'Microsoft MapPoint';
$Win32::OLE::Warn = 3;
# die on errors...
@states = ("Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut",
"Delaware","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas",
"Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota",
"Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey",
"New Mexico","New York","North Carolina", "North Dakota","Ohio","Oklahoma","Oregon",
"Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah",
"Vermont","Virginia","Washington","Washington, DC","West Virginia","Wisconsin","Wyoming");
my $MapPoint = Win32::OLE->new('MapPoint.Application', 'Quit');
my $Map = $MapPoint->NewMap();
for ($i = 0; $i <= 5; $i++) {
my $Results = $Map->FindPlaceResults($states[$i]);
print "$states[$i]\n";
my $Location = $Results->Item(1);
$Location->GoTo;
$Map->SaveAs($states[$i], 2);
}
$Map->{Saved} = -1; |
The first few lines tell the Perl interpreter to include the Win32::OLE module
required for communicating with MapPoint. The next section defines the array
of U.S. states. If you have the European version of MapPoint, replacing this
with County or Major City names should work fine.
The next section instantiates MapPoint and opens a new map. In Perl, Methods are
called using the -> characters. In the for loop that follows, FindPlaceResults is
used to bring up each state in the array, "GoTo" the states and then save an
html file. Note the last lines, properties are set using the curly brackets {}.
That's it! The files that are produced include an html file and directory for
each state and the image file itself is simply called image_map.gif. Perl is
great and file and directory manipulation, it would be trivial to get 51 gif
files with the name of the state such as North_Dakota.gif. Other Perl modules
would give you ability to manipulate or re-size the image and ftp it to a location
or otherwise send it somewhere, create a PDF document with the map, and more.
The applications are endless, I would encourage any developer to peruse the
available Perl modules and scripts on CPAN
to add to your arsenal of programming tools.
This article originally appeared on MP2K
Magazine.
|