ST_Intersection — 計算幾何 A 和 B 的共享部分所表示的幾何。
geometry ST_Intersection(
geometry geomA , geometry geomB , float8 gridSize = -1 )
;
geography ST_Intersection(
geography geogA , geography geogB )
;
返回一個表示兩個幾何的點集交集的幾何。換句話說,幾何 A 和幾何 B 之間共享的部分。
如果幾何沒有共同點(即不相交),則返回適當類型的空原子幾何。
如果提供了可選的 gridSize
參數,則輸入會被捕捉到給定大小的網格上,並且結果頂點會在同一個網格上計算。(需要 GEOS-3.9.0 或更高版本)
ST_Intersection 與 ST_Intersects 結合使用,可用於剪裁幾何,例如在邊界框、緩衝區或區域查詢中,您只需要幾何圖形在國家或感興趣區域內的部分。
![]() |
|
|
![]() |
|
如果存在 M 坐標值,此函式將捨棄它們。 |
![]() |
|
如果使用 3D 幾何,您可能需要使用基於 SFGCAL 的 ST_3DIntersection,它對 3D 幾何進行適當的 3D 交集。儘管此函式適用於 Z 坐標,但它會對 Z 坐標進行平均。 |
由 GEOS 模組執行
增強功能:3.1.0 接受 gridSize 參數
需要 GEOS >= 3.9.0 才能使用 gridSize 參數
已變更:3.0.0 不依賴於 SFCGAL。
可用性:1.5 引入了對地理數據類型的支援。
此方法實現 OGC 簡單要素 SQL 1.1 實現規範。 s2.1.1.3
此方法實現 SQL/MM 規範。SQL-MM 3: 5.1.18
此函式支援 3D 並且不會捨棄 z-索引。但是,結果僅使用 XY 計算。結果 Z 值將被複製、平均或插值。
SELECT ST_AsText(ST_Intersection('POINT(0 0)'::geometry, 'LINESTRING ( 2 0, 0 2 )'::geometry)); st_astext --------------- GEOMETRYCOLLECTION EMPTY SELECT ST_AsText(ST_Intersection('POINT(0 0)'::geometry, 'LINESTRING ( 0 0, 0 2 )'::geometry)); st_astext --------------- POINT(0 0)
按國家/地區剪裁所有線條(步道)。這裡我們假設國家/地區幾何是 POLYGON 或 MULTIPOLYGONS。注意:我們只保留產生 LINESTRING 或 MULTILINESTRING 的交集,因為我們不關心僅共享一個點的步道。需要 dump 將幾何集合展開為單個 MULT* 部分。以下程式碼相當通用,只需更改 where 子句即可適用於多邊形等。
select clipped.gid, clipped.f_name, clipped_geom from ( select trails.gid, trails.f_name, (ST_Dump(ST_Intersection(country.geom, trails.geom))).geom clipped_geom from country inner join trails on ST_Intersects(country.geom, trails.geom) ) as clipped where ST_Dimension(clipped.clipped_geom) = 1;
對於多邊形(例如多邊形地標),您也可以使用有時更快的技巧,即除了多邊形之外,將任何內容緩衝 0.0 會導致空的幾何集合。(因此,包含以 0.0 緩衝的多邊形、線條和點的幾何集合只會留下多邊形並溶解集合外殼。)
select poly.gid, ST_Multi( ST_Buffer( ST_Intersection(country.geom, poly.geom), 0.0 ) ) clipped_geom from country inner join poly on ST_Intersects(country.geom, poly.geom) where not ST_IsEmpty(ST_Buffer(ST_Intersection(country.geom, poly.geom), 0.0));
請注意,這不是真正的交集,請比較使用 ST_3DIntersection 的相同範例。
select ST_AsText(ST_Intersection(linestring, polygon)) As wkt from ST_GeomFromText('LINESTRING Z (2 2 6,1.5 1.5 7,1 1 8,0.5 0.5 8,0 0 10)') AS linestring CROSS JOIN ST_GeomFromText('POLYGON((0 0 8, 0 1 8, 1 1 8, 1 0 8, 0 0 8))') AS polygon; st_astext --------------------------------------- LINESTRING Z (1 1 8,0.5 0.5 8,0 0 10)