1

I apologize in advance for my beginner question, but R spatial analysis is completely new to me.

I'm trying to convert an entire dataset (lat,long) into UTM for Ecuador (zone=17). My code below is only converting the first lat/long coordinates. Any advice would be greatly appreciated!

require(proj4)
require(rgdal)
require(sp)
require(proj4)

## Load dataset, total h7_x length = 327463
h7 <- read.csv('h7.csv', header=T)
h7 <- data.frame(x=h7$h7_x, y=h7$h7_y)

## Convert Lat/Long to UTM
proj4string <- "+proj=utm +zone=17 +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs"

## Transformed data
pj.h7 <- project(h7, proj4string, inverse=TRUE)
latlon.h7 <- data.frame(lat=pj.h7$y, lon=pj.h7$x)
1
  • we prbly need to see the first few lines of h7.csv and the first few lines of latlon.h7
    – hrbrmstr
    Oct 31, 2015 at 22:46

1 Answer 1

0

You can do this:

library(rgdal)

## Load dataset, total h7_x length = 327463
h7 <- read.csv('h7.csv')
coordinates(h7) <- ~ h7_x + h7_y
proj4string(h7) <- CRS("+proj=utm +zone=17 +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs")

latlon.h7 <- spTransform(h7, CRS("+proj=longlat +datum=WGS84 +ellps=WGS84"))

And now a reproducible example:

# example data
d <- data.frame(x=c(830816, 848933, 773072), y=c(9933229, 9861005, 9755835), id=1:3)

# create a SpatialPoints object 
coordinates(d) <- ~ x + y
proj4string(d) <- CRS("+proj=utm +zone=17 +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs")

# transform
latlon <- spTransform(d, CRS("+proj=longlat +datum=WGS84 +ellps=WGS84"))
coords <- coordinates(latlon)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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