Mapscript

From DreamsteepWiki

Jump to: navigation, search

Mapscript examples

Installing the Python Mapscript Support Files
Quick and dirty (for use with ms4w only)

   1.

      Unpack /ms4w/Apache/cgi-bin/mapscript/python/mapscript*win32.zip in a safe location
   2.

      In the directory structure that results from the above step, navigate to \Python-2.5.1\Lib\site-packages
   3.

      You should find three files there:
              * _mapscript.pyd
              * mapscript.py
              * mapscript.pyc

   4.

      Copy the three files into the /ms4w/Apache/cgi-bin directory.
   5.

      Place any python cgi scripts that you want to run under ms4w in the /ms4w/Apache/cgi-bin directory, and add a "shebang" line at the top of each script. See the section on Configuring Apache to run Python scripts below for shebang-line details.

More robust (for system-wide access)

   1.

      Unpack /ms4w/Apache/cgi-bin/mapscript/python/mapscript*win32.zip in a safe location
   2.

      In the directory structure that results from the above step, navigate to \Python-2.5.1\Lib\site-packages
   3.

      You should find three files there:
              * _mapscript.pyd
              * mapscript.py
              * mapscript.pyc

   4.

      Copy the three files into the site-packages directory of your Python 2.5.x installation (eg., C:\Python25\Lib\site-packages)
   5.

      Add <drive_letter>:\ms4w\Apache\cgi-bin to your system's PYTHONPATH environment variable.

Configuring Apache to Run Python Scripts

You have a couple of options here:
via shebang lines

This may be the preferred approach if you have multiple Python installations on your machine.

Add a "shebang line" to the top of each python cgi script in /ms4w/apache/cgi-bin. This line must be the first line in the script, and it must begin with #! followed by the path to your systems python executable. For example:

#!c:/python25/python.exe -u

The -u option shown above causes the script to use unbuffered output, which is generally what you need in web-serving contexts.
via Apache directives

This approach obviates the need to edit every python script in your cgi-bin directory; however, it may be problematic if you have multiple Python installations.

Add the following line to the end of ms4w/Apache/conf/httpd.conf:

ScriptInterpreterSource Registry

You may also want to add these:

SetEnv PYTHONUNBUFFERED 1
PassEnv PYTHONPATH

The first of the above lines causes your python scripts to use unbuffered output (which you probably want), while the second passes the value of your system's PYTHONPATH environment variable through to Apache's environment (if set).
Testing the Configuration

Follow these steps to test your Python MapScript installation:

   1.

      Move the file /ms4w/python/test-cgi-mapscript.py into the /ms4w/Apache/cgi-bin/ folder.
   2.

      Open the file in a text editor.
   3.

      Edit the first line of that script to point to your proper Python path.
   4.

      In your web browser goto http://127.0.0.1/cgi-bin/test-cgi-mapscript.py
   5.

      The script tests that the Python CGI is working, and that the mapscript module can be successfully imported.

      If successful you will see an HTML page listing several environment settings on your machine, as well a list of the Python MapScript module attributes at the bottom of the page.

Python Troubleshooting:

    * If you get an Internal Server Error check the Apache logs for info.
    * Make sure your script points to a valid Python installation path.
    * Make sure you are using a recent Python version (containing the cgitb module).



PYTHON CGI


#!c:\python25\python.exe -u

import cgi
import cgitb; cgitb.enable();

cgi.test()

import mapscript

print "<h2>mapscript module attributes</h2>"
print dir(mapscript)


MAPSCRIPT_SNIPPETS

MAPFILES

MAP
  IMAGETYPE      PNG24
  IMAGECOLOR 100 110 110
  EXTENT       161.0 -488.0 1225518.0 1432254.0 # LAEA
  SIZE           500 500
  SHAPEPATH      "../data"
  SYMBOLSET      "../symbols/symbols35.sym"
  FONTSET        "../fonts/fonts.list"
END


RASTER LAYER
  LAYER
   DATA "raster/shdrlfi020g_ugl.tif"
   STATUS DEFAULT
   TYPE RASTER
   OFFSITE 70 74 66
  END 

POLYGON LINE POINTS LAYER

  LAYER #  layer begins here
    NAME         states
    DATA         states_ugl
    STATUS       DEFAULT
    TYPE         POLYGON
    OPACITY      50

    CLASS
      NAME       "ww"
      STYLE
        COLOR        232 232 232
        OUTLINECOLOR 222 32 32
      END
    END
  END # layer ends here


  SCALEBAR
    STATUS EMBED
    UNITS KILOMETERS
    INTERVALS 5
    TRANSPARENT TRUE
    OUTLINECOLOR 0 20 200
  END













						
						
Personal tools