collecting elevation data - extract geolocations from a geotiff file - r

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

Related

Converting coordinates, radius and site type data from .csv to raster in R

I am having a bit of a problem converting .csv files into raster in R... My .csv file contains coordinates (long and lat) radius (in deg) and site type. I was able to convert the coordinates into raster and was able to plot the circles using st_buffer() but I am facing two problems:
I can't convert the circles into a raster... I tried with rasterize() and fasterize() and both did not work all I'm getting is an empty raster layer
I can't seem to classify the coordinates and circles according to the site type
Any idea of what I might be doing wrong? and how can I classify my circles?
Thank you in advance!
Here is the code I used:
> head(sp_csv_data)
Longitude Latitude Radius Site_Type
1 -177.87567 -24.715167 10 MIG
2 -83.21360 14.401800 1 OBS
3 -82.59392 9.589192 1 NES
4 -82.41060 9.492750 1 NES;BRE
5 -81.17555 7.196750 5 OBS
6 -80.95770 8.852700 1 NES
##Projection systems used
rob_pacific <- "+proj=robin +lon_0=180 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs" # Best to define these first so you don't make mistakes below
longlat <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
####Converting to raster####
# Creating a empty raster at 0.5° resolution (you can increase the resolution to get a better border precision)
rs <- raster(ncol = 360*2, nrow = 180*2)
rs[] <- 1:ncell(rs)
crs(rs) <- CRS(longlat)
##Converting to raster
sp_raster <- rasterize(sp_csv_data[,1:2], rs, sp_csv_data[,3])
# Resampling to make sure that it's in the same resolution as sampling area
sp_raster <- resample(sp_raster, rs, resample = "ngb")
#converting into an sf spatial polygon dataframe
sp_raster <- as(sp_raster, "SpatialPolygonsDataFrame")
species_sp <- spTransform(sp_raster, CRS(longlat))
# Define a long & slim polygon that overlaps the meridian line & set its CRS to match that of world
polygon <- st_polygon(x = list(rbind(c(-0.0001, 90),
c(0, 90),
c(0, -90),
c(-0.0001, -90),
c(-0.0001, 90)))) %>%
st_sfc() %>%
st_set_crs(longlat)
# Transform the species distribution polygon object to a Pacific-centred projection polygon object
sp_robinson <- species_sp %>%
st_as_sf() %>%
st_difference(polygon) %>%
st_transform(crs = rob_pacific)
# There is a line in the middle of Antarctica. This is because we have split the map after reprojection. We need to fix this:
bbox1 <- st_bbox(sp_robinson)
bbox1[c(1,3)] <- c(-1e-5,1e-5)
polygon1 <- st_as_sfc(bbox1)
crosses1 <- sp_robinson %>%
st_intersects(polygon1) %>%
sapply(length) %>%
as.logical %>%
which
# Adding buffer 0
sp_robinson[crosses1, ] %<>%
st_buffer(0)
# Adding the circles to the coordinates
sp_robinson2 <- st_buffer(sp_robinson, dist = radius)
> print(sp_robinson2)
Simple feature collection with 143 features and 1 field
geometry type: POLYGON
dimension: XY
bbox: xmin: -17188220 ymin: -5706207 xmax: 17263210 ymax: 6179000
CRS: +proj=robin +lon_0=180 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs
First 10 features:
layer geometry
1 5 POLYGON ((3556791 4766657, ...
2 10 POLYGON ((13713529 4995696,...
3 10 POLYGON ((12834403 4946927,...
4 10 POLYGON ((9991443 4801974, ...
5 5 POLYGON ((4254202 4304190, ...
6 5 POLYGON ((11423719 4327354,...
7 10 POLYGON ((9582710 4282247, ...
8 10 POLYGON ((588877.2 4166512,...
9 5 POLYGON ((4522824 3894919, ...
10 10 POLYGON ((3828685 3886205, ...
sp_robinson3 <- fasterize(sp_robinson2, rs)
> print(sp_robinson3)
class : RasterLayer
dimensions : 360, 720, 259200 (nrow, ncol, ncell)
resolution : 0.5, 0.5 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
crs : +proj=robin +lon_0=180 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
source : memory
names : layer
values : NA, NA (min, max)
I want to convert sp_robinson2 into a raster called sp_robinson3 but as you can see both fasterize()and rasterize()are giving me an empty raster layer...
The reason why rasterize does not work in the end is obvious: the crs of the vector and raster do not match. But can you edit your question a bit more to explain what you want to achieve? It is very odd to create a raster and then polygons and then rasterize these again. My impression is that you are making things much more complicated than need be. You also talk about circles. Which circles? I am guessing you may want circles around your points, but that is not what you are doing. It would probably be helpful to figure out things step by step, first figure out how to get the general result you want, then how to get it Pacific centered.
Below is a cleaned up version of the first part of your code. It also makes it reproducible. You need to create example in code, like this:
lon <- c(-177.87567, -83.2136, -82.59392, -82.4106, -81.17555, -80.9577)
lat <- c(-24.715167, 14.4018, 9.589192, 9.49275, 7.19675, 8.8527)
radius <- c(10, 1, 1, 1, 5, 1)
site <- c("MIG", "OBS", "NES", "NES;BRE", "OBS", "NES")
sp_csv_data <- data.frame(lon, lat, radius, site)
## cleaned up projection definitions
rob_pacific <- "+proj=robin +lon_0=180 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs"
longlat <- "+proj=longlat +datum=WGS84"
##Converting to raster
# Creating a empty raster at 0.5° resolution
rs <- raster(res=0.5, crs=lonlat)
values(rs) <- 1:ncell(rs)
sp_raster <- rasterize(sp_csv_data[,1:2], rs, sp_csv_data[,3])
## makes no sense: sp_raster <- resample(sp_raster, rs, resample = "ngb")
#converting into an SpatialPolygonsDataframe
species_sp <- as(sp_raster, "SpatialPolygonsDataFrame")
## makes no sense: species_sp <- spTransform(sp_raster, CRS(longlat))

Changing WGS84 to EPSG:5330 in R

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

Switching between two CRS in R (with rgdal)

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

How to find the 4 coordinates (lat-long) of a pixel (cell) in a global file?

I am using a climate variable that can be downloaded from here:
ftp://sidads.colorado.edu/pub/DATASETS/nsidc0301_amsre_ease_grid_tbs/global/
This file is a binary (matrix) file with 586 lines and 1383 columns (global map).
I would like to know the 4 coordinates (lat-long) of a pixel (cell)`.
more info about the file :
These data are provided in EASE-Grid projections global cylindricalat 25 km resolution, are two-
byte
Spatial Coordinates:
N: 90° S: -90° E: 180° W: -180°
use the raster package and convert the data to raster objects:
file<- readBin("ID2r1-AMSRE-ML2010001A.v03.06H", integer(), size=2, n=586*1383, signed=T)
m = matrix(data=file,ncol=1383,nrow=586,byrow=TRUE)
r = raster(m, xmn=-180, xmx=180, ymn=-90, ymx=90)
Now the file is a properly spatially referenced object, but without a full specification of the cylindrical projection used you can't get back to lat-long coordinates.
There some more info here http://nsidc.org/data/ease/tools.html including a link to some grids that have the lat-long of grid cells for that grid system:
ftp://sidads.colorado.edu/pub/tools/easegrid/lowres_latlon/
MLLATLSB "latitude"
MLLonLSB "longitude"
so for example we can create a raster of latitude for the cells in your data grid:
> lat <- readBin("MLLATLSB",integer(), size=4, n=586*1383, endian="little")/100000
> latm = matrix(data=lat,ncol=1383,nrow=586,byrow=TRUE)
> latr = raster(latm, xmn=-180, xmx=180, ymn=-90, ymx=90)
and then latr[450,123] is the latitude of cell [450,123] in my data. Repeat with MLLONLSB for longitude.
However this is not sufficient(one lat and long for each pixel) as I would like to compare with ground based measurements so I need to define my region which correspond to this pixel (25 * 25 km or 0.25 degrees). for this purpose I have to know the 4 coordinates (lat-long) of that pixel (cell).
Thanks for any help
EASE GRID uses a Global Cylindrical Equal-Area Projection defined by EPSG 3410, which is metric. As long as I see it, spatial extent should be provided in meters, not geographic coordinates.
From here we see that map extent coordinates are:
xmin: -17609785.303313
ymin: -7389030.516717
xmax: 17698276.686747
ymax: 7300539.133283
So slightly changing your code we have this
library(raster)
library('rgdal')
wdata <- 'D:/Programacao/R/Raster/Stackoverflow'
wshp <- 'S:/Vetor/Administrativo/Portugal'
#setwd(wdata)
file <- readBin(file.path(wdata, "ID2r1-AMSRE-ML2010001D.v03.06H"),
integer(), size=2, n=586 * 1383, signed=T)
m <- matrix(data = file, ncol = 1383, nrow = 586, byrow = TRUE)
-17609785.303313 -7389030.516717 17698276.686747 7300539.133283
rm <- raster(m, xmn = -17609785.303313, xmx = 17698276.686747,
ymn = -7389030.516717, ymx = 7300539.133283)
proj4string(rm) <- CRS('+init=epsg:3410')
> rm
class : RasterLayer
dimensions : 586, 1383, 810438 (nrow, ncol, ncell)
resolution : 25530.05, 25067.53 (x, y)
extent : -17609785, 17698277, -7389031, 7300539 (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:3410 +proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +a=6371228 +b=6371228 +units=m +no_defs
data source : in memory
names : layer
values : 0, 3194 (min, max)
writeRaster(rm, file.path('S:/Temporarios', 'easegrridtest.tif'), overwrite = TRUE)
plot(rm, asp = 1)
We can now overlay some spatial data
afr <- readOGR(dsn = file.path(wdata), layer = 'Africa_final1_dd84')
proj4string(afr) <- CRS('+init=epsg:4326') # Asign projection
afr1 <- spTransform(afr, CRS(proj4string(rm)))
plot(afr1, add = T)
Now you can start playing with extract extent of your ROI, possibly with extent()
I'm not happy with the spatial adjustment. With this approach I got a huge error. I'm not sure about the position error but it is described for this product. Maybe something with the extent parameters.
Since you are interested in use it against ground measures, you could polygonize your ROI and use it in your GPS or GIS.
Also you could get extent of cells of interest with something like:
Choose an aproximate coordinate, identify cell and get the extent:
cell <- cellFromXY(rm, matrix(c('x'= -150000, 'y' =200000), nrow = 1, byrow = T))
r2 <- rasterFromCells(rm, cell, values=TRUE)
extent(r2)
class : Extent
xmin : -172759.8
xmax : -147229.7
ymin : 181362
ymax : 206429.6
And maybe identify a ROI (single cell) in a map (or plot)
cell <- cellFromXY(rm, matrix(c('x'= -1538000, 'y' =1748000), nrow = 1, byrow = T))
r2 <- rasterFromCells(rm, cell, values=TRUE)
r2p <- as(r2, 'SpatialPolygons')
extr2 <- extent(r2) + 300000
plot(rm, col = heat.colors(6), axes = T, ext = extr2)
plot(afr1, add = T, col = 'grey70')
plot(r2p, add = T)
For a particular area
And assuming that by cell you mean column and row values, you could proceed with raster::cellFromRowCol
cell2 <- cellFromRowCol(rm, rownr = mycellnrow, colnr = mycellnrow)
r3 <- rasterFromCells(rm, cell2, values=TRUE)
r3p <- as(r3, 'SpatialPolygons')
extr3 <- extent(r3) + 3000000
In this particular case, 123 and 450 seems to be far from any continental area...
Hope it helps.
More info on AMSR-E/Aqua Daily Gridded Brightness Temperatures here

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