-1
library(ggmap)
library(rgdal)

cities<-c("city A","city B")
country<-readOGR(dsn=".",layer="country")
gc<-geocode(cities,source="google")
gc$names<-c("city A","city B")
coordinates(gc)<-~lon+lat
proj4string(gc)<-proj4string(country) #because the "gc" doesnt have a crs at the moment
gc<-spTransform(gc,CRS(proj4string(country)))
plot(country)
plot(gc,add=T)

Result is that the points are not in the map at all. What is wrong? CRS, projection is the same, so it should have been working.

The point of this endeavor is to make the "cities" to appear in the shapefile of the country. Something in the projection has to be wrong although they appear to be the same.

The reason i put proj4string(gc)<-proj4string(country) before the spTransform line is because you can't use the spTransform function on something that has NA values for projection.

  • 2
    Please use words to introduce code. A description of the environment, and what you are attempting to accomplish are critical to determine whether you are proceeding correctly. – Vince Oct 18 '15 at 17:57
  • You are assigning an assumed CRS to 'gc' - then spTransform-ing it to the same CRS, which does nothing. You need to investigate the coordinates of gc (bbox(gc) or summary()) and see what you get - perhaps edit and paste the results of summary() in the question here? – Simbamangu Oct 19 '15 at 4:54
  • Doing, proj4string(gc) <- CRS("+proj=longlat +datum=WGS84") to the empty gc before the spTransform, made it appear in the right place.Do you know why it worked? – gsa Oct 19 '15 at 13:39
  • It will work - or seem to work - if the CRS of the 'empty' gc is the same or similar to 4326 (WGS84 latlon). Might appear to work (but not be accurate) if it's another unprojected CRS, like Arc 1960. – Simbamangu Oct 19 '15 at 14:13
4

Your example is not reproducible and you do not use library( ) statements to show which packages you are using. This makes it difficult to help you.

You state that "CRS, projection is the same" but you provide no evidence for that. What I do know is that these lines make no sense:

proj4string(gc) <- proj4string(country)
gc <- spTransform(gc, CRS(proj4string(country)))

You first declare the coordinates of gc to have the same CRS as country. In the next step you transform the coordinates to the CRS that they already have, which is pointless. Perhaps check this discussion.

I assume that geocode returns lon/lat values. If so, you probably should do

proj4string(gc) <- CRS("+proj=longlat +datum=WGS84")
gc <- spTransform(gc, CRS(proj4string(country)))
| improve this answer | |
  • Just for others' sake, geocode is from ggmap – hrbrmstr Oct 19 '15 at 14:58
  • It also exists in dismo and who knows where else. – Robert Hijmans Oct 19 '15 at 16:53
  • no, this is ggmap. source="google" kinda gives it away. – hrbrmstr Oct 19 '15 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.