Commit 0afc1719 authored by Wit, Allard de's avatar Wit, Allard de
Browse files

Added writing KML files

parent 3433a997
......@@ -4,6 +4,7 @@
import click
from .csv2db import process_csv
from .csv2kml import generate_KML
@click.group(help="With csvtools you can operate on CSV files")
......@@ -15,19 +16,21 @@ def cli():
@click.argument("csv_fname", type=click.Path(exists=True))
@click.argument("sqlite_fname", type=click.Path())
def cmd_csv2db(csv_fname, sqlite_fname):
"""Function for the commandline interface for csv2db
process_csv(csv_fname, sqlite_fname)
:param csv_fname: the CSV input filename
:param sqlite_fname: the SQLite output filename
"""
process_csv(csv_fname, sqlite_fname)
@click.command("csv2kml")
@click.argument("csv_fname", type=click.Path(exists=True))
@click.argument("kml_fname", type=click.Path())
def cmd_csv2kml(csv_fname, kml_fname):
generate_KML(csv_fname, kml_fname)
def main():
cli.add_command(cmd_csv2db)
cli.add_command(cmd_csv2kml)
cli()
if __name__ == "__main__":
if __name__ == '__main__':
main()
\ No newline at end of file
from pathlib import Path
import datetime as dt
import simplekml
import pandas
def generate_KML(fname_csv, fname_kml):
fname_csv = Path(fname_csv).absolute()
fname_kml = Path(fname_kml).absolute()
df = pandas.read_csv(fname_csv)
kml = simplekml.Kml()
failures = 0
for i, row in enumerate(df.itertuples()):
try:
pnt = kml.newpoint(name=row.city, coords=[(row.longitude, row.latitude)], description=row.comments)
occurred = dt.datetime.strptime(row.datetime, "%m/%d/%Y %H:%M").date()
pnt.timestamp.when = occurred.isoformat()
except ValueError as e:
failures += 1
if i == 1000:
break
if failures:
print(f"{failures} points failed to convert to KML.")
kml.save(fname_kml)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment