在R中创建地形图 [英] Create topographic map in R

查看:64
本文介绍了在R中创建地形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个脚本,该脚本将为给定的一组坐标生成二维地形图或轮廓图.我的目标与

I am trying to create a script that will generate a 2d topographic or contour map for a given set of coordinates. My goal is something similar to what is produced by

轮廓(火山)

,但对于用户设置的任何位置.事实证明,这极具挑战性!我已经尝试过:

but for any location set by the user. This has proved surprisingly challenging! I have tried:

library(elevatr)
library(tidyr)

# Generate a data frame of lat/long coordinates.
ex.df <- data.frame(x=seq(from=-73, to=-71, length.out=10), 
                       y=seq(from=41, to=45, length.out=10))

# Specify projection.
prj_dd <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"

# Use elevatr package to get elevation data for each point.
df.sp <- get_elev_point(ex.df, prj = prj_dd, src = "epqs")

# Convert from spatial to regular data frame, remove extra column.
# Use tidyr to convert to lat x lon table with elevation as fill.
# Sorry for the terrible code, I know this is sloppy.
df <- as.data.frame(df.sp)
df$elev_units <- NULL
df.w <- df %>% spread(y, elevation)
df.w <- as.matrix(df.w)

这将创建一个类似于 volcano 数据集的矩阵,但将其填充为 NA s,除了带有海拔数据的10个经纬度对. contour 可以处理 NA ,但是 contour(df.w)的结果只有一条细线.我不确定从这里要去哪里.我是否只需要更多积分?预先感谢您提供的任何帮助-我对R还是很陌生,我认为我付出的努力超过了我对这个项目的了解.

This creates a matrix similar to the volcano dataset but filled with NAs except for the 10 lat/lon pairs with elevation data. contour can handle NAs, but the result of contour(df.w) has only a single tiny line on it. I'm not sure where to go from here. Do I simply need more points? Thanks in advance for any help--I'm pretty new to R and I think I've bitten off more than I can chew with this project.

推荐答案

抱歉,响应延迟.我想我需要检查 elevatr 问题!

Sorry for delay in responding. I suppose I need to check SO for elevatr questions!

我将使用 elevatr :: get_elev_raster(),它返回一个栅格对象,可以直接使用 raster :: contour()绘制该对象.

I would use elevatr::get_elev_raster(), which returns a raster object which can be plotted directly with raster::contour().

下面的代码示例以较小的分辨率捕获较小的区域.最终的轮廓看起来不错.

Code example below grabs a smaller area and at a pretty coarse resolution. Resultant contour looks decent though.

library(elevatr)
library(raster)

# Generate a data frame of lat/long coordinates.
ex.df <- data.frame(x=seq(from=-73, to=-72.5, length.out=10), 
                    y=seq(from=41, to=41.5, length.out=10))

# Specify projection.
prj_dd <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"

# Use elevatr package to get elevation data for each point.
elev <- get_elev_raster(ex.df, prj = prj_dd, z = 10, clip = "bbox")

raster::contour(elev)

如果需要使用 graphic :: contour(),则需要先使用 raster :: as.matrix(elev)将栅格对象转换为矩阵..但这使坐标发生了变化,我还没有花费足够的时间尝试弄清楚那一部分...希望 raster 解决方案对您有用.

If it is a requirement to use graphic::contour(), you'll need to convert the raster object to a matrix first with raster::as.matrix(elev). That flips the coords though and I haven't spent enough time to try and get that part figured out... Hopefully the raster solution works for you.

这篇关于在R中创建地形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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