R:在时区多边形中查找点
R: Finding point in time zone polygon
我编写了一个函数来确定给定的 lat/lon 点是否落在 tz_world.shp 文件中的特定多边形内。我传入 lon = -77,lat = 42,这是在纽约州。第 155 个条目的 ID 是 'America/New_York'。但是,使用 point.inpoly returns 一个零行长度的对象。这是我的代码。请帮忙。谢谢。 BSL.
i = 155
timeZoneList = split( timeZonesShpFile, timeZonesShpFile$TZID )
ps = lapply( timeZoneList, Polygon )
p1 = lapply(seq_along(ps), function(i) Polygons(list(ps[[i]]), ID = names( timeZoneList )[i] ) )
my_spatial_polys = SpatialPolygons( p1, proj4string = CRS("+proj=longlat +datum=WGS84") )
polyNames = sapply(slot(my_spatial_spdf, 'polygons'), function(i) slot(i, 'ID'))
pt = SpatialPointsDataFrame(cbind(lon,lat), data.frame(row=1), proj4string=CRS("+proj=longlat +datum=WGS84" ))
thisPoly = my_spatial_polys[ i ]
thisList = getSpPPolygonsIDSlots( thisPoly )
thisDf = data.frame( row = 1, row.names = thisList )
thisSpdf = SpatialPolygonsDataFrame( thisPoly, thisDf )
pIp = point.in.poly( pt, thisSpdf )
> pIp
[1] coordinates row row.1
<0 rows> (or 0-length row.names)
坐标的屏幕视图显示了纽约州的典型边界 lon/lat 点,它应该围绕我提供的 lon/lat 点。
这是另一种方法:
library(maptools)
library(rgdal)
library(rgeos)
download.file("http://efele.net/maps/tz/world/tz_world.zip", tf <- tempfile(fileext = ".zip"))
unzip(tf, exdir = tempdir())
shp <- readShapeSpatial(file.path(tempdir(), "world", "tz_world.shp"))
over(
SpatialPoints(data.frame(lon = c(13, -77, 51), lat = c(52, 42, 0))), # Berlin, New York, London
shp[shp$TZID %in% c("America/New_York", "Europe/Berlin"), ]
)
# TZID
# 1 Europe/Berlin
# 2 America/New_York
# 3 <NA>
我编写了一个函数来确定给定的 lat/lon 点是否落在 tz_world.shp 文件中的特定多边形内。我传入 lon = -77,lat = 42,这是在纽约州。第 155 个条目的 ID 是 'America/New_York'。但是,使用 point.inpoly returns 一个零行长度的对象。这是我的代码。请帮忙。谢谢。 BSL.
i = 155
timeZoneList = split( timeZonesShpFile, timeZonesShpFile$TZID )
ps = lapply( timeZoneList, Polygon )
p1 = lapply(seq_along(ps), function(i) Polygons(list(ps[[i]]), ID = names( timeZoneList )[i] ) )
my_spatial_polys = SpatialPolygons( p1, proj4string = CRS("+proj=longlat +datum=WGS84") )
polyNames = sapply(slot(my_spatial_spdf, 'polygons'), function(i) slot(i, 'ID'))
pt = SpatialPointsDataFrame(cbind(lon,lat), data.frame(row=1), proj4string=CRS("+proj=longlat +datum=WGS84" ))
thisPoly = my_spatial_polys[ i ]
thisList = getSpPPolygonsIDSlots( thisPoly )
thisDf = data.frame( row = 1, row.names = thisList )
thisSpdf = SpatialPolygonsDataFrame( thisPoly, thisDf )
pIp = point.in.poly( pt, thisSpdf )
> pIp
[1] coordinates row row.1
<0 rows> (or 0-length row.names)
坐标的屏幕视图显示了纽约州的典型边界 lon/lat 点,它应该围绕我提供的 lon/lat 点。
这是另一种方法:
library(maptools)
library(rgdal)
library(rgeos)
download.file("http://efele.net/maps/tz/world/tz_world.zip", tf <- tempfile(fileext = ".zip"))
unzip(tf, exdir = tempdir())
shp <- readShapeSpatial(file.path(tempdir(), "world", "tz_world.shp"))
over(
SpatialPoints(data.frame(lon = c(13, -77, 51), lat = c(52, 42, 0))), # Berlin, New York, London
shp[shp$TZID %in% c("America/New_York", "Europe/Berlin"), ]
)
# TZID
# 1 Europe/Berlin
# 2 America/New_York
# 3 <NA>