7

I am using the following query to create a GeoJson response:

SELECT json_build_object(
    'type',         'FeatureCollection',
    'features',     jsonb_agg(feature)
)
FROM (
    SELECT json_build_object(
        'type',         'Feature',
        'id',           id,
        'geometry',     ST_AsGeoJSON(ST_ForcePolygonCW(wkb_geometry))::json,
        'properties',   to_jsonb(row) - 'id' - 'wkb_geometry'
    ) AS feature
    FROM (SELECT * FROM gis_states LIMIT 1) AS row
) AS features

The resulting data-set (https://pastebin.com/hvFQXUTr) does not validate at http://geojsonlint.com, even though I am using ST_ForcePolygonCW:

Line 1: Polygons and MultiPolygons should follow the right-hand rule

Any suggestions on how to fix this?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
mike.bronner
  • 513
  • 3
  • 13
  • Related: https://gis.stackexchange.com/questions/259944/polygons-and-multipolygons-should-follow-the-right-hand-rule – pLumo Dec 19 '18 at 15:07

1 Answers1

10

OK, so the documentation (https://postgis.net/docs/ST_ForceRHR.html) states that ST_ForcePolygonCW is a synonym of ST_ForceRHR(). However, I wonder if the linter at http://geojsonlint.com/ is working properly, or perhaps the error is too ambiguous, as I got it to render correctly by using ST_ForcePolygonCCW, which is the opposite.

mike.bronner
  • 513
  • 3
  • 13
  • 2
    The problem is that "Right-Hand-Rule" isn't a very useful term in GIS. Some GIS software (e.g., PostGIS) uses it to refer mean that the interior of a boundary is to your right as you walk around it (clockwise orientation). In most other contexts, it refers to the direction your fingers curl when the thumb of your right hand is pointing towards your head (counterclockwise orientation). – dbaston Dec 19 '18 at 19:04
  • 1
    This answer was very helpful, thanks. Multiple linters confirm ST_ForcePolygonCCW produces valid output for GeoJSON – agmin Sep 01 '21 at 00:36