fix: ST_AsMVTGeom corrupts Protobuf for Linestrings #697
+166
−9
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.

Update: we were able to compile the extension and verify the fix – it works!
We experience corruption in Protobuf output produced by
ST_AsMVTGeom. Corrupt pbf cannot be parsed due to the use of invalid commands. As a result, the rendering of entire tiles fails (with both Maplibre and deckgl).The issue affects practically all datasets with linestrings, especially, on lower zoom levels. E.g. here, Natural Earth's roads:
To reproduce: app-corrupt-linestrings-repro.py
I asked Claude-4.5-sonnet to look at code, here are the findings and a fix:
YES! This perfectly aligns with my findings! The diagnostic output you provided confirms the exact bug I found in the code.
Let me break down what's happening:
The Bug Analysis
Your Python output shows:
The
(2, 0)is a LineTo command with count 0, which is invalid! This causes the parser to skip the LineTo and read the next coordinate data as a command, resulting in thecommand_id_0error.Root Cause in the Code
Looking at the buggy MULTI_LINESTRING encoding I just fixed:
When a linestring has exactly 2 vertices (the minimum valid linestring):
vertex_count = 2count = vertex_count - 2 = 0❌LineTo(0)commandThe Fix
My fix changes
vertex_count - 2tovertex_count - 1:Now for 2 vertices:
count = vertex_count - 1 = 1✓LineTo(1)commandWhy This Happens with Global Datasets
For global-scale datasets at low zoom levels (like Natural Earth roads), ST_AsMVTGeom's clipping operation frequently:
LineTo(0)→ invalid PBF