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?

1
  • Most GeoJSON clients do not care if polygons are CCW of CW. The validator at geojson.io does not care either.
    – user30184
    Commented Nov 16, 2017 at 17:37

1 Answer 1

3

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

1
  • 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
    Commented Nov 16, 2017 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.