v.geometry: new tool (v.to.db wrapper) for more obvious access to vector geometry computation#7329
Open
petrasovaa wants to merge 3 commits into
Open
v.geometry: new tool (v.to.db wrapper) for more obvious access to vector geometry computation#7329petrasovaa wants to merge 3 commits into
petrasovaa wants to merge 3 commits into
Conversation
cwhite911
reviewed
May 13, 2026
Contributor
cwhite911
left a comment
There was a problem hiding this comment.
Overall, everything looks good. I just have a few minor suggestions.
| for record in data["records"]: | ||
| print(record["category"], record["compactness"]) | ||
| ``` | ||
|
|
Contributor
There was a problem hiding this comment.
Want to add a grass.tools example?
| # %end | ||
|
|
||
| # %option G_OPT_F_SEP | ||
| # % answer: {NULL} |
Comment on lines
+57
to
+61
| # %option G_OPT_F_FORMAT | ||
| # % options: plain,json,csv | ||
| # % answer: json | ||
| # % descriptions: plain;Plain text with pipe separator by default;json;JSON (JavaScript Object Notation);csv;CSV (Comma Separated Values) | ||
| # %end |
Contributor
There was a problem hiding this comment.
We can address this in another PR, but we should make the order of options for G_OPT_F_FORMAT constant across all tools.
Comment on lines
+125
to
+152
| def _available_cpus(): | ||
| """Number of CPUs this process may actually use. | ||
|
|
||
| Prefers affinity-aware sources over ``os.cpu_count()``, which reports | ||
| the host total and overcounts in containers and cgroup-limited jobs. | ||
| """ | ||
| if hasattr(os, "process_cpu_count"): # Python 3.13+ | ||
| return os.process_cpu_count() or 1 | ||
| if hasattr(os, "sched_getaffinity"): # Linux | ||
| return len(os.sched_getaffinity(0)) | ||
| return os.cpu_count() or 1 | ||
|
|
||
|
|
||
| def _resolve_nprocs(nprocs): | ||
| """Resolve G_OPT_M_NPROCS into a worker count for ThreadPoolExecutor. | ||
|
|
||
| Mirrors the semantics of G_set_omp_num_threads() in | ||
| lib/gis/omp_threads.c: 0 means use all available cores, a positive | ||
| number is used as-is, a negative number means cpu_count + nprocs | ||
| (clamped to at least 1). Belongs in a library helper eventually. | ||
| """ | ||
| nprocs = int(nprocs) | ||
| if nprocs > 0: | ||
| return nprocs | ||
| available = _available_cpus() | ||
| if nprocs == 0: | ||
| return available | ||
| return max(1, available + nprocs) |
Contributor
There was a problem hiding this comment.
General note. We should consider adding a version of this to grass.scripts since the same logic is widely reused.
Something like.
gs.resolve_nprocs(nprocs=0)
Contributor
Author
There was a problem hiding this comment.
Ok, this is now in a separate PR #7414. Once that is merged I will update it here.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
When somebody wants to compute perimeter/lengths/bbox of vector features, it's difficult to find the tool because v.to.db's name doesn't convey that. Plus for many data science workflows printing it and putting in dataframe is easier than dealing with attribute table. The v.to.db's interface is centered around the table, e.g. column is required even when just printing. Here is a quick summary of the tool:
It includes functions resolving number of cores, this is something that will need to go into a library I think, but I kept it here for now.
Before:
Instead using v.geometry: