Polygon intersection and deduplication in postgis:
create or replace function difference(geom1 geometry , geom2 geometry ) returns geometry as $$ declare begin if st_intersects(geom1 , geom2) then return st_difference(geom1 , geom2); else return geom1 ; end if; end; $$ language plpgsql ;
This function judges whether the second parameter and the first parameter overlap. If there is overlap, the first parameter is used as the template to remove the overlapped part. If there is no overlap, the first parameter is returned.
postgis, returns a rectangle based on two latitude and longitude:
create or replace function points_to_polygon(lon1 numeric , lat1 numeric , lon2 numeric , lat2 numeric ) returns geometry as $$ select st_geomfromtext('POLYGON((' || lon1 || ' ' || lat1 || ',' || lon2 || ' ' || lat1 || ',' || lon2 || ' ' || lat2 || ',' || lon1 || ' ' || lat2 || ',' || lon1 || '' || lat1 || '))', 4326); $$ language sql immutable ;