-
-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5769 from geographika/python-mapscript-examples
Update Python MapScript examples
- Loading branch information
Showing
8 changed files
with
360 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
Oops, something went wrong.