[postgis-tickets] [PostGIS] #2951: ST_Centroid for geography

PostGIS trac at osgeo.org
Fri Aug 4 12:59:01 PDT 2017


#2951: ST_Centroid for geography
--------------------------+---------------------------
  Reporter:  Mike Toews   |      Owner:  robe
      Type:  enhancement  |     Status:  new
  Priority:  medium       |  Milestone:  PostGIS 2.4.0
 Component:  liblwgeom    |    Version:
Resolution:               |   Keywords:
--------------------------+---------------------------

Comment (by robe):

 I think there is some memory pollution issue left at least with the
 multipolygon centroid checks.  I also think the calc for multipolygon may
 be a little off, but I'd have to do further spot checks to be sure.

 Attached is a table dump of geometries that centroid seems enough from
 what I would expect.


 I'm testing this on PostgreSQL 10beta2 and haven't ruled out it's not a 10
 specific bug.

 The thing that most troubles me is the memory pollution.


 {{{
 SELECT id, ST_AsText(ST_Centroid(geog)) AS geog_cent,
 ST_AsText(ST_Transform(ST_Centroid(ST_Transform(geog::geometry,26986)),4326))
 AS sm_cent,
 ST_Distance(ST_Transform(ST_Centroid(ST_Transform(geog::geometry,26986)),4326)::geography,
 ST_Centroid(geog)) As dist_disp
 FROM geog_cent_poly_prob
 WHERE id IN( 49);
 }}}

 yields this:


 {{{
  id |                 geog_cent                 |                  sm_cent
 |    dist_disp
 ----+-------------------------------------------+-------------------------------------------+-----------------
  49 | POINT(-71.0758016600248 42.4106304684328) | POINT(-71.0757460620397
 42.4107333642771) | 8466479.12566751
 (1 row)
 }}}


 I'm guessing the first call to ST_Centroid yields the right answer (those
 the as text looks okay), but the second call where I stuff it in a
 distance check gives some huge number presumably because it computed
 something crazy for the centroid.

 Now something different happens if I run my test with more than one
 record:


 {{{
 SELECT id, ST_AsText(ST_Centroid(geog)) AS geog_cent,
 ST_AsText(ST_Transform(ST_Centroid(ST_Transform(geog::geometry,26986)),4326))
 AS sm_cent,
 ST_Distance(ST_Transform(ST_Centroid(ST_Transform(geog::geometry,26986)),4326)::geography,
 ST_Centroid(geog)) As dist_disp
 FROM geog_cent_poly_prob;
  id |                 geog_cent                 |                  sm_cent
 |  dist_disp
 ----+-------------------------------------------+-------------------------------------------+--------------
   1 | POINT(-71.0703129986404 42.3452999176663) | POINT(-71.0703048387431
 42.3453017868431) |    0.7037137
   5 | POINT(-71.037584398837 42.336384333772)   | POINT(-71.0428341568715
 42.3388586422701) | 512.55960119
  49 | POINT(-71.0758046647449 42.4106309813902) | POINT(-71.0757460620397
 42.4107333642771) |          NaN
 (3 rows)
 }}}

 I get NaN.  If I run in pgAdmin instead of PSSQL, I've seen geog_cent
 return a POINT(0...).

 To prove it's the ST_Centroid,


 {{{
 SELECT id, ST_AsText(ST_Centroid(geog)) AS geog_cent,
 ST_AsText(ST_Centroid(geog)) As geog_cent2
 FROM geog_cent_poly_prob;
  id |                 geog_cent                 |
 geog_cent2
 ----+-------------------------------------------+-------------------------------------------
   1 | POINT(-71.0703129986404 42.3452999176663) | POINT(-71.0703129986404
 42.3452999176663)
   5 | POINT(-71.037584398837 42.336384333772)   | POINT(-71.037584398837
 42.336384333772)
  49 | POINT(-71.1593947384227 42.3629042590461) | POINT(-71.0945591847874
 42.3518693374157)
 (3 rows)
 }}}



 Note how the same exact query ST_AsText(ST_Centroid(...)) gives two
 different answers for the id=49 case.

 I've attached the sample table.

--
Ticket URL: <https://trac.osgeo.org/postgis/ticket/2951#comment:18>
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