[GRASS-SVN] r62669 - grass-addons/grass7/vector/v.fixed.segmentpoints
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Nov 8 08:23:59 PST 2014
Author: hellik
Date: 2014-11-08 08:23:59 -0800 (Sat, 08 Nov 2014)
New Revision: 62669
Added:
grass-addons/grass7/vector/v.fixed.segmentpoints/Makefile
grass-addons/grass7/vector/v.fixed.segmentpoints/v.fixed.segmentpoints.html
grass-addons/grass7/vector/v.fixed.segmentpoints/v.fixed.segmentpoints.py
Log:
v.fixed.segmentpoints addon - step 2
Added: grass-addons/grass7/vector/v.fixed.segmentpoints/Makefile
===================================================================
--- grass-addons/grass7/vector/v.fixed.segmentpoints/Makefile (rev 0)
+++ grass-addons/grass7/vector/v.fixed.segmentpoints/Makefile 2014-11-08 16:23:59 UTC (rev 62669)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = v.fixed.segmentpoints
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script
Added: grass-addons/grass7/vector/v.fixed.segmentpoints/v.fixed.segmentpoints.html
===================================================================
--- grass-addons/grass7/vector/v.fixed.segmentpoints/v.fixed.segmentpoints.html (rev 0)
+++ grass-addons/grass7/vector/v.fixed.segmentpoints/v.fixed.segmentpoints.html 2014-11-08 16:23:59 UTC (rev 62669)
@@ -0,0 +1,39 @@
+<h2>DESCRIPTION</h2>
+
+<em>v.fixed.segmenpoints</em> creates segment points along a vector line
+with fixed distances by using the <em>v.segment</em> module. A category
+of 1 line has to be given. Start and end point of the line will be
+considered.
+
+<h2>EXAMPLE</h2>
+
+<div class="code">
+ <pre>
+ # nc sampe data set
+ v.fixed.segmentpoints vector=streams at PERMANENT cat=40102 dir=C:\tmp distance=25
+ </pre>
+</div>
+
+<h2>DEPENDENCIES</h2>
+
+<ul>
+<li>v.segment</li>
+</ul>
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="v.segment.html">v.segment</a>
+</em>
+
+<h2>TODO</h2>
+
+Join exported text file back to segment point vector.
+
+<h2>AUTHOR</h2>
+
+Helmut Kudrnovsky
+
+<p>
+<i>Last changed: $Date: 2014-05-13 23:17:32 +0200 (Di, 13 Mai 2014) $</i>
+</p>
\ No newline at end of file
Added: grass-addons/grass7/vector/v.fixed.segmentpoints/v.fixed.segmentpoints.py
===================================================================
--- grass-addons/grass7/vector/v.fixed.segmentpoints/v.fixed.segmentpoints.py (rev 0)
+++ grass-addons/grass7/vector/v.fixed.segmentpoints/v.fixed.segmentpoints.py 2014-11-08 16:23:59 UTC (rev 62669)
@@ -0,0 +1,159 @@
+#!/usr/bin/env python
+
+"""
+MODULE: v.fixed.segmentpoints
+
+AUTHOR(S): Helmut Kudrnovsky <alectoria AT gmx at>
+
+PURPOSE: Creates segment points along a vector line with fixed distances
+ by using the v.segment module
+
+COPYRIGHT: (C) 2014 by the GRASS Development Team
+
+ This program is free software under the GNU General Public
+ License (>=v2). Read the file COPYING that comes with GRASS
+ for details.
+"""
+
+#%module
+#% description: segment points along a vector line with fixed distances
+#% keywords: vector
+#%end
+
+#%option G_OPT_V_INPUT
+#% key: vector
+#% description: Name of habitat vector map
+#% required: yes
+#%end
+
+#%option G_OPT_V_CAT
+#% key: cat
+#% description: Category of a vector line
+#% required: yes
+#%end
+
+#%option G_OPT_M_DIR
+#% key: dir
+#% description: Directory where the output will be found
+#% required : yes
+#%end
+
+#%option
+#% key: distance
+#% type: double
+#% key_desc: double
+#% description: fixed distance between segment points
+#% required : no
+#% answer: 100
+#%end
+
+import sys
+import os
+import math
+import grass.script as grass
+
+if not os.environ.has_key("GISBASE"):
+ grass.message( "You must be in GRASS GIS to run this program." )
+ sys.exit(1)
+
+def main():
+ vlines = options['vector'].split('@')[0]
+ vcat = options['cat']
+ directory = options['dir']
+ sdistance = options['distance']
+ fpoints = 'segmentpoints.txt'
+ global tmp
+
+ # Extract vector line
+ grass.message( "Extract vector line for which segment points should be calculated ..." )
+ grass.run_command('v.extract', input = vlines,
+ output = 'vlinesingle',
+ cats = vcat)
+ grass.message( "Extraction done." )
+ grass.message( "----" )
+
+ # Calculate vector line length and populate it to the attribute table
+ grass.message( "Calculate vector line length and populate it to the attribute table ..." )
+ grass.run_command("v.db.addcolumn", map = 'vlinesingle',
+ layer = 1,
+ columns = "vlength double")
+
+ grass.run_command("v.to.db", map = 'vlinesingle',
+ option = 'length',
+ layer = 1,
+ columns = 'vlength',
+ overwrite = True)
+
+ grass.message( "Calculate vector line length done." )
+ grass.message( "----" )
+
+ # Read length
+ tmp = grass.read_command('v.to.db', map = 'vlinesingle',
+ type = 'line',
+ layer = 1,
+ qlayer = 1,
+ option = 'length',
+ units = 'meters',
+ column = 'vlength',
+ flags = 'p')
+ vector_line_length = float(tmp.split('\n')[1].split('|')[1])
+
+ # Print vector line length
+ grass.message( "Vector line length in meter:" )
+ grass.message( vector_line_length )
+ grass.message( "----" )
+
+ # Calculation number of segment points (start and end point included)
+ # number of segment points without end point
+
+ number_segmentpoints_without_end = math.floor( vector_line_length / float(sdistance) )
+
+ number_segmentpoints_with_end = int(number_segmentpoints_without_end + 1)
+
+ grass.message( "Number of segment points (start and end point included):" )
+ grass.message( number_segmentpoints_with_end )
+ grass.message( "----" )
+
+ segmentpointsrange_without_end = range(1, number_segmentpoints_with_end, 1)
+ max_distancerange = float(sdistance) * number_segmentpoints_with_end
+ distancesrange_without_end = range(0, int(max_distancerange), int(sdistance))
+
+ # Write segment point input file for g.segment to G_OPT_M_DIR
+ grass.message( "Write segment point input file for g.segment" )
+ segment_points_file = os.path.join( directory, fpoints )
+ file = open(segment_points_file, 'a')
+ for f, b in zip(segmentpointsrange_without_end, distancesrange_without_end):
+ file.write("P %s %s %s\n" % (f, vcat, b))
+ file.close()
+
+ file = open(segment_points_file, 'a')
+ file.write("P %s %s -0" % (number_segmentpoints_with_end, vcat))
+ file.close()
+
+ # Give information where output file
+ grass.message( "Segment points file:" )
+ grass.message( segment_points_file )
+ grass.message( "----" )
+
+ # Run v.segment with the segment point input
+
+ grass.run_command("v.segment", input = 'vlinesingle',
+ output = 'segmentpoints',
+ file = segment_points_file)
+
+ grass.run_command("v.db.addtable", map = 'segmentpoints')
+
+
+# TODO join point segment file to point vector
+# grass.run_command("db.in.ogr", dsn = segment_points_file,
+# output = 't_segment_points_file')
+
+ grass.message( "Segment points file created" )
+ grass.message( "----" )
+
+ # v.fixed.segmentpoints done!
+ grass.message( "v.fixed.segmentpoints done!" )
+
+if __name__ == "__main__":
+ options, flags = grass.parser()
+ sys.exit(main())
More information about the grass-commit
mailing list