简体   繁体   English

识别空间点是否在空间多边形中

[英]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 个数据集:

  1. a network of bus stops of a city (I have longitude and latitude for every stops) and I import the dataset st_as_sf .一个城市的公交车站网络(我有每个站点的经度和纬度),然后导入数据集st_as_sf
  2. township of the city.市镇。 I have imported the shapefile with the sf library---the same format as the stops dataset.我已经使用 sf 库导入了 shapefile——与停止数据集的格式相同。

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:注意:

  • when doing point-in-polygon operation both objects need to be in the same CRS - in my case 4326, but any would do as long as it is the same for both objects;在进行多边形点操作时,两个对象都需要在同一个 CRS 中——在我的例子中是 4326,但只要两个对象相同,任何对象都可以; this mismatch is the root cause of your issue这种不匹配是您问题的根本原因
  • I suggest you use 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM