Reputation: 1017
Hello stackoverflow community!
Could someone kindly please assist me with an R GIS issue I am facing. I am trying to assign an identifiable variable to a list of addresses that I have geocoded. The geocodes were obtained from Google Maps API, and I therefore have latitude and longitude information (i.e., -155.6019 18.99883). I would like to use this information with a particular shape file. My dilema is that the shape file I have is not using the same latitude and longitude system. I have attached the code so that you can see what coordinate system is used in the shape file (i.e., 843662.6 2132942).
What I would like to please know is how to match the coordinates between my address list and this shape file so that I may use the "overlay" function to match the two together.
Thank you for your time!
#function to download shapefile from web
dlshapefile <- function(shploc,shpfile) {
temp <- tempfile()
download.file(shploc, temp)
unzip(temp)
}
temp <- tempfile()
require(maptools)
dlshapefile(shploc="http://files.hawaii.gov/dbedt/op/gis/data/highdist_n83.shp.zip", temp)
P4S.latlon <- CRS("+proj=longlat +datum=WGS84")
shapeFile <- readShapePoly("highdist_n83.shp", verbose=TRUE, proj4string=P4S.latlon)
Upvotes: 3
Views: 4353
Reputation: 7997
I would use readOGR
from the rgdal
package as that preserves the projection information.
require(rgdal)
in.dir <- "your_directory_name"
sh <- readOGR(in.dir, layer = "highdist_n83")
sh@proj4string # note projection details
sh2 <- spTransform(sh, CRS("+proj=longlat +datum=WGS84"))
sh2@proj4string # and after transforming
plot(sh2)
This gives me:
> sh@proj4string # note projection details
CRS arguments:
+proj=utm +zone=4 +datum=NAD83 +units=m +no_defs +ellps=GRS80
+towgs84=0,0,0
and:
> sh2@proj4string # and after transforming
CRS arguments:
+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
See if that works for you with the address data you wish to add.
EDIT If you want to go down the ggplot2
route you could do something like this:
# Plotting with ggplot - first transform shapefile to data frame
sh2.df <- fortify(sh2)
# Fake address data taken from Google maps
add <- data.frame(name = c("University of Hawaii", "Mokuleia Beach Park"),
lat = c(21.298971, 21.580508),
long = c(-157.817722, -158.191017))
# And plot
ggplot(data = sh2.df, aes(x = long, y = lat, group = group)) +
geom_polygon(colour = "grey20", fill = "white", size = 0.8) +
geom_point(data = add, size = 3, aes(x = long, y = lat, group = NULL), pch = 21, fill = "red") +
coord_equal() +
theme()
Which gives this:
Upvotes: 7