8

我是空间数据的新手。我有以下代码可以成功绘制有界地图。我想补充一点,data.frame 存储的点。对于无法从 OpenStreetMap 文档中弄清楚这一点,我提前道歉......下面的代码:

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.

我怀疑商店的格式不适合空间数据。

4

2 回答 2

10

我不知道OpenStreetMap包裹。但我提供了一个仍然绘制 OpenStreet 地图的替代方案,但使用ggmap包来获取和绘制地图。该get_map功能可以从多种来源获取地图:osm、google、stamen和cloudmade;设置为source。此外,源具有不同的样式,设置为maptype. 地图的边界在位置向量中给出。或者,位置矢量可以给地图中心设置适当的缩放级别。地图是用 绘制的ggplot2,因此可以将点和标签添加到地图中,就像它们被添加到任何 ggplot 对象一样。要运行以下命令,需要安装ggmap和包。ggplot2

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)

结果是:

在此处输入图像描述

标签可能会在后台丢失。在这种情况下,这样的事情可能是合适的。它使用此处的代码为文本提供大纲。

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)

在此处输入图像描述

于 2012-06-15T23:23:02.417 回答
2

由于 google startend 需要使用他们的 api 的计费信息,这里是一个仅使用 OpenStreetMaps 的解决方案。

样本数据

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

OSM-map在墨卡托,存储坐标是经纬度,所以:
方法1:将地图转换为纬度
方法2:将坐标转换为墨卡托

方法 1 - 将地图转换为纬度

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

在此处输入图像描述

方法 2 - 将点转换为墨卡托

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

在此处输入图像描述

于 2019-03-05T14:53:11.130 回答