-
Notifications
You must be signed in to change notification settings - Fork 13
/
breaklines2wkt.py
executable file
·161 lines (133 loc) · 4.37 KB
/
breaklines2wkt.py
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python3
#
#+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!
# #
# breaklines2wkt.py #
# #
#+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!
#
# Author: Pat Prodanovic, Ph.D., P.Eng.
#
# Date: Sept 25, 2015
#
# Modified: Feb 20, 2016
# Made it work for python 2 and 3
#
# Purpose: Takes a pputils 3d breakline and exports it to wkt format. This
# script parallels my breaklines2dxf script. Sometimes for merging breaklines
# it is easier to copy and past lines in a WKT file, than it is to merge via
# GIS or CAD programs.
#
# To create the 3d breakline from xyz and lines.csv, run mkbreakline.py
#
# Uses: Python 2 or 3, Numpy
#
# Example:
#
# python breaklines2wkt.py -l lines3d.csv -o lines3dWKT.csv
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Global Imports
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import os,sys # system parameters
import numpy as np # numpy
from progressbar import ProgressBar, Bar, Percentage, ETA
curdir = os.getcwd()
#
# I/O
if len(sys.argv) == 5 :
dummy2 = sys.argv[1]
lines_file = sys.argv[2]
dummy3 = sys.argv[3]
output_file = sys.argv[4]
else:
print('Wrong number of Arguments, stopping now...')
print('Usage:')
print('python breaklines2wkt.py -l lines3d.csv -o lines3dWKT.csv')
sys.exit()
# to create the output file
#drawing = dxf.drawing(output_file)
temp_file = "tempWKT.csv"
fout = open(temp_file,"w")
# use numpy to read the file
# each column in the file is a row in data read by np.loadtxt method
lines_data = np.loadtxt(lines_file, delimiter=',',skiprows=0,unpack=True)
# find out if the lines file is id,x,y,z or id,x,y
with open(lines_file, 'r') as f:
line = next(f) # read 1 line
n_attr = len(line.split(','))
f.close()
if (n_attr == 3):
shapeid_lns = lines_data[0,:]
x_lns = lines_data[1,:]
y_lns = lines_data[2,:]
z_lns = np.zeros(len(shapeid_lns))
else:
shapeid_lns = lines_data[0,:]
x_lns = lines_data[1,:]
y_lns = lines_data[2,:]
z_lns = lines_data[3,:]
# round lines nodes to three decimals
x_lns = np.around(x_lns,decimals=3)
y_lns = np.around(y_lns,decimals=3)
z_lns = np.around(z_lns,decimals=3)
# finds out how many unique breaklines there are
n_unique_lns = np.unique(shapeid_lns)
# number of nodes in the lines file
n_lns = len(x_lns)
w = [Percentage(), Bar(), ETA()]
pbar = ProgressBar(widgets=w, maxval=n_lns).start()
# write the header of the WKT file
fout.write(str('WKT,cat' + '\n'))
# number of segments in a linestring
segment_count = 0
for i in range(0,n_lns):
pbar.update(i+1)
if (i == 0):
# write the first linestring
fout.write(str('"LINESTRING ('))
if (i>0):
cur_lns_shapeid = shapeid_lns[i]
prev_lns_shapeid = shapeid_lns[i-1]
if (cur_lns_shapeid - prev_lns_shapeid < 0.001):
# update the segment_count
segment_count = segment_count + 1
# create tupples for vertexes to add
v0 = (x_lns[i-1], y_lns[i-1], z_lns[i-1])
v1 = (x_lns[i], y_lns[i], z_lns[i])
if (segment_count == 1):
fout.write(str(x_lns[i-1]) + ' ' + str(y_lns[i-1]) + ' ' + str(z_lns[i-1]) )
fout.write(',')
fout.write(str(x_lns[i]) + ' ' + str(y_lns[i]) + ' ' + str(z_lns[i]) )
fout.write(',')
else:
fout.write(str(x_lns[i]) + ' ' + str(y_lns[i]) + ' ' + str(z_lns[i]) )
fout.write(',')
# this is needed, as the else below is never executed
# for the last line in the lines file!
if (i == n_lns-1):
fout.write(')",' + str(shapeid_lns[i]))
else:
# new linestring
fout.write(')",' + str(shapeid_lns[i]-1))
fout.write('\n')
fout.write(str('"LINESTRING ('))
# re-set the segment_count
segment_count = 0
############################################################################
# closes the temp file
fout.close()
# creates the output file
fout2 = open(output_file,"w")
# each element of this list will be a line of the file read
master = list()
# open the temp file, and replace ',)' with ')'
with open(temp_file, 'r') as f:
for line in f:
master.append(line)
for i in range(len(master)):
new = master[i].replace(',)', ')')
fout2.write(new)
# remove the temp file
os.remove(temp_file)
pbar.finish()