geo: GeoJSON output (`ST_AsGeoJSON`, `to_json`, JSON changefeeds) writes the M ordinate of XYM geometries as the Z coordinate
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 54/100
Direzione di ricerca
Start with pkg/geo/encode.go, especially SpatialObjectToGeoJSON, and trace how it passes XYM geometries to the GeoJSON encoder. Check the GeoJSON encoding tests and compare XYM, XYZ, and XYZM behavior with the PostGIS examples in the issue. Done means XYM no longer appears as XYZ in ST_AsGeoJSON, to_json/to_jsonb, and JSON changefeeds.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Describe the problem
For geometries with an XYM layout (POINT M, LINESTRING M, ...), CockroachDB's GeoJSON encoder writes the
measure as the third element of each position. GeoJSON has no M ordinate: RFC 7946 (section 3.1.1) defines the
third element of a position as altitude or elevation. As a result, POINT M (1 2 3) and POINT Z (1 2 3)
produce identical GeoJSON. GeoJSON readers, including CockroachDB's own ST_GeomFromGeoJSON and PostGIS, load
the measure back as a Z value, and the geometry becomes XYZ.
This affects ST_AsGeoJSON, to_json/to_jsonb of geometry and geography values, and the JSON changefeed
format, which encodes spatial columns as GeoJSON. A JSON changefeed of a table with XYM data therefore delivers
3D geometries whose elevation is the measure. XY and XYZ are written as in PostGIS. XYZM is written with M as
a fourth element, which RFC 7946 discourages but which keeps Z in the third position.
PostGIS (3.5.2 and 3.6.4) drops the M ordinate in ST_AsGeoJSON and in to_json instead:
ST_AsGeoJSON('POINT M (1 2 3)') = {"type":"Point","coordinates":[1,2]}, and POINT ZM (1 2 3 4) gives
[1,2,3].
To Reproduce
cockroach start-single-node --insecure --external-io-dir=/tmp/externcockroach sql --insecure:
CREATE TABLE g (id INT PRIMARY KEY, geom GEOMETRY);
INSERT INTO g VALUES (1, 'POINT M (1 2 3)'), (2, 'POINT Z (1 2 3)'), (3, 'LINESTRING M (0 0 5, 1 1 6)'), (4, 'POINT ZM (1 2 3 4)');
SELECT id, ST_AsText(geom) AS source, ST_AsGeoJSON(geom) AS geojson, to_json(geom) AS to_json,
ST_AsText(ST_GeomFromGeoJSON(ST_AsGeoJSON(geom))) AS read_back
FROM g ORDER BY id;
CREATE CHANGEFEED FOR TABLE g INTO 'nodelocal://1/gj' WITH format = json, initial_scan = 'only';
Output on master:
id | source | geojson | to_json | read_back
-----+-----------------------------+-------------------------------------------------------+---------------------------------------------------------------+------------------------------
1 | POINT M (1 2 3) | {"type":"Point","coordinates":[1,2,3]} | {"coordinates": [1, 2, 3], "type": "Point"} | POINT Z (1 2 3)
2 | POINT Z (1 2 3) | {"type":"Point","coordinates":[1,2,3]} | {"coordinates": [1, 2, 3], "type": "Point"} | POINT Z (1 2 3)
3 | LINESTRING M (0 0 5, 1 1 6) | {"type":"LineString","coordinates":[[0,0,5],[1,1,6]]} | {"coordinates": [[0, 0, 5], [1, 1, 6]], "type": "LineString"} | LINESTRING Z (0 0 5, 1 1 6)
4 | POINT ZM (1 2 3 4) | {"type":"Point","coordinates":[1,2,3,4]} | {"coordinates": [1, 2, 3, 4], "type": "Point"} | POINT ZM (1 2 3 4)
The changefeed file /tmp/extern/gj/<date>/*.ndjson:
{"after": {"geom": {"coordinates": [1, 2, 3, 4], "type": "Point"}, "id": 4}, "key": [4]}
{"after": {"geom": {"coordinates": [1, 2, 3], "type": "Point"}, "id": 1}, "key": [1]}
{"after": {"geom": {"coordinates": [1, 2, 3], "type": "Point"}, "id": 2}, "key": [2]}
{"after": {"geom": {"coordinates": [[0, 0, 5], [1, 1, 6]], "type": "LineString"}, "id": 3}, "key": [3]}
Rows 1 and 2 are indistinguishable in the output.
Expected behavior
The M value must not be written in the altitude position. Following PostGIS, the encoder would drop M and write
[1, 2] for POINT M (1 2 3). Alternatively it could reject the layout, or the changefeed could use an encoding
that keeps M (e.g. EWKB/EWKT). In no case should an XYM geometry come back as XYZ.
Additional data / screenshots
Cause on master: geo.SpatialObjectToGeoJSON
(encode.go#L111-L155) unmarshals the EWKB and passes the geometry, with its
XYM layout, to github.com/twpayne/go-geom/encoding/geojson v1.4.2
(go.mod#L244). That encoder writes all ordinates of each coordinate in layout order, so M
lands in the third position. Its own decoder maps three-element positions to XYZ. to_json and the JSON
changefeed encoder reach SpatialObjectToGeoJSON through tree.AsJSON
(datum.go#L3887-L3890,
encoder_json.go#L376), and ST_AsGeoJSON calls it directly
(geo_builtins.go#L1885). Dropping M from XYM geometries (writing
XY) before encoding, as PostGIS does, would fix it.
Environment:
- CockroachDB version: master at d30c905fff79 (2026-09-23, current public
masterHEAD on 2026-10-18), built
from source withbazel build //pkg/cmd/cockroach-short --config=dev, and the edge binary
v26.4.0-alpha.3-dev-74a73764f18b (built 2026-10-18). Also reproduced on v26.3.2, v26.4.0-beta.2 and v25.4.17. - Server OS: Linux (Ubuntu 22.04), single node
- Client app:
cockroach sql
Additional context
Linear-referencing data (mileposts along roads or pipelines, time or distance along GPS tracks) is stored as
XYM. A JSON changefeed or GeoJSON export of such a table gives every consumer 3D geometries whose elevation is
the measure. A downstream spatial store keeps them as XYZ, and the measures are lost there. The source keeps the
correct values, so the difference only shows when the two copies are compared.
Jira issue: CRDB-70530
- Lingua principale
- Go
- Stelle
- 32.6k
- Fork
- 4.2k
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di cockroachdb/cockroach
-
C-bug O-community X-blathers-untriaged
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
cockroachdb/cockroach#176245 · 1 commento ·
-
sql: pg_database.dattablespace is 0 but pg_tablespace.oid is 1663, so they no longer joinForse già presa @1AbdullahButt l’ha presa 14 giorni fa. ApertaC-bug O-community X-blathers-triaged X-move-internal
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
cockroachdb/cockroach#176228 · 1 commento ·
-
PG compat: overflow in integer power calculation triggers incompatible SQLSTATEForse di nuovo libera @darshpatel1052 l’ha presa 18 giorni fa e non c’è nessuna pull request aperta. ApertaC-bug O-community X-blathers-untriaged
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
cockroachdb/cockroach#176223 · 2 commenti ·
-
util/log: bufferedSinkCloser.Close leaks a goroutine after timeoutForse già presa @dajiaohuang l’ha presa 15 giorni fa. ApertaC-bug O-community X-blathers-untriaged
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 84/100
cockroachdb/cockroach#176210 · 1 commento ·
-
C-bug O-community T-sql-queries X-blathers-triaged X-move-internal
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
cockroachdb/cockroach#176150 · 1 commento ·
Tutte le issue di cockroachdb/cockroach
Issue simili
-
☢️ Bug v3
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
I maintainer di solito rispondono entro 1 giorno
-
area/import-export area/projects concern/ux
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
go-vikunja/vikunja#4455 · 1 commento ·
I maintainer di solito rispondono entro 2 giorni
-
Dependabot PRs no longer merge automaticallyForse già presa @dwisiswant0 l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
projectdiscovery/urlfinder#160 ·
I maintainer di solito rispondono entro 1 giorno
-
database
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
chnm/legal-modernism#450 ·
I maintainer di solito rispondono entro 1 giorno
-
tech-debt
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno