-
-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathproject_csv.py
More file actions
executable file
·76 lines (51 loc) · 1.58 KB
/
project_csv.py
File metadata and controls
executable file
·76 lines (51 loc) · 1.58 KB
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
#!/usr/bin/env python
"""
Simple example to read a csv file and reproject point x/y data
Usage:
project_csv.py cities.csv 2 1 EPSG:4326 EPSG:3857
"""
import csv
import sys
from io import open
import mapscript
def main(input_file, x_field_idx, y_field_idx, input_proj, output_proj):
# set input and output projections
proj_in = mapscript.projectionObj(input_proj)
proj_out = mapscript.projectionObj(output_proj)
# open file
with open(input_file, encoding="utf-8") as f:
# read csv
csv_in = csv.reader(f)
headers = next(csv_in)
# setup output
csv_out = csv.writer(sys.stdout)
csv_out.writerow(headers)
for row in csv_in:
# set pointObj
point = mapscript.pointObj(float(row[x_field_idx]), float(row[y_field_idx]))
# project
point.project(proj_in, proj_out)
# update with reprojected coordinates
row[x_field_idx] = point.x
row[y_field_idx] = point.y
csv_out.writerow(row)
def usage():
"""
Display usage if program is used incorrectly
"""
print(
"Syntax: %s <csvfile> <x_col> <y_col> <epsg_code_in> <epsg_code_out>"
% sys.argv[0]
)
sys.exit(2)
# check input parameters
if len(sys.argv) != 6:
usage()
input_file = sys.argv[1]
# set x and y indices
x_field_idx = int(sys.argv[2])
y_field_idx = int(sys.argv[3])
# get projection codes
input_proj = "init=" + sys.argv[4].lower()
output_proj = "init=" + sys.argv[5].lower()
main(input_file, x_field_idx, y_field_idx, input_proj, output_proj)