0

I am creating grid over Jingsu province with r-package(sp) makegrid function. I want to calculate distance between each grid centre and waterbenthos sampling site, but the unit of longitude and latitude of grid are metres, and the unit of longitude and latitude of waterbenthos are decimal degrees. Question: how can I convert metres to decimal degree? so i can calculate their diatance.

I've tried to search on the internet for a few days, still can't solve this problem. Your answer is greatly appreciated!

library(sp)
library(rgdal)
Yixing <- readOGR(dsn = "D:/Data_R_afu/yixing", layer = "boundary")<br/>
grid <- makegrid(Yixing, cellsize = 1000) # cellsize in map units!<br/>
grid <- SpatialPoints(grid, proj4string = CRS(proj4string(Yixing)))<br/>
grid@coords<br/>
#       x1      x2<br/>
#[1,] 13311000 3627100<br/>
#[2,] 13311000 3628100<br/>
#[3,] 13312000 3628100<br/>
#[4,] 13313000 3628100<br/>
#...<br/>
#...<br/>
#...<br/>

waterbenthos<- readOGR(dsn = "D:/Data_R_afu/yixing", layer = 
                             "afu_waterbenthos_r_ln(Rt+1Dg+1)")<br/>
waterbenthos@coords<br/>
#      coords.x1 coords.x2<br/>
#[1,]  119.8853  31.37628<br/>
#[2,]  119.8853  31.37628<br/>
#[3,]  119.8853  31.37628<br/>
#[4,]  119.9273  31.40567<br/>
#...<br/>
#...<br/>
#...<br/>
afuya
  • 1
  • 1
  • You need to project your data into the same coordinates as your Benthos data first. Something like `Yixing = spTransform(Yixing, CRS(waterbenthos))` – dww Oct 11 '19 at 13:56
  • I have projected Yixing and waterbenthos into the same coordinates by arcmap 10.2, so they should be converted each other, but I don't know how. #Is projected: TRUE
    #proj4string :

    # [+proj=merc +lon_0=0 +lat_ts=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs+ellps=WGS84 +towgs84=0,0,0]
    – afuya Oct 12 '19 at 01:40
  • looks like you have a mercator projection with units in meters. Try putting both into geographic coordinate system - `+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0` – dww Oct 12 '19 at 02:24
  • Thanks for your kind suggestion, but I need the mercator projection to calculate distance in the unit of metre. Because I want to define a moving window with a fine radius, and set grid as centre of each window. Like this: #WindowCentroid <- data.frame(matrix(round(rnorm(40), 2), nrow=20))
    #colnames(WindowCentroid) <- c("CentroidX","CentroidY")
    #x1=c(0,0)
    #x2=c(3,4)
    #EuclideanDistance <- function(x1, x2) sqrt(sum((x1 - x2) ^ 2))
    #euc.dist <- c()
    #Radius = 2
    #WindowCentroidIndex = data.frame(NA, ncol = 2)
    – afuya Oct 12 '19 at 07:58
  • Ok - your question was confusing where it says "how can I convert metres to decimal degree?" Seems now that you actually want everything in meters. Could you post a link to your data sets in the question to make a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Otherwise its difficult to give a definitive answer – dww Oct 12 '19 at 16:29
  • Thank you very much for your kind and patience, I have solved my problem. I made a terrible mistake when I convert geographic coordinate system to projected coordinates in arcmap. When I corrected the mistake, my problem was fixed. – afuya Oct 13 '19 at 03:24

0 Answers0