PostGIS-将多面转换为单面 [英] PostGIS - convert multipolygon to single polygon
问题描述
是否可以在PostGIS中将包含多多边形的形状文件导入到单个多边形中?每当我尝试导入多边形的形状文件时,它都会作为多多边形(与单个多边形相对)存储在geom
列中.因此,我无法从multipolygon中将其提取为单个多边形值.
非常感谢所有有用的建议
您可以使用 ST_GeometryN 与 ST_NumGeometries 和generate_series函数一起获得所需的内容.
假设您有雅库布(Jakub)示例中的表格:
CREATE TABLE multi AS(
SELECT 1 as id, 2 as test, ST_GeomFromText('MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0)),((1 1,2 1,2 2,1 2,1 1)), ((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)))') AS geom
);
这个包含一个多面体,一个id和另一列.
要从表格中获取包括所有其他属性的每个多边形,请尝试以下操作:
SELECT id, test, ST_GeometryN(geom, generate_series(1, ST_NumGeometries(geom))) AS geom
FROM multi
"id"和"test"是原始表中每一行的值. generate_series在每行中创建从1到几何数的一系列数字.
因此,您将每个多重几何体拆分为单独的单个几何体部分,其他列中的值保持不变.
只需将示例中的列和表格替换为导出的shapefile中的列,即可获得包含单个多边形的表格.
希望这能回答您的问题.
Is it possible to import a shape file containing multipolygons into single polygon in PostGIS? Whenever I try importing a shape file of a polygon, it is stored as a multipolygon (as opposed to a single polygon) in a geom
column. Thus, I am unable to extract it as a single polygon value from the multipolygon.
All helpful suggestions much appreciated
You can use ST_GeometryN together with ST_NumGeometries and the generate_series function to obtain what you need.
Let's assume you have the table from Jakub's example:
CREATE TABLE multi AS(
SELECT 1 as id, 2 as test, ST_GeomFromText('MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0)),((1 1,2 1,2 2,1 2,1 1)), ((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)))') AS geom
);
This one contains a multipolygon, an id and another column.
To get each single polygon from the table including all other attributes try something like:
SELECT id, test, ST_GeometryN(geom, generate_series(1, ST_NumGeometries(geom))) AS geom
FROM multi
"id" and "test" are the values for each row in the original table. generate_series creates a series of numbers from 1 to the number of geometries in each row.
Therefore you will split each multi geometry in its separate single geometry parts and the values in the other columns remain the same.
Just replace the columns and table in the example with the columns from your exported shapefile and you will get the table with the single polygons.
Hope this answers your question.
这篇关于PostGIS-将多面转换为单面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!