Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查R中的形状文件中的点向量属于哪个区域_R_Shapefile_Ggmap_Sp_Rgdal - Fatal编程技术网

检查R中的形状文件中的点向量属于哪个区域

检查R中的形状文件中的点向量属于哪个区域,r,shapefile,ggmap,sp,rgdal,R,Shapefile,Ggmap,Sp,Rgdal,我有一个带有区域/多边形和特征的形状文件。然后,我有一个lat/long点的数据帧 我想在TIBLE中添加一列,其中包含给定点所在的区域索引 一些可复制的代码: ### SETUP library("tidyverse") library("rgdal") library("maptools") library("rgeos") library("ggmap") unzipURL <- function(url, exdir){ if(!dir.exists(exdir)){

我有一个带有区域/多边形和特征的形状文件。然后,我有一个lat/long点的数据帧

我想在TIBLE中添加一列,其中包含给定点所在的区域索引

一些可复制的代码:

### SETUP

library("tidyverse")

library("rgdal")
library("maptools")
library("rgeos")

library("ggmap")

unzipURL <- function(url, exdir){
  if(!dir.exists(exdir)){
    temp <- tempfile()
    download.file(url,temp)
    unzip(temp, exdir = exdir)
    unlink(temp)
  }
  return(exdir)
}

### DATA INGESTION

# Municipalities in Milan

municipi.mi.url <- "https://geoportale.comune.milano.it/Download/area_download/SIT/Municipi/Municipi_RDN2008.zip"

municipi.shp.path <- unzipURL(municipi.mi.url, exdir = file.path(".", "Municipi", criterion = is_git_root))

municipi.mi <- readOGR(dsn = municipi.shp.path, layer = "Municipi")
municipi.mi <- spTransform(municipi.mi,"+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")

# ARPA CONTROL STATIONS

centraline.mi.url <- "http://dati.comune.milano.it/dataset/d6960c75-0a02-4fda-a85f-3b1c4aa725d6/resource/59b3476e-9081-4601-a95a-a64925da3af5/download/qaria_stazione_shp.zip"

centraline.shp.path <- unzipURL(centraline.mi.url, exdir = file.path(".", "Centraline", criterion = is_git_root))

centraline.mi <- readOGR(dsn = centraline.shp.path, layer = "qaria_stazione")

### DATA PREP

municipi.mi@data$id = rownames(municipi.mi@data)
municipi.mi.points = fortify(municipi.mi, region="id")
municipi.mi.df = inner_join(municipi.mi.points, municipi.mi@data, by="id")

municipi.mi.df <- municipi.mi.df %>% mutate(MUNICIPIO = as.factor(MUNICIPIO))

# PLOT

ggplot(municipi.mi.df, mapping = aes(long, lat)) + 
  geom_polygon(mapping = aes(group=group, fill = MUNICIPIO)) +
  geom_path(mapping = aes(group=group), color="white") +
  geom_point(data=centraline.mi@data, mapping=aes(x=lon, y=lat), color = "white", cex=3) + 
  coord_map() +
  scale_fill_brewer(palette = "RdYlBu", type= "div") +
  theme(plot.background=element_blank(), 
        panel.background = element_blank(), 
        axis.title=element_blank(), 
        axis.ticks=element_blank(), 
        axis.text=element_blank()) +
  ggtitle("Municipalità di Milano",
          subtitle="Dislocazione centraline ARPA controllo della qualità dell'aria")
###设置
图书馆(“tidyverse”)
图书馆(“rgdal”)
库(“地图工具”)
图书馆(“rgeos”)
图书馆(“ggmap”)

unzipURL参见
sp::over
sf::st_相交
sf::st_连接
。下面是一个使用
覆盖
的示例:tnx;出于某种原因,我最初在over中出现了一个错误,但我认为这是由于预测不匹配造成的。很好用!