在R中,如何在缓冲点的空间多边形和人口普查区块的空间多边形上使用gIntersection?
目标:我想查找居住在纽约市每个高级中心800米半径范围内的人数。
在下面的spatialpolygon A中,我有250个点,每个高级中心一个,每个点周围有800米的缓冲区和属性数据。
class : SpatialPolygons
features : 255
extent : 921834.7, 1058046, 124227.6, 269527.5 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=lcc +lat_1=41.03333333333333 +lat_2=40.66666666666666 +lat_0=40.16666666666666 +lon_0=-74 +x_0=300000.0000000001 +y_0=0 +datum=NAD83 +units=us-ft +no_defs +ellps=GRS80 +towgs84=0,0,0
在下面的另一个空间多边形B中,我为每个人口普查区域提供了长篇大论,每个人口普查区块都有一个独特的GEOID,以及其他属性数据,包括人口数据。
class : SpatialPolygons
features : 39148
extent : 921834.7, 1067384, 125227.6, 273618 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=lcc +lat_1=41.03333333333333 +lat_2=40.66666666666666 +lat_0=40.16666666666666 +lon_0=-74 +x_0=300000.0000000001 +y_0=0 +datum=NAD83 +units=us-ft +no_defs +ellps=GRS80 +towgs84=0,0,0
如何在A的空间多边形中的每个缓冲区中找到空间多边形B中的GEOID?这是我心中的匹配问题。放下另一个我希望有一个data.frame,如下所示:
Generic GEOIDs from B Buffernumberfrom A
1234567 1
7654321 1
1726354 1
7162534 2
0987655 1
0891236 1
我的代码如下所示,您可以看到我使用gIntersection打墙。无论出于何种原因,我都无法使用over函数来生成除NULL之外的值。我认为这是因为范围不同所以我使它们相同。但这没有任何帮助。
我非常感谢您在解决这一挑战时提出的任何建议。
提前致谢。
我知道,可能有更多的库而不是我需要的但我在某些时候将它们全部装入以查看是否有任何帮助。
library(ggplot2)
library(maptools)
library(rgdal)
require(rgdal)
library(plyr)
library(raster)
library(shapefiles)
library(gridExtra)
library("spatialEco")
library(rgeos)
library(maps)
require("sp")
require("raster")
set projection
projx <- "+proj=lcc +lat_1=41.03333333333333 +lat_2=40.66666666666666 +lat_0=40.16666666666666 +lon_0=-74 +x_0=300000.0000000001 +y_0=0 +datum=NAD83 +units=us-ft +no_defs"
load in shapefile with demographic data by census block
setwd("/Users/Documents/cityblocks")
xx <- readShapePoly("/Users/Astro/Documents/cityblocks.shp", IDvar="GEOID10", proj4string=CRS(projx))
plot(xx)
read in csv that has the lat/long for the senior centers along with other data on each center
setwd("/Users/Documents")
another3 <-read.csv("fullscdata.csv")
use the same projection for the senior centers as I have for the census blocks in the shapefile
map <- data.frame(project(cbind(another3$Long, another3$Lat), projx), another3)
create coordinates to be projected
coordinates(map) <- c("X1", "X2")
create buffer
bf <- gBuffer(map, width = 800, byid=TRUE, quadsegs=24)
see what buffer looks like
plot(bf)
head(bf)
set projection string for bf sam as xx
proj4string(bf) <- CRS(proj4string(xx))
gIntersects(as(bf, 'SpatialPolygons'), xx)
gIntersects(as(xx, 'SpatialPolygons'), bf)
tests indicated that the census blocks are not in the buffers or vice versa. compare xx and bf. is it saying bf not in the city because the extents are different?
xx
bf
使用范围和geom类型搞乱,看看是否有效。我认为over函数没有计算值bc,范围是不同的,并且bc这两个对象没有被存储为多边形。但是,使范围相同并使它们成为两个多边形确实得到了over函数来生成值。 我也想过,人口普查区块的多边形范围可能只需要大于区域
表示缓冲区的多边形。可悲的是,这不是一种补救措施。
ob <- SpatialPolygons(bf@polygons,proj4string=bf@proj4string)
ob1 <- SpatialPolygons(xx@polygons,proj4string=xx@proj4string)
proj4string(ob) <- CRS(proj4string(ob1))
ob1@bbox <- borobounds@bbox
ob1@bbox <- ob@bbox
x <- c(921834.7, 1067384)
y <- c(125227.6, 273618)
xy <- cbind(x,y)
S <-SpatialPoints(xy)
ob1@bbox <- bbox(S)
borobounds <- readShapePoly("/Users/Documents/bb/nybbwi.shp", IDvar="BoroCode", proj4string=CRS(projx))
check to see if census blocks are within the the buffers
gIntersects(as(ob1, 'SpatialPolygons'), ob)
检查缓冲区是否在人口普查区块内
gIntersects(as(ob1, 'SpatialPolygons'), ob)
borobounds <- readShapePoly("/Users/Astro/Documents/bb/nybbwi.shp", IDvar="BoroCode", proj4string=CRS(projx))
at some points I made a raster of ob1 but that didn't work either.
I thought ggplotting them might provide some incite to what is wrong with the code. Something is wrong bc the census blocks
and buffered zones don't plot together even though they have the same coord ref and same extents.
ggplot() +
geom_polygon(data=ob, aes(x = long, y = lat, group=group), colour="yellow", size = 0.2) +
ggplot() +
geom_polygon(data=ob1, aes(x = long, y= lat, group=group, fill="NA"), colour="blue", size = 0.2)
这是我写gIntersection的方式。当我第一次尝试使用gIntersection时,我没有使用drop_lower_td = TRUE bc, 我收到一个错误,表明我应该使用它。
clip <- gIntersection(bf, xx, byid=TRUE, drop_lower_td=TRUE)