【问题标题】:How to add Hawaii and Alaska to spatial polygons in R?如何将夏威夷和阿拉斯加添加到 R 中的空间多边形?
【发布时间】:2015-02-09 23:18:25
【问题描述】:

如何将夏威夷和阿拉斯加添加到以下代码(取自 Josh O'Brien 在此处的回答:Latitude Longitude Coordinates to State Code in R)?

library(sp)
library(maps)
library(maptools)

# The single argument to this function, pointsDF, is a data.frame in which:
#   - column 1 contains the longitude in degrees (negative in the US)
#   - column 2 contains the latitude in degrees

latlong2state <- function(pointsDF) {
    # Prepare SpatialPolygons object with one SpatialPolygon
    # per state (plus DC, minus HI & AK)
    states <- map('state', fill=TRUE, col="transparent", plot=FALSE)
    IDs <- sapply(strsplit(states$names, ":"), function(x) x[1])
    states_sp <- map2SpatialPolygons(states, IDs=IDs,
                     proj4string=CRS("+proj=longlat +datum=wgs84"))

    # Convert pointsDF to a SpatialPoints object 
    pointsSP <- SpatialPoints(pointsDF, 
                    proj4string=CRS("+proj=longlat +datum=wgs84"))

    # Use 'over' to get _indices_ of the Polygons object containing each point 
    indices <- over(pointsSP, states_sp)

    # Return the state names of the Polygons object containing each point
    stateNames <- sapply(states_sp@polygons, function(x) x@ID)
    stateNames[indices]
}

# Test the function using points in Alaska (ak) and Hawaii (hi)

ak <- data.frame(lon = c(-151.0074), lat = c(63.0694))
hi <- data.frame(lon = c(-157.8583), lat = c(21.30694))
nc <- data.frame(lon = c(-77.335), lat = c(34.671))


latlong2state(ak)
latlong2state(hi)
latlong2state(nc)

latlong2state(ak)latlong2state(hi) 代码返回 NA,但如果代码修改正确,阿拉斯加和夏威夷将作为结果返回。

感谢任何帮助!

【问题讨论】:

    标签: r maps gis maptools sp


    【解决方案1】:

    您需要使用包含 50 个州的地图,您使用 states &lt;- map('state', fill=TRUE, col="transparent", plot=FALSE) 加载的地图没有夏威夷和阿拉斯加。

    例如,您可以从here 下载 20m 美国地图,然后将其解压缩到您的当前目录中。然后,您的 R 当前目录中应该有一个名为 cb_2013_us_state_5m 的文件夹。

    我已经对你发布的代码进行了一些调整,在夏威夷和阿尔萨卡工作过,没有尝试过其他国家。

    library(sp)
    library(rgeos)
    library(rgdal)
    
    # The single argument to this function, pointsDF, is a data.frame in which:
    #   - column 1 contains the longitude in degrees (negative in the US)
    #   - column 2 contains the latitude in degrees
    
    latlong2state <- function(pointsDF) {
      states <-readOGR(dsn='cb_2013_us_state_5m',layer='cb_2013_us_state_5m')
      states <- spTransform(states, CRS("+proj=longlat"))
    
      pointsSP <- SpatialPoints(pointsDF,proj4string=CRS("+proj=longlat"))
    
      # Use 'over' to get _indices_ of the Polygons object containing each point 
      indices <- over(pointsSP, states)
      indices$NAME
    }
    
    # Test the function using points in Alaska (ak) and Hawaii (hi)
    
    ak <- data.frame(lon = c(-151.0074), lat = c(63.0694))
    hi <- data.frame(lon = c(-157.8583), lat = c(21.30694))
    
    latlong2state(ak)
    latlong2state(hi)
    

    【讨论】:

    • 很好 - 感谢您的帮助!我不得不将states 的第一个引用修改为states50,然后它就起作用了。我已经相应地编辑了上面的代码,但如果我有什么误解,请随时回复。
    • 这对我不起作用——也许你忘记定义states50
    【解决方案2】:

    这是基于包maps 中的一个数据集,该数据集仅包含较低的 48 个。对于您的任务,它需要一个包含所有状态的 shapefile。 Census.gov 网站始终是找到这些的好地方。我对您发布的函数进行了一些更改,以便它可以与这个新的 shapefile 一起使用。

    #download a shapefile with ALL states
    tmp_dl <- tempfile()
    download.file("http://www2.census.gov/geo/tiger/GENZ2013/cb_2013_us_state_20m.zip", tmp_dl)
    unzip(tmp_dl, exdir=tempdir())
    ST <- readOGR(tempdir(), "cb_2013_us_state_20m")
    
    latlong2state <- function(pointsDF) {
        # Just copied the earlier code with some key changes
        states <- ST
    
        # Convert pointsDF to a SpatialPoints object 
        # USING THE CRS THAT MATCHES THE SHAPEFILE
        pointsCRS <- "+proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0"
        pointsSP <- SpatialPoints(pointsDF, proj4string=CRS(pointsCRS))
    
        # Use 'over' to get _indices_ of the Polygons object containing each point 
        indices <- over(pointsSP, states)
    
        # Return the state names of the Polygons object containing each point
        as.vector(indices$NAME)
    }
    

    让我们测试一下吧!

    ak <- data.frame(lon = c(-151.0074), lat = c(63.0694))
    hi <- data.frame(lon = c(-157.8583), lat = c(21.30694))
    nc <- data.frame(lon = c(-77.335), lat = c(34.671))
    
    latlong2state(ak)
    [1] "Alaska"
    
    latlong2state(hi)
    [1] "Hawaii"
    
    latlong2state(nc)
    [1] "North Carolina"
    

    【讨论】:

    • J.温彻斯特 - 感谢您的帮助。当我运行上述代码时,我收到以下信息:Error in nchar(projargs) : no method for coercing this S4 class to a vector。如果您有任何想法,请告诉我。谢谢。
    • 当我尝试从中提取投影信息时,这似乎是从 crs 函数没有将 states 识别为正确的类而上升的。我不知道你为什么得到它,因为它对我有用。但我修改了答案以解决它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    相关资源
    最近更新 更多