-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_offset.py
More file actions
executable file
·141 lines (124 loc) · 4.28 KB
/
parallel_offset.py
File metadata and controls
executable file
·141 lines (124 loc) · 4.28 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/python
import shapefile, sys, os, fiona
from shapely.geometry import Point, LineString
import matplotlib.pyplot as plt
print "\n***************** Parallel offset *****************"
# This script generates a set of polylines parallel to all input
# polylines found in the input ESRI shapefile. The lines are generated
# at the distance offDist from the input line, inside or outside of the
# input polygons. The output is in ESRI polyline Z format where Z is
# (constant) depth. In addition depth is also saved in the record #1 "Depth".
# Required input:
#----------------
offDist = 1.0
Depth = -0.2
WorkDir = os.getcwd()+"/"
InputFile = "Opinicon/Data/opinicon_perimeter_ed_simpl_0.4-3.shp"
#----------------
# Offset the following shapes to the right, default is offset left.
ShapesRight = [0,12,13,25,31,32,33,36,38,39,40,41,42,45,46,47,50,51,52,53,54]
#----------------
OutFile = "Opinicon/Output/opinicon_offset_ed_simpl_0.4-3."
#----------------
# If parallel offset algorithm crashes try to adjust the coordinate shifts
dX = -100
dY = -100
#---------------------------------------------------------------
InputFile = WorkDir + InputFile
OutFile = WorkDir + OutFile
print "<<< Reading perimeter >>>\n...", os.path.basename(InputFile)
# Read shoreline points
sh = shapefile.Reader(InputFile)
# Shape records
sh_records = sh.shapeRecords()
# Shapes:
sh_shapes = sh.shapes()
# Make temporary coordinates (perimeter + islands) and
# an array of pointers [ind] to starting points of the records
xt=[];yt=[];zt=[];
nShapes=range(0,len(sh_shapes))
j=0; ii=0; ind=[0]
shiftX=sh_shapes[0].points[0][0]
shiftY=sh_shapes[0].points[0][1]
for i in nShapes:
n=len(sh_shapes[j].points)
ii=ii+n
ind.append(ii)
j+=1
for k in range(0,n):
xt.append(sh_shapes[i].points[k][0] - shiftX + dX)
yt.append(sh_shapes[i].points[k][1] - shiftY + dY)
zt.append(0.0)
# Perimeter parallel offset line
#----------------------------------------------------------------------------
# all shapes, left offset is 1, right is 0
print "<<< Generating parallel offset lines >>>"
Shapes=[]
for i in range(0,len(sh_records)):
Shapes.append(1)
for i in ShapesRight:
Shapes[i]=0
# Initialize output shapefile
ShapeType=shapefile.POLYLINEZ
w=shapefile.Writer(ShapeType)
w.autobalance=1
w.field("Depth", "F",10,5)
# Compute offset for the following records
#-----------------------------------------
dist=0
for j in range(len(sh_records)):
xf=[]; yf=[]; p=[]
# prepare shapely polyline from the list of coordinates
for i in range(ind[j],ind[j+1]):
p.append(Point(xt[i],yt[i]))
xf.append(xt[i]); yf.append(yt[i])
line=LineString(p)
dist += line.length
# generate parallel offset. 1 - interior offset; 0 - exterior offset
if Shapes[j] == 1:
try:
offsetLine = line.parallel_offset(distance=offDist, side='left', join_style=2, mitre_limit=5.0)
except ValueError:
sys.exit("Bug in parallel offset, try different dX/dY")
else:
try:
offsetLine = line.parallel_offset(distance=offDist, side='right', join_style=2, mitre_limit=5.0)
except ValueError:
sys.exit("Bug in parallel offset, try different dX/dY ")
try:
# try to process offset line as a list of polylines
print "S" + str(j),
for i in range(len(list(offsetLine))):
vertex=[]
x1off,y1off=offsetLine[i].xy
for k in range(0,len(x1off)):
vertex.append([x1off[k] + shiftX - dX, y1off[k] + shiftY - dY, Depth])
print len(x1off),"|",
w.record(Depth)
w.poly([vertex])
plt.plot(x1off,y1off,c='r')
except TypeError:
# if multiline reader fails process offsetLine as a single line
vertex=[]
x1off,y1off=offsetLine.xy
print len(x1off),"|",
for k in range(0, len(x1off)):
vertex.append([x1off[k] + shiftX - dX, y1off[k] + shiftY - dY, Depth])
w.record(Depth)
w.poly([vertex])
plt.plot(x1off,y1off,c='r')
plt.plot(xf,yf,c='lightgrey')
# print ""
sys.stdout.flush()
print "\n<<< Writing perimeter offset >>>\n...", os.path.basename(OutFile)+'shp'
print "... perimeter including islands =",dist,"m"
for s in w.shapes():
s.shapeType = ShapeType
w.save(OutFile)
# write projection
with fiona.open(InputFile) as fp:
prj=open(OutFile+"prj","w")
prj.write(fp.crs_wkt)
prj.close()
# plot results
# plt.show()