简体   繁体   中英

Lat/Long Conversion to UTM Loop (R code)

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)

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 (this is how you should set up a question, it is not helpful to refer to files that others do not have.)

# 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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