Mapfile builder
2006-08-07 14:35 | Permanent Link | Locative, Open SourceNow 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.