Skip to content

Instantly share code, notes, and snippets.

@soiqualang
Created November 13, 2019 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soiqualang/8568cf25b9fe19072a432284c2a12b77 to your computer and use it in GitHub Desktop.
Save soiqualang/8568cf25b9fe19072a432284c2a12b77 to your computer and use it in GitHub Desktop.
PostGIS - Spatial query: #st_intersection #st_difference #st_union #st_symdifference
POLYGON((104.91514205932 19.444427490235, 104.51963424682 19.224700927735, 104.53062057495 18.73031616211, 104.93711471557 18.367767333985, 105.77207565307 18.499603271485, 105.58530807495 19.070892333985, 104.91514205932 19.444427490235))
POLYGON((105.22275924682 19.74105834961, 105.06895065307 19.070892333985, 105.59629440307 18.301849365235, 106.29941940307 18.477630615235, 106.38731002807 18.90609741211, 105.57432174682 19.76303100586, 105.22275924682 19.74105834961))
LINESTRING(104.23398971557 18.86215209961, 106.49717330932 19.356536865235)
POINT(105.42051315307 18.873138427735)
--st_intersection
select st_intersection(GeomFromEWKT('SRID=4326;POLYGON((104.91514205932 19.444427490235, 104.51963424682 19.224700927735, 104.53062057495 18.73031616211, 104.93711471557 18.367767333985, 105.77207565307 18.499603271485, 105.58530807495 19.070892333985, 104.91514205932 19.444427490235))'),GeomFromEWKT('SRID=4326;POLYGON((105.22275924682 19.74105834961, 105.06895065307 19.070892333985, 105.59629440307 18.301849365235, 106.29941940307 18.477630615235, 106.38731002807 18.90609741211, 105.57432174682 19.76303100586, 105.22275924682 19.74105834961))')) as the_geom, 1 as id
--st_difference
select st_difference(GeomFromEWKT('SRID=4326;POLYGON((104.91514205932 19.444427490235, 104.51963424682 19.224700927735, 104.53062057495 18.73031616211, 104.93711471557 18.367767333985, 105.77207565307 18.499603271485, 105.58530807495 19.070892333985, 104.91514205932 19.444427490235))'),GeomFromEWKT('SRID=4326;POLYGON((105.22275924682 19.74105834961, 105.06895065307 19.070892333985, 105.59629440307 18.301849365235, 106.29941940307 18.477630615235, 106.38731002807 18.90609741211, 105.57432174682 19.76303100586, 105.22275924682 19.74105834961))')) as the_geom, 1 as id
--st_union
select st_union(GeomFromEWKT('SRID=4326;POLYGON((104.91514205932 19.444427490235, 104.51963424682 19.224700927735, 104.53062057495 18.73031616211, 104.93711471557 18.367767333985, 105.77207565307 18.499603271485, 105.58530807495 19.070892333985, 104.91514205932 19.444427490235))'),GeomFromEWKT('SRID=4326;POLYGON((105.22275924682 19.74105834961, 105.06895065307 19.070892333985, 105.59629440307 18.301849365235, 106.29941940307 18.477630615235, 106.38731002807 18.90609741211, 105.57432174682 19.76303100586, 105.22275924682 19.74105834961))')) as the_geom, 1 as id
--st_symdifference
select st_symdifference(GeomFromEWKT('SRID=4326;POLYGON((104.91514205932 19.444427490235, 104.51963424682 19.224700927735, 104.53062057495 18.73031616211, 104.93711471557 18.367767333985, 105.77207565307 18.499603271485, 105.58530807495 19.070892333985, 104.91514205932 19.444427490235))'),GeomFromEWKT('SRID=4326;POLYGON((105.22275924682 19.74105834961, 105.06895065307 19.070892333985, 105.59629440307 18.301849365235, 106.29941940307 18.477630615235, 106.38731002807 18.90609741211, 105.57432174682 19.76303100586, 105.22275924682 19.74105834961))')) as the_geom, 1 as id
---------------------------------------
SELECT st_geomfromtext(ST_AsText((ST_Dump(ST_Split(circle, line))).geom)) As wkt
FROM (SELECT
ST_MakeLine(ST_MakePoint(10, 10),ST_MakePoint(190, 190)) As line,
ST_Buffer(ST_GeomFromText('POINT(100 90)'), 50) As circle) As foo
SELECT st_geomfromtext(ST_AsText((ST_Dump(ST_Split(circle, line))).geom)) As wkt
FROM (SELECT
GeomFromEWKT('SRID=4326;LINESTRING(104.23398971557 18.86215209961, 106.49717330932 19.356536865235)') As line,
GeomFromEWKT('SRID=4326;POLYGON((104.91514205932 19.444427490235, 104.51963424682 19.224700927735, 104.53062057495 18.73031616211, 104.93711471557 18.367767333985, 105.77207565307 18.499603271485, 105.58530807495 19.070892333985, 104.91514205932 19.444427490235))') As circle) As foo
SELECT st_geomfromtext(ST_AsText((ST_Dump(ST_Split(GeomFromEWKT('SRID=4326;POLYGON((104.91514205932 19.444427490235, 104.51963424682 19.224700927735, 104.53062057495 18.73031616211, 104.93711471557 18.367767333985, 105.77207565307 18.499603271485, 105.58530807495 19.070892333985, 104.91514205932 19.444427490235))'), GeomFromEWKT('SRID=4326;LINESTRING(104.23398971557 18.86215209961, 106.49717330932 19.356536865235)')))).geom)) As wkt
----------------------------
SELECT (ST_Dump(ST_Split(GeomFromEWKT('SRID=4326;POLYGON((104.91514205932 19.444427490235, 104.51963424682 19.224700927735, 104.53062057495 18.73031616211, 104.93711471557 18.367767333985, 105.77207565307 18.499603271485, 105.58530807495 19.070892333985, 104.91514205932 19.444427490235))'), GeomFromEWKT('SRID=4326;LINESTRING(104.23398971557 18.86215209961, 106.49717330932 19.356536865235)')))).geom
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment