Opened 5 years ago

Closed 5 years ago

#4506 closed defect (fixed)

ST_Within wrong results

Reported by: georamos Owned by: pramsey
Priority: medium Milestone: PostGIS 2.4.9
Component: postgis Version: 2.4.x
Keywords: st_within wrong result Cc:

Description

Hello all.

I have a set of points and a polygon and want to select the points that are within the area. Both layers are in the same projection (WGS84 - EPSG: 4326) and are valid.

To determine wich points are within the area i'm using ST_WITHIN but the function is returning elements that are not within an area. You can run this query to test it out:

with points as (
    select ST_Transform('0104000020E61000000100000001010000009B8BBFED094A22C0C8CA2F83315C4340'::geometry, 4326) as geom
    union
    select ST_Transform('0104000020E61000000100000001010000007C5EF1D4237522C0D0E5CDE15A614340'::geometry, 4326) as geom
), area as (
    select ST_Transform('0106000020E610000001000000010300000001000000380000009AEE75525F2E22C0E8887C9752614340EE974F560C2F22C0E529ABE97A6043400D6C9560713022C0A585CB2A6C604340A643A7E7DD3022C0FCC74274086043405378D0ECBA2F22C0F29881CAF85F4340570A815CE23022C068E6C93505604340E12879758E3122C0D09A1F7F695F43404EB7EC10FF3022C023A46E675F5F4340B456B439CE3522C0342C465D6B5D4340D9EBDD1FEF3522C03ACD02ED0E5D43403E97A949F03E22C0707CED99255B43405859DB148F4322C0469A7807785A434052651877835022C0F374AE28255A4340A05225CADE5222C0E71890BDDE594340357A3540695022C00C23BDA8DD594340E99E758D965322C097FDBAD39D5943403048FAB48A6E22C059BE2EC37F584340B03A72A4337022C0889FFF1EBC5843403A083A5AD57222C078962023A05843400DE36E10AD7522C01EFF058200594340C32B499EEB7322C02DB0C7444A594340177E703E757422C06FF607CA6D594340A4367172BF7322C0CA15DEE5225A4340D80FB1C1C27122C05AA148F7735A43401FD95C35CF7122C0EA92718C645B4340D0471971017022C086915ED4EE5B43405114E813796A22C06478EC67B15C4340F1F109D9796B22C0BF805EB8735D43402979758E016922C042791F47735E4340F2B1BB40496922C083328D26175F4340C8B3CBB73E6C22C039B35DA10F60434036ACA92C0A6B22C090BE49D3A06043403FC91D36916922C0B7973446EB604340D7BFEB33676522C0A4552DE928614340E6CFB7054B6522C0D2E5CDE15A614340CC7B9C69C26622C03D44A33B8861434005172B6A306522C002BA2F67B661434073F38DE89E6522C0BC74931804624340C5AD8218E86222C02BBEA1F0D962434068D0D03FC15D22C06FBBD05CA763434048179B560A5922C0EE26F8A6E96343407768588CBA5622C0417FA1478C644340535E2BA1BB5422C06CB41CE8A16443401171732A195022C03EB2B96A9E6543402E20B41EBE4C22C005323B8BDE654340A9C29FE1CD4A22C0DBA50D87A56543408DD2A57F494A22C0923CD7F7E164434008AF5DDA704022C0DDEEE53E396443404FAC53E57B3E22C01EFB592C4564434071395E81E83922C0202A8D98D9634340698D4127843622C0541C075E2D6343404548DDCEBE3222C0B20DDC813A63434036ACA92C0A3322C03C50A73CBA6343400EDAAB8F872E22C097361C96066443402CF180B2292F22C0FFE89B340D6243409AEE75525F2E22C0E8887C9752614340'::geometry, 4326) as geom
)
SELECT points.*, ST_Within(points.geom,area.geom), ST_IsValid(points.geom) valid_point, ST_IsValid(area.geom) valid_area
from points, area
where ST_Within(points.geom,area.geom);

Odd enough, if you test it with only one point at a time, the result is correct, but if you use both point the result is wrong.

Attachments (2)

graphical_representation.png (60.9 KB ) - added by georamos 5 years ago.
query_results.png (19.7 KB ) - added by georamos 5 years ago.

Download all attachments as: .zip

Change History (7)

by georamos, 5 years ago

by georamos, 5 years ago

Attachment: query_results.png added

comment:1 by Algunenano, 5 years ago

I'm getting correct results with trunk:

template_postgis=# Select postgis_full_version();
                                                                                                                                                                                                         postgis_full_version                
                                                                                                                                                                                         
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 POSTGIS="3.0.0alpha5dev r17804" [EXTENSION] PGSQL="120" GEOS="3.8.0dev-CAPI-1.11.0 " PROJ="6.1.0" GDAL="GDAL 3.1.0dev-d029a90b04-dirty, released 2019/08/25" LIBXML="2.9.9" LIBJSON="0.13.1" LIBPROTOBUF="1.3.1" WAGYU="0.4.3 (Internal)" (c
ore procs from "3.0.0alpha4dev r17620" need upgrade) TOPOLOGY (topology procs from "3.0.0alpha4dev r17620" need upgrade) RASTER (raster procs from "3.0.0alpha4dev r17620" need upgrade)
(1 row)

template_postgis=# with points as (
    select ST_Transform('0104000020E61000000100000001010000009B8BBFED094A22C0C8CA2F83315C4340'::geometry, 4326) as geom
    union
    select ST_Transform('0104000020E61000000100000001010000007C5EF1D4237522C0D0E5CDE15A614340'::geometry, 4326) as geom
), area as (
    select ST_Transform('0106000020E610000001000000010300000001000000380000009AEE75525F2E22C0E8887C9752614340EE974F560C2F22C0E529ABE97A6043400D6C9560713022C0A585CB2A6C604340A643A7E7DD3022C0FCC74274086043405378D0ECBA2F22C0F29881CAF85F4340570A815CE23022C068E6C93505604340E12879758E3122C0D09A1F7F695F43404EB7EC10FF3022C023A46E675F5F4340B456B439CE3522C0342C465D6B5D4340D9EBDD1FEF3522C03ACD02ED0E5D43403E97A949F03E22C0707CED99255B43405859DB148F4322C0469A7807785A434052651877835022C0F374AE28255A4340A05225CADE5222C0E71890BDDE594340357A3540695022C00C23BDA8DD594340E99E758D965322C097FDBAD39D5943403048FAB48A6E22C059BE2EC37F584340B03A72A4337022C0889FFF1EBC5843403A083A5AD57222C078962023A05843400DE36E10AD7522C01EFF058200594340C32B499EEB7322C02DB0C7444A594340177E703E757422C06FF607CA6D594340A4367172BF7322C0CA15DEE5225A4340D80FB1C1C27122C05AA148F7735A43401FD95C35CF7122C0EA92718C645B4340D0471971017022C086915ED4EE5B43405114E813796A22C06478EC67B15C4340F1F109D9796B22C0BF805EB8735D43402979758E016922C042791F47735E4340F2B1BB40496922C083328D26175F4340C8B3CBB73E6C22C039B35DA10F60434036ACA92C0A6B22C090BE49D3A06043403FC91D36916922C0B7973446EB604340D7BFEB33676522C0A4552DE928614340E6CFB7054B6522C0D2E5CDE15A614340CC7B9C69C26622C03D44A33B8861434005172B6A306522C002BA2F67B661434073F38DE89E6522C0BC74931804624340C5AD8218E86222C02BBEA1F0D962434068D0D03FC15D22C06FBBD05CA763434048179B560A5922C0EE26F8A6E96343407768588CBA5622C0417FA1478C644340535E2BA1BB5422C06CB41CE8A16443401171732A195022C03EB2B96A9E6543402E20B41EBE4C22C005323B8BDE654340A9C29FE1CD4A22C0DBA50D87A56543408DD2A57F494A22C0923CD7F7E164434008AF5DDA704022C0DDEEE53E396443404FAC53E57B3E22C01EFB592C4564434071395E81E83922C0202A8D98D9634340698D4127843622C0541C075E2D6343404548DDCEBE3222C0B20DDC813A63434036ACA92C0A3322C03C50A73CBA6343400EDAAB8F872E22C097361C96066443402CF180B2292F22C0FFE89B340D6243409AEE75525F2E22C0E8887C9752614340'::geometry, 4326) as geom
)
SELECT points.*, ST_Within(points.geom,area.geom), ST_IsValid(points.geom) valid_point, ST_IsValid(area.geom) valid_area
from points, area
;
                                 geom                                 | st_within | valid_point | valid_area 
----------------------------------------------------------------------+-----------+-------------+------------
 0104000020E61000000100000001010000009B8BBFED094A22C0C8CA2F83315C4340 | t         | t           | t
 0104000020E61000000100000001010000007C5EF1D4237522C0D0E5CDE15A614340 | f         | t           | t
(2 rows)

What postgis and GEOS version are you using?

comment:2 by georamos, 5 years ago

The versions are:

·        PostGIS Version: “POSTGIS="2.4.8 r17696" PGSQL="96" GEOS="3.7.1-CAPI-1.11.1 27a5e771" PROJ="Rel. 4.9.3, 15 August 2016" GDAL="GDAL 2.3.2, released 2018/09/21" LIBXML="2.9.1" LIBJSON="0.11" LIBPROTOBUF="1.0.2" (core procs from "2.4.7 r17330" need upgrade) RASTER (raster procs from "2.4.7 r17330" need upgrade)”
·        PostgreSQL Version: PostgreSQL 9.6.15 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36), 64-bit
·        SO Version: CentOS Linux 7 (Core) [Kernel: Linux 3.10.0-957.27.2.el7.x86_64]

comment:3 by pramsey, 5 years ago

In 17815:

Remove tolerance in point-in-ring stabline tests
References #4506

comment:4 by pramsey, 5 years ago

In 17816:

Remove tolerance in point-in-ring stabline tests
References #4506

comment:5 by pramsey, 5 years ago

Resolution: fixed
Status: newclosed

In 17817:

Remove tolerance in point-in-ring stabline tests
Closes #4506

Note: See TracTickets for help on using tickets.