Created
June 30, 2023 00:37
-
-
Save vduenasg/a97b0cf6dc4c7f8f48b2ee7e423e7782 to your computer and use it in GitHub Desktop.
Generador archivo .kml
This file contains hidden or 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
| import os | |
| # Opening my .txt file where my locations are defined, and storing in locations variable as a list | |
| path_project = 'D:/Git Files/Scripts/KML/' | |
| path_locations = path_project + 'data.txt' | |
| my_locations_file = open(path_locations, encoding='utf-8') | |
| locations_raw = my_locations_file.readlines() | |
| db_locations = [] | |
| for i in range(len(locations_raw)): | |
| db_locations.append(locations_raw[i].strip('\n').split('\t')) | |
| def gen_kml(): | |
| folder_name = 'KML' | |
| if not os.path.exists(folder_name): | |
| os.makedirs(folder_name) | |
| config_file = open(path_project + folder_name + '/' + 'locations.kml', 'w') | |
| yellow_code = ['m_ylw-pushpin', 's_ylw-pushpin', 's_ylw-pushpin_hl', 'http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png'] | |
| blue_code = ['msn_blue-pushpin', 'sn_blue-pushpin', 'sh_blue-pushpin', 'http://maps.google.com/mapfiles/kml/pushpin/blue-pushpin.png'] | |
| green_code = ['m_ylw-pushpin00', 's_ylw-pushpin000', 's_ylw-pushpin_hl0', 'http://maps.google.com/mapfiles/kml/pushpin/grn-pushpin.png'] | |
| red_code = ['msn_red-pushpin', 'sn_red-pushpin', 'sn_red-pushpin', 'http://maps.google.com/mapfiles/kml/pushpin/red-pushpin.png'] | |
| placemark_colors = [yellow_code, blue_code, green_code, red_code] | |
| pushpin_colors = {'AMARILLO': '<styleUrl>#m_ylw-pushpin</styleUrl>\n', | |
| 'AZUL': '<styleUrl>#msn_blue-pushpin</styleUrl>\n', | |
| 'VERDE': '<styleUrl>#m_ylw-pushpin00</styleUrl>\n', | |
| 'ROJO': '<styleUrl>#msn_red-pushpin</styleUrl>\n'} | |
| # Defining default file template | |
| config_file.write("""<?xml version="1.0" encoding="UTF-8"?>\n | |
| <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">\n | |
| <Document>\n | |
| <name>Locations.kml</name>\n | |
| <StyleMap id="style_Template1">\n | |
| <Pair>\n | |
| <key>normal</key>\n | |
| <styleUrl>#style_Template1_normal</styleUrl>\n | |
| </Pair>\n | |
| <Pair>\n | |
| <key>highlight</key>\n | |
| <styleUrl>#style_Template1_highlight</styleUrl>\n | |
| </Pair>\n | |
| </StyleMap>\n""") | |
| # Defining yellow, blue, green, red color templates | |
| for color in placemark_colors: | |
| config_file.write('''<StyleMap id="''' + color[0] + '''">\n | |
| <Pair>\n | |
| <key>normal</key>\n | |
| <styleUrl>#''' + color[1] + '''</styleUrl>\n | |
| </Pair>\n | |
| <Pair>\n | |
| <key>highlight</key>\n | |
| <styleUrl>#''' + color[2] + '''</styleUrl>\n | |
| </Pair>\n | |
| </StyleMap>\n | |
| <Style id="''' + color[1] + '''">\n | |
| <IconStyle>\n | |
| <scale>1.1</scale>\n | |
| <Icon>\n | |
| <href>''' + color[3] + '''</href>\n | |
| </Icon>\n | |
| <hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>\n | |
| </IconStyle>\n | |
| </Style>\n | |
| <Style id="''' + color[2] + '''">\n | |
| <IconStyle>\n | |
| <scale>1.3</scale>\n | |
| <Icon>\n | |
| <href>''' + color[3] + '''</href>\n | |
| </Icon>\n | |
| <hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>\n | |
| </IconStyle>\n | |
| </Style>\n''') | |
| old_site_project = '' | |
| for site in db_locations: | |
| site_project = site[0] | |
| site_location = site[1] | |
| site_latitude = site[2] | |
| site_longitude = site[3] | |
| site_color = site[4] | |
| # Create a folder for every group of locations | |
| if site_project != old_site_project: | |
| if old_site_project != '': | |
| config_file.write('</Folder>\n') | |
| config_file.write('<Folder>\n') | |
| config_file.write('<name>' + site_project + '</name>\n') | |
| config_file.write('<open>1</open>\n') | |
| # Put site on map | |
| config_file.write('<Placemark>\n') | |
| config_file.write('<name>' + site_location + '</name>\n') | |
| config_file.write('<styleUrl>#style_Template1</styleUrl>\n') | |
| config_file.write(pushpin_colors.get(site_color)) | |
| config_file.write('<Point>\n') | |
| config_file.write('<coordinates>' + site_longitude + ',' + site_latitude + '</coordinates>\n') | |
| config_file.write('</Point>\n') | |
| config_file.write('</Placemark>\n') | |
| # Keeping track of site project folder | |
| old_site_project = site_project[:] | |
| config_file.write('</Folder>\n') | |
| config_file.write('</Document>\n') | |
| config_file.write('</kml>\n') | |
| config_file.close() | |
| gen_kml() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment