I would like to change my coordinate form WGS84 to EPSG:5330. Hope, anyone can help me thanks
ID,X,Y
1,106.6874498,-6.2107887
2,106.6883199,-6.2069667
Easy-cheesy with the sp library.
library(sp)
# Create SpatialPoints out of coordinates.
# Assign WGS84 (EPSG 4326) coordinate reference system.
pts <- SpatialPoints(coords = data.frame(x = c(106.6874498, 106.6883199),
y = c(-6.2107887, -6.2069667)),
proj4string = CRS("+init=epsg:4326"))
# Transform SpatialPoints to EPSG 5330.
pts_epsg5330 <- spTransform(x = pts, CRSobj = CRS("+init=epsg:5330"))
Result:
# Get coordinates of new SpatialPoints.
> coordinates(pts_epsg5330)
x y
[1,] 3532231 213991.4
[2,] 3532328 214415.3
Related
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
I am trying to add elevation data to a plot using the rayshader package. I can plot the area in which I want to find elevation data using the leaflet package.
library(whitebox)
library(leaflet)
library(rayshader)
library(rayrender)
library(raster)
# define bounding box with longitude/latitude coordinates
bbox <- list(
p1 = list(long = -3.6525599, lat = 40.4065001),
p2 = list(long = -3.7525599, lat = 40.4965001)
)
leaflet() %>%
addTiles() %>%
addRectangles(
lng1 = bbox$p1$long, lat1 = bbox$p1$lat,
lng2 = bbox$p2$long, lat2 = bbox$p2$lat,
fillColor = "transparent"
) %>%
fitBounds(
lng1 = bbox$p1$long, lat1 = bbox$p1$lat,
lng2 = bbox$p2$long, lat2 = bbox$p2$lat,
)
I am following this blog: https://wcmbishop.github.io/rayshader-demo/ and currently at the part:
"Downloading Elevation Data"
The author uses the USGS data collected from this link:
https://elevation.nationalmap.gov/arcgis/rest/services/3DEPElevation/ImageServer/exportImage?bbox=-122.522%2C37.707%2C-122.354%2C37.84&bboxSR=4326&size=600%2C480&imageSR=4326&time=&format=jpgpng&pixelType=F32&noData=&noDataInterpretation=esriNoDataMatchAny&interpolation=+RSP_BilinearInterpolation&compression=&compressionQuality=&bandIds=&mosaicRule=&renderingRule=&f=html
I replaced the lat, long coordinates in the link with mine:
https://elevation.nationalmap.gov/arcgis/rest/services/3DEPElevation/ImageServer/exportImage?bbox=-3.6525599%2C40.4065001%2C-3.7525599%2C40.4965001&bboxSR=4326&size=600%2C480&imageSR=4326&time=&format=jpgpng&pixelType=F32&noData=&noDataInterpretation=esriNoDataMatchAny&interpolation=+RSP_BilinearInterpolation&compression=&compressionQuality=&bandIds=&mosaicRule=&renderingRule=&f=html
Which returned an empty image - which is no surprise since the USGS is a U.S. data provider. So I download the following file:
The DTM Spain Mainland 20m http://data.opendataportal.at/dataset/dtm-spain/resource/38816df0-9d50-476f-832e-0f7f5fa21771
The file is 2.4 GB in size and is a file for the whole of Spain but I only want the elevation data for the bounds of my lat & long points.
rgdal::GDALinfo("DTM Spain_Mainland (2019) 20m.tif")
rows 48394
columns 58187
bands 1
lower left origin.x -23380
lower left origin.y 3901190
res.x 20
res.y 20
ysign -1
oblique.x 0
oblique.y 0
driver GTiff
projection +proj=utm +zone=30 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m
+no_defs
file DTM Spain_Mainland (2019) 20m.tif
apparent band summary:
GDType hasNoDataValue NoDataValue blockSize1 blockSize2
1 Float32 TRUE -32767 1 58187
apparent band statistics:
Bmin Bmax Bmean Bsd
1 -4294967295 4294967295 NA NA
Metadata:
AREA_OR_POINT=Point
My question is, how can I either get the geotiff for the long and lat positions in bbox or filter the current DTM Spain_Mainland (2019) 20m.tif file down to just the long, lat coordinates?
You could have a look at the elevatr package, which will give you the raster you want:
library(elevatr)
library(raster)
bbox2 <- data.frame(x = c(-3.6525599, -3.7525599), y = c(40.4065001, 40.4965001))
elev <- get_elev_raster(bbox2, z = 13, clip = "bbox",
prj = "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
I'm (trying to) do operations on pairs of geographical points. I have the coordinates of my points in WGS84, and I need to have them in the Lambert 2 Extended CRS (LIIE). I'm trying to do it using rgdal.
Here's what I'm doing :
library("rgdal")
library("sp")
# Loading CRS
WGS84<-"+proj=longlat +ellps=WGS84 +towgs84=0,0,0,-0,-0,-0,0 +no_defs"
LIIE<-"+proj=lcc +lat_1=46.8 +lat_0=46.8 +lon_0=0 +k_0=0.99987742 +x_0=600000 +y_0=2200000 +a=6378249.2 +b=6356515 +towgs84=-168,-60,320,0,0,0,0 +pm=paris +units=m +no_defs"
# Loading the pairs of points
matrix<-read.table(file="file_with_coordinates.csv", header=TRUE, sep=";", stringsAsFactors = F)
The columns of matrix are as follow : origin_id, destination_id, or_lon, or_lat, de_lon, de_lat. Obviously, only the last 4 columns need to be transformed from WGS84 to LIIE.
I'm able to transform the coordinates by doing this :
matrix_sp<-SpatialPoints(coords = od_matrix[,c("de_lon","de_lat","or_lon","or_lat")],proj4string = CRS(WGS84))
matrix_sp_liie<-spTransform(od_matrix_sp, CRSobj = CRS(LIIE))
matrix_liie<-data.frame(matrix_sp_liie)
However, I therefore lose the origin and destination IDs... (And I don't have anything left that could allow me to merge back together matrix_liie with the origin/destination ids in matrix_sp).
I tried this (it's basically the same code but with destination_id and oririgin_id included in the first line), but I couldn't really get to something interesting (I get a Error in .local(obj, ...) : cannot derive coordinates from non-numeric matrix error).
od_matrix_sp<-SpatialPoints(coords = od_matrix[,c("destination_id","oririgin_id","de_lon","de_lat","or_lon","or_lat")],proj4string = CRS(WGS84))
matrix_sp_liie<-spTransform(od_matrix_sp, CRSobj = CRS(LIIE))
matrix_liie<-data.frame(matrix_sp_liie)
Any idea on how I could achieve this ?
Thanks.
Sample from CSV :
origin_id destination_id or_lon or_lat de_lon de_lat
123_a 005 3.88 45.6 1.56 46.7
123_b 006 5.10 41.1 2.4 42.6
Hi it's sp that does the conversion, and you can do that without use SpatialPoints, just specify which columns in matrix are the coordinates with coordinates, here an example :
library("sp")
# Some coordinates
latlong <- data.frame(
ID = 1:8,
LETTERS = LETTERS[1:8],
lon = runif(n = 8, min = 2.0798, max = 2.9931),
lat = runif(n = 8, min = 48.6823, max = 49.0698)
)
# Loading CRS
WGS84<-"+proj=longlat +ellps=WGS84 +towgs84=0,0,0,-0,-0,-0,0 +no_defs"
LIIE<-"+proj=lcc +lat_1=46.8 +lat_0=46.8 +lon_0=0 +k_0=0.99987742 +x_0=600000 +y_0=2200000 +a=6378249.2 +b=6356515 +towgs84=-168,-60,320,0,0,0,0 +pm=paris +units=m +no_defs"
# Set which var are the coord
coordinates(latlong) <- c("lon", "lat")
# Set the proj
proj4string(latlong) <- CRS(WGS84)
# Convert
latlon_conv <- spTransform(x = latlong, CRSobj = CRS(LIIE))
# Back to data.frame
latlon_conv <- as.data.frame(latlon_conv)
latlon_conv # maybe change columns names...
# ID LETTERS lon lat
# 1 1 A 632441.1 2440172
# 2 2 B 633736.7 2434332
# 3 3 C 586298.5 2411320
# 4 4 D 645107.6 2410351
# 5 5 E 642454.6 2443052
# 6 6 F 628371.7 2448833
# 7 7 G 625445.7 2436324
# 8 8 H 624509.7 2443864
EDIT : After seeing #Spacedman comment, you can effectively use SpatialPointsDataFrame instead of SpatialPoints :
latlong.sp <- SpatialPointsDataFrame(
coords = latlong[, c("lon", "lat")],
data = latlong[, c("ID", "LETTERS")],
proj4string = CRS(WGS84)
)
latlon_conv <- spTransform(x = latlong.sp, CRSobj = CRS(LIIE))
latlon_conv.df <- as.data.frame(latlon_conv)
latlon_conv.df
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
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)