-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdwca2shp.py
53 lines (39 loc) · 1.82 KB
/
dwca2shp.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
#!/usr/bin/env python
import argparse
import sys
from dwca import DwCAReader
from output.shapefile_out import ShapefileOutput
from utils import (dwcaline_to_epsg4326, valid_dwca, CannotConvertException,
unicode_to_ascii)
DEFAULT_IMPORTED_FIELDS = ['occurrenceID', 'scientificName', 'eventDate']
def parse_args():
parser = argparse.ArgumentParser(
description="Convert a DarwinCore Archive file to an ESRI Shapefile.")
parser.add_argument('source_file',
help="Source DwC-A file",
type=argparse.FileType('r'))
parser.add_argument('destination',
help="Name of shapefile (directory) to be created")
parser.add_argument('--crs',
help="Output projection, default to epsg:4326",
default='epsg:4326')
return parser.parse_args()
def main(args):
with DwCAReader(args.source_file) as dwca:
if valid_dwca(dwca):
ordered_fields = list(dwca.core_terms)
# Only last part as Shapefiles field names are limited to 10 chars
ordered_fields_truncated = [f.rsplit('/')[-1] for f in ordered_fields]
import pdb; pdb.set_trace()
with ShapefileOutput(args.destination, args.crs, ordered_fields_truncated) as out:
for line in dwca.each_line():
try:
gis_data = dwcaline_to_epsg4326(line)
additional_values = [unicode_to_ascii(line.data[f]) for f in ordered_fields]
out.insert_line(gis_data['lat'], gis_data['lon'], additional_values)
sys.stdout.write('.')
except CannotConvertException:
pass
if __name__ == "__main__":
args = parse_args()
main(args)