You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言创建蜥蜴位置栅格图层时xmax函数报错求助

问题解决:openproj报错“unable to find an inherited method for function ‘xmax’ for signature ‘"data.frame"’”

报错原因

  • 变量名raster与R生态中raster包的同名核心函数冲突,导致openproj无法正确识别输入的OpenStreetMap对象,错误将其解析为data.frame类型。
  • 投影字符串开头存在多余空格,干扰了投影规则的正常解析。

解决方案

  1. 重命名冲突变量:将openmap返回的对象从raster改为其他名称(如osm_map),彻底避免与内置函数重名。
  2. 修正投影字符串:移除投影参数开头的多余空格,确保投影规则被正确识别。
  3. 适配代码修改:更新后续代码中对该变量的引用。

修改后的完整代码

library(tidyverse)
library(janitor)
library(lubridate)
library(sp)
library(OpenStreetMap)
library(pbapply)
library(mgsub)
library(stringr)
library(data.table)

animal.ID<-c("TM 1","TM 1","TM 1","TM 1")
X<-c(489755 ,489792 ,489537 ,489505)
Y<-c(3722898 ,3722405 ,3722746 ,3722467)

data.TMex<-data.frame(animal.ID,X,Y)

utm.points <- cbind(data.TMex$X, data.TMex$Y) 
utm.locations <- SpatialPoints(utm.points, proj4string=CRS("+proj=utm +zone=12 +datum=NAD27")) 
proj.lat.lon <- as.data.frame(spTransform(utm.locations, CRS("+proj=longlat +datum=NAD27"))) 
colnames(proj.lat.lon) <- c("x","y") 

# 重命名变量,避免与raster函数冲突
osm_map <- openmap(
  c(max(proj.lat.lon$y)+0.005, min(proj.lat.lon$x)-0.005), 
  c(min(proj.lat.lon$y)-0.005, max(proj.lat.lon$x)+0.005), 
  type = "bing"
)

# 修正投影字符串开头的空格
raster.utm <- openproj(osm_map, projection = "+proj=utm +zone=12 +datum=NAD27 +units=m +no_defs")

autoplot.OpenStreetMap(raster.utm, expand = TRUE) + 
  theme_bw() + 
  theme(legend.position="bottom") +
  theme(panel.border = suppressWarnings(element_rect(colour = "black", fill=NA, size=1))) +
  geom_point(data=data.TMex, aes(X, Y, color= animal.ID), size = 3, alpha = 0.8) +
  theme(axis.title = element_text(face="bold")) + 
  labs(x="Easting", y="Northing") + 
  guides(color=guide_legend("Identifier")) 

效果说明

修改后的代码可正常完成:

  • 将OpenStreetMap地图从经纬度投影转换为UTM Zone 12N投影
  • 叠加蜥蜴的UTM坐标点位
  • 生成符合需求的可视化地图

内容的提问来源于stack exchange,提问作者Jason Edelkind

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 23:15:33