Building OpenStreetMap with ggmap

I am trying to get the areas of Warsaw and draw them on a Google map. Using this code, where 2536107 is the relationship code for OpenStreetMap of one Warsaw region, gives me almost what I want, but with a few errors. There is a general outline, but also lines between points that should not be connected. What am I doing wrong?

map <- get_googlemap('warsaw', zoom =10) 
warszawa <- get_osm(relation(2536107), full = T)
warszawa.sp <- as_sp(warszawa, what='lines')
warsawfort <- fortify(warszawa.sp)

mapa_polski <- ggmap(map, extent='device', legend="bottomleft") 
warsawfort2 <- geom_polygon(aes(x = long, y = lat), 
               data = warsawfort, fill="blue", colour="black", 
               alpha=0.0, size = 0.3)

base <- mapa_polski + warsawfort2 
base

Edit: I decided that this should somehow be related to the construction order of each point / line, but I have no idea how to fix it.

+4
source share
2 answers

There is a way to create your map without using external packages: do not use osmar...

, - Mapzen, . , warsaw.osm-admin.*. , osm_id (!!). , .

library(ggmap)
library(ggplot2)
library(rgdal)

setwd(" <directory with your shapefiles> ")
pol    <- readOGR(dsn=".",layer="warsaw.osm-admin")
spp    <- pol[pol$osm_id==-2536107,]
wgs.84 <- "+proj=longlat +datum=WGS84"
spp    <- spTransform(spp,CRS(wgs.84))

map    <- get_googlemap('warsaw', zoom =10) 
spp.df <- fortify(spp)

ggmap(map, extent='device', legend="bottomleft") +
  geom_polygon(data = spp.df, aes(x = long, y=lat, group=group), 
               fill="blue", alpha=0.2) +
  geom_path(data=spp.df, aes(x=long, y=lat, group=group), 
            color="gray50", size=0.3)

: (1) osm , , ,

spp    <- pol[pol$osm_id==-2536107,]

, (2) WGS84 (long/lat). , :

spp    <- spTransform(spp,CRS(wgs.84))

osmar , . warszawa.sp - SpatialLinesDataframe, (12 ), . fortify(...), ggplot . , ggplot , , , -, , -. . , :

xx=coordinates(warszawa.sp)
colors=rainbow(11)
plot(t(bbox(warszawa.sp)))
lapply(1:11,function(i)lines(xx[[i]][[1]],col=colors[i],lwd=2))

"" (, , , ..). , .

@ako.

"" SpatialLines, . gPolygonize(...) rgeos SpatialLines SpatialPolygons, ggplot fortify(...). ( , ) , OP warszaw.sp 12 , - gPolygonize(...). , SpatialLines 11 , warszawa.sp . , , SpatialLines, osm. , , .

library(rgeos)
coords <- coordinates(warszawa.sp)
sll <- lapply(coords[1:11],function(x) SpatialLines(list(Lines(list(Line(x[[1]])),ID=1))))
spp <- gPolygonize(sll)
spp.df <- fortify(spp)
ggmap(map, extent='device', legend="bottomleft") +
  geom_polygon(data = spp.df, aes(x = long, y=lat, group=group), 
               fill="blue", alpha=0.2) +
  geom_path(data=spp.df, aes(x=long, y=lat, group=group), 
            color="gray50", size=0.3)
+9

, - . , group = id, , , .

, , - script. Qgis' line to polygon "", , ArcMap, . , . - . , , RGDAL , , .

. enter image description here

+2

Source: https://habr.com/ru/post/1547809/


All Articles