Created
July 10, 2018 06:07
-
-
Save amithkk/8be79ef3540aa1f1ec1724a875889413 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python3 | |
""" | |
KMZGEN - Python Script to generate kmz with thumbnails from a .csv file | |
GNU GPL v3 - http://github.com/amithkk | |
""" | |
import os | |
from PIL import Image, ImageDraw, ImageFont | |
import shutil | |
import uuid | |
import csv | |
import simplekml | |
### CONSTANTS | |
INPUTCSV = 'prlist.csv' | |
NAME_COL = 0 | |
PR_COL = 1 | |
YF_COL = 2 | |
LAT_COL = 3 | |
LON_COL = 4 | |
GHI_COL = 5 | |
NET_COL = 6 | |
FONTNAME = 'Monaco.ttf' | |
FONTSIZE = 15 | |
### END CONSTANTS | |
def create_thumb(title): | |
para = title.split("\n") | |
MAX_W, MAX_H = 200, 124 | |
im = Image.new('RGB', (MAX_W, MAX_H), (0, 0, 0, 0)) | |
draw = ImageDraw.Draw(im) | |
try: | |
font = ImageFont.truetype( | |
'C:\\Windows\\Fonts\\' + FONTNAME, FONTSIZE) | |
except OSError: | |
print("Font Does Not Exist.") | |
exit(1) | |
current_h, pad = 15, 4 | |
for line in para: | |
w, h = draw.textsize(line, font=font) | |
draw.text(((MAX_W - w) / 2, current_h), line, font=font) | |
current_h += h + pad | |
filename = str(uuid.uuid1()) + ".png" | |
im.save(os.path.join(os.getcwd(), 'temp', filename)) | |
return filename | |
def main(): | |
ip = csv.reader(open(INPUTCSV, 'r')) | |
header_list = next(ip) | |
next(ip) | |
kmprs = simplekml.Kml() | |
try: | |
os.mkdir(os.path.join(os.getcwd(), 'temp')) | |
except FileExistsError: | |
pass | |
for row in ip: | |
thumbf = "{name}\nPR:{PR:.2f}\nYF:{YF:.2f}\nGHI:{GHI:.2f}\nNET:{NET:.2f}".format(name=row[NAME_COL], | |
PR=float(row[PR_COL]), | |
YF=float(row[YF_COL]), | |
GHI=float(row[GHI_COL]), | |
NET=float(row[NET_COL])) | |
descrf = "<br/>".join(["%s : %s" % t for t in zip(header_list, row)]) | |
thumbfile = create_thumb(thumbf) | |
pnt = kmprs.newpoint(name=row[NAME_COL], coords=[(row[LON_COL], row[LAT_COL])], description=descrf) | |
pnt.style.iconstyle.icon.href = 'files/' + thumbfile | |
pnt.style.iconstyle.scale = 4.0 | |
pnt.style.labelstyle.scale = 0 | |
kmprs.addfile(os.path.join(os.getcwd(), 'temp', thumbfile)) | |
try: | |
kmprs.savekmz('final.kmz') | |
except PermissionError: | |
print("KML File Unwritable. Close all KML programs") | |
shutil.rmtree(os.path.join(os.getcwd(), 'temp')) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment