-
-
Notifications
You must be signed in to change notification settings - Fork 416
doc: Modernize segment library examples and documentation #7035
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
Abhi-d-gr8
wants to merge
9
commits into
OSGeo:main
Choose a base branch
from
Abhi-d-gr8:fix/pr-20-segment-docs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1a2efbe
doc: examples for Segmement Library in dox and as code
wenzeslaus 992b39e
doc: advanced example for segmentlib in dox from example code
wenzeslaus 482494e
doc: improved desc for open and get_row in segmentlib
wenzeslaus f77f8e6
Apply suggestions from clang-format
echoix 9e6fe11
Add language in markdown fenced code blocks
echoix c67c06c
Update README.md
echoix 3f785fa
doc: convert example modules documentation to Markdown
Abhi-d-gr8 d4f4f31
doc: fix end of files and trailing whitespace
Abhi-d-gr8 b1d9cfa
doc: add cmake build for segment examples and fix linting
Abhi-d-gr8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| build_program_in_subdir(r.example.segment DEPENDS grass_gis grass_raster grass_segment) | ||
| build_program_in_subdir(r.example.segmulti DEPENDS grass_gis grass_raster grass_segment) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # to use this file, make this relative to GRASS include/ directory | ||
| # or set -DMODULE_TOPDIR=... in make command line | ||
| # or (when everything fails) use absolute path to the GRASS source code | ||
| MODULE_TOPDIR = ../../.. | ||
|
|
||
| PGM = r.example.segment | ||
|
|
||
| LIBES = $(GISLIB) $(RASTERLIB) $(SEGMENTLIB) | ||
| DEPENDENCIES = $(GISDEP) $(RASTERDEP) $(SEGMENTDEP) | ||
|
|
||
| include $(MODULE_TOPDIR)/include/Make/Module.make | ||
|
|
||
| default: cmd |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| To compile the example simply use `make` (in this directory): | ||
|
|
||
| ```shell | ||
| make | ||
| ``` | ||
|
|
||
| To run (the asterisks will match your operating system and version | ||
| specific directory and file): | ||
|
|
||
| ```shell | ||
| ../../../bin.*/grass* --tmp-location XY --exec bash <<EOF | ||
| g.region res=0.1 | ||
| r.mapcalc -s expression='raster_map_1 = rand(0., 15)' | ||
| r.example.segment input=raster_map_1 output=raster_map_2 | ||
| r.univar raster_map_1 | ||
| r.univar raster_map_2 | ||
| EOF | ||
| ``` | ||
|
|
||
| Both assumes you have GRASS GIS locally compiled. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
|
|
||
| /**************************************************************************** | ||
| * | ||
| * MODULE: r.example.segment | ||
| * AUTHOR(S): Vaclav Petras | ||
| * | ||
| * PURPOSE: Slightly modifies the input data and stores the result | ||
| * (Code explains use of Segment Library) | ||
| * | ||
| * COPYRIGHT: (C) 2019 by Vaclav Petras 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. | ||
| * | ||
| *****************************************************************************/ | ||
|
|
||
| #include <stdlib.h> | ||
|
|
||
| #include <grass/gis.h> | ||
| #include <grass/glocale.h> | ||
| #include <grass/raster.h> | ||
| #include <grass/segment.h> | ||
|
|
||
| /* function declaration */ | ||
| static void process(SEGMENT *raster_seg); | ||
|
|
||
| /* main function driving the execution */ | ||
| int main(int argc, char *argv[]) | ||
| { | ||
| /* input and output raster names and file descriptors */ | ||
| char *input_name; | ||
| char *output_name; | ||
| int input_fd; | ||
| int output_fd; | ||
|
|
||
| /* buffer for reading and writing rasters */ | ||
| void *buffer; | ||
|
|
||
| /* type of the map (CELL/DCELL/...) */ | ||
| RASTER_MAP_TYPE map_type; | ||
|
|
||
| /* variables for current and maximum rows and columns */ | ||
| int nrows, ncols; | ||
| int row; | ||
|
|
||
| /* history structure holds meta-data (title, comments,..) */ | ||
| struct History history; | ||
|
|
||
| /* options and description */ | ||
| struct GModule *module; | ||
| struct Option *input; | ||
| struct Option *output; | ||
|
|
||
| /* initialize GRASS GIS library */ | ||
| G_gisinit(argv[0]); | ||
|
|
||
| /* initialize module and its description */ | ||
| module = G_define_module(); | ||
| G_add_keyword(_("raster")); | ||
| G_add_keyword(_("example")); | ||
| G_add_keyword(_("segment library")); | ||
| G_add_keyword(_("random access")); | ||
| module->description = | ||
| _("Random access to raster using the Segment Library"); | ||
|
|
||
| /* define parameters */ | ||
| input = G_define_standard_option(G_OPT_R_INPUT); | ||
| output = G_define_standard_option(G_OPT_R_OUTPUT); | ||
|
|
||
| /* options and flags parser */ | ||
| if (G_parser(argc, argv)) | ||
| exit(EXIT_FAILURE); | ||
|
|
||
| /* stores options and flags to variables */ | ||
| input_name = input->answer; | ||
| output_name = output->answer; | ||
|
|
||
| /* determine the input map type (CELL/FCELL/DCELL) */ | ||
| map_type = Rast_map_type(input_name, ""); | ||
| size_t cell_size = Rast_cell_size(map_type); | ||
|
|
||
| /* open existing raster map for reading */ | ||
| input_fd = Rast_open_old(input_name, ""); | ||
|
|
||
| /* open the raster for writing (checks if it possible) */ | ||
| output_fd = Rast_open_new(output_name, map_type); | ||
|
|
||
| /* allocate input buffer */ | ||
| buffer = Rast_allocate_buf(map_type); | ||
|
|
||
| nrows = Rast_window_rows(); | ||
| ncols = Rast_window_cols(); | ||
|
|
||
| /* size of a segment */ | ||
| int srows = 64; | ||
| int scols = 64; | ||
|
|
||
| /* number of segments in memory */ | ||
| int num_seg = 4; | ||
|
|
||
| /* segment structure */ | ||
| SEGMENT raster_seg; | ||
|
|
||
| /* initialize the segment structures */ | ||
| if (Segment_open(&raster_seg, G_tempfile(), nrows, ncols, srows, scols, | ||
| cell_size, num_seg) != 1) | ||
| G_fatal_error("Unable to create temporary segment file"); | ||
|
|
||
| /* load data into the segment structures */ | ||
| for (row = 0; row < Rast_window_rows(); row++) { | ||
| Rast_get_row(input_fd, buffer, row, map_type); | ||
| if (Segment_put_row(&raster_seg, buffer, row) < 1) | ||
| G_fatal_error(_("Unable to write temporary segment file")); | ||
| } | ||
|
|
||
| /* run the actual processing */ | ||
| process(&raster_seg); | ||
|
|
||
| /* make sure any pending disk operations take place */ | ||
| Segment_flush(&raster_seg); | ||
| /* store the data permanently in a raster map */ | ||
| for (row = 0; row < Rast_window_rows(); row++) { | ||
| Segment_get_row(&raster_seg, buffer, row); | ||
| Rast_put_row(output_fd, buffer, map_type); | ||
| } | ||
|
|
||
| /* memory cleanup */ | ||
| G_free(buffer); | ||
|
|
||
| /* closing raster maps and segment structures */ | ||
| Segment_close(&raster_seg); | ||
| Rast_close(input_fd); | ||
| Rast_close(output_fd); | ||
|
|
||
| /* add command line incantation to history file */ | ||
| Rast_short_history(output_name, "raster", &history); | ||
| Rast_command_history(&history); | ||
| Rast_write_history(output_name, &history); | ||
|
|
||
| exit(EXIT_SUCCESS); | ||
| } | ||
|
|
||
| /* This would be the main processing function. | ||
| * Here we just hardcode a cell to modify. | ||
| */ | ||
| static void process(SEGMENT *raster_seg) | ||
| { | ||
| /* variable we use to hold the value */ | ||
| DCELL value; | ||
|
|
||
| /* row and column to access */ | ||
| int row = 4; | ||
| int col = 2; | ||
|
|
||
| /* pass the pointer, get the value */ | ||
| Segment_get(raster_seg, (void *)&value, row, col); | ||
|
|
||
| value = value + 100; | ||
|
|
||
| /* pass the pointer, set the value */ | ||
| Segment_put(raster_seg, (void *)&value, row, col); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # r.example.segment | ||
|
|
||
| ## DESCRIPTION | ||
|
|
||
| *r.example.segment* changes one cell value in hardcoded location. | ||
| It is meant to demonstrate how to use the Segment Library together with | ||
| GRASS GIS raster maps. | ||
|
|
||
| ## EXAMPLE | ||
|
|
||
| Create a modified version of the raster map "elevation" | ||
| (North Carolina sample dataset): | ||
|
|
||
| ```bash | ||
| g.region raster=elevation | ||
| r.example.segment input=elevation output=modified_elevation | ||
| r.univar raster_map_1 | ||
| r.univar raster_map_2 | ||
| ``` | ||
|
|
||
| ## SEE ALSO | ||
|
|
||
| * [r.example](r.example.html) | ||
| * [r.example.segmulti](r.example.segmulti.html) | ||
| * [v.example](v.example.html) | ||
|
|
||
| * [GRASS Programmer's Manual](https://grass.osgeo.org/programming7/) | ||
|
|
||
| ## AUTHORS | ||
|
|
||
| Vaclav Petras |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # to use this file, make this relative to GRASS include/ directory | ||
| # or set -DMODULE_TOPDIR=... in make command line | ||
| # or (when everything fails) use absolute path to the GRASS source code | ||
| MODULE_TOPDIR = ../../.. | ||
|
|
||
| PGM = r.example.segmulti | ||
|
|
||
| LIBES = $(GISLIB) $(RASTERLIB) $(SEGMENTLIB) | ||
| DEPENDENCIES = $(GISDEP) $(RASTERDEP) $(SEGMENTDEP) | ||
|
|
||
| include $(MODULE_TOPDIR)/include/Make/Module.make | ||
|
|
||
| default: cmd |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| To compile the example simply use `make` (in this directory): | ||
|
|
||
| ```shell | ||
| make | ||
| ``` | ||
|
|
||
| To run (the asterisks will match your operating system and version | ||
| specific directory and file): | ||
|
|
||
| ```shell | ||
| ../../../bin.*/grass* --tmp-location XY --exec bash <<EOF | ||
| g.region res=0.1 | ||
| r.mapcalc -s expression='raster_1 = rand(0., 15)' | ||
| r.mapcalc -s expression='raster_2 = rand(0., 15)' | ||
| r.mapcalc -s expression='raster_3 = rand(0., 15)' | ||
| r.example.segmulti input=raster_1,raster_2,raster_3 output=raster_out | ||
| r.univar raster_1 | ||
| r.univar raster_2 | ||
| r.univar raster_3 | ||
| r.univar raster_out | ||
| r.info -g raster_out | ||
| r.describe raster_out | ||
| g.gui -f | ||
| EOF | ||
|
|
||
| ``` | ||
|
|
||
| Both assumes you have GRASS GIS locally compiled. | ||
|
|
||
| To precisely time the execution, you can use *perf*: | ||
|
|
||
| ```shell | ||
| perf stat -r 100 r.example.segmulti ... --overwrite | ||
| ``` |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[pre-commit] reported by reviewdog 🐶
grass/doc/raster/r.example.segment/r.example.segment.md
Line 32 in 3f785fa