def store_kml(obj, obj_id, admin_level, name=u'unknown'):
"""
Store a shapely geometry object as a KML file.
:param BaseGeometry obj: The object to store
:param int obj_id: The object ID of region.
:param int admin_level: Admin level of region [default=0]
:param str|unicode name: Name of the region to store in KML.
:return:
"""
ascii_name = gpx_utils.enforce_unicode(name).encode('ascii', 'replace')
filename = './%s_%s.kml' % (ascii_name.replace(' ', '_'), obj_id)
__LOG.info(u'store_kml: storing a %s with size: %s ', obj.geom_type, obj.area)
ns = '{http://www.opengis.net/kml/2.2}'
sls = styles.LineStyle(color='ffff0000')
sas = styles.PolyStyle(color='5500ff00')
sps = styles.BalloonStyle(bgColor='ff0000ff')
style = styles.Style(styles=[sls, sas, sps])
kf = kml.KML(ns)
if obj.geom_type == 'LineString' or obj.geom_type == 'MultiLineString' or obj.geom_type == 'LinearRing':
d = kml.Document(ns, str(obj_id), 'Traces', 'GPX Visualization')
elif obj.geom_type == 'Polygon' or obj.geom_type == 'MultiPolygon':
d = kml.Document(ns, str(obj_id), 'Border of {0} ({1})'.format(ascii_name, obj_id), 'Border visualization')
else:
d = kml.Document(ns, str(obj_id), 'Points', 'Point visualization')
kf.append(d)
p = kml.Placemark(ns, str(obj_id), ascii_name, '{0}'.format(ascii_name), styles=[style])
p.geometry = obj
d.append(p)
fil = open(filename, 'w')
fil.write(kf.to_string(prettyprint=True))
fil.flush()
fil.close()
__LOG.debug(u'store_kml: store successful (%s/%s) -> %s' % (admin_level, obj_id, filename))
评论列表
文章目录