问题描述
我正试图获取osmdata返回的非na值。例如,使用电子邮件地址。但是,以下内容返回的邮件大多为丢失的邮件。如何设置查询,使其仅返回非缺失值... value = "!null"
也不起作用。
library(osmdata)
san <- opq(bBox = 'San Jose,California') %>%
add_osm_feature(key = 'email') %>%
osmdata_sf()
df <- san$osm_points
nrow(df)
sum(!is.na(df$email))
解决方法
osmdata
软件包遵循与开放街道地图数据本身相同的层次结构。如果进一步调查数据,您将看到以下内容:
library (osmdata)
#> Data (c) OpenStreetMap contributors,ODbL 1.0. https://www.openstreetmap.org/copyright
san <- opq(bbox = 'San Jose,California') %>%
add_osm_feature(key = 'email') %>%
osmdata_sf()
df <- san$osm_points
nrow(df)
#> [1] 738
sum(!is.na(df$email))
#> [1] 39
length (which (is.na (san$osm_multipolygons$email)))
#> [1] 0
length (which (is.na (san$osm_polygons$email)))
#> [1] 3
length (which (is.na (san$osm_points$email)))
#> [1] 699
osm_multipolygons
是层次结构中的最高级别的对象,它们都具有电子邮件地址。每个多边形还包含许多多边形,并非所有多边形都必须具有电子邮件地址,因此有3个多边形没有电子邮件。默认情况下,在OSM本身和osmdata
中的点列表都包含每个上级对象的每个点,因此不可避免地会包含很多没有电子邮件地址的点。因此,您无法对每个类别中没有缺失值的对象发出查询(请参见repo issue#221中的更多信息)。
尽管如此,您仍可以通过unique_osmdata()
function获得所需的结果,该方法将每种不同类型的对象减少到仅原始调用中所要求的那些唯一值。这应该给您您想要的东西:
san_unique <- unique_osmdata (san)
length (which (is.na (san_unique$osm_multipolygons$email)))
#> [1] 0
length (which (is.na (san_unique$osm_polygons$email)))
#> [1] 0
length (which (is.na (san_unique$osm_points$email)))
#> [1] 0
san_unique <- unique_osmdata (san)
san_unique
#> Object of class 'osmdata' with:
#> $bbox : 37.124503,-122.045672,37.4692175,-121.589153
#> $overpass_call : The call submitted to the overpass API
#> $meta : metadata including timestamp and version numbers
#> $osm_points : 'sf' Simple Features Collection with 39 points
#> $osm_polygons : 'sf' Simple Features Collection with 36 polygons
#> $osm_multilines : NULL
#> $osm_multipolygons : 'sf' Simple Features Collection with 1 multipolygons
length (which (is.na (san_unique$osm_multipolygons$email)))
#> [1] 0
length (which (is.na (san_unique$osm_polygons$email)))
#> [1] 0
length (which (is.na (san_unique$osm_points$email)))
#> [1] 0