Django

Code

Changeset 7213

Show
Ignore:
Timestamp:
03/10/08 15:45:51 (4 months ago)
Author:
jbronn
Message:

gis: Fixed #6746 by marking safe GoogleMap internal XHTML/JavaScript; added support for GPolygon and GPolyline overlays via the polygons and polylines keywords; the zoom keyword may now take a geometry for automatic zoom level determination; *.pyc files are now ignored in django.contrib.gis modules.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/gis/django/contrib/gis/maps/google/gmap.py

    r7027 r7213  
    11from django.conf import settings 
     2from django.contrib.gis import geos 
    23from django.template.loader import render_to_string 
     4from django.utils.safestring import mark_safe 
     5 
     6# Declaring the GoogleMapException prior to getting the  
     7# default `GZOOM` GoogleZoom instance. 
     8class GoogleMapException(Exception): pass 
     9from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline 
     10from django.contrib.gis.maps.google.zoom import GoogleZoom 
     11GZOOM = GoogleZoom() 
    312 
    413# The default Google Maps URL (for the API javascript) 
     
    615GOOGLE_MAPS_URL='http://maps.google.com/maps?file=api&v=%s&key=' 
    716 
    8 class GoogleMapException(Exception): pass 
    9          
    1017class GoogleMap(object): 
    11     "A class for generating Google Maps javascript." 
     18    "A class for generating Google Maps JavaScript." 
    1219 
    1320    # String constants 
    14     onunload = 'onunload="GUnload()"' # Cleans up after Google Maps 
    15     vml_css  = 'v\:* {behavior:url(#default#VML);}' # CSS for IE VML 
    16     xmlns    = 'xmlns:v="urn:schemas-microsoft-com:vml"' # XML Namespace (for IE VML). 
     21    onunload = mark_safe('onunload="GUnload()"') # Cleans up after Google Maps 
     22    vml_css  = mark_safe('v\:* {behavior:url(#default#VML);}') # CSS for IE VML 
     23    xmlns    = mark_safe('xmlns:v="urn:schemas-microsoft-com:vml"') # XML Namespace (for IE VML). 
    1724 
    1825    def __init__(self, key=None, api_url=None, version=None, 
    19                  center_lat=0.0, center_lon=0.0, zoom=1,  
    20                  dom_id='map', load_func='gmap_load',  
    21                  kml_urls=[], template='gis/google/js/google-map.js', 
     26                 center=None, center_lat=0.0, center_lon=0.0, 
     27                 zoom=None, dom_id='map', load_func='gmap_load',  
     28                 kml_urls=[], polygons=[], polylines=[], 
     29                 template='gis/google/js/google-map.js', 
    2230                 extra_context={}): 
    2331 
     
    2735        if not key: 
    2836            try: 
    29                 self._key = settings.GOOGLE_MAPS_API_KEY 
     37                self.key = settings.GOOGLE_MAPS_API_KEY 
    3038            except AttributeError: 
    31                 raise GoogleMapException, 'Google Maps API Key not found (try adding GOOGLE_MAPS_API_KEY to your settings).' 
     39                raise GoogleMapException('Google Maps API Key not found (try adding GOOGLE_MAPS_API_KEY to your settings).') 
    3240        else: 
    33             self._key = key 
     41            self.key = key 
    3442         
    3543        # Getting the Google Maps API version, defaults to using the latest ("2.x"), 
    3644        #  this is not necessarily the most stable. 
    3745        if not version: 
    38             try: 
    39                 self._version = settings.GOOGLE_MAPS_API_VERSION 
    40             except AttributeError: 
    41                 self._version = '2.x' 
     46            self.version = getattr(settings, 'GOOGLE_MAPS_API_VERSION', '2.x') 
    4247        else: 
    43             self._version = version 
     48            self.version = version 
    4449 
    4550        # Can specify the API URL in the `api_url` keyword. 
    4651        if not api_url: 
    47             try: 
    48                 self._url = settings.GOOGLE_MAPS_URL % self._version 
    49             except AttributeError: 
    50                 self._url = GOOGLE_MAPS_URL % self._version 
     52            self.api_url = mark_safe(getattr(settings, 'GOOGLE_MAPS_URL', GOOGLE_MAPS_URL) % self.version) 
    5153        else: 
    52             self._url = api_url 
     54            self.api_url = api_url 
    5355 
    54         # Setting the DOM id of the map, the center lat/lon, the load function, 
    55         # and the zoom
     56        # Setting the DOM id of the map, the load function, the JavaScript 
     57        # template, and the KML URLs array
    5658        self.dom_id = dom_id 
    57         self.center_lat = center_lat 
    58         self.center_lon = center_lon 
    5959        self.load_func = load_func 
    6060        self.template = template 
    61         self.zoom = zoom 
     61        self.kml_urls = kml_urls 
     62         
     63        # Does the user want any GPolygon or GPolyline overlays? 
     64        self.polygons, self.polylines = [], [] 
     65        if polygons: 
     66            for poly in polygons: 
     67                if isinstance(poly, GPolygon):  
     68                    self.polygons.append(poly) 
     69                else: 
     70                    self.polygons.append(GPolygon(poly)) 
     71        if polylines: 
     72            for pline in polylines: 
     73                if isinstance(pline, GPolyline): 
     74                    self.polylines.append(pline) 
     75                else: 
     76                    self.polylines.append(GPolyline(pline)) 
     77        
     78        # Automatically determining the zoom level if there are 
     79        # GPolygon and/or GPolyline overlays. 
     80        if (self.polygons or self.polylines) and zoom is None: 
     81            envelopes = [p.envelope for p in self.polygons] 
     82            envelopes.extend([p.envelope for p in self.polylines]) 
     83            # Creating a MultiPolygon of all the envelopes, this will 
     84            # be used in determining the zoom level. 
     85            zoom = geos.MultiPolygon(envelopes) 
     86            zoom.srid = 4326 
     87         
     88        # If a GEOSGeometry object is passed in for the `zoom` keyword 
     89        # argument, then try to automatically determine an appropriate 
     90        # zoom level. 
     91        if isinstance(zoom, geos.GEOSGeometry): 
     92            self.zoom = GZOOM.get_zoom(zoom) 
     93        else: 
     94            self.zoom = zoom 
     95 
     96        # The map center coordinate -- the `center_lon` and `center_lat` keyword 
     97        # are deprecated. 
     98        if not center: 
     99            center = (center_lon, center_lat) 
     100        self.center = center 
    62101 
    63102        # Setting the parameters for the javascript template. 
    64         params = {'center_lat' : center_lat, 
    65                   'center_lon' : center_lon, 
    66                   'dom_id' : dom_id, 
    67                   'kml_urls' : kml_urls, 
    68                   'load_func' : load_func, 
    69                   'zoom' : zoom, 
     103        params = {'center' : self.center, 
     104                  'dom_id' : self.dom_id, 
     105                  'kml_urls' : self.kml_urls, 
     106                  'load_func' : self.load_func, 
     107                  'zoom' : self.zoom, 
     108                  'polygons' : self.polygons, 
     109                  'polylines' : self.polylines, 
    70110                  } 
    71111        params.update(extra_context) 
    72         self.js = render_to_string(template, params) 
     112        self.js = render_to_string(self.template, params) 
    73113 
    74114    @property 
    75115    def body(self): 
    76116        "Returns HTML body tag for loading and unloading Google Maps javascript." 
    77         return '<body %s %s>' % (self.onload, self.onunload
     117        return mark_safe('<body %s %s>' % (self.onload, self.onunload)
    78118 
    79119    @property 
    80120    def onload(self): 
    81121        "Returns the `onload` HTML <body> attribute." 
    82         return 'onload="%s()"' % self.load_func 
     122        return mark_safe('onload="%s()"' % self.load_func) 
    83123 
    84124    @property 
    85125    def api_script(self): 
    86126        "Returns the <script> tag for the Google Maps API javascript." 
    87         return '<script src="%s%s" type="text/javascript"></script>' % (self._url, self._key
     127        return mark_safe('<script src="%s%s" type="text/javascript"></script>' % (self.api_url, self.key)
    88128 
    89129    @property 
    90130    def scripts(self): 
    91131        "Returns all <script></script> tags required for Google Maps JavaScript." 
    92         return '%s\n  <script type="text/javascript">\n//<![CDATA[\n%s//]]>\n  </script>' % (self.api_script, self.js
     132        return mark_safe('%s\n  <script type="text/javascript">\n//<![CDATA[\n%s//]]>\n  </script>' % (self.api_script, self.js)
    93133 
    94134    @property 
    95135    def style(self): 
    96136        "Returns additional CSS styling needed for Google Maps on IE." 
    97         return '<style type="text/css">%s</style>' % self.vml_css 
     137        return mark_safe('<style type="text/css">%s</style>' % self.vml_css) 
    98138 
    99139    @property 
    100140    def xhtml(self): 
    101141        "Returns XHTML information needed for IE VML overlays." 
    102         return '<html xmlns="http://www.w3.org/1999/xhtml" %s>' % self.xmlns 
     142        return mark_safe('<html xmlns="http://www.w3.org/1999/xhtml" %s>' % self.xmlns) 
  • django/branches/gis/django/contrib/gis/maps/google/__init__.py

    r6108 r7213  
    2020     </head> 
    2121     {{ google.body }} 
    22      <div id="{{ google.dom_id }}"></div> 
     22     <div id="{{ google.dom_id }}" style="width:600px;height:400px;"></div> 
    2323     </body> 
    2424     </html> 
     
    4848     <body onload="gmap_load()" onunload="GUnload()"> 
    4949 
    50    - The `id` property returns the DOM id for the map.  Defaults to "map". 
     50   - The `dom_id` property returns the DOM id for the map.  Defaults to "map". 
    5151 
    5252  The following attributes may be set or customized in your local settings: 
     
    5757      version. 
    5858""" 
    59 from django.contrib.gis.maps.google.gmap import GoogleMap 
     59from django.contrib.gis.maps.google.gmap import GoogleMap, GZOOM 
     60from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline 
    6061from django.contrib.gis.maps.google.zoom import GoogleZoom 
  • django/branches/gis/django/contrib/gis/maps/google/zoom.py

    r6108 r7213  
    100100        "Converts a pixel to a longitude, latitude pair at the given zoom level." 
    101101        if len(px) != 2: 
    102             raise TypeError, 'Pixel should be a sequence of two elements.' 
     102            raise TypeError('Pixel should be a sequence of two elements.') 
    103103 
    104104        # Getting the number of pixels for the given zoom level. 
     
    139139 
    140140        # Checking the input type. 
    141         if not isinstance(geom, GEOSGeometry) and geom.srid == 4326: 
    142             raise TypeError, 'get_zoom() expects a GEOS Geometry with an SRID of 4326.' 
     141        if not isinstance(geom, GEOSGeometry) or geom.srid != 4326: 
     142            raise TypeError('get_zoom() expects a GEOS Geometry with an SRID of 4326.') 
    143143 
    144144        # Getting the envelope for the geometry, and its associated width, height 
     
    157157            if (env_w > tile_w) or (env_h > tile_h): 
    158158                if z == 0:  
    159                     raise GoogleMapException, \ 
    160                         'Geometry width and height should not exceed that of the Earth.' 
     159                    raise GoogleMapException('Geometry width and height should not exceed that of the Earth.') 
    161160                return z-1 
    162161         
    163162        # Otherwise, we've zoomed in to the max. 
    164163        return self._nzoom-1 
     164 
  • django/branches/gis/django/contrib/gis/templates/gis/google/js/google-map.js

    r6108 r7213  
    1 {% block vars %}var map;{% for kml_url in kml_urls %}var kml{{ forloop.counter }};{% endfor %}{% endblock %} 
     1{% autoescape off %}{% block vars %}var map;{% endblock %} 
    22{% block functions %}{% endblock %} 
    33{% block load %}function {{ load_func }}(){ 
    44  if (GBrowserIsCompatible()) { 
    55    map = new GMap2(document.getElementById("{{ dom_id }}")); 
    6     map.addControl(new GSmallMapControl()); 
    7     map.addControl(new GMapTypeControl()); 
    8     map.setCenter(new GLatLng({{ center_lat }}, {{ center_lon }}), {{ zoom }}); 
    9     {% for kml_url in kml_urls %}kml{{ forloop.counter }} = new GGeoXml("{{ kml_url }}"); 
     6    {% block controls %}map.addControl(new GSmallMapControl()); 
     7    map.addControl(new GMapTypeControl());{% endblock %} 
     8    map.setCenter(new GLatLng({{ center.1 }}, {{ center.0 }}), {{ zoom }}); 
     9    {% for kml_url in kml_urls %}var kml{{ forloop.counter }} = new GGeoXml("{{ kml_url }}"); 
    1010    map.addOverlay(kml{{ forloop.counter }});{% endfor %} 
     11    {% for polygon in polygons %}var poly{{ forloop.counter }} = new {{ polygon }}; 
     12    map.addOverlay(poly{{ forloop.counter }});{% endfor %} 
     13    {% for polyline in polylines %}var polyline{{ forloop.counter }} = new {{ polyline }}; 
     14    map.addOverlay(polyline{{ forloop.counter }});{% endfor %} 
    1115    {% block load_extra %}{% endblock %} 
    1216  }else { 
     
    1418  } 
    1519} 
    16 {% endblock %} 
     20{% endblock %}{% endautoescape %}