将纬度/经度点映射到R中的形状文件 [英] Map Lat/Lon Points to a Shape File in R

查看:129
本文介绍了将纬度/经度点映射到R中的形状文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用shapefile为每组纬度/经度坐标标识邮政编码.

I am trying to identify the zip code for each set of lat/lon coordinate using a shapefile.

Lat Lon数据提取自: https://data. cityofchicago.org/Public-Safety/Crimes-2017/d62x-nvdr (Crimes _-__ 2001_to_present.csv)

Lat Lon data is extracted from: https://data.cityofchicago.org/Public-Safety/Crimes-2017/d62x-nvdr (Crimes_-_2001_to_present.csv)

形状文件: https://www2.census.gov/geo/老虎/PREVGENZ/zt/z500shp/ zt17_d00.shp(伊利诺伊州的邮政编码定义)

Shapefile: https://www2.census.gov/geo/tiger/PREVGENZ/zt/z500shp/ zt17_d00.shp (zip code definitions for the state of Illinois)

library(rgeos)
library(maptools)

ccs<-read.csv("Crimes_-_2001_to_present.csv")
zip.map <- readOGR("zt17_d00.shp")
latlon<-ccs[,c(20,21)]
str(latlon)
   'data.frame':   6411517 obs. of  2 variables:
    $ Latitude : num  42 41.7 41.9 41.8 42 ...
    $ Longitude: num  -87.7 -87.6 -87.7 -87.6 -87.7 ...
coordinates(latlon) = ~Longitude+Latitude
write.csv(cbind(latlon,over(zip.map,latlon)),"zip.match.csv")

这是我得到的错误:

错误(函数(类,fdef,mtable)): 无法找到签名" SpatialPolygonsDataFrame","data.frame""的函数"over"的继承方法

Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘over’ for signature ‘"SpatialPolygonsDataFrame", "data.frame"’

我想念什么?感谢您的帮助!

What am I missing? Any help is appreciated!

推荐答案

从错误消息中看来,您的coordinates(latlon) = ~Longitude+Latitude行未成功将数据框转换为Spatial对象.您可能希望在转换后先通过class(latlon)调用进行检查.

From the error message, it looks like your coordinates(latlon) = ~Longitude+Latitude line did not successfully transform the data frame into a Spatial object. You may wish to check that first with a class(latlon) call after the transformation.

将shapefile和&将其与latlon图层重叠,以确保您的数据集确实重叠.如果不相同,请检查它们是否共享相同的投影(sp::identicalCRS).

It would also be helpful to plot out the shapefile & overlay it with the latlon layer, just to make sure that your datasets actually do overlap. If they don't, check whether they share the same projection (sp::identicalCRS).

以下是使用伪数据的示例,因为问题中的shapefile链接不起作用.

The following is an example using dummy data, since the shapefile link in the question doesn't work.

library(rgdal)

# load Scotland shapefile, which came with the package
dsn <- system.file("vectors", package = "rgdal")[1]
shapefile <- readOGR(dsn=dsn, layer="scot_BNG")
shapefile <- spTransform(shapefile, CRS("+proj=longlat +datum=WGS84")) #change CRS

# create dummy data frame with coordinates in Scotland (I clicked randomly on Google Maps)
csvfile <- data.frame(lat = c(-4.952, -4.359, -2.425),
                      long = c(57.57, 56.59, 57.56))

# convert data frame to SpatialPoints class
coordinates(csvfile) <- ~lat+long

# make sure the two files share the same CRS
csvfile@proj4string <- shapefile@proj4string

# visual check
plot(shapefile, border = "grey")
points(csvfile, col = "red", cex = 5)
axis(1) # showing the axes helps to check whether the coordinates are what you expected
axis(2)

# if everything works out so far, the following should work
points_in_shape <- over(csvfile, shapefile)

> points_in_shape
  SP_ID          NAME ID_x COUNT   SMR  LONG  LAT     PY EXP_ AFF   X_COOR   Y_COOR ID_y
1    50 Ross-Cromarty    5    15 352.1 57.71 5.09 129271  4.3  10 220678.6 870935.6    5
2    11 Perth-Kinross   29    16 111.3 56.60 4.09 346041 14.4  10 291372.7 746260.5   29
3     3  Banff-Buchan    2    39 450.3 57.56 2.36 231337  8.7  16 385776.1 852378.2    2

> cbind(csvfile, points_in_shape["NAME"])
     lat  long          NAME
1 -4.952 57.57 Ross-Cromarty
2 -4.359 56.59 Perth-Kinross
3 -2.425 57.56  Banff-Buchan

这篇关于将纬度/经度点映射到R中的形状文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆