forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_csv.py
More file actions
executable file
·50 lines (41 loc) · 1.14 KB
/
project_csv.py
File metadata and controls
executable file
·50 lines (41 loc) · 1.14 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
#!/usr/bin/python
#
# Script : project_csv.py
#
# Purpose: Simple example to read a csv file and reproject point x/y data
#
# $Id$
#
import sys
import csv
import mapscript
# example invocation
# ./reproj.py ./foo.csv 2 3 EPSG:32619 EPSG:4326
# check input parameters
if (len(sys.argv) != 6):
print sys.argv[0] + \
" <csvfile> <x_col> <y_col> <epsg_code_in> <epsg_code_out>"
sys.exit(1)
else:
# set x and y indices
x = int(sys.argv[2])
y = int(sys.argv[3])
# set input and output projections
projObjIn = mapscript.projectionObj("init="+sys.argv[4].lower())
projObjOut = mapscript.projectionObj("init="+sys.argv[5].lower())
# open file
fCsv = open(sys.argv[1], 'r')
# read csv
csvIn = csv.reader(fCsv)
# setup output
csvOut = csv.writer(sys.stdout)
for aRow in csvIn: # each record
# set pointObj
point = mapscript.pointObj(float(aRow[x]), float(aRow[y]))
# project
point.project(projObjIn, projObjOut)
# update with reprojected coordinates
aRow[x] = point.x
aRow[y] = point.y
csvOut.writerow(aRow)
fCsv.close()