[英]Identifying if a spatial point is in a spatial polygon
here my R code link about coords of municipalita dataset with coords of stops 这里我的 R 代码链接关于带有停止坐标的市政数据集的坐标
I have a problem with my R code, because I have a error and I don't know how fix it.我的 R 代码有问题,因为我有一个错误,我不知道如何解决它。
I have 2 datasets:我有 2 个数据集:
st_as_sf
.st_as_sf
。 The I'm getting is :我得到的是:
Error in st_geos_binop("intersects", x, y, sparse = sparse, prepared = prepared, : st_crs(x) == st_crs(y) is not TRUE
st_geos_binop("intersects", x, y, sparse = sparse, Prepared = Prepared, 中的错误:st_crs(x) == st_crs(y) 不是 TRUE
And here is my code:这是我的代码:
# MUNICIPALITA --------------
library("rgdal")
library("raster")
library("sf")
#in questo file provo leggere con la libreria sp e non sf i file shape
#f=system.file("C:/Users/CELESTE/Desktop/ambiti_amm/A_SCOM.shp",package = "sf")
municipalita=shapefile("C:/Users/CELESTE/Desktop/ambiti_amm/A_SCOM.shp")
municipalita_df=as.data.frame(municipalita)
municipalita_df=municipalita_df[,-c(3:5,8:20)]
point.in.polygon()
municipalita_sf=st_as_sf(municipalita, crs = 4326)
municipalita_sp=as_Spatial(municipalita_sf)
class(municipalita)
municipalita$SCOM_COD=as.numeric(municipalita$SCOM_COD)
municipalita$A_SCOM_TY=as.numeric(municipalita$A_SCOM_TY)
municipalita=subset(municipalita,municipalita[,1]==6)
m=as.data.frame(m)
municipalita=municipalita[,-1]
municipalita=municipalita[-c(2,3,5,7:9),]
m=st_as_sf(municipalita)
mappa_municipalita=mapview(m)
mappa_municipalita
colnames(m)[1]="Quartieri"
colnames(m)[2]="ID_quartiere"
colnames(m)[3]="ID_municipalita"
mapview(m, zcol = "Quartieri")
mapview(m,zcol="ID_quartiere")
mapview(m,zcol="ID_municipalita")
colnames(m$)
class(valori_unici_sp)
##########################
install.packages("spatialEco")
library("spatialEco")
pip=point.in.poly(valori_unici_sp,municipalita_sp)
It is hard to be 100% certain without having access to your data, but it seems that you did not assign a CRS to your valori_unici_sp
object (or if you did it was not EPSG:4326 like with your shapefile).在无法访问您的数据的情况下很难 100% 确定,但您似乎没有为您的
valori_unici_sp
对象分配 CRS(或者如果您分配了 CRS,则它不像您的 shapefile 那样使用 EPSG:4326)。
Note that:注意:
sf::st_join()
to link the points to polygons, with setting left = FALSE
, meaning the join operation will not be one sided (left join in SQL speak) but filtering (inner join in SQL speak)sf::st_join()
将点链接到多边形,设置left = FALSE
,这意味着连接操作不会是一侧的(SQL 中的左连接)而是过滤(SQL 中的内连接) For a reproducible example addressing your use case consider this code;对于解决您的用例的可重现示例,请考虑此代码; it uses the
nc.shp
shapefile (available in all {sf}
installations) and three semi random North Carolina cities.它使用
nc.shp
shapefile(适用于所有{sf}
安装)和三个半随机的北卡罗来纳州城市。 It then links the cites to the shapefile to get county data.然后将引用链接到 shapefile 以获取县数据。
library(sf)
# included with sf package
shape <- st_read(system.file("shape/nc.shp", package="sf")) %>%
st_transform(4326)
# three semi random cities
cities <- data.frame(name = c("Raleigh", "Greensboro", "Wilmington"),
x = c(-78.633333, -79.819444, -77.912222),
y = c(35.766667, 36.08, 34.223333)) %>%
st_as_sf(coords = c("x", "y"), crs = 4326)
# the action is here!
result <- st_join(cities, shape, left = F)
# check the structure of outcome
result
Simple feature collection with 3 features and 15 fields
geometry type: POINT
dimension: XY
bbox: xmin: -79.81944 ymin: 34.22333 xmax: -77.91222 ymax: 36.08
geographic CRS: WGS 84
name AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID BIR74 SID74 NWBIR74 BIR79 SID79
1 Raleigh 0.219 2.130 1938 1938 Wake 37183 37183 92 14484 16 4397 20857 31
2 Greensboro 0.170 1.680 1903 1903 Guilford 37081 37081 41 16184 23 5483 20543 38
3 Wilmington 0.042 0.999 2238 2238 New Hanover 37129 37129 65 5526 12 1633 6917 9
NWBIR79 geometry
1 6221 POINT (-78.63333 35.76667)
2 7089 POINT (-79.81944 36.08)
3 2100 POINT (-77.91222 34.22333)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.