-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdwca2cartodb.py
executable file
·60 lines (49 loc) · 2.06 KB
/
dwca2cartodb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
import argparse
import sys
from cartodb import CartoDBException
from dwca import DwCAReader
from utils import query_yes_no, dwcaline_to_epsg4326, valid_dwca
from output.cartodb_out import CartoDBOutput
DEFAULT_IMPORTED_FIELDS = ['occurrenceID', 'scientificName', 'eventDate']
def main():
# TODO: Better CLI Help
# TODO: split into parse_args() and main(args) (see dwca2shp)
parser = argparse.ArgumentParser(
description="Import a DarwinCore Archive file into CartDB.")
parser.add_argument('--domain',
help='Your CartoDB domain (without .cartodb.com).',
required=True)
parser.add_argument('--api-key',
dest='apikey',
help='Your CartoDB API key.',
required=True)
parser.add_argument('--table',
help="CartoDB destination table name",
required=True)
parser.add_argument('--truncate-table',
action='store_true',
dest='truncate',
help="Truncate destination table prior to import.")
parser.add_argument('source_file',
help="Source DwC-A file",
type=argparse.FileType('r'))
args = parser.parse_args()
target_table = args.table
out = CartoDBOutput(args.apikey, args.domain, args.table)
if args.truncate:
if query_yes_no("Are you sure you want to truncate the database ? Data will be LOST !", default="no"):
out.truncate_table()
with DwCAReader(args.source_file) as dwca:
if valid_dwca(dwca):
for line in dwca.each_line():
try:
out.insert_line(**dwcaline_to_epsg4326(line))
sys.stdout.write('.')
except CartoDBException as e:
print ("CartoDB error: ", e)
else:
# TODO: more detailed message
print "Invalid source DwC-A file."
if __name__ == "__main__":
main()