[GRASS-SVN] r68962 - in grass-addons/grass7/vector: . v.clip
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Jul 14 12:15:58 PDT 2016
Author: zofcimb
Date: 2016-07-14 12:15:58 -0700 (Thu, 14 Jul 2016)
New Revision: 68962
Added:
grass-addons/grass7/vector/v.clip/
grass-addons/grass7/vector/v.clip/Makefile
grass-addons/grass7/vector/v.clip/v.clip.html
grass-addons/grass7/vector/v.clip/v.clip.py
grass-addons/grass7/vector/v.clip/v_clip_poly.png
grass-addons/grass7/vector/v.clip/v_clip_region.png
Log:
v.clip: added first implementation of clipping
Added: grass-addons/grass7/vector/v.clip/Makefile
===================================================================
--- grass-addons/grass7/vector/v.clip/Makefile (rev 0)
+++ grass-addons/grass7/vector/v.clip/Makefile 2016-07-14 19:15:58 UTC (rev 68962)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = v.clip
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script
Property changes on: grass-addons/grass7/vector/v.clip/Makefile
___________________________________________________________________
Added: svn:mime-type
+ text/x-makefile
Added: svn:eol-style
+ native
Added: grass-addons/grass7/vector/v.clip/v.clip.html
===================================================================
--- grass-addons/grass7/vector/v.clip/v.clip.html (rev 0)
+++ grass-addons/grass7/vector/v.clip/v.clip.html 2016-07-14 19:15:58 UTC (rev 68962)
@@ -0,0 +1,86 @@
+<h2>DESCRIPTION</h2>
+
+<em>v.clip</em> module enables extracting those features of input
+vector map, which overlay features of clip map, as well as their
+storing in a new vector map.
+
+<p>In default, boundaries of clip map are dissolved before
+clipping. Alternatively, flag <b>-d</b> can be ticked to retain the
+boundaries of clip map. Flag <b>-b</b> facilitates clipping by current
+computational region.
+
+<p>It is possible to clip vector maps consisting of points, lines,
+areas or combinations of these. However, the current version does not
+fully support clipping of mixed geometry containing points. In such a
+case, the output map will only store clipped lines and/or areas.
+
+<h2>NOTES</h2>
+
+<em>v.clip</em> is a front-end
+to <em><a href="v.overlay.html">v.overlay</a></em>, as well
+as <em><a href="v.select.html">v.select</a></em>. Clipping of areas
+and/or lines can be achieved
+using <em><a href="v.overlay.html">v.overlay</a></em>. Clipping of
+points can be performed
+with <em><a href="v.select.html">v.select<a></em>.
+
+<h2>EXAMPLES</h2>
+
+<h3>Basic use</h3>
+
+Clip railroads by counties Wake and Johnston in North Carolina (Nort
+Carolina data set).
+
+<div class="code"><pre>
+v.extract input=boundary_county where="NAME='WAKE' OR NAME='JOHNSTON'" output=county_WAKE_JOHNSTON
+v.clip input=railroads clip=county_WAKE_JOHNSTON output=railroads_WAKE_JOHNSTON
+</pre></div>
+
+<center>
+ <img src="v_clip_poly.png" alt="v.clip example" height="255" width="600" ><br>
+ <em>Figure: v.clip example - basic use</em>
+</center>
+
+
+<h3>Retain boundaries of clip map</h3>
+
+<div class="code"><pre>
+v.clip -d input=railroads clip=county_WAKE_JOHNSTON output=railroads_WAKE_JOHNSTON
+</pre></div>
+
+<h3>Clip by current computational region</h3>
+
+Clip hospitals by computational region adjusted to counties Wake and
+Johnston in North Carolina (North Carolina data set).
+
+It is not obligatory to enter name of clip map. In case it is stated,
+the clip map will be omitted.
+
+<div class="code"><pre>
+v.extract input=boundary_county where="NAME='WAKE' OR NAME='JOHNSTON'" output=county_WAKE_JOHNSTON
+v.clip -r input=hospitals output=hospitals_clip
+</pre></div>
+
+<center>
+ <img src="v_clip_region.png" alt="v.clip example" height="259" width="600"><br>
+ <em>Figure: v.clip example - clip by computational region</em>
+</center>
+
+<h2>SEE ALSO</h2>
+
+<em>
+ <a href="v.overlay.html">v.overlay</a>,
+ <a href="v.select.html">v.select</a>,
+ <a href="v.dissolve.html">v.dissolve</a>,
+ <a href="v.in.region.html">v.in.region</a>
+</em>
+
+<h2>AUTHOR</h2>
+
+Zofie
+Cimburova, <a href="http://geomatics.fsv.cvut.cz/research/osgeorel/">OSGeoREL</a>,
+Czech Technical University in Prague, Czech Republic
+
+<p>
+<i>Last changed: $Date: 2016-06-28 19:30:00 +0001 (Tue, 28 Jun 2016)$</i>
+
Property changes on: grass-addons/grass7/vector/v.clip/v.clip.html
___________________________________________________________________
Added: svn:mime-type
+ text/html
Added: svn:keywords
+ Author Date Id
Added: svn:eol-style
+ native
Added: grass-addons/grass7/vector/v.clip/v.clip.py
===================================================================
--- grass-addons/grass7/vector/v.clip/v.clip.py (rev 0)
+++ grass-addons/grass7/vector/v.clip/v.clip.py 2016-07-14 19:15:58 UTC (rev 68962)
@@ -0,0 +1,199 @@
+#!/usr/bin/env python
+
+############################################################################
+#
+# 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 run_command, message, parser
+import grass.script as grass
+from grass.exceptions import CalledModuleError
+
+TMP = []
+
+
+def cleanup():
+ for name in TMP:
+ try:
+ grass.run_command('g.remove', flags='f',
+ type='vector', name=name, quiet=True)
+
+ except CalledModuleError as e:
+ grass.fatal(_("Deleting of temporary layer failed. "
+ "Check above error messages and "
+ "see following details:\n%s") % e)
+
+
+def section_message(msg):
+ grass.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 = grass.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):
+ grass.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
+ grass.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 = grass.vector_info_topo(output_map)
+ if vinfo['primitives'] == 0:
+ grass.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
+ grass.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:
+ grass.run_command('v.overlay', ainput=input_data, binput=clip_data,
+ operator='and', output=out_data, olayer='0,1,0')
+ except CalledModuleError as e:
+ grass.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:
+ grass.run_command('v.select', ainput=input_data, binput=clip_data,
+ output=out_data, operator='overlap')
+
+ except CalledModuleError as e:
+ grass.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())
Property changes on: grass-addons/grass7/vector/v.clip/v.clip.py
___________________________________________________________________
Added: svn:mime-type
+ text/x-python
Added: svn:eol-style
+ native
Added: grass-addons/grass7/vector/v.clip/v_clip_poly.png
===================================================================
(Binary files differ)
Property changes on: grass-addons/grass7/vector/v.clip/v_clip_poly.png
___________________________________________________________________
Added: svn:mime-type
+ image/png
Added: grass-addons/grass7/vector/v.clip/v_clip_region.png
===================================================================
(Binary files differ)
Property changes on: grass-addons/grass7/vector/v.clip/v_clip_region.png
___________________________________________________________________
Added: svn:mime-type
+ image/png
More information about the grass-commit
mailing list