Checking if Longitude/Latitude within Greater Toronto Area in R

别说谁变了你拦得住时间么 提交于 2019-11-28 06:29:52

问题


I have a dataset of Longitude and Latitude values and I want to check whether they are in the Greater Toronto Area. Alternative, labeling them with the Closest Census Metropolitan would suffice as well.

Is there a way to accomplish this, preferably using R?


回答1:


Here is a working example of how to do this with the rgdal package. There are plenty of other ways to do this. I provided a link to where you can get a Toronto shape file if you do not already have one. If you have any questions please let me know.

library(rgdal)

myTestDF <- data.frame(MyDate = c("A","Toronto","C"),
                       latitude = c(74.3224,43.686094, 88.9237),
                       longitude = c(66.2222, -79.401350, -49.0074))

setwd("C:/WhereShapeFilesAre")

#Download shape file from open data site and unzip contents into a folder this example uses the wgs84 format. 
#http://www1.toronto.ca/wps/portal/contentonly?vgnextoid=c1a6e72ced779310VgnVCM1000003dd60f89RCRD&vgnextchannel=75d6e03bb8d1e310VgnVCM10000071d60f89RCRD

TorontoShape<- readOGR(".", "citygcs_regional_mun_wgs84")

myTestPoints <- myTestDF

coordinates(myTestPoints) <-  ~ longitude + latitude
proj4string(myTestPoints) <- proj4string(TorontoShape)

cbind(myTestDF, over(myTestPoints, TorontoShape))


来源:https://stackoverflow.com/questions/43243431/checking-if-longitude-latitude-within-greater-toronto-area-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!