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

问题描述

here my R code link about coords of municipalita dataset with coords of stops

我的R代码有问题,因为我有一个错误,并且不知道如何解决

我有2个数据集:

  1. 城市公交车站的网络(每个车站都有经度和纬度),然后导入数据集st_as_sf
  2. 城市之乡。我已经使用sf库导入了shapefile,该库的格式与止损数据集相同。

我得到的是:

st_geos_binop(“ intersects”,x,y,稀疏=稀疏,prepared =准备,错误: st_crs(x)== st_crs(y)不正确

这是我的代码

# 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)

解决方法

在无法访问数据的情况下很难100%确定,但是似乎您没有为valori_unici_sp对象分配CRS(或者,如果您没有这样做,则不是EPSG:4326 shapefile)。

请注意:

  • 在进行多边形点操作时,两个对象都需要在同一个CRS中(在我的情况下为4326),但是只要两个对象都相同,任何方法都可以。这种不匹配是您问题的根本原因
  • 我建议您使用sf::st_join()通过设置left = FALSE将点链接到多边形,这意味着联接操作将不是单面的(在SQL语言中为左联接),而是过滤(在SQL中的内部联接)说话)

有关可解决您的用例的示例,请考虑以下代码;它使用nc.shp shapefile(在所有{sf}安装中都可用)和三个半随机的北卡罗来纳州城市。然后将这些引用链接到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)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...