Repository navigation
feat(dashboard): support PostGIS geometry/geography in the database data grid - #5190
Conversation
Vercel Deployment Info - dashboard
|
|
Hello, thanks for the PR! I'm not sure about the approach though. The grid currently reads rows with Proposal:
Test schema and dataSET ROLE postgres;
CREATE EXTENSION IF NOT EXISTS postgis;
CREATE SCHEMA IF NOT EXISTS gis;
CREATE TABLE gis.places (
id serial PRIMARY KEY,
name text NOT NULL,
description text,
location geometry(Point, 4326) NOT NULL,
entrance geometry(Point, 4326) DEFAULT ST_SetSRID(ST_MakePoint(0, 0), 4326)
);
CREATE TABLE gis.parcels (
id serial PRIMARY KEY,
label text,
boundary geometry(Polygon, 3857)
);
CREATE TABLE gis.shapes (
id serial PRIMARY KEY,
kind text,
shape geometry
);
CREATE TABLE gis.peaks (
id serial PRIMARY KEY,
name text,
summit geometry(PointZ, 4326)
);
CREATE TABLE gis.airports (
id serial PRIMARY KEY,
code text,
location geography(Point, 4326),
route geography(LineString, 4326)
);
-- curve types can't be represented as GeoJSON
CREATE TABLE gis.curves (
id serial PRIMARY KEY,
curve geometry
);
INSERT INTO gis.places (name, description, location, entrance) VALUES
('Brandenburg Gate', 'Berlin', 'SRID=4326;POINT(13.377704 52.516275)', NULL),
('Eiffel Tower', 'Paris', ST_GeomFromGeoJSON('{"type":"Point","coordinates":[2.294481,48.858370]}'), DEFAULT),
('Precision test', 'many decimals', 'SRID=4326;POINT(13.404953812345678 52.520006598765432)', NULL);
INSERT INTO gis.parcels (label, boundary) VALUES
('Lot A', ST_Transform('SRID=4326;POLYGON((13.37 52.51,13.38 52.51,13.38 52.52,13.37 52.52,13.37 52.51))'::geometry, 3857)),
('Lot with hole', ST_Transform('SRID=4326;POLYGON((0 0,10 0,10 10,0 10,0 0),(2 2,4 2,4 4,2 4,2 2))'::geometry, 3857));
INSERT INTO gis.shapes (kind, shape) VALUES
('point, no SRID', 'POINT(1 2)'),
('linestring', 'SRID=4326;LINESTRING(0 0,1 1,2 1)'),
('multipolygon', 'SRID=4326;MULTIPOLYGON(((0 0,1 0,1 1,0 0)),((5 5,6 5,6 6,5 5)))'),
('multipoint', 'SRID=4326;MULTIPOINT((0 0),(1 1))'),
('collection', 'SRID=4326;GEOMETRYCOLLECTION(POINT(0 0),LINESTRING(0 0,1 1))'),
('empty', 'POLYGON EMPTY'),
('3D line', 'SRID=4326;LINESTRING Z (0 0 0,1 1 10)'),
('measured line (M)', 'SRID=4326;LINESTRING M (0 0 1,1 1 2)'),
('null', NULL);
INSERT INTO gis.peaks (name, summit) VALUES
('Zugspitze', 'SRID=4326;POINT Z (10.9863 47.4211 2962)'),
('Mont Blanc', 'SRID=4326;POINT Z (6.8652 45.8326 4808)');
INSERT INTO gis.airports (code, location, route) VALUES
('BER', 'SRID=4326;POINT(13.5033 52.3667)', 'SRID=4326;LINESTRING(13.5033 52.3667,2.5479 49.0097)'),
('CDG', 'SRID=4326;POINT(2.5479 49.0097)', NULL);
INSERT INTO gis.curves (curve) VALUES
('CIRCULARSTRING(0 0,1 1,2 0)');Reading, SELECT row_to_json(t)
FROM (
SELECT id, code,
ST_AsGeoJSON(location, 15, 4)::json AS location,
ST_AsGeoJSON(route, 15, 4)::json AS route
FROM gis.airports
) t;
-- curves: fall back to EWKT (use GeometryType(col::geometry) for geography columns)
SELECT row_to_json(t)
FROM (
SELECT id,
CASE
WHEN GeometryType(curve) IN ('CIRCULARSTRING', 'COMPOUNDCURVE', 'CURVEPOLYGON', 'MULTICURVE', 'MULTISURFACE')
THEN to_json(ST_AsEWKT(curve))
ELSE ST_AsGeoJSON(curve, 15, 4)::json
END AS curve
FROM gis.curves
) t;Writing, GeoJSON goes through -- geography: cast the result
UPDATE gis.airports
SET route = ST_GeomFromGeoJSON('{"type":"LineString","crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:EPSG::4326"}},"coordinates":[[13.5033,52.3667],[11.7861,48.3538]]}')::geography
WHERE id = 1;
-- geometry with a non-4326 SRID: the crs member carries it
INSERT INTO gis.parcels (label, boundary) VALUES (
'Lot B',
ST_GeomFromGeoJSON('{"type":"Polygon","crs":{"type":"name","properties":{"name":"EPSG:3857"}},"coordinates":[[[0,0],[100,0],[100,100],[0,100],[0,0]]]}')
);
-- WKT/EWKT (e.g. curves shown as EWKT) as a plain literal
UPDATE gis.curves SET curve = 'CIRCULARSTRING(0 0,1 2,2 0)' WHERE id = 1;Arrays of spatial types ( |
Add isSpatialType, isGeographyType, isGeoJSONGeometry and getSpatialValueError for the database data grid. They recognise geometry and geography columns (also when PostGIS lives in its own schema) and check that a value is in a text format PostGIS accepts: GeoJSON, WKT/EWKT or hex EWKB for geometry, and WKT/EWKT or hex EWKB for geography, whose input function does not parse GeoJSON.
Geometry values come back from row_to_json as GeoJSON objects, so the record form rendered them as "[object Object]". The form now: - pretty-prints GeoJSON for geometry columns, as it does for json/jsonb, and uses a multiline input for geometry and geography columns - validates values with getSpatialValueError and explains that geography columns need WKT/EWKT or hex EWKB because PostGIS does not parse GeoJSON for geography - sends edited GeoJSON as text and skips columns whose GeoJSON did not change, so saving a row no longer rewrites untouched geometry values The edit diff moves from EditRecordForm into getColumnUpdateOptions so it can be unit tested.
Geometry and geography cells now open the multiline editor used for json/jsonb, so GeoJSON and longer WKT values can be edited inline in the data grid instead of in a single-line input. Closes nhost#4540
Select columns explicitly and read geometry and geography as GeoJSON with ST_AsGeoJSON(col, 15, 4), falling back to EWKT for the types GeoJSON cannot represent. Write GeoJSON through ST_GeomFromGeoJSON, cast for geography, and send WKT/EWKT/hex EWKB as plain literals. Drop the client-side spatial validation and GeoJSON diffing.
8e6c1e4 to
6972fc0
Compare
|
Thanks for the proposal and the test schema. I reworked the branch to match it. Spatial columns are now read through ST_AsGeoJSON(col, 15, 4)::json, with ST_AsEWKT as the fallback for curve and surface types. Filtering and ordering stay on the raw columns in an inner subquery. On write, GeoJSON goes through ST_GeomFromGeoJSON, cast to geography where needed, and WKT, EWKT and hex EWKB are sent as plain literals. The client-side validation and GeoJSON diffing are gone. The [object Object] fix stays, and geography now shows as GeoJSON too. One deviation: the CASE uses ST_GeometryType instead of GeometryType, since the latter appends M for measured geometries. The PR description is updated. The checks on the new head need a safe_to_test approval. |
|
Thanks, and sorry for the delay, we will be reviewing it soon. |
|
|
|
|
|
|
Closes #4540
Problem
Geometry columns rendered as
[object Object]in the record form and could not be edited. Geography columns showed as hex EWKB.Approach
As suggested in review, PostGIS converts values in SQL. This is what the Hasura console and the GraphQL API do, so the dashboard no longer guesses formats on the client.
Read
fetchTablenow selects an explicit column list (formatSelectList). Spatial columns are read as:4always includes the CRS, so the SRID survives a round-trip throughST_GeomFromGeoJSON.ST_CircularString,ST_CompoundCurve,ST_CurvePolygon,ST_MultiCurve,ST_MultiSurface,ST_PolyhedralSurface,ST_Tin,ST_Triangle) cannot be represented as GeoJSON, so they are read as EWKT.ST_GeometryTypeis used instead ofGeometryTypebecause the latter appendsMfor measured geometries.Filtering, ordering and limit run in an inner subquery on the raw columns. The conversion only happens in the outer select, so sorting by a spatial column still sorts on the geometry.
Without column metadata the select list falls back to
*.Write
createRecordandupdateRecordsend a GeoJSON value (an object, or text starting with{) asST_GeomFromGeoJSON(value), with::geographyfor geography columns.Form and grid
jsonandjsonb.EditRecordFormintogetColumnUpdateOptions. Spatial values are read as objects but edited as text, so the text is parsed and compared with the original object. An unchanged geometry is not sent on save.Compared with the first version of this PR, the client-side validation, the WKT and EWKB patterns and geography-as-hex are gone.
Out of scope:
geometry[]arrays and spatial filter operators in the grid.Testing
spatialTypeHelpers,formatSelectList,recordFormValues(includinggetColumnUpdateOptions),fetchTable,createRecord,updateRecordandDataGridTextCell.database/dataGridand the 4DataGridTextCelltests pass locally.