在查询结果中将 MULTILINESTRING 转换为 LINESTRING
Convert MULTILINESTRING to LINESTRING in query result
我有一个 roadfollowing
table,包含列
COLUMN geom geometry(LineString,4326);
我还有一个 sub_polygon
table,包含一列
COLUMN geom geometry(MultiPolygon,4326);
我想从 roadfollowing
table 中存储的线串中减去 sub_polygon
中存储的多边形,并用这些新数据更新 table。
我尝试执行以下查询:
WITH RESULTS as (
SELECT ST_Difference(public.roadnetwork.geom, public.sub_polygon.geom)
FROM public.roadnetwork, public.sub_polygon
),
FILTERED_RESULTS as (
SELECT RESULTS.st_difference FROM RESULTS where GeometryType(RESULTS.st_difference) <> 'GEOMETRYCOLLECTION'
)
UPDATE public.roadnetwork
SET geom = FILTERED_RESULTS.st_difference
FROM FILTERED_RESULTS;
但我收到以下错误:
ERROR: Geometry type (MultiLineString) does not match column type (LineString)
我修改了查询以检查字符串格式的结果:
WITH RESULTS as (
SELECT ST_Difference(public.roadnetwork.geom, public.sub_polygon.geom)
FROM public.roadnetwork, public.sub_polygon
),
FILTERED_RESULTS as (
SELECT ST_AsText(RESULTS.st_difference) FROM RESULTS where GeometryType(RESULTS.st_difference) <> 'GEOMETRYCOLLECTION'
)
SELECT * from FILTERED_RESULTS;
而且我可以看到结果中有一些MULTILINESTRING
,无法复制到roadnetwork.geom
列,因为数据不一致:
...
MULTILINESTRING((51.5054201 25.3462475,51.505411 25.3462656,51.5052981 25.3464467,51.5051894 25.3466039,51.5049763 25.3469023,51.5048058 25.347141,51.5046538 25.347324,51.5044476 25.3475493,51.5041983 25.3478035,51.5038722 25.3481104,51.5035605 25.3483885,51.509695 25.3489269,51.5026179 25.3492445,51.5022888 25.349556),(51.5022888 25.349556,51.5022898 25.3495551),(51.5022888 25.349556,51.5017303 25.3500517))
LINESTRING(51.5017303 25.3500517,51.5014725 25.3502989,51.5013472 25.3504121)
LINESTRING(51.5013472 25.3504121,51.501175 25.3505679)
...
如何更新我的查询以将 MULTILINESTRING
转换为 LINESTRING
以便我可以成功更新我的 table?
您可以使用 st_dump 将 MultiLineStrings 扩展为 LineStrings。
像这样
WITH RESULTS as (
SELECT ST_Difference(public.roadnetwork.geom, public.sub_polygon.geom)
FROM public.roadnetwork, public.sub_polygon
),
FILTERED_RESULTS as (
SELECT RESULTS.st_difference FROM RESULTS where GeometryType(RESULTS.st_difference) <> 'GEOMETRYCOLLECTION'
),
expanded_results as (
select (a.p_geom).geom
from (SELECT ST_Dump(FILTERED_RESULTS.st_difference))
),
SELECT * from expanded_results;
我有一个 roadfollowing
table,包含列
COLUMN geom geometry(LineString,4326);
我还有一个 sub_polygon
table,包含一列
COLUMN geom geometry(MultiPolygon,4326);
我想从 roadfollowing
table 中存储的线串中减去 sub_polygon
中存储的多边形,并用这些新数据更新 table。
我尝试执行以下查询:
WITH RESULTS as (
SELECT ST_Difference(public.roadnetwork.geom, public.sub_polygon.geom)
FROM public.roadnetwork, public.sub_polygon
),
FILTERED_RESULTS as (
SELECT RESULTS.st_difference FROM RESULTS where GeometryType(RESULTS.st_difference) <> 'GEOMETRYCOLLECTION'
)
UPDATE public.roadnetwork
SET geom = FILTERED_RESULTS.st_difference
FROM FILTERED_RESULTS;
但我收到以下错误:
ERROR: Geometry type (MultiLineString) does not match column type (LineString)
我修改了查询以检查字符串格式的结果:
WITH RESULTS as (
SELECT ST_Difference(public.roadnetwork.geom, public.sub_polygon.geom)
FROM public.roadnetwork, public.sub_polygon
),
FILTERED_RESULTS as (
SELECT ST_AsText(RESULTS.st_difference) FROM RESULTS where GeometryType(RESULTS.st_difference) <> 'GEOMETRYCOLLECTION'
)
SELECT * from FILTERED_RESULTS;
而且我可以看到结果中有一些MULTILINESTRING
,无法复制到roadnetwork.geom
列,因为数据不一致:
...
MULTILINESTRING((51.5054201 25.3462475,51.505411 25.3462656,51.5052981 25.3464467,51.5051894 25.3466039,51.5049763 25.3469023,51.5048058 25.347141,51.5046538 25.347324,51.5044476 25.3475493,51.5041983 25.3478035,51.5038722 25.3481104,51.5035605 25.3483885,51.509695 25.3489269,51.5026179 25.3492445,51.5022888 25.349556),(51.5022888 25.349556,51.5022898 25.3495551),(51.5022888 25.349556,51.5017303 25.3500517))
LINESTRING(51.5017303 25.3500517,51.5014725 25.3502989,51.5013472 25.3504121)
LINESTRING(51.5013472 25.3504121,51.501175 25.3505679)
...
如何更新我的查询以将 MULTILINESTRING
转换为 LINESTRING
以便我可以成功更新我的 table?
您可以使用 st_dump 将 MultiLineStrings 扩展为 LineStrings。
像这样
WITH RESULTS as (
SELECT ST_Difference(public.roadnetwork.geom, public.sub_polygon.geom)
FROM public.roadnetwork, public.sub_polygon
),
FILTERED_RESULTS as (
SELECT RESULTS.st_difference FROM RESULTS where GeometryType(RESULTS.st_difference) <> 'GEOMETRYCOLLECTION'
),
expanded_results as (
select (a.p_geom).geom
from (SELECT ST_Dump(FILTERED_RESULTS.st_difference))
),
SELECT * from expanded_results;