-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert2geometryindex.py
165 lines (135 loc) · 5.63 KB
/
insert2geometryindex.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
/***************************************************************************
insert2geometryindex
A script for adding new procedures geometry to your geometry index file
-------------------
begin : 2017-09-04
author : Ondrej Pesek
email : [email protected]
copyright : (C) Norwegian Institute for Nature Research
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import gpxpy
import gpxpy.gpx
import argparse
from pathlib import Path
from os import sep
from istsosdat import standardize_norwegian
def main():
if args.__dict__['d'] is True and args.__dict__['gpx_path'][-1] != sep:
print("WARNING: Your path doesn't end with '{}'. It will parse all "
"files beginning with '{}' and ending with 'GPX' in the path"
"'{}{}'".format(sep,
args.__dict__['gpx_path'].split(sep)[-1],
args.__dict__['gpx_path'].rsplit(sep, 1)[0],
sep))
yetImported = list()
if Path(args.__dict__['index_file']).is_file():
yetImported = get_imported_procedures(args.__dict__['index_file'])
f = open(args.__dict__['index_file'], 'a')
else:
print('New geometry index file named {} will be created.'.format(
args.__dict__['index_file']))
f = open(args.__dict__['index_file'], 'w')
f.write('procid,[multiple_procnames],crs,x,y\n')
if args.__dict__['d'] is False:
gpx_file = open(args.__dict__['gpx_path'], 'r')
gpx = gpxpy.parse(gpx_file)
for waypoint in gpx.waypoints:
waypointNames = get_possible_procedure_names(waypoint)
if not any(i in yetImported for i in waypointNames):
names = ','.join(waypointNames)
f.write('{},33W,{},{}\n'.format(names,
waypoint.latitude,
waypoint.longitude))
else:
import glob
files = glob.glob("{}*.gpx".format(args.__dict__['gpx_path']))
for f in glob.glob("{}*.GPX".format(args.__dict__['gpx_path'])):
files.append(f)
for file in files:
gpx_file = open(file, 'r')
gpx = gpxpy.parse(gpx_file)
for waypoint in gpx.waypoints:
waypointNames = get_possible_procedure_names(waypoint)
if not any(i in yetImported for i in waypointNames):
names = ','.join(waypointNames)
f.write('{},WGS84,{},{}\n'.format(names,
waypoint.longitude,
waypoint.latitude))
def get_imported_procedures(csvFile):
"""
Get names of procedures that were imported anytime before
:param csvFile: csvFile containing procedures geometry
:return namesList: List of names of imported procedures
"""
namesList = list()
with open(csvFile, 'r') as openedCsv:
openedCsv.readline()
for line in openedCsv.readlines():
for i in line.split(',')[:-3]:
namesList.append(i)
return namesList
def get_possible_procedure_names(procedure):
"""
Get all often used variants of procedure name
:param procedure: Name of procedure
:return names: List containing mostly used variations of procedure name
"""
name = standardize_norwegian(procedure.name)
if 'TOV' in name:
name = ''.join(name.split('TOV'))
if 'Nyveg' in name:
name = ''.join(name.split('Nyveg'))
if 'NYV' in name:
name = ''.join(name.split('NYV'))
if 'Veg' in name:
name = ''.join(name.split('Veg'))
while name[0].isalpha() is False:
name = name[1:]
if '--' in name:
name = '-'.join(name.split('--'))
names = [name]
if '-' in name:
names.append(''.join(name.split('-')))
names.append('_'.join(name.split('-')))
else:
for i in reversed(range(len(name))):
try:
int(name[i])
except:
names.append('-'.join([name[:i + 1], name[i + 1:]]))
names.append('_'.join([name[:i + 1], name[i + 1:]]))
break
return names
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Update a geometry index file with new procedures')
parser.add_argument(
'-index_file',
type=str,
dest='index_file',
required=True,
help='Path to the index file')
parser.add_argument(
'-gpx_path',
type=str,
dest='gpx_path',
required=True,
help='Path to the GPX file containing geometry of procedures')
parser.add_argument(
'-d',
action='store_true',
help='Use if you would like to parse all GPX files in the directory')
args = parser.parse_args()
main()