在R中使用st_read读取WFS时要素数重置为0的问题解决
问题解答:WFS读取无要素及提示信息解析
提示信息含义
Re-reading with feature count reset from 26598 to 0的具体含义:
- 你的WFS请求中包含
RESULTTYPE=hits参数,该参数会要求服务器仅返回匹配的要素数量(此处为26598个),不返回实际的空间要素数据。 sf::st_read()首先解析到服务器返回的要素计数26598,但后续读取实际数据时发现没有要素内容,因此将要素计数重置为0,最终返回仅包含字段结构但无要素的空集合。
解决方法
核心是移除RESULTTYPE=hits参数,让WFS服务器返回实际要素数据,以下两种方式均可实现:
方式1:直接修改请求URL
删除URL中的&RESULTTYPE=hits部分,修改后的代码如下:
url <- "https://www.ogd.stadt-zuerich.ch/wfs/geoportal/3D_Blockmodell_LoD1?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.1.0&TYPENAME=lod1_gebaeude_min_3d&BBOX=2681065.44918748084455729,1242274.55883355368860066,2684374.57787289563566446,1251789.53122574021108449,EPSG:2056" res <- sf::st_read(url)
方式2:用st_read内置参数构建请求(更规范)
推荐通过st_read的参数直接构建WFS请求,避免手动拼接URL出错:
res <- sf::st_read( dsn = "https://www.ogd.stadt-zuerich.ch/wfs/geoportal/3D_Blockmodell_LoD1", layer = "lod1_gebaeude_min_3d", query = list( SERVICE = "WFS", VERSION = "1.1.0", REQUEST = "GetFeature", BBOX = "2681065.44918748084455729,1242274.55883355368860066,2684374.57787289563566446,1251789.53122574021108449,EPSG:2056" ) )
可选:大数量要素分批读取
如果要素数量较大(此处2万+),可添加MAXFEATURES和STARTINDEX参数分批读取,避免请求超时:
total_features <- 26598 batch_size <- 5000 res_list <- list() for (start in seq(0, total_features, batch_size)) { batch <- sf::st_read( dsn = "https://www.ogd.stadt-zuerich.ch/wfs/geoportal/3D_Blockmodell_LoD1", layer = "lod1_gebaeude_min_3d", query = list( SERVICE = "WFS", VERSION = "1.1.0", REQUEST = "GetFeature", BBOX = "2681065.44918748084455729,1242274.55883355368860066,2684374.57787289563566446,1251789.53122574021108449,EPSG:2056", STARTINDEX = start, MAXFEATURES = batch_size ) ) res_list[[length(res_list)+1]] <- batch } # 合并所有批次数据 res <- do.call(rbind, res_list)
内容的提问来源于stack exchange,提问作者guessr
相关产品推荐
相关产品推荐

