Ticket #12416: write_kml.diff

File write_kml.diff, 2.1 KB (added by jbronn, 15 years ago)
  • django/contrib/gis/utils/kml.py

     
     1from django.utils.encoding import smart_unicode
     2from django.utils.safestring import mark_safe
     3
     4_altitude_modes = {'clampToGround' : None,
     5                   'relativeToGround' : None,
     6                   'absolute' : None,
     7                   }
     8
     9def write_kml(geom, altitude_mode=None, extrude=None, id=None, tessellate=None):
     10    gtype = str(geom.geom_type)
     11   
     12    if gtype in ('MultiPolygon', 'MultiLineString', 'MultiPoint'):
     13        geom_elem = 'MultiGeometry'
     14        elem_replace = [gtype.replace('Multi', '')]
     15    elif gtype == 'GeometryCollection':
     16        geom_elem = 'MultiGeometry'
     17        elem_replace = ['Point', 'LineString', 'LinearRing', 'Polygon']
     18    else:
     19        geom_elem = gtype
     20        elem_replace = [gtype]
     21
     22    def kml_element(element, value):
     23        return (u'<%(element)s>%(value)s</%(element)s>' %
     24                {'element' : element, 'value' : value}
     25                )
     26
     27    tag_replace_kml = []
     28
     29    if not tessellate is None:
     30        tessellate = int(bool(tessellate))
     31        tag_replace_kml.append(kml_element('tessellate', tessellate))       
     32   
     33    if not extrude is None:
     34        extrude = int(bool(extrude))
     35        tag_replace_kml.append(kml_element('extrude', extrude))
     36
     37    if not altitude_mode is None:
     38        if not altitude_mode in _altitude_modes:
     39            raise ValueError('Altitude mode must be one of: "%s"' %
     40                             '","'.join(_altitude_modes.keys()))
     41        tag_replace_kml.append(kml_element('altitudeMode', altitude_mode))
     42
     43    kml = geom.kml
     44   
     45    if tag_replace_kml:
     46        replace_kml = u''.join(tag_replace_kml)
     47        for elem in elem_replace:
     48            elem = u'<%s>' % elem
     49            kml = kml.replace(elem, elem + replace_kml)
     50
     51    if not id is None:
     52        elem = u'<%s>' % geom_elem
     53        kml = kml.replace(elem, u'<%s id="%s">' % (geom_elem, smart_unicode(id)))
     54
     55    return kml
Back to Top