Skip to content

man: Improve segment library documentation, provide examples #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

wenzeslaus
Copy link
Member

This PR adds two examples into doc/examples which 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:

  • Is the remark about value initialization correct? (I don't really see a point in using calloc instead of malloc there considering that the non-memory case does initialize with zeros.)
  • What about the precondition for Segment_get_row()? Does Segment_get() need the same one? Segment_flush() description suggests that only writing to disk and Segment_get_row() need that and Segment_get_row() does not call any page function if that has any influence.

@wenzeslaus wenzeslaus added the enhancement New feature or request label May 24, 2019
@wenzeslaus wenzeslaus requested a review from metzm May 24, 2019 03:22

/* determine the input map type (CELL/FCELL/DCELL) */
map_type = Rast_map_type(input_name, "");
size_t cell_size = Rast_cell_size(map_type);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move "size_t cell_size;" up, just after "RASTER_MAP_TYPE map_type;"

ncols = Rast_window_cols();

/* size of a segment */
int srows = 64;
Copy link
Contributor

@metzm metzm May 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move "int srows, scols, num_seg;" up

int num_seg = 4;

/* segment structure */
SEGMENT raster_seg;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move "SEGMENT raster_seg;" up


/* pass the pointer, set the value */
Segment_put(raster_seg, values, row, col);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • G_free(values):

ncols = Rast_window_cols();

/* size of a segment */
int srows = 64;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move "size_t cell_size, segment_cell_size;" up

output_fd = Rast_open_new(output_name, map_type);

/* segment structure */
SEGMENT raster_seg;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move up

G_fatal_error(_("Unable to create temporary segment file"));

/* array to store file descriptors */
int *input_fds = G_malloc(ninputs * sizeof(int));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move "int *input_fds;" up

}

/* allocate input buffer */
DCELL *row_buffer = Rast_allocate_d_buf();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move "DCELL *row_buffer, *seg_buffer;" up


/* pass the pointer, set the value */
Segment_put(raster_seg, values, row, col);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • G_free(values);


<em>
<a href="r.example.html">r.example</a>
<a href="r.example.html">r.example.segment</a>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r.example.segment.html

* <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).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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++) {
Copy link
Contributor

@metzm metzm May 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for (int row = 0; ...) is C++ style


\skip G_free
\until Rast_close(output_fd);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Segment_close() ?

@metzm
Copy link
Contributor

metzm commented May 24, 2019

This PR adds two examples into doc/examples which 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.

Please use standard C style in sample code.
Maybe documentation does not need to be that long? avoid tl;dr

Besides asking for general review of best practices in using this library, I'm asking these two questions:

* Is the remark about value initialization correct? (I don't really see a point in using calloc instead of malloc there considering that the non-memory case does initialize with zeros.)

See "man calloc". malloc does not initialize the memory while calloc does.

* What about the precondition for `Segment_get_row()`? Does `Segment_get()` need the same one?

No.

Segment_flush() description suggests that only writing to disk and Segment_get_row() need that and Segment_get_row() does not call any page function if that has any influence.

Yes, that's the reason: Segment_get_row() bypasses the internal cache and reads directly from the temporary file.

@metzm
Copy link
Contributor

metzm commented May 24, 2019

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.

@mlennert
Copy link
Contributor

Jumping into this discussion a bit late, so just two short comments:

  • I think we definitely need enhanced and detailed documentation of the C-code. @metzm, today you are probably the only one to really know the nitty-gritty stuff in this library, so for future sustainability (and possibly attracting the rare C-developer out there), good doc seems a must to me.

  • At this stage, I haven't had the chance to delve into the doc enough to understand whether new (simple) examples are needed. They have the advantage of focusing on the features documented, thus avoiding distraction through other unrelated parts of code. They have the disadvantage of having to be updated when the library changes. Actual production modules obviously provide more motivation to keep them up to date.

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 ?

@wenzeslaus
Copy link
Member Author

Note to myself: It seems that it worth explicitly noting that functions such as Segment_get() don't do bounds check for row and col (and thus crash will occur rather than an error message).

@wenzeslaus wenzeslaus added this to the 8.2.0 milestone Dec 2, 2021
@wenzeslaus wenzeslaus modified the milestones: 8.2.0, 8.4.0 Feb 27, 2022
@wenzeslaus wenzeslaus modified the milestones: 8.3.0, 8.4.0 Feb 10, 2023
@neteler
Copy link
Member

neteler commented Nov 7, 2023

@wenzeslaus would you mind to rebase this PR?

@wenzeslaus wenzeslaus modified the milestones: 8.4.0, 8.5.0 Apr 26, 2024
@echoix
Copy link
Member

echoix commented Jun 21, 2024

@wenzeslaus I manually resolved the conflicts

@github-actions github-actions bot added C Related code is in C HTML Related code is in HTML libraries docs labels Jun 21, 2024
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@echoix echoix changed the title Improve segment library documentation, provide examples man: Improve segment library documentation, provide examples Jun 21, 2024
Copy link
Member

@echoix echoix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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

Copy link
Member

@echoix echoix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is our oldest open PR, number 20. We have to port the docs changes in HTML to markdown. Also, since the last update of the PR, the folder structure changed (in early January 2025, in #4815)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember to apply the changes in a new markdown file when updating the PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember to apply the changes in a new markdown file when updating the PR.

@echoix echoix added the conflicts/needs rebase Rebase to or merge with the latest base branch is needed label Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C Related code is in C conflicts/needs rebase Rebase to or merge with the latest base branch is needed docs enhancement New feature or request HTML Related code is in HTML libraries
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants