简体   繁体   中英

st_within as a condition of insert

I have built myself a tracker, and as part of the spec I gave myself for security reasons, i dont want people knowing where I leave my car overnight.

SO I have a concept of exclusion zones, I have the web map only showing data outside said exclusion zones, but I also only want to save data when transmitted that isnt within an exclusion zone (there can be more than one so am thinking subquery

can anyone help?

It is possible, if not preferrable that this be a stored proc, any ideas (I am useless when it comes to subqueries hence asking)

the SQL I am using to get the data (retrospective exclusion zones) is thus

SELECT geom 
FROM public.data 
WHERE layer = %layer_id% and not exists(
     SELECT * 
     FROM public.exclusion_zone 
     WHERE layer = %layer_id% and ST_CONTAINS(the_geom, geom))

For example, this code returns all geometries (say, points) from public.data , which are not completely inside geometries (say, polygons) from public.exclusion_zone :

SELECT *
FROM public.data 
WHERE the_geom NOT IN (
    SELECT d.the_geom 
    FROM public.data d, public.exclusion_zone e
    WHERE ST_Within (d.the_geom, e.the_geom)
);

or even better (assuming that operations with integer IDs are faster than to compare geometries):

SELECT * FROM public.data
WHERE id NOT IN (
    SELECT d.id 
    FROM public.data d, public.exclusion_zone e
    WHERE ST_Within (d.the_geom, e.the_geom)
);

See more: ST_Within

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM