Browse Source

Make it a cli app with parameters.

main
gabriel becker 2 years ago
parent
commit
c84d7b6684
  1. BIN
      data/default.kmz
  2. 1
      requirements.txt
  3. 8
      run.sh
  4. 67
      src/__main__.py

BIN
data/default.kmz

Binary file not shown.

1
requirements.txt

@ -1,2 +1,3 @@
pandas pandas
openpyxl openpyxl
click

8
run.sh

@ -0,0 +1,8 @@
source /home/gabriel/dev/Python/Environments/xlsx2kmz/bin/activate
python3 /home/gabriel/dev/Python/xlsx2kmz/src/__main__.py \
-i data/housing.xlsx \
-o testoutput.kml \
-b data/Acoomodations.kmz \
-n "Price,Who,Includes Bills" \
-d "Url,Min Stay,Who,Price,Notes" \
-l Location

67
src/__main__.py

@ -1,3 +1,4 @@
from calendar import prmonth
import os import os
import zipfile import zipfile
import numpy as np import numpy as np
@ -5,18 +6,40 @@ import pandas as pd
from pathlib import Path from pathlib import Path
import xml.etree.ElementTree as xmltree import xml.etree.ElementTree as xmltree
from xml.dom import minidom from xml.dom import minidom
import click
class Config:
name_columns = ['Price', 'Includes Bills']
description_columns = ['Url', 'Min Stay', 'Notes']
location_column = 'Location'
@staticmethod
def set_name_columns(name_columns):
if isinstance(name_columns, str):
name_columns = name_columns.split(',')
Config.name_columns = name_columns
@staticmethod
def set_description_columns(description_columns):
if isinstance(description_columns, str):
description_columns = description_columns.split(',')
Config.description_columns = description_columns
@staticmethod
def set_location_column(location_column):
assert isinstance(location_column, str)
Config.location_column = location_column
def load_kmz(kmz_file_path): def load_kmz(kmz_file_path):
print(Path(kmz_file_path).resolve())
print(Path(kmz_file_path).resolve().exists())
file_to_extract = 'doc.kml' file_to_extract = 'doc.kml'
xml = None xml = None
try: try:
with zipfile.ZipFile(kmz_file_path) as z: with zipfile.ZipFile(kmz_file_path) as z:
xml = xmltree.fromstring(z.read(file_to_extract)) xml = xmltree.fromstring(z.read(file_to_extract))
except: except:
print("Invalid file") click.echo("Invalid file")
return xml return xml
@ -41,13 +64,11 @@ def append_placemarks_to_xml(xml, placemarks):
def map_dit(dict_list): def map_dit(dict_list):
items = list() items = list()
name_columns = ['Price', 'Includes Bills']
description_columns = ['Url', 'Min Satay', 'Notes']
location_column = 'Location'
for item in dict_list: for item in dict_list:
name = ' '.join([str(item[x]) for x in name_columns]) name = ' '.join([str(item[x]) for x in Config.name_columns])
description = '\n'.join([str(item[x]) for x in description_columns if item[x] is not np.nan]) description = '\n'.join([str(item[x]) for x in Config.description_columns if item[x] is not np.nan])
location = parse_location(item[location_column]) location = parse_location(item[Config.location_column])
items.append(dict(name=name, description=description, location=location)) items.append(dict(name=name, description=description, location=location))
return items return items
@ -74,24 +95,42 @@ def map_dict_element_to_placemark_xml(element: dict):
return xml_element return xml_element
def main(): def process(xlsx_path, base_kmz_path, output_path):
dict_elements = map_dit(load_xlsx('data/housing.xlsx')) dict_elements = map_dit(load_xlsx(xlsx_path))
placemarks = [map_dict_element_to_placemark_xml(dict_element) for dict_element in dict_elements] placemarks = [map_dict_element_to_placemark_xml(dict_element) for dict_element in dict_elements]
kmz_xml = load_kmz('data/Acoomodations.kmz') kmz_xml = load_kmz(base_kmz_path)
kmz_xml = remove_all_current_placemarkers(kmz_xml) kmz_xml = remove_all_current_placemarkers(kmz_xml)
kmz_xml = append_placemarks_to_xml(kmz_xml, placemarks) kmz_xml = append_placemarks_to_xml(kmz_xml, placemarks)
xmltree.register_namespace("", 'http://www.opengis.net/kml/2.2') xmltree.register_namespace("", 'http://www.opengis.net/kml/2.2')
tree = xmltree.ElementTree(kmz_xml) tree = xmltree.ElementTree(kmz_xml)
with open ('testoutput.kml', "w") as file: with open (output_path, "w") as file:
rough_string = xmltree.tostring(kmz_xml, 'utf-8') rough_string = xmltree.tostring(kmz_xml, 'utf-8')
reparsed = minidom.parseString(rough_string) reparsed = minidom.parseString(rough_string)
content = reparsed.toprettyxml(indent="\t") content = reparsed.toprettyxml(indent="\t")
file.write(content) file.write(content)
import click
@click.command()
@click.option("-i", "--xlsx-path", prompt="XLSX containign data to convert to kml.")
@click.option("-o", "--output-path", default='output.kml', prompt="Output fiel path.", help="Where to save output.")
@click.option("-b", "--kmz-path", default='data/default.kmz', help="Base kmz file that provides templating.")
@click.option("-n", "--name", default='name', prompt='Columns to be used as name.')
@click.option("-d", "--description", default='description', prompt='Columns to be used as description.')
@click.option("-l", "--location", default='locaiton', prompt='Columns to be used as location.')
def convert_xlsx_to_kml(xlsx_path, kmz_path, output_path, name, description, location):
Config.set_name_columns(name)
Config.set_description_columns(description)
Config.set_location_column(location)
process(
xlsx_path, kmz_path, output_path
)
if __name__ == '__main__': if __name__ == '__main__':
main() convert_xlsx_to_kml()

Loading…
Cancel
Save