man: Improve segment library documentation, provide examples - #20
man: Improve segment library documentation, provide examples#20wenzeslaus wants to merge 17 commits into
Conversation
|
|
||
| /* determine the input map type (CELL/FCELL/DCELL) */ | ||
| map_type = Rast_map_type(input_name, ""); | ||
| size_t cell_size = Rast_cell_size(map_type); |
There was a problem hiding this comment.
move "size_t cell_size;" up, just after "RASTER_MAP_TYPE map_type;"
| ncols = Rast_window_cols(); | ||
|
|
||
| /* size of a segment */ | ||
| int srows = 64; |
There was a problem hiding this comment.
Move "int srows, scols, num_seg;" up
| int num_seg = 4; | ||
|
|
||
| /* segment structure */ | ||
| SEGMENT raster_seg; |
There was a problem hiding this comment.
move "SEGMENT raster_seg;" up
|
|
||
| /* pass the pointer, set the value */ | ||
| Segment_put(raster_seg, values, row, col); | ||
| } |
| ncols = Rast_window_cols(); | ||
|
|
||
| /* size of a segment */ | ||
| int srows = 64; |
There was a problem hiding this comment.
as for r.example.segment, move int srows, sclos, nsegs; up
| int nsegs = 4; | ||
|
|
||
| map_type = DCELL_TYPE; | ||
| size_t cell_size = Rast_cell_size(map_type); |
There was a problem hiding this comment.
Move "size_t cell_size, segment_cell_size;" up
| output_fd = Rast_open_new(output_name, map_type); | ||
|
|
||
| /* segment structure */ | ||
| SEGMENT raster_seg; |
| G_fatal_error(_("Unable to create temporary segment file")); | ||
|
|
||
| /* array to store file descriptors */ | ||
| int *input_fds = G_malloc(ninputs * sizeof(int)); |
| } | ||
|
|
||
| /* allocate input buffer */ | ||
| DCELL *row_buffer = Rast_allocate_d_buf(); |
There was a problem hiding this comment.
move "DCELL *row_buffer, *seg_buffer;" up
|
|
||
| /* pass the pointer, set the value */ | ||
| Segment_put(raster_seg, values, row, col); | ||
| } |
|
|
||
| <em> | ||
| <a href="r.example.html">r.example</a> | ||
| <a href="r.example.html">r.example.segment</a> |
| * <b>Buf</b> will be filled with <em>ncols*len</em> bytes of data | ||
| * corresponding to the <b>row</b> in the data matrix. | ||
| * \pre *buf* points to an allocated array of length <em>ncols*len</em> | ||
| * where *len* is size of one stored value (cell). |
There was a problem hiding this comment.
e.g. cell or a custom data structure
| * Segment_init(), all-in-memory mode initialization, and temporary | ||
| * file handling. | ||
| * | ||
| * The values are not guaranteed to be initialized to zero or NULL. |
There was a problem hiding this comment.
not values, but bytes, they are indeed initialized to zero because of lseek and calloc
| the raster you need to access: | ||
|
|
||
| \code | ||
| for (int row = 0; row < Rast_window_rows(); row++) { |
There was a problem hiding this comment.
for (int row = 0; ...) is C++ style
|
|
||
| \skip G_free | ||
| \until Rast_close(output_fd); | ||
|
|
Please use standard C style in sample code.
See "man calloc". malloc does not initialize the memory while calloc does.
No.
Yes, that's the reason: Segment_get_row() bypasses the internal cache and reads directly from the temporary file. |
|
Considering examples on how to use the segment library, it is IMHO sufficient to mention current working modules like r.cost, r.walk, r.watershed, r.stream.extract. |
|
Jumping into this discussion a bit late, so just two short comments:
I don't think this has been done as such in the code doc, yet, but maybe we could just add specific references to the existing production modules directly into the doc ? |
|
Note to myself: It seems that it worth explicitly noting that functions such as |
|
@wenzeslaus would you mind to rebase this PR? |
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
echoix
left a comment
There was a problem hiding this comment.
@wenzeslaus is this still relevant now? It is currently in a state where it up to date enough to pass CI, but there are still some very old (6 years) review comments.
Also, could you take a new look, to tell us if it still shows the patterns we want to show as an example?
It's kind of funny to still see the issue/pr number #20, that is still open. Probably the oldest we have open. Edit: it is the oldest open PR or issue we have
There was a problem hiding this comment.
Remember to apply the changes in a new markdown file when updating the PR.
There was a problem hiding this comment.
Remember to apply the changes in a new markdown file when updating the PR.
|
Hi! @wenzeslaus and @echoix, I was just seeing the oldest PRs for fun and found that this was stuck on some old merge conflicts and needed the docs converted to Markdown, so I decided to take a look ... I've gone ahead and rebased this work and converted the docs to Markdown to resolve the conflicts. |
Move r.example.segment and r.example.segmulti from doc/raster to doc/examples/raster to follow the directory structure introduced in OSGeo#4815, and adjust the Makefiles and READMEs for the new depth. Add Markdown documentation files as the canonical documentation and keep the HTML files in sync with them. Fix the cross-links in the SEE ALSO sections (review comment), use the current programming manual URL, link the Segment Library page directly, use the map names from the example in the r.example.segment EXAMPLE section, and drop the obsolete CVS date keyword. Use --tmp-project in the READMEs to match the current command line interface. In the example code, process the raster as DCELL regardless of the input map type. Previously, the segment cell size followed the input map type while process() assumed DCELL, so for CELL and FCELL inputs the modification was silently lost (verified by running the tool before and after the change). Free the value buffer in r.example.segmulti (review comment), allocate it with sizeof(DCELL) instead of sizeof(DCELL *), and mark a message for translation. In the library manual page, point the example includes to the new paths, mention Segment_close() in the cleanup step (review comment), show the buffer deallocation in the processing extract, rename the complete example section to match its content (one input raster), and fix typos. Use an absolute path for the doxygen EXAMPLE_PATH so the examples are found both from the top directory and from per-directory runs such as make -C lib/segment htmldox. Update prepared with AI assistance (Claude).
Address review comments on the changed function documentation: In Segment_open(), state that the underlying bytes read as zeros in both the all-in-memory mode (G_calloc) and the temporary file mode (file created by seeking), instead of the incorrect claim that no initialization is guaranteed, while still noting that values are not initialized to raster NULL values. Describe when the all-in-memory mode is used instead of the vague automatic adjustment wording, and attribute the out of memory fatal error to the underlying G_calloc(). Move the comment about the mode marker to the cache allocation because the library detects the all-in-memory mode by the non-NULL cache, not by scb. In Segment_get_row(), extend the description of the stored value size with examples (review comment) and smooth the wording. Update prepared with AI assistance (Claude).
Compile r.example.segment and r.example.segmulti in the step which compiles the other example modules, so the examples stay buildable without being part of the regular build. Update prepared with AI assistance (Claude).
This PR adds two examples into
doc/exampleswhich can be compiled as GRASS modules and executed. It adds 3 new sections to the library dox file: about general use with raster, about use with list of rasters (includes portions of one of the examples, so the code is only at one place), and a complete example (here including whole files). Finally, it modifies documentation for two functions.Besides asking for general review of best practices in using this library, I'm asking these two questions:
Segment_get_row()? DoesSegment_get()need the same one?Segment_flush()description suggests that only writing to disk andSegment_get_row()need that andSegment_get_row()does not call any page function if that has any influence.