A simple example is shown in this section using multiple scripting programming languages. Examples 14-5 through 14-9 provide a reference point for programmers who want to see how MapScript can be used in various languages. All these examples do the exact same thing—open a map file, draw the map, and save it to an image file. They are meant to be run in the same folder as the global.map map file, and they all produce an image file called worldmap.png.
# map1.py
# Python MapScript Example 1
import mapscript
# Set the map file to use
mapfile = "global.map"
# Create a mapObj, initialized with the mapfile above
mapobject = mapscript.mapObj(mapfile)
# Create an imgObj that has an image of the map
mapimage = mapobject.draw()
# Save the mapimage to a file
mapimage.save("worldmap.png")# map1.pl
# Perl MapScript Example 1
use mapscript;
# Set the map file to use
$mapfile = "global.map";
# Create a mapObj, initialized with the mapfile above
$mapobject = new mapscript::mapObj($mapfile);
# Create an imgObj that has an image of the map
$mapimage = $mapobject->draw();
# Save the mapimage to a file
$mapimage->save("worldmap.png");<?PHP
// map1.php
// PHP MapScript Example 1
// MapScript extension could also be loaded in php.ini
if (!extension_loaded("MapScript"))
dl('php_mapscript.'.PHP_SHLIB_SUFFIX);
// Set the map file to use
$mapfile = "global.map";
// Create a mapObj, initialized with the mapfile above
$mapobject = ms_newMapObj($mapfile);
// Create an imgObj that has an image of the map
$mapimage = $mapobject->draw();
// Save the mapimage to a file
$mapimage->saveImage("worldmap.png");
?>import edu.umn.gis.mapscript.*;
public class map1 {
public static void main(String[] args) {
System.loadLibrary("mapscript");
mapObj mapobject;
imageObj mapimage;
String mapfile;
// Set the map file to use
mapfile = "global.map";
// Create a mapObj, initilized with the mapfile above
mapobject = new mapObj(mapfile);
// Create an imgObj that has an image of the map
mapimage=mapobject.draw();
// Save the mapimage to a file
mapimage.save("worldmap.png",mapobject);
}
}# map1.rb
# Ruby MapScript Example 1
require "mapscript"
include Mapscript
# Set the map file to use
mapfile = "global.map"
# Create a mapObj, initialized with the mapfile above
mapobject = MapObj.new(mapfile)
# Create an imgObj that has an image of the map
mapimage = mapobject.draw
# Save the mapimage to a file
mapimage.save("worldmap.png")