名前

ST_Intersection — (T) geomAとgeomBの共有部分を表すジオメトリを返します。ジオグラフィ実装では、インタセクトするためにジオメトリに変換して、WGS84に戻します。

概要

geometry ST_Intersection( geometry geomA , geometry geomB );

geography ST_Intersection( geography geogA , geography geogB );

説明

ジオメトリのインタセクションとなる点集合を表現するジオメトリを返します。

言い換えると、ジオメトリAとジオメトリBとで共有されている部分のことです。

ジオメトリの共有部分が無い(非接触になる)場合には、空ジオメトリコレクションが返されます。

ST_IntersectionとST_Intersectsとの併用は、バウンディングボックス、バッファ、領域のクエリ等で、対象とする国または地域にあるジオメトリの部分の返ってほしいところを切り取るのに、非常に便利です。

[注記]

ジオグラフィ: ジオメトリ実装にかぶさる、薄いラッパです。ジオグラフィオブジェクトのバウンディングボックスに最適なSRIDを決定し(UTMが望ましいですが、ランベルト正積方位図法(北/南)、最悪のシナリオでメルカトルに頼ります)、その平面でバッファを生成し、WGS84ジオグラフィに戻します。

[重要]

GEOMETRYCOLLECTIONを引数として呼ばないでください。

GEOSモジュールで実現しています。

このメソッドはSFCGALを使っても提供されます。

初出: 1.5 では、ジオグラフィ型が導入されました。

このメソッドはOpenGIS Simple Features Implementation Specification for SQL 1.1.に準拠しています。 s2.1.1.3

このメソッドはSQL/MM仕様に準拠しています。 SQL-MM 3: 5.1.18

SELECT ST_AsText(ST_Intersection('POINT(0 0)'::geometry, 'LINESTRING ( 2 0, 0 2 )'::geometry));
 st_astext
---------------
GEOMETRYCOLLECTION EMPTY
(1 row)
SELECT ST_AsText(ST_Intersection('POINT(0 0)'::geometry, 'LINESTRING ( 0 0, 0 2 )'::geometry));
 st_astext
---------------
POINT(0 0)
(1 row)

---Clip all lines (trails) by country (here we assume country geom are POLYGON or MULTIPOLYGONS)
-- NOTE: we are only keeping intersections that result in a LINESTRING or MULTILINESTRING because we don't
-- care about trails that just share a point
-- the dump is needed to expand a geometry collection into individual single MULT* parts
-- the below is fairly generic and will work for polys, etc. by just changing the where clause
SELECT clipped.gid, clipped.f_name, clipped_geom
FROM (SELECT trails.gid, trails.f_name, (ST_Dump(ST_Intersection(country.the_geom, trails.the_geom))).geom As clipped_geom
FROM country
        INNER JOIN trails
        ON ST_Intersects(country.the_geom, trails.the_geom))  As clipped
        WHERE ST_Dimension(clipped.clipped_geom) = 1 ;

--For polys e.g. polygon landmarks, you can also use the sometimes faster hack that buffering anything by 0.0
-- except a polygon results in an empty geometry collection
--(so a geometry collection containing polys, lines and points)
-- buffered by 0.0 would only leave the polygons and dissolve the collection shell
SELECT poly.gid,  ST_Multi(ST_Buffer(
                                ST_Intersection(country.the_geom, poly.the_geom),
                                0.0)
                                ) As clipped_geom
FROM country
        INNER JOIN poly
        ON ST_Intersects(country.the_geom, poly.the_geom)
        WHERE Not ST_IsEmpty(ST_Buffer(ST_Intersection(country.the_geom, poly.the_geom),0.0));
                

関連情報

ST_Difference, ST_Dimension, ST_Dump, ST_SymDifference, ST_Intersects, ST_Multi