File: project_csv.py

package info (click to toggle)
mapserver 6.0.1-3.2%2Bdeb7u2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 17,940 kB
  • sloc: ansic: 209,190; cpp: 53,949; cs: 12,101; python: 5,323; perl: 3,332; makefile: 841; lex: 706; yacc: 609; java: 466; xml: 379; sh: 253; tcl: 158; ruby: 53
file content (50 lines) | stat: -rwxr-xr-x 1,220 bytes parent folder | download | duplicates (2)
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: project_csv.py 7900 2008-09-09 02:03:23Z tomkralidis $
#

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()