Convert UTM to DD while keeping label column
It goes like this:
library(sp)
d <- data.frame(x=c(0,1000), y=c(0,1000), section=1:2)
coordinates(d) <- ~x+y
proj4string(d) = "+proj=utm +zone=1"
geo <- spTransform(d, CRS("+proj=longlat +datum=WGS84"))
as.data.frame(geo)
# section x y
#1 1 178.5113 0.000000000
#2 2 178.5202 0.009019487
Or with "terra":
library(terra)
d <- data.frame(x=c(0,1000), y=c(0,1000), section=1:2)
v <- vect(d, geom=c("x", "y"), crs="+proj=utm +zone=1")
g <- project(v, "+proj=longlat +datum=WGS84")
as.data.frame(g, geom="XY")
# section x y
#1 1 178.5113 0.000000000
#2 2 178.5202 0.009019487
How to convert UTM to long/Lat while the points distributed over different zones 38,39,40,41
If all you have is the coordinate numbers and not the zone identifier then there's no way of knowing which of any of the UTM zones your points are in. The UTM coordinate numbers are repeated for each zone, and only the zone identifier can tell you which zone a point is in. The point (453199,4094292) could be anywhere in the world, never mind just those four zones in Iran.
You need to go back to your source and get the UTM zone information for each point, and then you can construct the correct projection string for each point, and do the transformation for each different UTM zone you have to get the lat-long coordinates.
Easiest way convert UTM ZONE to lat long
In order to convert UTM coordinates (easting
and northing
) to latitude and longitude you need the zone number
and zone letter
as well.
Without these your easting / northing values could be in any of the 60 zones defined by UTM.
As for libraries, there are packages for Python, Javascript and probably others.
Sample for JS:
utm.toLatLon(easting, northing, zoneNum, zoneLetter)
//returns { latitude, longitude }
utm.fromLatLon(latitude, longitude)
//returns { easting, northing, zoneNum, zoneLetter }
Converting latitude and longitude data to UTM with points from multiple UTM zones in R
UPDATE
Here is a much faster and elegant workaround using dplyr
and spTransfrom
Augmented data (60k+ rows):
test_coordinates <- data.frame(x = c(13.637398, -3.58627, -5.178889), y = c(41.30736, 40.72913, 40.17528), x_correct = c(385936, 450492, 314480), y_correct = c(4573773, 4508854, 4449488 ), zone = c(33, 30, 30), key = c(1, 2, 3), country = c("italy", "spain", "spain"))
test_coordinates = rbind(test_coordinates, test_coordinates[rep(1,60*1000),]) # simulate big data
library(dplyr)
library(sp)
get_utm <- function(x, y, zone, loc){
points = SpatialPoints(cbind(x, y), proj4string = CRS("+proj=longlat +datum=WGS84"))
points_utm = spTransform(points, CRS(paste0("+proj=utm +zone=",zone[1]," +ellps=WGS84")))
if (loc == "x") {
return(coordinates(points_utm)[,1])
} else if (loc == "y") {
return(coordinates(points_utm)[,2])
}
}
test_coordinates %<>%
mutate(zone2 = (floor((x + 180)/6) %% 60) + 1, keep = "all"
) %>%
group_by(zone2) %>%
mutate(utm_x = get_utm(x, y, zone2, loc = "x"),
utm_y = get_utm(x, y, zone2, loc = "y"))
Output (5 rows only)
test_coordinates
# A tibble: 603 × 10
# Groups: zone2 [2]
x y x_correct y_correct zone key country zone2 utm_x utm_y
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl>
1 13.6 41.3 385936 4573773 33 1 italy 33 385936. 4573773.
2 -3.59 40.7 450492 4508854 30 2 spain 30 450492. 4508854.
3 -5.18 40.2 314480 4449488 30 3 spain 30 314480. 4449488.
4 13.6 41.3 385936 4573773 33 1 italy 33 385936. 4573773.
5 13.6 41.3 385936 4573773 33 1 italy 33 385936. 4573773.
ORIGINAL RESPONSE
Replace:
data.frame(t(sapply(converted_points,c))) #Makes this list into a dataframe
With:
test_coordinates$utm_x <- unlist(converted_points)[c(T,F)]
test_coordinates$utm_y <- unlist(converted_points)[c(F,T)]
x y x_correct y_correct zone key country utm_x utm_y
1 13.637398 41.30736 385936 4573773 33 1 italy 385935.7 4573773
2 -3.586270 40.72913 450492 4508854 30 2 spain 450492.4 4508854
3 -5.178889 40.17528 314480 4449488 30 3 spain 314479.5 4449488
Related Topics
Difference Between Mean(C(1,2,21)) and Mean(1,2,21)
Convert String Back into Object in R
How to Organize Large Shiny Apps
Hyperlinking Text in a Ggplot2 Visualization
Put Multiple Data Frames into List (Smart Way)
Methods for Doing Heatmaps, Level/Contour Plots, and Hexagonal Binning
How to Append a Plot to an Existing PDF File
How to Add a Scale Bar (For Linear Distances) to Ggmap
Insert Portions of a Markdown Document Inside Another Markdown Document Using Knitr
Writing Functions in R, Keeping Scoping in Mind
Multiple Graphs of Each Time Series
Confidence Intervals for Predictions from Logistic Regression
Circular Heatmap That Looks Like a Donut
How Does the Removesparseterms in R Work
Putting X-Axis at Top of Ggplot2 Chart
Using R and Plot.Ly - How to Script Saving My Output as a Webpage