Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Wit, Allard de
csvtools
Commits
0afc1719
Commit
0afc1719
authored
Jan 27, 2022
by
Wit, Allard de
Browse files
Added writing KML files
parent
3433a997
Changes
2
Hide whitespace changes
Inline
Side-by-side
csvtools/cmd.py
View file @
0afc1719
...
...
@@ -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
csvtools/csv2kml.py
0 → 100644
View file @
0afc1719
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
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment