if 语句中的 st_intersects 在不满足 if 语句时运行

问题描述 投票:0回答:1

我希望此函数将美国州名转换为大写州缩写。如果(且仅当!)名称不容易转换,我希望它使用纬度/经度来查找它与哪个州相交。

library(tidyverse)
library(sf)

# Import state data to check state names
state.sf <- st_as_sf(maps::map("state", fill=TRUE, plot =FALSE)) %>%
  mutate(state = str_to_title(ID),
         short.state.name = state.abb[match(state, state.name)]) 


# Correct state names
state_name_corrector <- function(State, Lat, Long) {
  
  
  if(State %in% state.abb){
    return(State)
  }else{
    # If the State name is spelled out, find the abbreivation
    state.abbreviation <- state.abb[which(state.name == State)]
    
    # If the spelled out name isn't found, just use the lat/long
    if(length(state.abbreviation) == 0 & !is.na(Lat) & !is.na(Long)){
      sf.for.intersects <- st_as_sf(data.frame(Lat, Long),
                                    coords = c("Long", "Lat"),
                                    crs = st_crs(state.sf))
      sf_use_s2(FALSE)
      intersected_state <- state.sf %>%
        slice(which(st_intersects(sf.for.intersects, state.sf, sparse = FALSE))) %>%
        pull(short.state.name)
      
      # Return the state abbreviation found via lat/long
      if (length(intersected_state) > 0) {
        return(intersected_state)
      } else {
        return(NA)
      }
      
    } else if (length(state.abbreviation) > 0){
      return(state.abbreviation)
    }
    
    # If there's no state and no lat/long, just give up
    return(NA)
    }
  }
  

# test data
test.df <- data.frame(state = c("al", "FL", "NA", "GA"),
                      Lat = c(32.1, 30.2, 30, 30),
                      Long = c(-87, -82, -82, -82))


mapply(state_name_corrector, test.df$state, test.df$Lat, test.df$Long)

这给出了四个相同的警告:

although coordinates are longitude/latitude, st_intersects assumes that they are planar

表明测试数据的所有四行都经过了

st_intersects
,尽管只有两行的
state
名称不在
state.abb
中。

为什么

st_intersects
被评估四次?

r if-statement geospatial
1个回答
0
投票

你的假设是不正确的。跑步:

st_intersects(sf.for.intersects, state.sf, sparse = FALSE)

生成两个警告。

这很容易看出:

state_name_corrector("al", 1, 1)
although coordinates are longitude/latitude, st_intersects assumes that they are planar
although coordinates are longitude/latitude, st_intersects assumes that they are planar
© www.soinside.com 2019 - 2024. All rights reserved.