Уведомления

Группа в Telegram: @pythonsu

#1 Ноя. 15, 2010 20:13:37

toksin
От:
Зарегистрирован: 2010-09-09
Сообщения: 31
Репутация: +  -1  -
Профиль   Отправить e-mail  

Kml builder

Написал не большей модуль для создания kml файлов. Модуль может создавать метки, линии и полигоны.
Что такое kml можно узнать здесь http://ru.wikipedia.org/wiki/Kml
Подробней о модуле в моем блоге http://illusory-planet.blogspot.com/2010/11/kml-builder-on-python3.html
Я писал этот модуль для создания меток в google earth.

'''
Created on 15 11 2010
@author: toksin
site: illusory-planet.blogspot.com
license: free
'''

class Kml(object):
'''
Creates a KML file on the specified parameters.
Parameters of the class - KML (path(/home/user/), filename),
the default file is created default.kml, in the
same directory where the script is run.
'''


def __init__(self, file_path='', file_name='default.kml'):
'''
Initialized variables file name, filepath,
header document and footer document.
'''
self.file_path = file_path
self.file_name = file_name
self.head = '''<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"><Document>'''
self.foot = r'</Document></kml>'
self.content = self.head


def _transformation_coordinats(self, coordinats):
'''
Transformation [[latitude, longitude, altitud],[latitude, longitude, altitud]]
to
latitude, longitude, altitud
latitude, longitude, altitud
'''
if coordinats:
cr_list = ''
for item in range(len(coordinats)):
latitude, longitude, altitud = coordinats[item]
cr_list += ''' {0},{1},{2}'''.format(latitude, longitude, altitud)

return cr_list


def set_point(self, latitude, longitude, altitud, name, description):
'''
Creates a point with latitude, longitude,
altitude, name and description.
'''
self.content += '''<Placemark>
<name>{3}</name>
<description>{4}</description>
<Point>
<coordinates>{0},{1},{2}</coordinates>
</Point>
</Placemark>'''.format(latitude, longitude, altitud, name, description)


def set_line(self, coordinats, name, hexcolor='random'):
'''
Draws a line on the received coordinates.
The first parameter specifies the coordinates
of the second sets the line color in hexadecimal
format (default color is chosen at random), such as ffffffff - black.
Format coordinats [latitude, longitude, altitud] like [33.245, 12.023, 100]
or [-23.001, -125.221,1000], altitude = meters.
'''
line = '''<name>{2}</name>
<Style id="sn_ylw-pushpin">
<IconStyle>
<scale>1,1</scale>
<Icon>
<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
</Icon>
<hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
</IconStyle>
<LineStyle>
<color>{0}</color>
</LineStyle>
</Style>
<StyleMap id="msn_ylw-pushpin">
<Pair>
<key>normal</key>
<styleUrl>#sn_ylw-pushpin</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#sh_ylw-pushpin</styleUrl>
</Pair>
</StyleMap>
<Style id="sh_ylw-pushpin">
<IconStyle>
<scale>1,3</scale>
<Icon>
<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
</Icon>
<hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
</IconStyle>
<LineStyle>
<color>{0}</color>
</LineStyle>
</Style>
<Placemark>
<name>{1}</name>
<styleUrl>#msn_ylw-pushpin</styleUrl>
<LineString>
<tessellate>4</tessellate>
<coordinates>'''.format(hexcolor, name, self.file_name)

line += self._transformation_coordinats(coordinats)

line += ''' </coordinates>
</LineString>
</Placemark>'''
self.content += line


def set_polygon(self, coordinats, hexcolor='random'):
'''
Creates a polygon requires at least 3 points,
hexcolor = set color polygon in hexadecimal format.
'''
str_coord = self._transformation_coordinats(coordinats)
polygon = '''
<Placemark>
<Polygon>
<altitudeMode>absolute</altitudeMode>
<outerBoundaryIs>
<LinearRing>
<coordinates>
{1}
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
<Style>
<PolyStyle>
<color>#{0}</color>
<outline>0</outline>
</PolyStyle>
</Style>
</Placemark>
'''.format( hexcolor,str_coord+str_coord[:str_coord.find(' ')-1])

self.content += polygon


def create_kml(self):
self.content += self.foot
try:
out_file = open(self.file_path + self.file_name, 'w')
out_file.write(self.content)
out_file.close()
except IOError as err:
print(str(err))



Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version