-
-
Notifications
You must be signed in to change notification settings - Fork 351
/
Copy pathv.clip.py
224 lines (181 loc) · 6.23 KB
/
v.clip.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python3
############################################################################
#
# MODULE: v.clip
# AUTHOR: Zofie Cimburova, CTU Prague, Czech Republic
# PURPOSE: Clips vector features
# COPYRIGHT: (C) 2016 Zofie Cimburova
# This program is free software under the GNU General
# Public License (>=v2). Read the file COPYING that
# comes with GRASS for details.
#
#############################################################################
# %module
# % description: Extracts features of input map which overlay features of clip map.
# % keyword: vector
# % keyword: clip
# % keyword: area
# %end
# %option G_OPT_V_INPUT
# % label: Name of vector map to be clipped
# % key: input
# %end
# %option G_OPT_V_INPUT
# % key: clip
# % label: Name of clip vector map
# %end
# %option G_OPT_V_OUTPUT
# % key: output
# %end
# %flag
# % key: d
# % description: Do not dissolve clip map
# %end
# %flag
# % key: r
# % description: Clip by region
# % suppress_required: yes
# % guisection: Region
# %end
# flags -d and -r are mutualy exclusive
# with flag -r, suppress_required: yes, but input and output must be defined
# %rules
# % exclusive: -d, -r
# % requires_all: -r, input, output
# %end
import os
import sys
import atexit
from grass.script import parser
import grass.script as gs
from grass.exceptions import CalledModuleError
TMP = []
def cleanup():
for name in TMP:
try:
gs.run_command("g.remove", flags="f", type="vector", name=name, quiet=True)
except CalledModuleError as e:
gs.fatal(
_(
"Deleting of temporary layer failed. "
"Check above error messages and "
"see following details:\n%s"
)
% e
)
def section_message(msg):
gs.message("{delim}\n{msg}\n{delim}".format(msg=msg, delim="-" * 80))
def main():
input_map = opt["input"]
clip_map = opt["clip"]
output_map = opt["output"]
flag_dissolve = flg["d"]
flag_region = flg["r"]
# ======================================== #
# ========== INPUT MAP TOPOLOGY ========== #
# ======================================== #
vinfo = gs.vector_info_topo(input_map)
# ==== only points ==== #
if vinfo["points"] > 0 and vinfo["lines"] == 0 and vinfo["areas"] == 0:
# ==================================== #
# ========== CLIP BY REGION ========== #
# ==================================== #
if flag_region:
clip_by_region(input_map, output_map, clip_select)
# ================================== #
# ========== DEFAULT CLIP ========== #
# ================================== #
else:
section_message("Clipping.")
# perform clipping
clip_select(input_map, clip_map, output_map)
# ==== lines, areas, lines + areas ==== #
# ==== points + areas, points + lines, points + areas + lines ==== #
else:
if vinfo["points"] > 0:
gs.warning(
"Input map contains multiple geometry, "
"only lines and areas will be clipped."
)
# ==================================== #
# ========== CLIP BY REGION ========== #
# ==================================== #
if flag_region:
clip_by_region(input_map, output_map, clip_overlay)
# ===================================================== #
# ========== CLIP WITHOUT DISSOLVED CLIP MAP ========== #
# ===================================================== #
elif flag_dissolve:
section_message("Clipping without dissolved clip map.")
clip_overlay(input_map, clip_map, output_map)
# ========================================================== #
# ========== DEFAULT CLIP WITH DISSOLVED CLIP MAP ========== #
# ========================================================== #
else:
section_message("Default clipping with dissolved clip map.")
# setup temporary map
temp_clip_map = "%s_%s" % ("temp", str(os.getpid()))
TMP.append(temp_clip_map)
# dissolve clip_map
gs.run_command("v.dissolve", input=clip_map, output=temp_clip_map)
# perform clipping
clip_overlay(input_map, temp_clip_map, output_map)
# ======================================== #
# ========== OUTPUT MAP TOPOLOGY========== #
# ======================================== #
vinfo = gs.vector_info_topo(output_map)
if vinfo["primitives"] == 0:
gs.warning("Output map is empty.")
return 0
# clip input map by computational region
# clip_select for points, clip_overlay for areas and lines
def clip_by_region(input_map, output_map, clip_fn):
section_message("Clipping by region.")
# setup temporary map
temp_region_map = "%s_%s" % ("temp", str(os.getpid()))
TMP.append(temp_region_map)
# create a map covering current computational region
gs.run_command("v.in.region", output=temp_region_map)
# perform clipping
clip_fn(input_map, temp_region_map, output_map)
def clip_overlay(input_data, clip_data, out_data):
try:
gs.run_command(
"v.overlay",
ainput=input_data,
binput=clip_data,
operator="and",
output=out_data,
olayer="0,1,0",
)
except CalledModuleError as e:
gs.fatal(
_(
"Clipping steps failed."
" Check above error messages and"
" see following details:\n%s"
)
% e
)
def clip_select(input_data, clip_data, out_data):
try:
gs.run_command(
"v.select",
ainput=input_data,
binput=clip_data,
output=out_data,
operator="overlap",
)
except CalledModuleError as e:
gs.fatal(
_(
"Clipping steps failed."
" Check above error messages and"
" see following details:\n%s"
)
% e
)
if __name__ == "__main__":
atexit.register(cleanup)
opt, flg = parser()
sys.exit(main())