R 从TXT到空间数据:lat/long格式问题
我试图从R中导入的txt文件创建一个空间数据集,其结构如下:R 从TXT到空间数据:lat/long格式问题,r,coordinates,geospatial,spatial,data-conversion,R,Coordinates,Geospatial,Spatial,Data Conversion,我试图从R中导入的txt文件创建一个空间数据集,其结构如下: CitiesTXTSel= COM City_NAME LONGI_DMS LATI_DMS 445 VILLEMOTIER 51916 462046 98 CHAZEY-BONS 54054 454811 57 BOZ 45434 462425 当我使用下面的函数时,会收到以下错误消息: “CheckN
CitiesTXTSel=
COM City_NAME LONGI_DMS LATI_DMS
445 VILLEMOTIER 51916 462046
98 CHAZEY-BONS 54054 454811
57 BOZ 45434 462425
当我使用下面的函数时,会收到以下错误消息:
“CheckNumericImpresse2Double(obj)中出错:非有限坐标”
我猜Cooridate的预格式化是不正确的,但我不知道如何解决这个问题。请问你能帮忙吗?导入的坐标以Lat/long DMS表示。我不知道R,但标题中的DMS可能代表度-分-秒-您可能需要将它们转换为十进制度?嗨,Jon,这确实是一个解决方案。我在txt文件中有另一个地理参考系统的坐标。我试着用这个来代替。
# Reproducing your data frame
CitiesTXTSel <- data.frame(
COM = c(445, 98, 7),
City_NAME = c("VILLEMOTIER", "CHAZEY-BONS", "BOZ"),
LONGI_DMS = c(51916, 54054, 45434),
LATI_DMS = c(462046, 454811, 462425),
stringsAsFactors = FALSE
)
# a function to convert your input to decimal degrees
convert_DMS_dec <- function(DMS) {
DMS_pad <- sprintf("%7s", DMS)
deg <- substr(DMS_pad, 1, 3)
deg <- gsub(" ", "", deg) # remove leading whitespace
min <- substr(DMS_pad, 4, 5)
sec <- substr(DMS_pad, 6, 7)
DMS_split <- paste(deg, min, sec)
dec_deg <- conv_unit(DMS_split, "deg_min_sec", "dec_deg")
round(as.numeric(dec_deg), 4) # precision to about one arc-second
}
CitiesTXTSel$long <- convert_DMS_dec(CitiesTXTSel$LONGI_DMS)
CitiesTXTSel$lat <- convert_DMS_dec(CitiesTXTSel$LATI_DMS)
# re-order the data frame so columns three and four match your code
CitiesTXTSel <- CitiesTXTSel[, c(1:2, 5:6, 3:4)]
# Your code, indented a little for clarity
Cities= SpatialPointsDataFrame(
coords=CitiesTXTSel[,3:4],
data=CitiesTXTSel[,1:2],
proj4string=CRS("+proj=longlat +datum=wgs84")
)
# Reproducing your data frame
CitiesTXTSel <- data.frame(
COM = c(445, 98, 7),
City_NAME = c("VILLEMOTIER", "CHAZEY-BONS", "BOZ"),
LONGI_DMS = c(51916, 54054, 45434),
LATI_DMS = c(462046, 454811, 462425),
stringsAsFactors = FALSE
)
# a function to convert your input to decimal degrees
convert_DMS_dec <- function(DMS) {
DMS_pad <- sprintf("%7s", DMS)
deg <- substr(DMS_pad, 1, 3)
deg <- gsub(" ", "", deg) # remove leading whitespace
min <- substr(DMS_pad, 4, 5)
sec <- substr(DMS_pad, 6, 7)
DMS_split <- paste(deg, min, sec)
dec_deg <- conv_unit(DMS_split, "deg_min_sec", "dec_deg")
round(as.numeric(dec_deg), 4) # precision to about one arc-second
}
CitiesTXTSel$long <- convert_DMS_dec(CitiesTXTSel$LONGI_DMS)
CitiesTXTSel$lat <- convert_DMS_dec(CitiesTXTSel$LATI_DMS)
# re-order the data frame so columns three and four match your code
CitiesTXTSel <- CitiesTXTSel[, c(1:2, 5:6, 3:4)]
# Your code, indented a little for clarity
Cities= SpatialPointsDataFrame(
coords=CitiesTXTSel[,3:4],
data=CitiesTXTSel[,1:2],
proj4string=CRS("+proj=longlat +datum=wgs84")
)
coordinates COM City_NAME
(5.3211, 46.3461) 445 VILLEMOTIER
(5.6817, 45.8031) 98 CHAZEY-BONS
(4.9094, 46.4069) 7 BOZ