Opened 7 years ago

Closed 7 years ago

#3357 closed enhancement (fixed)

v.rast.stats: add support for -d flag (densified lines)

Reported by: veroandreo Owned by: grass-dev@…
Priority: normal Milestone: 7.2.2
Component: Vector Version: svn-trunk
Keywords: v.rast.stats, v.to.rast Cc:
CPU: Unspecified Platform: Unspecified

Description

Currently, G7:v.rast.stats uses the default thin lines from G7:v.to.rast to get statistics from a vector of lines. This is however not always the best choice. In my case, for mice traplines, I need all the pixels overlapped by the line. Given that v.to.rast has this nice -d flag for densified lines, would that be possible to also have that option in v.rast.stats since it internally calls v.to.rast to get raster statistics for lines or polygons? That would reduce the current workflow from: v.to.rast -d ; r.to vect type=area ; v.rast.stats to only v.rast.stats (with -d flag in case densified lines are preferred :)).

Attachments (1)

v_rast_stats_flag.diff (1.4 KB ) - added by neteler 7 years ago.
Flag -d added

Download all attachments as: .zip

Change History (11)

comment:1 by neteler, 7 years ago

I have locally implemented the flag (patch attached) but it requires more work:

v.rast.stats myzipcodes_wake raster=elevation   column_prefix=elev method=minimum,maximum,average,range,stddev,percentile   percentile=95 -d
WARNING: Area conversion and line densification are mutually exclusive,
         disabling line densification.

comment:2 by veroandreo, 7 years ago

I applied the patch and tested both with polygons and lines vector maps. Using your example, I get the same warning (but I would say that's expected and correct behavior for polygons, i.e.: if line densification is used with polygons, adjacent polygons would have overlapping areas in the intermediate raster map).

For a lines vector map, I get the following warning

g.copy vector=railroads,myrailroads
v.rast.stats myrailroads raster=elevation column_prefix=elev method=average -d
WARNING: No areas selected from vector map <myrailroads>

but, it seems to work properly (showing only an extract).

v.db.select myrailroads
cat|OBJECTID|FNODE_|TNODE_|LPOLY_|RPOLY_|LENGTH|RR24_100_|RR24_100_I|MAJOR1|MINOR1|SCALE|elev_average
3063|3063|4109|4084|2|2|642.898743|3063|224|180|208|24000|70.5424867674362
3064|3064|4107|4039|2|2|1208.258545|3064|9|180|208|24000|
3065|3065|3999|4113|2|2|1892.670776|3065|739|180|201|24000|
3066|3066|4114|4103|2|2|147.106094|3066|201|180|201|24000|
3067|3067|4114|4104|2|2|147.106094|3067|238|180|201|24000|
3068|3068|4114|4094|2|2|316.95166|3068|216|180|208|24000|79.0518072219122
3069|3069|4115|4102|2|2|147.106094|3069|240|180|208|24000|
3070|3070|4116|4104|2|2|147.106094|3070|241|180|208|24000|
3071|3071|4119|4106|2|2|158.241898|3071|181|180|201|24000|
3072|3072|4101|4120|2|2|192.714844|3072|2|180|208|24000|
3073|3073|4101|4120|2|2|192.714844|3073|5|180|201|24000|
3074|3074|4105|4122|2|2|249.740448|3074|221|180|208|24000|
3075|3075|4122|4118|2|2|221.97583|3075|223|180|208|24000|75.6025638580322
3076|3076|4105|4123|2|2|249.740448|3076|220|180|208|24000|77.1286170596168
3077|3077|4125|4119|2|2|92.723|3077|189|180|201|24000|

in reply to:  2 ; comment:3 by mlennert, 7 years ago

Replying to veroandreo:

I applied the patch and tested both with polygons and lines vector maps. Using your example, I get the same warning (but I would say that's expected and correct behavior for polygons, i.e.: if line densification is used with polygons, adjacent polygons would have overlapping areas in the intermediate raster map).

For a lines vector map, I get the following warning

g.copy vector=railroads,myrailroads
v.rast.stats myrailroads raster=elevation column_prefix=elev method=average -d
WARNING: No areas selected from vector map <myrailroads>

AFAICT this comes from the v.to.rast call. It is harmless, but probably a bit confusing. You should be able to solve this by adding a type parameter to v.rast.stats and send that to v.to.rast.

by neteler, 7 years ago

Attachment: v_rast_stats_flag.diff added

Flag -d added

in reply to:  3 comment:4 by neteler, 7 years ago

Replying to mlennert:

AFAICT this comes from the v.to.rast call. It is harmless, but probably a bit confusing. You should be able to solve this by adding a type parameter to v.rast.stats and send that to v.to.rast.

Instead of adding a new type parameter the patch now contains an extra test to check when to apply -d internally. Please test.

comment:5 by veroandreo, 7 years ago

Removing previous patch and applying the second one, I get the following:

v.rast.stats myrailroads raster=elevation column_prefix=elev method=average -d
Preprocessing input data...
Traceback (most recent call last):
  File "/home/veroandreo/software/grass7_trunk/dist.x86_64-pc-linux-gnu/scripts/v.rast.stats", line 312, in <module>
    main()
  File "/home/veroandreo/software/grass7_trunk/dist.x86_64-pc-linux-gnu/scripts/v.rast.stats", line 139, in main
    from pygrass.vector import VectorTopo
ImportError: No module named pygrass.vector
WARNING: No data base element files found

Here the output of svn diff

[veroandreo@utwks62764 v.rast.stats]$ svn diff
Index: v.rast.stats.py
===================================================================
--- v.rast.stats.py	(revision 71311)
+++ v.rast.stats.py	(working copy)
@@ -31,6 +31,11 @@
 #% key: c
 #% description: Continue if upload column(s) already exist
 #%end
+#%flag
+#% key: d
+#% label: Create densified lines (default: thin lines)
+#% description: All cells touched by the line will be set, not only those on the render path
+#%end
 #%option G_OPT_V_MAP
 #%end
 #%option G_OPT_V_FIELD
@@ -131,8 +136,17 @@
 
     grass.message(_("Preprocessing input data..."))
     try:
-        grass.run_command('v.to.rast', input=vector, layer=layer, output=rastertmp,
-                          use='cat', quiet=True)
+        from pygrass.vector import VectorTopo
+        inmap = VectorTopo(vector)
+        inmap.open('r')
+        nlines = inmap.num_primitive_of('lines')
+        # Create densified lines rather than thin lines
+        if flags['d'] and nlines > 0:
+            grass.run_command('v.to.rast', input=vector, layer=layer, output=rastertmp,
+                              use='cat', flags='d', quiet=True)
+        else:
+            grass.run_command('v.to.rast', input=vector, layer=layer, output=rastertmp,
+                              use='cat', quiet=True)
     except CalledModuleError:
         grass.fatal(_("An error occurred while converting vector to raster"))

comment:6 by lucadelu, 7 years ago

Not able to replace the previous diff... here your are the working one

Index: scripts/v.rast.stats/v.rast.stats.py
===================================================================
--- scripts/v.rast.stats/v.rast.stats.py	(revision 71311)
+++ scripts/v.rast.stats/v.rast.stats.py	(working copy)
@@ -31,6 +31,11 @@
 #% key: c
 #% description: Continue if upload column(s) already exist
 #%end
+#%flag
+#% key: d
+#% label: Create densified lines (default: thin lines)
+#% description: All cells touched by the line will be set, not only those on the render path
+#%end
 #%option G_OPT_V_MAP
 #%end
 #%option G_OPT_V_FIELD
@@ -131,8 +136,17 @@
 
     grass.message(_("Preprocessing input data..."))
     try:
-        grass.run_command('v.to.rast', input=vector, layer=layer, output=rastertmp,
-                          use='cat', quiet=True)
+        from grass.pygrass.vector import VectorTopo
+        inmap = VectorTopo(vector)
+        inmap.open('r')
+        nlines = inmap.num_primitive_of('line')
+        # Create densified lines rather than thin lines
+        if flags['d'] and nlines > 0:
+            grass.run_command('v.to.rast', input=vector, layer=layer, output=rastertmp,
+                              use='cat', flags='d', quiet=True)
+        else:
+            grass.run_command('v.to.rast', input=vector, layer=layer, output=rastertmp,
+                              use='cat', quiet=True)
     except CalledModuleError:
         grass.fatal(_("An error occurred while converting vector to raster"))

Now working on testsuite and later, if it works, I will commit the changes

in reply to:  6 comment:7 by mmetz, 7 years ago

Replying to lucadelu:

Not able to replace the previous diff... here your are the working one

Index: scripts/v.rast.stats/v.rast.stats.py
===================================================================
--- scripts/v.rast.stats/v.rast.stats.py	(revision 71311)
+++ scripts/v.rast.stats/v.rast.stats.py	(working copy)
@@ -31,6 +31,11 @@
 #% key: c
 #% description: Continue if upload column(s) already exist
 #%end
+#%flag
+#% key: d
+#% label: Create densified lines (default: thin lines)
+#% description: All cells touched by the line will be set, not only those on the render path
+#%end
 #%option G_OPT_V_MAP
 #%end
 #%option G_OPT_V_FIELD
@@ -131,8 +136,17 @@
 
     grass.message(_("Preprocessing input data..."))
     try:
-        grass.run_command('v.to.rast', input=vector, layer=layer, output=rastertmp,
-                          use='cat', quiet=True)
+        from grass.pygrass.vector import VectorTopo
+        inmap = VectorTopo(vector)
+        inmap.open('r')
+        nlines = inmap.num_primitive_of('line')

don't forget to close inmap.

simpler alternative

        nlines = grass.vector_info_topo(vector)['lines']

+        # Create densified lines rather than thin lines
+        if flags['d'] and nlines > 0:
+            grass.run_command('v.to.rast', input=vector, layer=layer, output=rastertmp,
+                              use='cat', flags='d', quiet=True)
+        else:
+            grass.run_command('v.to.rast', input=vector, layer=layer, output=rastertmp,
+                              use='cat', quiet=True)
     except CalledModuleError:
         grass.fatal(_("An error occurred while converting vector to raster"))

Now working on testsuite and later, if it works, I will commit the changes

comment:8 by lucadelu, 7 years ago

Thanks Markus, what do you think about the warning?

WARNING: No areas selected from vector map <myrailroads>

in reply to:  8 comment:9 by mmetz, 7 years ago

Replying to lucadelu:

Thanks Markus, what do you think about the warning?

WARNING: No areas selected from vector map <myrailroads>

This warning comes from v.to.rast and does not make sense because there are no areas in the vector myrailroads. To be fixed in v.to.rast.

comment:10 by lucadelu, 7 years ago

Resolution: fixed
Status: newclosed

Changes committed in r71314. Open a ticket for the warning #3377.

Closing because it is a new feature and should not be backported.

Note: See TracTickets for help on using tickets.