Skip to content

Commit 16eb9de

Browse files
Fix eol of mapscript python examples to LF instead of CRLF
1 parent 1036f98 commit 16eb9de

File tree

4 files changed

+336
-336
lines changed

4 files changed

+336
-336
lines changed
Lines changed: 72 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,72 @@
1-
#!/usr/bin/env python
2-
3-
"""
4-
Simple example to read a csv file and reproject point x/y data
5-
6-
Usage:
7-
8-
project_csv.py cities.csv 2 1 EPSG:4326 EPSG:3857
9-
10-
"""
11-
12-
import sys
13-
import csv
14-
from io import open
15-
import mapscript
16-
17-
18-
def main(input_file, x_field_idx, y_field_idx, input_proj, output_proj):
19-
20-
# set input and output projections
21-
proj_in = mapscript.projectionObj(input_proj)
22-
proj_out = mapscript.projectionObj(output_proj)
23-
24-
# open file
25-
with open(input_file, encoding='utf-8') as f:
26-
# read csv
27-
csv_in = csv.reader(f)
28-
headers = next(csv_in)
29-
30-
# setup output
31-
csv_out = csv.writer(sys.stdout)
32-
csv_out.writerow(headers)
33-
34-
for row in csv_in:
35-
# set pointObj
36-
point = mapscript.pointObj(float(row[x_field_idx]), float(row[y_field_idx]))
37-
# project
38-
point.project(proj_in, proj_out)
39-
40-
# update with reprojected coordinates
41-
row[x_field_idx] = point.x
42-
row[y_field_idx] = point.y
43-
44-
csv_out.writerow(row)
45-
46-
47-
def usage():
48-
"""
49-
Display usage if program is used incorrectly
50-
"""
51-
print("Syntax: %s <csvfile> <x_col> <y_col> <epsg_code_in> <epsg_code_out>" % sys.argv[0])
52-
sys.exit(2)
53-
54-
55-
# check input parameters
56-
57-
if (len(sys.argv) != 6):
58-
usage()
59-
60-
61-
input_file = sys.argv[1]
62-
63-
# set x and y indices
64-
65-
x_field_idx = int(sys.argv[2])
66-
y_field_idx = int(sys.argv[3])
67-
68-
# get projection codes
69-
70-
input_proj = "init="+sys.argv[4].lower()
71-
output_proj = "init="+sys.argv[5].lower()
72-
main(input_file, x_field_idx, y_field_idx, input_proj, output_proj)
1+
#!/usr/bin/env python
2+
3+
"""
4+
Simple example to read a csv file and reproject point x/y data
5+
6+
Usage:
7+
8+
project_csv.py cities.csv 2 1 EPSG:4326 EPSG:3857
9+
10+
"""
11+
12+
import sys
13+
import csv
14+
from io import open
15+
import mapscript
16+
17+
18+
def main(input_file, x_field_idx, y_field_idx, input_proj, output_proj):
19+
20+
# set input and output projections
21+
proj_in = mapscript.projectionObj(input_proj)
22+
proj_out = mapscript.projectionObj(output_proj)
23+
24+
# open file
25+
with open(input_file, encoding='utf-8') as f:
26+
# read csv
27+
csv_in = csv.reader(f)
28+
headers = next(csv_in)
29+
30+
# setup output
31+
csv_out = csv.writer(sys.stdout)
32+
csv_out.writerow(headers)
33+
34+
for row in csv_in:
35+
# set pointObj
36+
point = mapscript.pointObj(float(row[x_field_idx]), float(row[y_field_idx]))
37+
# project
38+
point.project(proj_in, proj_out)
39+
40+
# update with reprojected coordinates
41+
row[x_field_idx] = point.x
42+
row[y_field_idx] = point.y
43+
44+
csv_out.writerow(row)
45+
46+
47+
def usage():
48+
"""
49+
Display usage if program is used incorrectly
50+
"""
51+
print("Syntax: %s <csvfile> <x_col> <y_col> <epsg_code_in> <epsg_code_out>" % sys.argv[0])
52+
sys.exit(2)
53+
54+
55+
# check input parameters
56+
57+
if (len(sys.argv) != 6):
58+
usage()
59+
60+
61+
input_file = sys.argv[1]
62+
63+
# set x and y indices
64+
65+
x_field_idx = int(sys.argv[2])
66+
y_field_idx = int(sys.argv[3])
67+
68+
# get projection codes
69+
70+
input_proj = "init="+sys.argv[4].lower()
71+
output_proj = "init="+sys.argv[5].lower()
72+
main(input_file, x_field_idx, y_field_idx, input_proj, output_proj)
Lines changed: 94 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,94 @@
1-
#!/usr/bin/env python
2-
3-
"""
4-
Dump the contents of the passed in Shapefile
5-
6-
Usage:
7-
8-
python shpdump.py polygon.shp
9-
10-
"""
11-
12-
import mapscript
13-
import sys
14-
import os
15-
16-
17-
def plural(x):
18-
"""
19-
Returns an 's' if plural
20-
21-
Useful in print statements to avoid something like 'point(s)'
22-
"""
23-
if x > 1:
24-
return 's'
25-
return ''
26-
27-
28-
def get_shapefile_object(sf_path):
29-
30-
# make sure can access .shp file, create shapefileObj
31-
32-
if os.access(sf_path, os.F_OK):
33-
sf_obj = mapscript.shapefileObj(sf_path, -1)
34-
else:
35-
print("Can't access {}".format(sf_path))
36-
sys.exit(2)
37-
38-
return sf_obj
39-
40-
41-
def main(sf_path):
42-
43-
if not sf_path.lower().endswith(".shp"):
44-
sf_path += ".shp"
45-
46-
sf_obj = get_shapefile_object(sf_path)
47-
48-
# create an empty Shapefile object
49-
50-
s_obj = mapscript.shapeObj()
51-
52-
# loop through each shape in the original Shapefile
53-
54-
for i in range(sf_obj.numshapes):
55-
56-
# get the object at index i
57-
sf_obj.get(i, s_obj)
58-
print("Shape %i has %i part%s" % (i, s_obj.numlines, plural(s_obj.numlines)))
59-
print("Bounds (%f, %f) (%f, %f)" % (s_obj.bounds.minx, s_obj.bounds.miny, s_obj.bounds.maxx, s_obj.bounds.maxy))
60-
61-
# loop through parts of each shape
62-
63-
for j in range(s_obj.numlines):
64-
65-
# get the jth part of the ith object
66-
67-
part = s_obj.get(j)
68-
print("Part %i has %i point%s" % (j, part.numpoints, plural(part.numpoints)))
69-
70-
# loop through points in each part
71-
72-
for k in range(part.numpoints):
73-
74-
# get the kth point of the jth part of the ith shape
75-
76-
point = part.get(k)
77-
print("%i: %f, %f" % (k, point.x, point.y))
78-
79-
80-
def usage():
81-
"""
82-
Display usage if program is used incorrectly
83-
"""
84-
print("Syntax: %s <shapefile_path>" % sys.argv[0])
85-
sys.exit(2)
86-
87-
88-
# make sure passing in filename argument
89-
if len(sys.argv) != 2:
90-
usage()
91-
92-
93-
sf_path = sys.argv[1]
94-
main(sf_path)
1+
#!/usr/bin/env python
2+
3+
"""
4+
Dump the contents of the passed in Shapefile
5+
6+
Usage:
7+
8+
python shpdump.py polygon.shp
9+
10+
"""
11+
12+
import mapscript
13+
import sys
14+
import os
15+
16+
17+
def plural(x):
18+
"""
19+
Returns an 's' if plural
20+
21+
Useful in print statements to avoid something like 'point(s)'
22+
"""
23+
if x > 1:
24+
return 's'
25+
return ''
26+
27+
28+
def get_shapefile_object(sf_path):
29+
30+
# make sure can access .shp file, create shapefileObj
31+
32+
if os.access(sf_path, os.F_OK):
33+
sf_obj = mapscript.shapefileObj(sf_path, -1)
34+
else:
35+
print("Can't access {}".format(sf_path))
36+
sys.exit(2)
37+
38+
return sf_obj
39+
40+
41+
def main(sf_path):
42+
43+
if not sf_path.lower().endswith(".shp"):
44+
sf_path += ".shp"
45+
46+
sf_obj = get_shapefile_object(sf_path)
47+
48+
# create an empty Shapefile object
49+
50+
s_obj = mapscript.shapeObj()
51+
52+
# loop through each shape in the original Shapefile
53+
54+
for i in range(sf_obj.numshapes):
55+
56+
# get the object at index i
57+
sf_obj.get(i, s_obj)
58+
print("Shape %i has %i part%s" % (i, s_obj.numlines, plural(s_obj.numlines)))
59+
print("Bounds (%f, %f) (%f, %f)" % (s_obj.bounds.minx, s_obj.bounds.miny, s_obj.bounds.maxx, s_obj.bounds.maxy))
60+
61+
# loop through parts of each shape
62+
63+
for j in range(s_obj.numlines):
64+
65+
# get the jth part of the ith object
66+
67+
part = s_obj.get(j)
68+
print("Part %i has %i point%s" % (j, part.numpoints, plural(part.numpoints)))
69+
70+
# loop through points in each part
71+
72+
for k in range(part.numpoints):
73+
74+
# get the kth point of the jth part of the ith shape
75+
76+
point = part.get(k)
77+
print("%i: %f, %f" % (k, point.x, point.y))
78+
79+
80+
def usage():
81+
"""
82+
Display usage if program is used incorrectly
83+
"""
84+
print("Syntax: %s <shapefile_path>" % sys.argv[0])
85+
sys.exit(2)
86+
87+
88+
# make sure passing in filename argument
89+
if len(sys.argv) != 2:
90+
usage()
91+
92+
93+
sf_path = sys.argv[1]
94+
main(sf_path)

0 commit comments

Comments
 (0)