在地图上查找多边形截距

时间:2016-08-03 20:49:30

标签: r maps data-visualization polygons

我正在尝试在此地图上找到截取R中的状态边界的Radii。 enter image description here

到目前为止,这是我的代码。感谢Gregoire Vincke用户提供的大部分解决方案。

library("maps")
library("mapproj")
library("RColorBrewer")
library("mapdata")
library("ggplot2")
library("rgeos")
library("dismo")
library("ggmap")
library("rgdal")

data("stateMapEnv") #US state map

dat <- read.csv("R/longlat.csv",header = T)

map('state',fill = T, col = brewer.pal(9,"Pastel2"))

#draws circles around a point, given lat, long and radius

plotCircle <- function(lonDec, latDec, mile) {
  ER <- 3959
  angdeg <- seq(1:360)
  lat1rad <- latDec*(pi/180)
  lon1rad <- lonDec*(pi/180)
  angrad <- angdeg*(pi/180)
  lat2rad <- asin(sin(lat1rad)*cos(mile/ER) + cos(lat1rad)*sin(mile/ER)*cos(angrad))
  lon2rad <- lon1rad + atan2(sin(angrad)*sin(mile/ER)*cos(lat1rad),cos(mile/ER)-sin(lat1rad)*sin(lat2rad))
  lat2deg <- lat2rad*(180/pi)
  lon2deg <- lon2rad*(180/pi)
  polygon(lon2deg,lat2deg,lty = 1 , col = alpha("blue",0.35))
  }

point <- mapproject(dat$lng,dat$lat)
points(point, col = alpha("black",0.90), cex = 0.4, pch = 20) #plots points

plotCircle(-71.4868,42.990684,20)
plotCircle(-72.57085,41.707932,12)
...
#this goes on for every point

我想在新数据框中存储拦截州边界的点,任何帮助都将不胜感激!

2 个答案:

答案 0 :(得分:1)

编辑:以下是使用R(sp,rgdal,rgeos)中的地理空间分析软件包的工作流程的概述。

  1. 您需要一个状态边界的多边形shapefile,而不是使用maps包和stateMapEnv,如下所示: https://www.census.gov/geo/maps-data/data/cbf/cbf_state.html
  2. 然后你可以从Rgdal包中加载带有readOGR的R中的shapefile,以获得一个多边形的 SpatialPolygons (我们称之为state_poly每个州的对象。

    1. 从长/纬度坐标创建 SpatialPoints 对象:
    2. pts <- SpatialPoints(dat[, c("lng", "lat")], proj4string = CRS("+proj=longlat"))

      1. 此时ptsstate_poly应位于经度/纬度坐标以绘制围绕点的固定半径的圆圈,您需要将它们转换为投影坐标(即以米为单位)。有关详细信息,请参阅此问题: Buffer (geo)spatial points in R with gbuffer

      2. 创建一个围绕每个点的圆周半径的向量,并将其与gBuffer(来自rgeos)和点图层一起使用:

      3. circ <- gBuffer(pts, width = radii, byid = TRUE)

        byid参数表示它为每个点单独执行,使用半径中的不同值,其顺序与点相同。

        1. 将状态多边形转换为行:state_lines <- as(state_poly, "SpatialLines")

        2. 使用gIntersects(circ, state_lines, byid = TRUE)

        3. 由于byid = TRUE,返回值是一个矩阵,spgeom1中每个圆圈有一行,spgeom2中每个状态边界有一列。请注意,如果圆与两个状态之间的边界相交,则该行中应该有两个“TRUE”值(每个状态一个)。如果它与水或美国的外围相交,则该行中可能只有一个“TRUE”值。

答案 1 :(得分:0)

这是最终密码!

library("maps")
library("mapproj")
library("RColorBrewer")
library("mapdata")
library("ggplot2")
library("rgeos")
library("dismo")
library("ggmap")
library("rgdal")

#import shape file (.shp), make sure all the other files in the zip are included in
#your file location!
state_poly <- readOGR(dsn = 'C:/Users/chopp/Documents/R', layer='cb_2015_us_state_500k')

#data containing lng and lat coordinates with radii
data <- read.csv("R/longlat.csv", header = T)

#create spatial point objects out of your lng and lat data
pts <- SpatialPoints(data[,c("lng","lat")], proj4string = CRS("+proj=longlat"))

#convert spatial points to projected coordinates (points and map lines)
ptsproj <- spTransform(pts, CRS("+init=epsg:3347"))
state_poly_proj<- spTransform(state_poly, CRS("+init=epsg:3347"))

#convert radii units to meters, used in our gBuffer argument later on 
radii <- data$rad*1609.344

#create circular polygons with. byid = TRUE will create a circle for each point
circ <- gBuffer(ptsproj, width = radii, byid = TRUE)



#convert state polygons to state lines    
state_lines<- as(state_poly_proj, "SpatialLines")

#use gIntersects with byid = TRUE to return a matrix where "TRUE" represents 
#crossing state boundaries or water
intdata <- gIntersects(circ, state_lines, byid = TRUE)

#write the matrix out into a csv file
write.csv(intdata,"R/Agents Intercepts 2.csv")