10

I am trying to convert a geometry column to a geography column and calculate the area using this query:

SELECT dccode,SUM(CAST(St_Area(geom::geography,true) AS NUMERIC)) as Area 
       FROM  schema.table   
       GROUP BY dccode

but I am getting a strange error:

...
NOTICE:  Coordinate values were coerced into range [-180 -90, 180 90] for GEOGRAPHY
NOTICE:  Coordinate values were coerced into range [-180 -90, 180 90] for GEOGRAPHY
NOTICE:  Coordinate values were coerced into range [-180 -90, 180 90] for GEOGRAPHY
NOTICE:  Coordinate values were coerced into range [-180 -90, 180 90] for GEOGRAPHY
NOTICE:  Coordinate values were coerced into range [-180 -90, 180 90] for GEOGRAPHY


ERROR:  lwgeom_area_spher(oid) returned area < 0.0
********** Error **********

ERROR: lwgeom_area_spher(oid) returned area < 0.0

I've run this query to verify that there is no invalid geometry:

select gid, dccode, st_isvalidreason(geom) from table.schema where st_isvalid(geom) = false;

but it doesn't return anything. How can I isolate the geometry that is < 0.0?

8
  • 1
    what srid is your geometry data in? the coercing to rang warning is probably the issue
    – Ian Turton
    Commented Oct 25, 2018 at 14:10
  • It is in srid 4326
    – Matt
    Commented Oct 25, 2018 at 14:12
  • and is it lon/lat or lat/lon?
    – Ian Turton
    Commented Oct 25, 2018 at 14:12
  • 1
    but which way are the coordinates stored? it seems PostGIS believes some of your coordinates lay off planet and it is moving them to make a Geography polygon.
    – Ian Turton
    Commented Oct 25, 2018 at 14:25
  • 4
    The other error which will generate negative area is incorrect polygon ring rotation. Please Edit the question to provide an example of your coordinates.
    – Vince
    Commented Oct 25, 2018 at 14:55

1 Answer 1

1

I encountered this issue with geometries that spanned the antimeridian, but which were brought into PostGIS with SRS 4326. Casting to the Geography type seemed to introduce some issue with vertex ordering, although I'm not sure about the exact nature of this issue. Projecting to an appropriate antimeridian-spanning CRS for my region of interest before calculating the area addressed the issue. So: ST_Area(ST_Transform(geom, 3851)), using https://epsg.io/3851

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.