Showing posts with label osmar. Show all posts
Showing posts with label osmar. Show all posts

Sunday, September 20, 2015

Convert OpenStreetMap Objects to KML with R

A quick geo-tip:
With the osmar and maptools package you can easily pull an OpenStreetMap object and convert it to KML, like below (thanks to adibender helping out on SO). I found the relation ID by googling for it (www.google.at/search?q=openstreetmap+relation+innsbruck).

# get OSM data
library(osmar)
library(maptools)

innsbruck <- get_osm(relation(113642), full = T)
sp_innsbruck <- as_sp(innsbruck, what = "lines")

# convert to KML
for( i in seq_along(sp_innsbruck) ) {
kmlLine(sp_innsbruck@lines[[i]], kmlfile = "innsbruck.kml",
lwd = 3, col = "blue", name = "Innsbruck")
}

shell.exec("innsbruck.kml")
Read more »

Make a KML-File from an OpenStreetMap Trail

Ever wished to use a trail on OSM on your GPS or smartphone? With this neat little R-Script this can easily be done. You'll just need to search OpenStreetMap for the ID of the trail (way), put this as argument to osmar::get_osm, convert to KML and you're good to go!




# get OSM data
library(osmar)
library(maptools)

rotewandsteig <- get_osm(way(166274005), full = T)
sp_rotewandsteig <- as_sp(rotewandsteig, what = "lines")

# convert to KML
kmlLine(sp_rotewandsteig@lines[[1]], kmlfile = "rotewandsteig.kml",
lwd = 3, col = "blue", name = "Rotewandsteig")

# view it
shell.exec("rotewandsteig.kml")
Read more »

A Short Example with R-Package osmar..

Following up my last post in which I praised the capabilities of the osmar-package I give a short example...

ps: You can also find this example at GitHub HERE.








library(osmar)

# this pulls the data from the OSM-Api:
mydistrict <- get_osm(relation(85647), full = TRUE)

# make a spatial object:
mydistrict_sp <- as_sp(mydistrict, what = 'lines')
summary(mydistrict_sp)

# just for fun i'll plot some bubbles on my map:
mydata <- data.frame(x = runif(100, 12.300, 12.550),
y = runif(100, 47.300, 47.550),
data = sample(1:30, 100, replace = T))
mydata_sp <- SpatialPointsDataFrame(mydata[, c('x', 'y')], data=mydata,
proj4string=CRS('+proj=longlat +datum=WGS84'))
# see what's in there:
summary(mydata_sp)

# plot:
setwd(tempdir())
pdf("testOSM.pdf")
par(oma = rep(0, 4))
mycex = (mydata_sp@data[,'data']/max(mydata_sp@data[,'data'])*2)^2
plot(mydistrict_sp, axes = T)
colpts = rgb(0.2, 0.5, 0.4, alpha = 0.6)
points(x = mydata_sp@coords[,'x'],
y = mydata_sp@coords[,'y'],
cex = mycex, col = 0,
pch = 21, bg = colpts)
title('MyData in MyDistrict')

# legend:
l1 = min(mydata_sp@data[,'data'])
l2 = round(mean(mydata_sp@data[,'data']), 0)
l3 = max(mydata_sp@data[,'data'])
l = c(l1, l2, l3)
lcex = (c(l/l3*2))^2
points(x = rep(12, 3), y = seq(47.7, 47.6, -.05),
cex = lcex, col = 0,
pch = 21, bg = colpts)
text(l, x = rep(12, 3) +.045, y = seq(47.7, 47.6, -.05))

graphics.off()

# open map:
shell.exec("testOSM.pdf")
Read more »

osmar - Don't Miss this New R-Geo-Package!

The osmar-package enables you to retrieve all geographic elements of OpenStreetMap via its API.
I.e., you can retrieve a street, river, state-boundary or whatever and use this as a spatial object in R.

It's overwhelming thinking of the endless playground that is opened for R-users by this package!

And, owing to altruistic R-package authors (like the ones of osmar, Thomas Schlesinger and Manuel J. A. Eugster) the oligarch's (ESRI) power evermore crumbles away..
Read more »