[postgis-users] Results of ST_DWithin missing data around 0, 0 for large request areas

Szymon Haas haasel77 at gmail.com
Tue Aug 13 09:55:32 PDT 2019

 Dear PostGIS users,

We are using PostGIS as an engine to retrieve data in GeoJSON format.
As the data is worldwide and requests concern large geographic areas we did
a choice of using "geography" data type.

The main functionality of our system is retrieving data in some "places"
(points, lines, areas), including buffers.
Thus, after several tries, we decided to use ST_DWithin function.

Recently, during the tests, we found an issue with that function.
Briefly - for very large areas (from the observations BBOX wider than 180
degrees EW) results of the ST_DWithin are missing data in a circular area
around the 0,0 point (the prime meridian and equator cross-section).

To reproduce the issue one can create a table/view with a grid of points
(lines or polygons would have the same effect) around the globe:

SELECT row_number() over() AS eid, ST_Translate(point, j, i)::geography AS
      generate_series(-89, 89) AS i,
      generate_series(-180, 179) AS j,
      (SELECT ('POINT(0 0)')::geometry AS point) AS b )

adding these indexes will speedup test queries:
CREATE INDEX tmp_points_1x1_geog
    ON tmp_points_1x1 USING gist(geog)
    TABLESPACE pg_default;

CREATE INDEX tmp_points_1x1_eid
    ON tmp_points_1x1 USING btree(eid)
    TABLESPACE pg_default;

The below test query returns in Json format data for requested geojson area
(here it's 190 degrees wide):
SELECT row_to_json(fc)
    SELECT 'FeatureCollection' AS type, array_to_json(array_agg(f)) AS
    FROM (
        SELECT 'Feature' AS type,
        ST_AsGeoJSON(lg.geog)::json AS geometry,
        row_to_json(lp) AS properties
        FROM tmp_points_1x1 AS lg
        INNER JOIN (SELECT eid
                    FROM tmp_points_1x1
                    WHERE st_dwithin(geog,
                    ) AS lp
        ON lg.eid = lp.eid
        ) AS f
    ) AS fc;

On the visualization of the output you can see the missing data:

If the request area is narrowed to <180 degrees wide, e.g. 170 degrees:
the problem of missing data doesn't appear any more:

Is it a known issue?
Are there chances for that to be resolved?
Or maybe it's not a bug (e.g. request geometries' BBOXes should not exceed
180 degrees?)

Thank you for any explanation,
Eliasz Haas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20190813/ea5336e1/attachment.html>

More information about the postgis-users mailing list