代码之家  ›  专栏  ›  技术社区  ›  John L. Godlee

基于空间多边形数据帧位置的颜色点

  •  0
  • John L. Godlee  · 技术社区  · 7 年前

    我从这里下载了一个大的SpatialPolygonsDataFrame:

    http://omap.africanmarineatlas.org/BIOSPHERE/pages/3_terrestrial%20vegetation.htm

    我使用以下内容创建了SpatialPolygonsDataFrame:

    white_veg <- readOGR(dsn="data", 
                         layer="Whites vegetation")
    

    我有一个单独的数据帧,看起来像这样:

    id <- c(1,2,3,4,5,6,7,8)
    lat <- c(13.8, 13.552, 13.381, -15.440, -15.860, 13.967, -27.750, -27.750)
    lon <- c(2.250, 2.687, 2.865, 23.250, 23.340, 30.527, 21.420, 21.420)
    x <- c(566, 537, 554, 879, 811, 268, 216, 216)
    y <- c(8, 10.32, 3.83, 64.8, 53.7, 4, 5.8, 2.2)
    
    locations <- data.frame(id, lat, lon, x, y)
    

    我想在中创建一个新列 locations white_veg lat + lon 坐标在内部,例如 white_veg@data$DESCRIPTIO .

    我读过 a question 在…上 gcontains() rgeos 包,但这只返回长度为1的逻辑向量,我不确定这表示什么:

    locations_coords <- data.frame(locations$lat, locations$lon)
    locations_spoints <- SpatialPoints(locations_coords,proj4string=CRS(proj4string(white_veg)))
    gContains(white_veg, locations_spoints)
    

    this on the GIS SE ,使用 over sp 但答案不够透彻,我无法理解。

    1 回复  |  直到 7 年前
        1
  •  0
  •   John L. Godlee    7 年前

    over sp 包装如下:

    locations_veg_class <- locations %>% 
    mutate(veg_class = over(locations_spoints, white_veg)$DESCRIPTIO)
    

    哪里 locations_spoints 是我在问题中创建的空间点对象。