【发布时间】:2017-09-25 22:24:57
【问题描述】:
我在具有其他 14 列的数据框中有 UTM 坐标。
我正在使用以下代码: 1. 将 UTM(东距和北距)从形状文件中拉到单独的数据框中。 2. 使用 proj4 将其转换为 lat/long。 3. 使用innerjoin 和我根据行名在两个数据框中创建的列将其连接回原始df。
有没有更简洁的方法来转换坐标,希望不会将坐标数据移入和移出数据框? proj4 似乎只适用于两列。这是我完成后正在查看的内容。
> higeo= readOGR(dsn=".", layer="ahupuaa") #Read in shape file
OGR data source with driver: ESRI Shapefile
Source: ".", layer: "ahupuaa"
with 725 features
It has 8 fields
> higeo@data$id = rownames(sf@data) #add row numbers as a column
> higeo.points = fortify(higeo, region="id") #pull point data from each “region”
> higeo.df = inner_join(higeo.points, higeo@data, by="id") #join individual points back to data via id field
> higeo.df$rowid = rownames(higeo.df) #begin again, add “rowid” this time.
> hicoords <- data.frame(x=higeo.df$lon, y=higeo.df$lat) #create xy values in new df, because proj4 only handles 2 columns
> proj4string <- "+proj=utm +zone=4 +north +ellps=WGS84 +datum=WGS84 +units=m +no_defs"#define proj4string to contain constants
> hiconv <- project(hicoords, proj4string, inverse=TRUE)#converts to lat long
> hiconv.df$rowid = rownames(hiconv.df)# add rownames as column
> newhi.df <- inner_join(hiconv.df, higeo.df, by="rowid") #rejoin dfs by rowid
【问题讨论】:
-
你使用
fortify是因为你想使用ggplot2吗? -
我正在使用
fortify,因为我知道一些ggplot2,我认为它会做我想要的,即提取构成形状文件的实际点的值并放入它们在数据框中。如果我不使用ggplot2。有没有更好的方法来做到这一点? -
查看我的更新答案。我有一个可重现的例子:gist.github.com/jsta/cbf16ae5f354b7c64cc158b5fac3073f
-
我收到来自
download.file的错误。否则,我想我明白你在说什么。我会在手动下载一些形状文件后尝试让它工作并让你知道。 i.imgur.com/JamYooc.png
标签: r latitude-longitude utm