Plotting points from a data.frame using OpenStreetMap

南笙酒味 提交于 2019-11-30 12:18:33

问题


I am a complete novice with spacial data. I have the following code that successfully plots a bounded map. I would like to add, as points the data.frame stores. I apologize in advance for not being able to figure this out from the OpenStreetMap documentation...code below:

library(OpenStreetMap)
stores <- data.frame(name=c("Commercial","Union","Bedford"),
                 longitude=c(-70.25042295455933,-70.26050806045532,-70.27726650238037),
                 latitude=c(43.657471302616806,43.65663299041943,43.66091757424481))
lat <- c(43.68093,43.64278)
lon <- c(-70.29548,-70.24097)
portland <- openmap(c(lat[1],lon[1]),c(lat[2],lon[2]),zoom=15,'osm')
plot(portland,raster=TRUE)
#can't figure out what to put here.

I suspect the format of stores isn't proper for spatial data.


回答1:


I don't know the OpenStreetMap package. But I offer an alternative that still draws an OpenStreet Map, but uses the ggmap package to fetch and draw the map. The get_map function can fetch maps from a variety of sources: osm, google, stamen, and cloudmade; set with source. In addition the sources have different styles, set with maptype. The boundary of the map is given in a location vector. Alternatively, the location vector can give the centre of the map with an appropriate zoom level set. The map is drawn with ggplot2, and so points and labels can be added to the map as if they were being added to any ggplot object. To run the following, the ggmap and ggplot2 packages need to be installed.

library(ggmap)

stores <- data.frame(name=c("Commercial","Union","Bedford"),
        longitude=c(-70.25042295455933,-70.26050806045532,-70.27726650238037),
        latitude=c(43.657471302616806,43.65663299041943,43.66091757424481))
location = c(-70.2954, 43.64278, -70.2350, 43.68093)

# Fetch the map
portland = get_map(location = location, source = "osm")

# Draw the map
portlandMap = ggmap(portland)

# Add the points layer
portlandMap = portlandMap + geom_point(data = stores, aes(x = longitude, y = latitude), size = 5)

# Add the labels
portlandMap + geom_text(data = stores, aes(label = name, x = longitude+.001, y = latitude), hjust = 0)

The result is:

The labels could be getting lost in the background. In that case, something like this might be appropriate. It uses code from here to give the text an outline.

portlandMap = ggmap(portland) + geom_point(data = stores, aes(x = longitude, y = latitude), size = 5)

theta <- seq(pi/16, 2*pi, length.out=32)
xo <- diff(location[c(1,3)])/250
yo <- diff(location[c(2,4)])/250

for(i in theta) {
    portlandMap <- portlandMap + geom_text(data = stores,  
    aes_(x = stores$longitude + .001 + cos(i) * xo, 
         y = stores$latitude + sin(i) * yo, 
         label = stores$name), 
    size = 5, colour = 'black', hjust = 0)
 }

portlandMap + 
   geom_text(data = stores, aes(x = longitude + .001, y = latitude, label=name), 
     size = 5, colour = 'white', hjust = 0)




回答2:


Since google startend requiring billing information for using their api, here is a solution using OpenStreetMaps only.

sample data

library( OpenStreetMap )
library( ggplot2 )
stores <- data.frame(name=c("Commercial","Union","Bedford"),
                     longitude=c(-70.25042295455933,-70.26050806045532,-70.27726650238037),
                     latitude=c(43.657471302616806,43.65663299041943,43.66091757424481))
lat <- c(43.68093,43.64278)
lon <- c(-70.29548,-70.24097)

portland <- OpenStreetMap::openmap( c( lat[1], lon[1] ), c( lat[2], lon[2] ),zoom = 15, 'osm')

the OSM-map is in mercator, and the store-coordinates are lat-lon, so:
method 1: convert the map to latlon
method 2: convert or the coordinates to mercator

method 1 - convert map to latlon

autoplot( OpenStreetMap::openproj( portland ) ) +
  geom_point( data = stores, aes( x = longitude, y = latitude, size = 5 ) ) +
  geom_text( data = stores, aes( x = longitude + .001, y = latitude, label = name ), hjust = 0 ) +
  theme( legend.position = "none" )

method 2 - convert points to mercator

stores2 <- as.data.frame( 
  OpenStreetMap::projectMercator( lat = stores$latitude, 
                                  long = stores$longitude ) 
  )
stores2 <- cbind( stores, stores2)
autoplot( portland ) +
  geom_point( data = stores2, aes( x = x, y = y, size = 5 ) ) +
  geom_text( data = stores2, aes( x = x + 100, y = y, label = name ), hjust = 0 ) +
  theme( legend.position = "none" )



来源:https://stackoverflow.com/questions/11056738/plotting-points-from-a-data-frame-using-openstreetmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!