简体   繁体   English

R-将经纬度转换为网格编号

[英]R - Convert latitude and longitude to grid numbers

I'm currently doing a classification project and the data I'm using includes lat/long attributes. 我目前正在做一个分类项目,我使用的数据包括经/纬度属性。 In order to simply the model(s) I'm thinking it might be easier to replace the raw coordinates with a single column of 'grid' numbers. 为了简化模型,我想用一列“网格”数字替换原始坐标可能会更容易。

By this I mean chop-up the area that the coordinates cover into an arbitrary number of grid points, number each square within the grid, and then replace the lat/long figures with the grid number which they fall in. For example, a 9 square grid might look like this: 我的意思是将坐标覆盖的区域切成任意数量的网格点,对网格中的每个正方形进行编号,然后将经纬度数字替换为它们所属的网格号。例如,一个9方格可能看起来像这样:

123
456
789

I've done a fair bit of searching on here and Google and can't seem to find a solution. 我已经在这里和Google上进行了大量搜索,但似乎找不到解决方案。 The closest I can find is the Universal Transverse Mercator coordinate system (which some R packages support), but the squares within this grid are too large. 我能找到的最接近的是通用横轴墨卡托坐标系(某些R包支持该坐标系),但是此网格内的正方形太大。 I'd like to be able to set the size of the grid myself. 我希望能够自己设置网格的大小。

I'm at a bit of a loss, and was wondering if the kind people of this forum knew of any R packages or techniques to achieve what I'd like. 我有点茫然,想知道这个论坛的友人是否知道任何R软件包或实现我想要的技术。 I'll append an example of my lat/long columns. 我将附加一个经纬度示例。 Thanks. 谢谢。

Latitude    Longitude
41.95469    -87.800991
41.95469    -87.800991
41.994991   -87.769279
41.974089   -87.824812
41.974089   -87.824812
41.9216     -87.666455
41.891118   -87.654491
41.867108   -87.654224
41.867108   -87.654224
41.896282   -87.655232
41.919343   -87.694259

Not especially elegant, but this works 并不是特别优雅,但是可以用

pos <- data.frame(lat=c(
41.95469,
41.95469,    
41.994991,   
41.974089,   
41.974089,   
41.9216,     
41.891118,   
41.867108,   
41.867108,   
41.896282,   
41.919343),   
long=c(
-87.824812,
-87.769279,
-87.800991,
-87.800991,
-87.824812,
-87.666455,
-87.654491,
-87.654224,
-87.654224,
-87.655232,
-87.694259))

gridx <- seq(from=-87.9,to=-87.6,by=0.01)
gridy <- seq(from=41.8,to=42,by=0.01)
xcell <- unlist(lapply(pos$long,function(x) min(which(gridx>x))))
ycell <- unlist(lapply(pos$lat,function(y) min(which(gridy>y))))
pos$cell <- (length(gridx) - 1) * ycell + xcell

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

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