Last active
August 29, 2015 14:00
-
-
Save nextsux/11478584 to your computer and use it in GitHub Desktop.
kachlicka
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python2 | |
import csv | |
import sys | |
# overeni povinnych parametru | |
if len(sys.argv) < 3: | |
print("Usage: %s <file.csv> <output.kml>" % sys.argv[0]) | |
sys.exit(1) | |
# otevreme vstupni soubor | |
with open(sys.argv[1], 'rb') as csvfile: | |
# otevreme vystupni soubor | |
with open(sys.argv[2], 'wb') as out: | |
# na vstup pouzijeme csv parser, carka jako oddelovac poli, uvozovka jako obalovaci znak | |
c = csv.reader(csvfile, delimiter=',', quotechar='"') | |
# zapiseme hlavicku KML souboru | |
out.write("""<?xml version="1.0" encoding="UTF-8"?> | |
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom"> | |
<Document> | |
<name>%(outname)s</name> | |
<Style id="sh_donut"> | |
<IconStyle> | |
<scale>0.85</scale> | |
<Icon> | |
<href>http://maps.google.com/mapfiles/kml/shapes/donut.png</href> | |
</Icon> | |
</IconStyle> | |
<ListStyle> | |
</ListStyle> | |
</Style> | |
<Style id="sn_donut"> | |
<IconStyle> | |
<scale>0.8</scale> | |
<Icon> | |
<href>http://maps.google.com/mapfiles/kml/shapes/donut.png</href> | |
</Icon> | |
</IconStyle> | |
<ListStyle> | |
</ListStyle> | |
</Style> | |
<StyleMap id="msn_donut"> | |
<Pair> | |
<key>normal</key> | |
<styleUrl>#sn_donut</styleUrl> | |
</Pair> | |
<Pair> | |
<key>highlight</key> | |
<styleUrl>#sh_donut</styleUrl> | |
</Pair> | |
</StyleMap> | |
""" % {'outname': sys.argv[2]}) | |
# incializujeme citac bodu | |
i = 0 | |
# pro kazdy radek v CSV | |
for coord in c: | |
# zapiseme placemark do vystupniho souboru | |
# i - sekvencni citac pro nazev obrazku v url, format na 3 mista s nulami jako prefix | |
# lat, lon - souradnice z CSV, cislo s plovouci desetinnou carkou na 13 des. mist | |
out.write(""" <Placemark> | |
<description> | |
<![CDATA[<img src="http://kachlik.com/dp/termo/%(id)03i.jpg"/><br /> | |
<img src="http://kachlik.com/dp/optic/%(id)03i.jpg"/>]]> | |
</description> | |
<styleUrl>#msn_donut</styleUrl> | |
<Point> | |
<gx:drawOrder>1</gx:drawOrder> | |
<coordinates>%(lat).13f,%(lon).13f,0</coordinates> | |
</Point> | |
</Placemark> | |
""" % { | |
'lat': float(coord[0]), | |
'lon': float(coord[1]), | |
'id': i, | |
}) | |
# inkrementujeme sekvenci obrazku | |
i += 1 | |
# na konec souboru zapiseme paticku | |
out.write(""" </Document> | |
</kml> | |
""") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment