Skip to content

v.to.rast: avoid zero categories metadata when using use=cat#7019

Open
Abhi-d-gr8 wants to merge 3 commits intoOSGeo:mainfrom
Abhi-d-gr8:fix-2739-v-to-rast-cats
Open

v.to.rast: avoid zero categories metadata when using use=cat#7019
Abhi-d-gr8 wants to merge 3 commits intoOSGeo:mainfrom
Abhi-d-gr8:fix-2739-v-to-rast-cats

Conversation

@Abhi-d-gr8
Copy link
Contributor

Fixes #2739

While working with v.to.rast, I noticed that rasterizing with use=cat results in r.info reporting Number of Categories: 0, even though the raster clearly contains category values. This is confusing for users and looks like a regression.
The issue seems to come from an earlier change where writing individual category rules was disabled for performance reasons. In the use=cat case without labels, this means no category metadata ends up being written at all.

This PR restores meaningful category metadata by usingRast_set_cats_fmt() to store a category format instead of enumerating all categories. This keeps the fast path intact and avoids the performance cost of writing per-category rules, while ensuring r.info no longer reports zero categories.

I also added a small regression test to make sure use=cat produces non-zero category metadata going forward.

@github-actions github-actions bot added vector Related to vector data processing Python Related code is in Python C Related code is in C module tests Related to Test Suite labels Feb 1, 2026
@Abhi-d-gr8 Abhi-d-gr8 force-pushed the fix-2739-v-to-rast-cats branch from 6fdc5bc to c688eb9 Compare February 2, 2026 18:15
@wenzeslaus
Copy link
Member

Given the starting point is a performance regression and unclear behavior, a reproducible benchmark and more reasoning is needed here. The test should be more specific to show that the desired behavior is indeed there.

Copy link
Member

@wenzeslaus wenzeslaus left a comment

Choose a reason for hiding this comment

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

See my previous comment.

Comment on lines +64 to +73
module = SimpleModule("r.info", map=self.output, flags="gr")
self.runModule(module)
info = module.outputs.stdout

# r.info -gr uses shell format and prints ncats= (not "categories=")
ncats = None
for line in info.splitlines():
if line.startswith("ncats="):
ncats = int(float(line.split("=", 1)[1]))
break
Copy link
Member

Choose a reason for hiding this comment

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

Use grass.tool for new code.

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 module Python Related code is in Python tests Related to Test Suite vector Related to vector data processing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] v.to.rast writing 0 categories in metedata when use=cat

2 participants