del.icio.us stuff
 

Mapfile builder

| Permanent Link | Locative, Open Source

Now for something completely different: python code!

The other day, Sean Gillies wrote a blog post about OWSLib. There was a nugget in there that led me to start playing with the ElementTree package. It turns out to be a really nice way to play with XML without either having to know very much about XML processing or having to do very much work. That's the perfect combination for me.

One of the things I'm working on is leading me to need to build MapServer mapfiles using Python. I tried Python Mapscript but it did a little more than I wanted. Then the lightbulb went off in my head...

It occurred to me that with a little tweaking, an ElementTree can be constructed and output as a Map File. So here's the tweaking: mapfiler.py (source) and here's a sample python script (source) that produces a mapfile.

Here's the result:

 MAP
  DEBUG ON
  EXTENT -180 -90 180 90
  NAME sl
  UNITS dd

  PROJECTION
   "init=epsg:4326"
  END

  WEB

   METADATA
    "wms_title" "My WMS"
    "wms_srs" "EPSG:4326"
   END
  END

  LAYER
   CONNECTION data.shp
   CONNECTIONTYPE OGR
   NAME "my layer one"
   STATUS default
   TYPE POLYGON

   CLASS
    COLOR 255 255 204
    OUTLINECOLOR 0 0 0
   END

   METADATA
    "wms_title" "My WMS"
    "wms_abstract" "Cool Layers"
    "wms_service" "GetMap"
    WMS_SRS "init=epsg:4326"

   END
  END

  LAYER
   NAME "my layer two"
  END
 END

There are a few hacks in there, but it's good enough for what I need. You have to make sure to throw in the " " characters where you need them. And if you want things like your metadata blocks to be nicely indented, you have to add a \t so the writer knows where to put the indent.

Comments

2006-08-07 15:19 | Posted by Sean Gillies | http://zcologia.com/news
Now all you need to do is enhance MapServer so that it can load a mapfile *string*.
2006-08-07 16:24 | Posted by Allan
Actually, this opens the way for XML mapfiles... use the MapElementTree constructor to read it in, then use the writer to turn it into a mapfile... but is that better than what we have now? I guess it might make it easier to tweak things.
Comments are closed for this entry.
« January 2009 »
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
random stuff
Creative Commons License

Copyright 2006,2007 © Allan Doyle