0

I am using postGIS and want to create cube from a 2D linestring. I use the ST_Extrude option to create the 3D polygons and combines these with the ST_AsGeoJSON operator to export my individual polygons, but it seems that when I run these through a validator like geojson.io that I keep getting errors regarding some of the polygons: "Polygons and MultiPolygons should follow the right-hand rule".

I've also tried to force the RHR using the ST_ForcePolygonCCW operator, as shown in my example query below, but that does not seem to fix anything, same error.

With exampleRegion (geom) as (select ST_GeomFromText('LINESTRING (0 0 0, 0 1 0, 1 1 0, 1 0 0, 0 0 0)'))
SELECT (a.p_geom).path[1] As path, ST_AsGEOJSON(ST_ForcePolygonCCW((a.p_geom).geom)) As geom_ewkt
FROM (SELECT st_dump(st_extrude(ST_MakePolygon(geom), 0, 0, 3)) AS p_geom FROM exampleRegion) AS a

The following table is the result of the query specified in my post:

1 "{""type"":""Polygon"",""coordinates"":[[[0,0,0],[1,0,0],[1,1,0],[0,1,0],[0,0,0]]]}" 
2 "{""type"":""Polygon"",""coordinates"":[[[0,0,3],[1,0,3],[1,1,3],[0,1,3],[0,0,3]]]}" 
3 "{""type"":""Polygon"",""coordinates"":[[[0,0,0],[0,0,3],[0,1,3],[0,1,0],[0,0,0]]]}" 
4 "{""type"":""Polygon"",""coordinates"":[[[0,1,0],[0,1,3],[1,1,3],[1,1,0],[0,1,0]]]}" 
5 "{""type"":""Polygon"",""coordinates"":[[[1,1,0],[1,1,3],[1,0,3],[1,0,0],[1,1,0]]]}" 
6 "{""type"":""Polygon"",""coordinates"":[[[1,0,0],[1,0,3],[0,0,3],[0,0,0],[1,0,0]]]}" 

Is there some other way to create valid geoJSON format to describe my 3D object?

1
  • Could you add an example about the generated GeoJSON?
    – user30184
    Commented Nov 14, 2022 at 6:52

1 Answer 1

2

GeoJSON format https://www.rfc-editor.org/rfc/rfc7946 is made for 2D geometries or optionally 25D geometries with height as a third element. The format itself and tools which are built around it do not deal with true 3D geometries.

As an example, take just one extruded vertical face as a polygon.

As WKT:

POLYGON Z ((0 1 0,0 1 3,1 1 3,1 1 0,0 1 0))

As GeoJSON:

{
    "type": "FeatureCollection",
    "name": "testwkt",
    "features": [{
        "type": "Feature",
        "properties": {},
        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [0, 1, 0],
                    [0, 1, 3],
                    [1, 1, 3],
                    [1, 1, 0],
                    [0, 1, 0]
                ]
            ]
        }
    }]
}

In 2D space the polygon has only two distinct points: 0,1 and 1,1. That makes the polygon invalid. It is also invalid for PostGIS:

SELECT ST_IsValidReason(
ST_GeomFromText(
'POLYGON Z ((0 1 0,0 1 3,1 1 3,1 1 0,0 1 0))'
))

Result:
"Too few points in geometry component[0 1 0]"

You must use some other format than GeoJSON for 3D geometries.

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.