[GRASS-dev] Parsing output of r.category which includes labels

Nikos Alexandris nik at nikosalexandris.net
Thu Aug 23 04:26:12 PDT 2018


* Moritz Lennert <mlennert at club.worldonline.be> [2018-08-23 13:02:03 +0200]:

>On 23/08/18 11:52, Nikos Alexandris wrote:
>>* Stefan Blumentrath <Stefan.Blumentrath at nina.no> [2018-08-23 07:23:12 +0000]:
>>
>>>Dear Nikos,
>>>
>>>Can you give us a bit more context?  What is it you want to achieve?
>>>How are you using r.stats and what is it you want to do with the
>>>output?
>>>
>>>Personally, I am not too familiar with performance implications of
>>>NumPy vs. plain Python, but rather use NumPy for convenience in
>>>matrix/table operations (avoiding pandas)...
>>
>>```
>>for category in categories:
>>     statistics_filename = prefix + '_' + category
>>     r.stats(input=(base,reclassified),
>>             output=statistics_filename,
>>             flags='ncapl',
>>             separator=',',
>>             quiet=True)
>>```
>>
>>Instead, I want to (modify the above so as to) collect/direct all
>>iterations in one output file.
>
>You can check the r.neighborhoodmatrix addon for one solution which I 
>shamelessly took from a SE answer:
>
>https://trac.osgeo.org/grass/browser/grass-addons/grass7/raster/r.neighborhoodmatrix/r.neighborhoodmatrix.py#L152
>
>The code takes a list of filenames and the merges these files.
>
>I've been confronted with a similar problem using v.db.select these 
>days and I've been thinking about adding a flag / parameter to 
>relevant modules allowing to append an existing file, instead of 
>overwriting it. Should just be a case of using mode "a" instead of 
>"w", so shouldn't be too complicated.
>
>If you want to, try it with r.stats, by applying this change:
>
>Index: raster/r.stats/main.c
>===================================================================
>--- raster/r.stats/main.c	(révision 72717)
>+++ raster/r.stats/main.c	(copie de travail)
>@@ -223,7 +223,7 @@
>
>     name = option.output->answer;
>     if (name != NULL && strcmp(name, "-") != 0) {
>-	if (NULL == freopen(name, "w", stdout)) {
>+	if (NULL == freopen(name, "a", stdout)) {
> 	    G_fatal_error(_("Unable to open file <%s> for writing"), name);
> 	}
>     }
>
>and report back if it works as expected...
>
>Moritz

I am sneaking in your scripts a lot lately. Just didn't see this, nor
did my greps/ags hit it.

The modification to r.stats works fine. So, it's a matter of an extra
flag then. Yet, I guess, this is to be explained with a proper warning (?).

Thank you Moritz, Nikos

ps- Some of my e-mails fail to deliver to your mailbox.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/grass-dev/attachments/20180823/b59f1287/attachment.sig>


More information about the grass-dev mailing list