2

I have a a dataset of Vancouver,BC where Coordinate values are projected in UTM Zone 10 . Now I can convert it using spTransform by the following piece of code and looks like the it correctly transforms the coordinates ,as I can see if I plot them in a map .

cord_transform <- function(df){
  coordinates(df) <- ~X+Y 
  proj4string(df) <- "+proj=utm +zone=10 +ellps=GRS80 +units=m +no_defs"
  df.lat.long <- spTransform(df,CRSobj=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
  df.lat.long


}

However I realized I dont need a SPdataframe ,rather my purpose serves if I can transform the UTM coordinates in lat -long coordinates in the original data.table (since the data is huge I want to use data.table ) but when I follow this answer,and write a similar code for the data.table

func <- function(x,y){
  proj4string <- "+proj=utm +zone=10 +ellps=GRS80 +units= m + no_defs"
  inp <- list("x"=x,y="y")
  print(inp)
  pj <- project(inp,proj4string,inverse = TRUE)
  print(pj)
  latlon <- list(lat = pj$y,lng = pj$x)
  latlon



}
crime.data.2016.jan[,c("lat","lng"):=func(X,Y)] 

I get this error :

Error in project(inp, proj4string, inverse = TRUE) : 
  unknown unit conversion id 

a subset of the data looks like this :

         NEIGHBOURHOOD        X       Y
 1:       Renfrew-Collingwood 497445.9 5454283
 2: Central Business District 491580.1 5458745
 3:                           492757.5 5458792
 4:              South Cambie 491371.1 5455550
 5:                  West End 490417.0 5459169
 6:               Shaughnessy 489926.6 5454845
 7:                   Marpole 490409.0 5450869
 8:            Mount Pleasant 492545.4 5456743
 9:                  Fairview 490767.8 5456820
10:                 Kitsilano 488624.5 5457182
11:                 Killarney 497326.3 5452640
12:          Hastings-Sunrise 496763.8 5458220
13:  Kensington-Cedar Cottage 494917.4 5455246
14:                Kerrisdale 488711.0 5452722
15:       Victoria-Fraserview 495207.1 5452375
16:                Strathcona 493345.6 5458515
17:         Dunbar-Southlands 486589.5 5454941
18:                  Oakridge 490997.4 5453021
19:                    Sunset 492937.0 5451823
20:             Arbutus Ridge 488145.0 5455054
21:        Grandview-Woodland 494940.8 5458213
22:                Riley Park 492607.3 5455102
23:           West Point Grey 485649.1 5456957
24:              Stanley Park 490531.0 5460906
25:                  Musqueam 485508.0 5453015
                NEIGHBOURHOOD        X       Y
3

Your string has wrong spaces:

proj4string <- "+proj=utm +zone=10 +ellps=GRS80 +units= m + no_defs"

should be

proj4string <- "+proj=utm +zone=10 +ellps=GRS80 +units=m +no_defs"
  • Hi , if I change it runs without error but introduces NAs Warning messages: 1: In project(inp, proj4string) : NAs introduced by coercion 2: In project(inp, proj4string) : y is shorter than x, recycling – Bg1850 Nov 28 '17 at 2:24
  • When I changed inp <- list("x"=x,y="y") from a list to a dataframe it worked like a charm . Thanks . – Bg1850 Nov 28 '17 at 2:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.