Lat/Long Conversion to UTM Loop (R code) - r

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

Related

Reproject data.frame of coordinates from Lambert93 to WGS83, in R, using raster and sp library

I am trying to reproject some coordinates from Lambert 93 to WGS84.
I spent enought time looking for documentation and to understand better but I don't see a solution yet.
I'm looking someone that could explain me where i got wrong.
require(sp)
require(raster)
# >>>>>>>>>>>>>>>>>
# >>>>>>>>>>>>>>>>> I define the coordinates that I will use later
# >>>>>>>>>>>>>>>>>
# define coordinates ----
# Lambert 93
# epsg 2154
crs_l93 <- " +proj=lcc +lat_1=49 +lat_2=44 +lat_0=46.5 +lon_0=3
+x_0=700000+y_0=6600000 +ellps=GRS80 +units=m +no_defs"
# WGS84
# epsg 4326
crs_wgs84 <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
# >>>>>>>>>>>>>>>>>
# >>>>>>>>>>>>>>>>> I define the coordinates i want to project (coord_l93)
# >>>>>>>>>>>>>>>>> and the coordinates goal (what i want them to be projected to : the result)
# >>>>>>>>>>>>>>>>> The coordinates where projected on https://epsg.io/ manually.
# >>>>>>>>>>>>>>>>>
# get data ----
# brut
coord_l93 <- data.frame("coord_x" = c(839500 , 830500 , 826500 , 826500 ) ,
"coord_y" = c(6458500, 6461500, 6467500, 6470500))
# cherché sur https://epsg.io/
coord_wgs84_hoped <- data.frame("coord_x" = c(4.7771833 , 4.6633664 , 4.6139595 , 4.6147419 ) ,
"coord_y" = c(45.2116938, 45.2404655, 45.2952272, 45.3222348))
# >>>>>>>>>>>>>>>>>
# >>>>>>>>>>>>>>>>> I set my data as SpatialPointsDataFrame and i set their projection
# >>>>>>>>>>>>>>>>>
# define new variable
sp_brut_l93 <- coord_l93
sp_brut_wgs84_hoped <- coord_wgs84_hoped
# define projection
sp::coordinates(sp_brut_l93) <- sp_brut_l93
proj4string(sp_brut_l93) <- CRS(crs_l93)
sp::coordinates(sp_brut_wgs84_hoped) <- sp_brut_wgs84_hoped
proj4string(sp_brut_wgs84_hoped) <- CRS(crs_wgs84)
# >>>>>>>>>>>>>>>>>
# >>>>>>>>>>>>>>>>> I make the projection by two different ways
# >>>>>>>>>>>>>>>>>
# project sp_coord_l93 into wgs84
sp_proj_wgs84 <- spTransform(spbv, CRS(crs_wgs84))
sp_proj_wgs84_V2 <- spTransform(spbv, "+init=epsg:4326")
# >>>>>>>>>>>>>>>>>
# >>>>>>>>>>>>>>>>> final check
# >>>>>>>>>>>>>>>>>
cat(sp_proj_wgs84)
cat(sp_brut_wgs84_hoped)
# check proj
# check that the to ways of projecting are identical
identical(coordinates(sp_proj_wgs84), coordinates(sp_proj_wgs84_V2))
# check that the projected is same as the hoped
identical(coordinates(sp_brut_wgs84_hoped), coordinates(sp_proj_wgs84))
My projection happen wrongly and i really miss the point here.
Hope anyone can explain me here.
Thank youin
Here is your code, simplified, and working
First define coordinate reference systems and data.frames with points
crs193 <- "+proj=lcc +lat_1=49 +lat_2=44 +lat_0=46.5 +lon_0=3 +x_0=700000 +y_0=6600000 +ellps=GRS80 +units=m +no_defs"
wgs84 <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
xyl93 <- data.frame("coord_x" = c(839500 , 830500 , 826500 , 826500 ) ,
"coord_y" = c(6458500, 6461500, 6467500, 6470500))
xy84 <- data.frame("coord_x" = c(4.7771833 , 4.6633664 , 4.6139595 , 4.6147419 ) ,
"coord_y" = c(45.2116938, 45.2404655, 45.2952272, 45.3222348))
Create SpatialPoints by identifying the columns in the data.frame that hold the coordinates (not by assigning the coordinate reference system, although we also do that)
library(sp)
# method 1
coordinates(xyl93) <- ~ coord_x + coord_y
proj4string(xyl93) <- CRS(crs193)
# method 2
xy84 <- sp::SpatialPoints(xy84, proj4string=CRS(wgs84))
Now we can transform
x <- spTransform(xy84, crs193)
coordinates(x)
# coord_x coord_y
#[1,] 839500 6458500
#[2,] 830500 6461500
#[3,] 826500 6467500
#[4,] 826500 6470500

Unknown CRS in QGIS when projecting to EPSG:25833 in R

I want to project a spatial data frame to EPSG 25833 in R but QGIS does not seem to know it (for reproducibility, I use the code jazzurro created in his/her answer to this question with minor changes)
library(rgdal)
mydf <- structure(list(longitude = c(128.6979, 153.0046, 104.3261, 124.9019,
126.7328, 153.2439, 142.8673, 152.689), latitude = c(-7.4197,
-4.7089, -6.7541, 4.7817, 2.1643, -5.65, 23.3882, -5.571)), .Names = c("longitude",
"latitude"), class = "data.frame", row.names = c(NA, -8L))
### Get long and lat from your data.frame. Make sure that the order is in lon/lat.
xy <- mydf[,c(1,2)]
# Here I use the projection EPSG:25833
spdf <- SpatialPointsDataFrame(coords = xy, data = mydf,
proj4string = CRS("+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"))
#Export as shapefile
writeOGR(spdf, "file location", "proj_test", driver="ESRI Shapefile",overwrite_layer = T) #now I write the subsetted network as a shapefile again
Now when I load the shapefile into QGIS it doesn´t know the projection.
Any ideas?
In making your SpatialPointsDataFrame:
# Wrong!
spdf <- SpatialPointsDataFrame(coords = xy, data = mydf,
proj4string = CRS("+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"))`
You are telling the data frame what crs your points are in, so you should specify 4326 since your original data is lon/lat.
So it should be:
spdf <- SpatialPointsDataFrame(coords = xy, data = mydf,
proj4string = CRS("+proj=longlat +datum=WGS84"))
And then you can transform the data to another CRS using spTransform:
spTransform(spdf, CRS('+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs'))
For this particular data, we get an error because one of the points doesn't convert to your target CRS:
Error in spTransform(xSP, CRSobj, ...) : failure in points 3 In
addition: Warning message: In spTransform(xSP, CRSobj, ...) : 1
projected point(s) not finite
I prefer working in sf so we could also do:
library(sf)
sfdf <- st_as_sf(mydf, coords = c('longitude', 'latitude'), crs=4326, remove=F)
sfdf_25833 <- sfdf %>% st_transform(25833)
sfdf_25833
#> Simple feature collection with 8 features and 2 fields (with 1 geometry empty)
#> geometry type: POINT
#> dimension: XY
#> bbox: xmin: 5589731 ymin: -19294970 xmax: 11478870 ymax: 19337710
#> epsg (SRID): 25833
#> proj4string: +proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
#> longitude latitude geometry
#> 1 128.6979 -7.4197 POINT (10198485 -17980649)
#> 2 153.0046 -4.7089 POINT (5636527 -19294974)
#> 3 104.3261 -6.7541 POINT EMPTY
#> 4 124.9019 4.7817 POINT (11478868 18432292)
#> 5 126.7328 2.1643 POINT (11046583 19337712)
#> 6 153.2439 -5.6500 POINT (5589731 -19158700)
#> 7 142.8673 23.3882 POINT (6353660 16093116)
#> 8 152.6890 -5.5710 POINT (5673080 -19163103)
and you can write and open with QGIS using:
write_sf(sfdf_25833, 'mysf.gpkg')

How to set proper projection for raster?

I'm trying to plot rainfall data from weather radar. Data file is 900x900 points matrix (900x900km). Projection informations from original cappi file:
<projection lat_lr="48.133400" lat_ul="56.186500" type="aeqd" lon_lr="25.157600" size_x="900" size_y="900" lon_ul="11.812900">
<lon_0>19.092600</lon_0>
<lat_0>52.346800</lat_0>
<ellps>+ellps=sphere</ellps>
</projection>
I'm reading data file (example: https://meteomodel.pl/examples/out.txt ) to matrix, and convert to raster:
a1 = as.matrix(read.table("/home/user/out.txt", header=F, as.is=TRUE))
a1[a1==0] <- NA
maxDBz <- 95.5
minDBz <- -31.5
step <- (maxDBz - minDBz) / 254
a1 <- minDBz + (a1 * step)
r <- raster(a1)
Then I'm trying to set extent and CRS:
e <- extent(11.812900, 25.157600, 48.133400, 56.186500)
r <- setExtent(r, e)
crs(r) <- "+proj=aeqd +lat_0=52.346800 +lon_0=19.092600 +x_0=900 +y_0=900 +ellps=sphere +datum=WGS84 +units=km +no_defs"
Data are plotted, however projection is incorrect:
https://meteomodel.pl/examples/Rplot01.png
Correct image from Polish Institute of Meteorology and Water Management:
https://meteomodel.pl/examples/cappi.png
What am I doing wrong?
What you are doing wrong is setting the extent using lon/lat crs, whereas the data have "+proj=aeqd. These need to match.
I do not know what the correct extent is, but you can approximate it like this:
p <- "+proj=aeqd +lat_0=52.346800 +lon_0=19.092600 +x_0=900 +y_0=900 +ellps=sphere +datum=WGS84 +units=km +no_defs"
e <- extent(11.812900, 25.157600, 48.133400, 56.186500)
r <- raster()
extent(r) <- e
rr <- projectExtent(r, p)
extent(rr)
#class : Extent
#xmin : -541.0182
#xmax : 452.2122
#ymin : -488.8849
#ymax : 431.1854
The txt file provided sugests that the extent you want is
e <- extent(-449997.470, 451000.522, -451003.637, 449998.274)
And that suggests that the units in your crs should be m, not km
p <- "+proj=aeqd +lat_0=52.346800 +lon_0=19.092600 +x_0=900 +y_0=900 +ellps=sphere +units=m "

What format should my data be in for a Home Range analysis in adehabitatHR?

I have latlong data of an animal tracked in South Africa and I'm using adehabitatHR for my analysis. Here's a sample of my data:
Latitude Longitude
-25.870265 27.947412
-25.816235 28.022442
-25.751107 28.1113
-25.670537 28.185403
-25.619823 28.290013
I need to transform my data so I can determine home range. I've been using this tutorial to help me and his data looks like it's in decimal degrees too http://www.mikemeredith.net/blog/1212_Data_for_home_range_analysis_in_R.htm
Here's my code:
library(raster)
library(rgdal)
library(maptools)
library(adehabitatHR)
library(sp)
data <- read.csv("stackoverflowEg.csv", sep = ",", header = T)
head(data)
coordinates(data) <- c("X", "Y")
proj4string(data) <- CRS("+init=epsg:4326")
## I got the projected coordinate system from here [http://epsg.io/22235][1]
track <- spTransform(data, CRS("+proj=longlat +init=epsg:22235"))
summary(track)
cp <- mcp(track, percent=95)
cp
The problem is that the resulting value for the mcp is too small (2.230023e-07) so I think I'm doing something wrong with my projections. Any help would be much appreciated.
Try this
coordinates(df) <- ~Longitude+Latitude
proj4string(df) <- CRS('+init=epsg:4326')
dfutm <- spTransform(df, CRS('+init=epsg:32735')) # utm 35S for SAfrica
dfutm
SpatialPoints:
Longitude Latitude
[1,] 594921.4 7138341
[2,] 602485.7 7144268
[3,] 611454.0 7151409
[4,] 618966.7 7160268
[5,] 629521.1 7165787
Coordinate Reference System (CRS) arguments: +init=epsg:32735 +proj=utm +zone=35 +south
+datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0

Convert latitude/longitude to state plane coordinates

I've got a dataset with latitude and longitude which I'd like to convert to the state plane coordinates for Illinois East, using EPSG 2790 (http://spatialreference.org/ref/epsg/2790/) or maybe ESRI 102672 (http://spatialreference.org/ref/esri/102672/).
This has definitely been asked before; my code is based on the answers here ("Non Finite Transformation Detected" in spTransform in rgdal R Package and http://r-sig-geo.2731867.n2.nabble.com/Converting-State-Plane-Coordinates-td5457204.html).
But for some reason I can't get it to work:
library(rgdal)
library(sp)
data = data.frame(long=c(41.20,40.05), lat=c(-86.14,-88.15))
coordinates(data) <- ~ long + lat
proj4string(data) <- CRS("+init=epsg:4326") # latitude/longitude
data.proj <- spTransform(data, CRS("+init=epsg:2790")) # illinois east
Gives:
non finite transformation detected:
long lat
41.20 -86.14 Inf Inf
Error in spTransform(data, CRS("+init=epsg:2790")) : failure in points 1
In addition: Warning message:
In spTransform(data, CRS("+init=epsg:2790")) :
2 projected point(s) not finite
Here's some more working code that clarifies what's going on:
# convert a state-plane coordinate to lat/long
data = data.frame(x=400000,y=0)
coordinates(data) <- ~ x+y
proj4string(data) <- CRS("+init=epsg:2804")
latlong = data.frame(spTransform(data, CRS("+init=epsg:4326")))
setnames(latlong,c("long","lat"))
latlong
gives:
long lat
1 -77 37.66667
and:
# convert a lat/long to state-plane
data = latlong
coordinates(data) <- ~ long+lat
proj4string(data) <- CRS("+init=epsg:4326")
xy = data.frame(spTransform(data, CRS("+init=epsg:2804")))
setnames(xy,c("y","x"))
xy
gives:
> xy
y x
1 4e+05 -2.690839e-08
And here's a function:
# this is for Maryland
lat_long_to_xy = function(lat,long) {
library(rgdal)
library(sp)
data = data.frame(long=long, lat=lat)
coordinates(data) <- ~ long+lat
proj4string(data) <- CRS("+init=epsg:4326")
xy = data.frame(spTransform(data, CRS("+init=epsg:2804")))
setnames(xy,c("y","x"))
return(xy[,c("x","y")])
}
When you set the coordinates for your data, you have to set the latitude before the longitude.
In other words, change:
coordinates(data) <- ~ long + lat
to
coordinates(data) <- ~ lat+long
And it should work.
library(rgdal)
library(sp)
data = data.frame(long=c(41.20,40.05), lat=c(-86.14,-88.15))
coordinates(data) <- ~ lat+long
proj4string(data) <- CRS("+init=epsg:4326")
data.proj <- spTransform(data, CRS("+init=epsg:2790"))
data.proj
Gave me this output:
SpatialPoints:
lat long
[1,] 483979.0 505572.6
[2,] 315643.7 375568.0
Coordinate Reference System (CRS) arguments: +init=epsg:2790 +proj=tmerc
+lat_0=36.66666666666666 +lon_0=-88.33333333333333 +k=0.9999749999999999 +x_0=300000
+y_0=0 +ellps=GRS80 +units=m +no_defs
I had an issue converting in the other direction and found this response on GIS Stack Exchange which may be helpful to future seekers. Depending on whether your coordinate system is NAD83 or NAD83 (HARN), the spatial reference epsg code will differ. If you use the wrong system, it may not be able to convert the point to values beyond the limits of any coordinate plane.
https://gis.stackexchange.com/questions/64654/choosing-the-correct-value-for-proj4string-for-shape-file-reading-in-r-maptools
Reading in lat+long versus long+lat does make a difference in the output- in my case (going from State Plane to WGS84) I had to write
coordinates(data) <- ~ long+lat
You can confirm this by plotting a known reference point to determine if it converted correctly.
The esri code (102649) in rgdal didn't work for me, I had to manually code it in from the proj4js page to go from state plane (0202 Arizona Central) to WGS84:
d<- data.frame(lon=XCord, lat=YCord)
coordinates(d) <- c("lon", "lat")
proj4string(d) <- CRS("+proj=tmerc +lat_0=31 +lon_0=-111.9166666666667 +k=0.9999 +x_0=213360 +y_0=0 +ellps=GRS80 +datum=NAD83 +to_meter=0.3048006096012192 +no_defs")
CRS.new <- CRS("+init=epsg:4326") # WGS 84
d.ch102649 <- spTransform(d, CRS.new)

Resources