Source

moinmoin-osm / parser / osm.py

# -*- coding: utf-8 -*-
"""
    MoinMoin - OSM map with many markers
    Given to the public domain, if you have issues
    with this type of contribution let me know to
    consider it
"""

from MoinMoin import wikiutil, i18n

Dependencies = []

class Parser:
    """
        Outputs an OpenStreetMap with markers, the first marker
        is the center of the map, you must provide the proper
        zoom to make the markers appear in the map
    """
    Dependencies = []
    
    def __init__(self, raw, request, **kw):
        self.raw = raw
        self.request = request
        self.form = request.form
        self._ = request.getText
        self.defaults = {
            "icon" : "http://open78layers.org/dev/img/marker.png",
            "centermap" : (4.65514589,-74.109660387),
            "mapzoom" : 6,
            "divname" : "map",
            "width" : 500,
            "height" : 300,
            }
        try :
            args=kw.get('format_args','').split(",")
            for pair in args:
                k,val = pair.split("=")
                self.defaults[k]=val
        except:
            """If the user does not pass properly formatted params, we omit them"""
            pass

    def format(self, formatter):
        """ Send the text. """
        markers=[]
        try:
            i=0
            for latlon in self.raw.split("\n"):
                try :
                    lat,lon = latlon.split(",")
                    float(lat),float(lon) #Make sure we have numbers
                    markers.append("d[%d]={'lat':%s,'lon':%s};" % (i,lat,lon))
                    i+=1
                except:
                    pass 
            self.defaults.update({'listmarkers':"\n".join(markers)})
            self.request.write("""<script
    src="http://static.slec.net/slecstats/js/jquery-latest.min.js"
    type="text/javascript"></script><script
    src="http://openlayers.org/api/2.10/OpenLayers.js"
    type="text/javascript"></script><script
    src="http://openstreetmap.org/openlayers/OpenStreetMap.js"
    type="text/javascript"></script><script
    src="http://static.slec.net/slecstats/js/map.js"
    type="text/javascript"></script><script type="text/javascript">
    jQuery(document).ready(function () {
       var d= Array();
       %(listmarkers)s
       var %(divname)s = my_map('id_%(divname)s',d,'%(icon)s',%(mapzoom)s);
    });

</script>
   <div style="width: %(width)spx; height: %(height)spx;" id="id_%(divname)s"></div>
   """ % self.defaults)
        except:
            self.request.write(formatter.sysmsg(1) + 
                formatter.text(self._('Error while trying to parse blockquote')) +
                formatter.sysmsg(0))