Skip to content
Permalink
Browse files

Add test CSV file and refactor project_csv.py example

  • Loading branch information...
geographika committed Mar 27, 2019
1 parent fed48ca commit 16dbde808504f80238b64874a4379f63ae0040c0
Showing with 77 additions and 50 deletions.
  1. +70 −50 mapscript/python/examples/project_csv.py
  2. +7 −0 tests/cities.csv
@@ -1,50 +1,70 @@
#!/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()
"""
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 sys
import csv
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)
@@ -0,0 +1,7 @@
city,lat,lng,country,iso2,admin,capital,population,population_proper
Dublin,53.333056,-6.248889,Ireland,IE,Dublin,primary,1059000,968976
Cork,51.898611,-8.495833,Ireland,IE,Cork,admin,188907,136797
Limerick,52.664722,-8.623056,Ireland,IE,Limerick,admin,90054,78078
Galway,53.271944,-9.048889,Ireland,IE,Galway,admin,75594,70686
Waterford,52.258333,-7.111944,Ireland,IE,Waterford,admin,49275,49275
Drogheda,53.718889,-6.347778,Ireland,IE,Louth,,36533,33441

0 comments on commit 16dbde8

Please sign in to comment.
You can’t perform that action at this time.