【问题标题】:How to obtain SpatialPolygonsDataframe that contains coordinates如何获取包含坐标的 SpatialPolygonsDataframe
【发布时间】:2016-10-28 15:00:42
【问题描述】:

我有一个西班牙人口普查区的 SpatialPolygonsDataFrame 和一组坐标。

我想做的是获取包含一对坐标的人口普查区域子集(作为新的 SpatialPolygonsDataFrame)。

在此示例之后,我使用“over”最接近:https://www.nceas.ucsb.edu/scicomp/usecases/point-in-polygon 但是“over”不返回 SpatialPolygonsDataFrame。 seccion 包含列“Long”和“Lat”以及坐标。 map 是 SpatialPolygonsDataFrame

# turn seccion into a SpatialPoints
coordinates(seccion) <- c("Long", "Lat")
# the coordinates are in the same lat/lon reference system (as is "map")
proj4string(seccion) <- proj4string(map)
# use 'over' with map as a SpatialPolygonsDataFrame
# object, to determine which section (if any) contains the coordinates
sec <- over(seccion, map)

我尝试了其他方法,也查看了光栅库,但直到现在,都没有运气......

【问题讨论】:

    标签: r gis


    【解决方案1】:

    您可以使用raster 包中的intersect 函数

    示例数据:

    library(raster)
    pols <- shapefile(system.file("external/lux.shp", package="raster"))
    pnts <- SpatialPoints(matrix(c(6, 5.91, 6.13, 6.23, 6.18, 
                           50.08, 49.95, 49.63, 49.64, 49.59),
                           ncol=2), proj4string=crs(pols))
    
    x <- intersect(pols, pnts)
    

    查看结果:

    plot(pols, col='light gray')
    plot(x, col='blue', add=TRUE)
    points(pnts, pch=20, col='red')
    

    可以通过over 到达那里(注意参数的顺序)

    v <- over(pols, pnts)
    xx <- pols[which(!is.na(v)), ]
    

    【讨论】:

      猜你喜欢
      • 2015-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-27
      • 2011-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多