[PostGIS] #5671: Bug in ST_Area function with use_spheroid=false

PostGIS trac at osgeo.org
Mon Feb 19 06:13:54 PST 2024


#5671: Bug in ST_Area function with use_spheroid=false
----------------------+-----------------------------------
  Reporter:  azakh    |      Owner:  pramsey
      Type:  defect   |     Status:  new
  Priority:  medium   |  Milestone:
 Component:  postgis  |    Version:  3.3.x
Resolution:           |   Keywords:  ST_Area, use_spheroid
----------------------+-----------------------------------
Description changed by azakh:

Old description:

> ST_Area fails to calculate the area of the geography polygon with one
> inner ring, with `use_spheroid=false`:
>
> {{{
> SELECT ST_Area(
> 'POLYGON(
>   (
>     -111.772 33.307,
>     -111.772 33.304,
>     -111.783 33.304,
>     -111.783 33.307,
>     -111.772 33.307
>   ),
>   (
>     -111.781442 33.304825,
>     -111.776964 33.3048,
>     -111.773047 33.304778,
>     -111.773177 33.306408,
>     -111.781442 33.304825
>   )
> )
> '::geography, false);
> }}}
>
> The result is:
> {{{
> ERROR:  lwgeom_area_spher(oid) returned area < 0.0
> }}}
>
> `use_spheroid=true` version works and gives `270218.45392724127`, but I
> would expect the on-the-sphere version also to work.
>
> The polygon seems to be correct, though it has almost flat angle at the
> `(-111.776964 33.3048)` vertex. Let's call the vertex **X**.
>
> If **X** is removed then `ST_Area` is `270337.25718276866`.
>
> If we retain **X** but make a cyclic shift of the inner ring: **AXBCA**
> => **XBCAX**, then the function succeeds but gives `91737.85992092389`
> which is quite different from the correct value.
>
> Let's give small amends to the **X** latitude with **AXBCA** vertex
> order: \\ **X** => `(-111.776964 (33.3048 + amend))`.
>
> {{{
>   Amend    |        ST_Area
> -------------------------------
> -1.0e-05   |   269904.2853637372
> -1.0e-06   |   270298.26808849035
> -1.0e-07   |   270628.6930016738
> -1.0e-08   |   269541.18027120724
> -1.0e-09   |   315257.7589488026
> -1.0e-10   |   ERROR:  lwgeom_area_spher(oid) returned area < 0.0
> -1.0e-11   |   262120.12981526955
> -1.0e-12   |    41142.8857341336
> -1.0e-13   |   144916.06439020386
> -1.0e-14   |    41142.8857341336
> -1.0e-15   |   ERROR:  lwgeom_area_spher(oid) returned area < 0.0
>  1.0e-15   |   ERROR:  lwgeom_area_spher(oid) returned area < 0.0
>  1.0e-14   |   159848.6351422007
>  1.5e-14   |   113982.3188750676
>  5.0e-14   |   243067.1526449226
>  7.5e-14   |    95027.49039558775
>  1.0e-13   |    41142.8857341336
>  5.0e-13   |   159848.59909125822
>  7.5e-13   |   320241.96397413884
>  1.0e-10   |   131857.65595877985
>  2.5e-10   |   185479.48530059034
>  1.0e-12   |    91867.01242231275
>  2.5e-12   |   165036.9786292576
>  5.0e-12   |   204984.361034764
>  7.5e-12   |    35520.7773073109
>  1.0e-11   |   116531.9857301203
>  2.5e-11   |    64257.524312080175
>  5.0e-11   |   253059.59064874944
>  5.0e-10   |   261657.84858001219
>  7.5e-10   |   260622.88009819158
>  1.0e-09   |   265687.2263686325
>  1.0e-08   |   270029.18385391496
>  1.0e-07   |   270240.35224941676
>  1.0e-06   |   270325.2882698695
>  1.0e-05   |   270771.5448612002
> }}}
>
> Monotonous area growth is expected but oscillating behaviour alternating
> with ERRORs is observed.

New description:

 ST_Area fails to calculate the area of the geography polygon with one
 inner ring, with `use_spheroid=false`:

 {{{
 SELECT ST_Area(
 'POLYGON(
   (
     -111.772 33.307,
     -111.772 33.304,
     -111.783 33.304,
     -111.783 33.307,
     -111.772 33.307
   ),
   (
     -111.781442 33.304825,
     -111.776964 33.3048,
     -111.773047 33.304778,
     -111.773177 33.306408,
     -111.781442 33.304825
   )
 )
 '::geography, false);
 }}}

 The result is:
 {{{
 ERROR:  lwgeom_area_spher(oid) returned area < 0.0
 }}}

 `use_spheroid=true` version works and gives `270218.45392724127`, but I
 would expect the on-the-sphere version also to work.

 The polygon seems to be correct, though it has almost flat angle at the
 `(-111.776964 33.3048)` vertex. Let's call the vertex **X**.

 If **X** is removed then `ST_Area` is `270337.25718276866`.

 If we retain **X** but make a cyclic shift of the inner ring: **AXBCA** =>
 **XBCAX**, then the function succeeds but gives `91737.85992092389` which
 is quite different from the correct value.

 Let's give small amends to the **X** latitude with **AXBCA** vertex order:
 \\ **X** => `(-111.776964 (33.3048 + amend))`.

 {{{
   Amend    |        ST_Area
 -------------------------------
 -1.0e-05   |   269904.2853637372
 -1.0e-06   |   270298.26808849035
 -1.0e-07   |   270628.6930016738
 -1.0e-08   |   269541.18027120724
 -1.0e-09   |   315257.7589488026
 -1.0e-10   |   ERROR:  lwgeom_area_spher(oid) returned area < 0.0
 -1.0e-11   |   262120.12981526955
 -1.0e-12   |    41142.8857341336
 -1.0e-13   |   144916.06439020386
 -1.0e-14   |    41142.8857341336
 -1.0e-15   |   ERROR:  lwgeom_area_spher(oid) returned area < 0.0
  0.0e000   |   ERROR:  lwgeom_area_spher(oid) returned area < 0.0
  1.0e-15   |   ERROR:  lwgeom_area_spher(oid) returned area < 0.0
  1.0e-14   |   159848.6351422007
  1.5e-14   |   113982.3188750676
  5.0e-14   |   243067.1526449226
  7.5e-14   |    95027.49039558775
  1.0e-13   |    41142.8857341336
  5.0e-13   |   159848.59909125822
  7.5e-13   |   320241.96397413884
  1.0e-12   |    91867.01242231275
  2.5e-12   |   165036.9786292576
  5.0e-12   |   204984.361034764
  7.5e-12   |    35520.7773073109
  1.0e-11   |   116531.9857301203
  2.5e-11   |    64257.524312080175
  5.0e-11   |   253059.59064874944
  1.0e-10   |   131857.65595877985
  5.0e-10   |   261657.88463095468
  1.0e-09   |   265687.2263686325
  1.0e-08   |   270029.18385391496
  1.0e-07   |   270240.35224941676
  1.0e-06   |   270325.2882698695
  1.0e-05   |   270771.5448612002
 }}}

 Monotonous area growth is expected but oscillating behaviour alternating
 with ERRORs is observed.

--
-- 
Ticket URL: <https://trac.osgeo.org/postgis/ticket/5671#comment:1>
PostGIS <http://trac.osgeo.org/postgis/>
The PostGIS Trac is used for bug, enhancement & task tracking, a user and developer wiki, and a view into the subversion code repository of PostGIS project.


More information about the postgis-tickets mailing list