[R-sig-Geo] display location on google earth
Barry Rowlingson
b.rowlingson at lancaster.ac.uk
Thu Dec 29 19:19:30 CET 2016
You made a couple of little mistakes! See below:
On Thu, Dec 29, 2016 at 5:51 PM, Felipe Carrillo via R-sig-Geo
<r-sig-geo at r-project.org> wrote:
> Hi;I have a shapefile named 'Wolf' that I can display in google with the following code: # Show the shapefile on top of google
> writeOGR(Wolf, "Wolf.kml", "Wolf", driver="KML")
> system("open Wolf.kml")
>
> However, I am having problems with a single set of coordinates that I would like to use to display a location.Could someone show me how to display on google with the following coordinates?
> Latitude 38°34'02", Longitude 121°23'14" NAD27
> I have tried:
> require(rgdal)
> library(sp)
You've defined lat and lon here as characters - these should be numeric:
> lat <- '38.34477586'
> lon <- '-121.23387625'
lon <- -121.23387625
lat <- 38.34477586
> # make a data frame
> coords <- as.data.frame(cbind(lon=lon,lat=lat))
> coords
> # make it a spatial object
> coordinates(coords) <- ~lat+lon
Here you've got your lat-lon the wrong way round. coordinate
assignment wants lon first, so:
coordinates(coords)=~lon+lat
> coords <- spTransform(coords)
>
> But I am getting the following error:
> Error in (function (classes, fdef, mtable) :
> unable to find an inherited method for function ‘spTransform’ for signature ‘"CRS", "missing"’
That error is because you've tried to transform something without giving a CRS.
You seem to have lat-long in NAD27 datum, which I think is EPSG code
4267. So first give it that code:
proj4string(coords)=CRS("+init=epsg:4267")
and now you can convert to whatever you want:
spTransform(coords, "+init=epsg:4326")
SpatialPoints:
lon lat
1 -121.2349 38.34469
Coordinate Reference System (CRS) arguments: +init=epsg:4326
+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
Which seems to concur with this converter tool:
http://tagis.dep.wv.gov/convert/
which returns:
38.344689,-121.234935
but I'm no expert when it comes to datum shifts in CRS transformations...
>
>
>
>
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
More information about the R-sig-Geo
mailing list