简体   繁体   English

将纬度/经度或URM坐标添加到R中的shapefile

[英]Add lat/long or URM coordinates to shapefiles in R

I am new to GIS in R, and I'm attempting to add either lat/long or UTM coordinates to a shapefile. 我是R语言中的GIS的新手,我正尝试将lat / long或UTM坐标添加到shapefile。 I downloaded the boundaries of the city of Chicago (City_Boundaries.shp)from here: http://www.cityofchicago.org/city/en/depts/doit/supp_info/gis_data.html 我从这里下载了芝加哥市的边界(City_Boundaries.shp): http : //www.cityofchicago.org/city/en/depts/doit/supp_info/gis_data.html

I loaded the maptools and rgeos libraries: 我加载了maptools和rgeos库:

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

I brought the data into R & attempted to add a UTM code for zone 16T: 我将数据导入R&并尝试为区域16T添加UTM代码:

city<- readShapeSpatial("C:/Users/Luke.Gerdes/Documents/GIS Files/Chicago/City_Boundary.shp")
proj4string(city) <- CRS("+proj=utm +north +zone=16T + datum=WGS84")

However, the resulting data doesn't make sense to me. 但是,结果数据对我来说没有意义。 When I take a look at the "coords" slots within "city", the coordinate values are, for example, X=1092925 and Y=1944820. 当我查看“城市”内的“坐标”插槽时,坐标值为例如X = 1092925和Y = 1944820。 I used an outside tool ( http://home.hiwaay.net/~taylorc/toolbox/geography/geoutm.html ) to find GPS coordinates. 我使用外部工具( http://home.hiwaay.net/~taylorc/toolbox/geography/geoutm.html )查找GPS坐标。 The results were: lat=17.511, long=-81.42. 结果是:lat = 17.511,long = -81.42。 This is somewhere between Jamaica and the coast of Honduras. 介于牙买加和洪都拉斯海岸之间。 It seems to me that the shapefile coordinates exist in their own universe; 在我看来,shapefile坐标存在于自己的宇宙中。 as the name shapefile perhaps implies, the coordinates provide an accurate depiction of the city's shape, but these coordinates do not automatically map onto the globe. 就像shapefile的名称所暗示的那样,坐标提供了城市形状的准确描述,但是这些坐标不会自动映射到地球上。

My ultimate goal is to determine if a number of geo-tagged events took place within Chicago. 我的最终目标是确定在芝加哥是否发生了一些带有地理标记的事件。 I am comfortable converting those points, which are listed by lat/long, into UTM, as per below: 我很乐意将经纬度列出的点转换为UTM,如下所示:

  SP <- SpatialPoints(cbind(-87.63044, 41.79625), proj4string=CRS("+proj=longlat +datum=WGS84"))
  SP <- spTransform(SP, CRS("+proj=utm +north +zone=16T +datum=WGS84"))

I'm also willing to try to work with the events data in their original format if that makes more sense. 如果更有意义,我也愿意尝试使用原始格式的事件数据。 Ultimately, I need help to turn the Chicago city boundaries into a polygon that I can check against my events data. 最终,我需要帮助将芝加哥城市边界变成一个多边形,可以对照事件数据进行检查。 Something like this: 像这样:

  gContains(city, SP)

How do I get my shapefiles and events data into a common (and accurate) frame of reference so that I can check whether points are in the city? 如何将我的shapefile和事件数据放入一个通用(准确)的参照系中,以便可以检查点是否在城市中? Any help you can render will be greatly appreciated. 您可以提供的任何帮助将不胜感激。

The general advice is to read shapefiles with rgdal::readOGR , as in 一般建议是使用rgdal::readOGR读取shapefile,如

library(rgdal)
city = readOGR(".", "City_Boundary")

That will not only give city its proper CRS: 这不仅会给city适当的CRS:

proj4string(city)
[1] "+proj=tmerc +lat_0=36.66666666666666 +lon_0=-88.33333333333333 +k=0.9999749999999999 +x_0=300000 +y_0=0 +datum=NAD83 +units=us-ft +no_defs +ellps=GRS80 +towgs84=0,0,0"

but also warn you if you want to change it without reprojecting: 但也要警告您是否要更改它而不重新投影:

proj4string(city) <- CRS("+proj=utm +north +zone=16T + datum=WGS84")
Warning message:
In `proj4string<-`(`*tmp*`, value = <S4 object of class "CRS">) :
  A new CRS was assigned to an object with an existing CRS:
+proj=tmerc +lat_0=36.66666666666666 +lon_0=-88.33333333333333 +k=0.9999749999999999 +x_0=300000 +y_0=0 +datum=NAD83 +units=us-ft +no_defs +ellps=GRS80 +towgs84=0,0,0
without reprojecting.
For reprojection, use function spTransform in package rgdal

meaning that after doing this, city is wrong . 意味着这样做之后, city错误的 You reproject city by using spTransform , as in 您可以使用spTransform重新投影city ,如

city = spTransform(city, CRS("+proj=utm +north +zone=16T + datum=WGS84"))

Your rgeos::gContains call might fail because the two CRS strings for utm differ; 您的rgeos::gContains调用可能会失败,因为utm的两个CRS字符串不同。 remove the space in + datum=WGS84 in the former and you'll be fine ( TRUE ). 删除前面的+ datum=WGS84中的空格,就可以了( TRUE )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM