3

I am trying to output a PostgreSQL9.6 query to geojson as in this answer:

SELECT jsonb_build_object(
    'type',     'FeatureCollection',
    'features', jsonb_agg(feature)
)
FROM (
  SELECT jsonb_build_object(
    'type',       'Feature',
    'id',         gid,
    'geometry',   ST_AsGeoJSON(geom)::jsonb,
    'properties', to_jsonb(row) - 'gid' - 'geom'
  ) AS feature
  FROM (SELECT * FROM paris.plots LIMIT 100) row) features;

I get an error on geojsonlint.com that the polygons should follow teh counterclock-wise rule. A comment under the answer notes that polygons should be forced to be forced to be reorderd counter-clock wise with ST_ForcePolygonCCW. Hence I updated my code to:

SELECT jsonb_build_object(
    'type',     'FeatureCollection',
    'features', jsonb_agg(feature)
)
FROM (
  SELECT jsonb_build_object(
    'type',       'Feature',
    'id',         gid,
    'geometry',   ST_AsGeoJSON(ST_ForcePolygonCCW(geom))::jsonb,
    'properties', to_jsonb(row) - 'gid' - 'geom'
  ) AS feature
  FROM (SELECT * FROM paris.plots LIMIT 100) row) features;

I get an error:

Kernel error: ERROR:  function st_forcepolygonccw(geometry) does not exist

But the function is listed in the Postgis references. Any idea?

Duccio A
  • 427
  • 6
  • 12
  • Most GeoJSON clients do not care if polygons are CCW of CW. The validator at geojson.io does not care either. – user30184 Nov 16 '17 at 17:37

1 Answers1

3

That function was introduced in PostGIS 2.4. In earlier versions, you can achieve the same thing with ST_Reverse(ST_ForceRHR(geom)).

dbaston
  • 13,048
  • 3
  • 49
  • 81
  • With ST_Reverse(ST_ForceRHR(geom)), geojsonlint.com still gives me the same error, but you are right that it doesn't really matter. – Duccio A Nov 16 '17 at 19:02