NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum

Hello,

I was wondering if there is an algortihm in R that will extract the lat and lons for OSM_ids. Ideally, it would give one lat and lon (as opposed to a Polygon).

I have been using QGIS and post gis to extract them via the attribute table - copied and pasted to Libre Office/Excel. I have looked at some of the methods for getting the lat and longs for nodes, ways, relations via the osm api or overpass api, but they don't seem to do OSM_ids (at least as far as I can determine).

Ideally, I would like to try an algorthim that doesn't involve extracting the information from an external source, but rather one that extracts the lat and long tidily from the OSM_id, i.e. reverse engineering.

I would any assistance you could offer. Many thanks.

asked 18 Jun '14, 10:56

Student's gravatar image

Student
26225
accept rate: 0%

I don't quite understand what you mean by "I have looked at some of the methods for getting the lat and longs for nodes, ways, relations via the osm api or overpass api, but they don't seem to do OSM_ids".

Can you give an example of an "OSM_id" so that we can be sure we're talking about the same thing? I'm not aware of a globally unique "OSM_id" though of course nodes, ways and relations have unique IDs - but (for example) there's both a node 133449728 and a way 133449728.

(18 Jun '14, 11:12) SomeoneElse ♦

Yes I will post an example of an OSM_id later. It would be very useful if node ids (or way ids or relation ids) and OSM_ids were the same thing. Although I dont think so because i obtained node ids for the same dataset using osmconvert and they turned for to be different to the osm_id , in that the node ids were much longer. Anyhow, I will post you an example later (I dont have my laptop on me now). Thanks for the quick reply!

(18 Jun '14, 11:35) Student

wkt_geom gid osm_id POLYGON((-6.33255269999999992 53.28642250000000047,-6.33246350000000024 53.28647680000000264,-6.3323330999999996 53.28646859999999919,-6.33258360000000042 53.28635700000000242,-6.33255269999999992 53.28642250000000047)) 396 25857740 POLYGON((-6.34702140000000004 53.40131290000000064,-6.34636050000000029 53.40164039999999801,-6.34611160000000041 53.40146130000000113,-6.34678970000000042 53.40113379999999665,-6.34702140000000004 53.40131290000000064)) 256 23750977

above is an example of the data I copy from the attribute table in QGIS. If you are interested it is from the OSM buildings shape file for Ireland. It gives the osm_id and the lats and longs. Which is what I need. But I would like to get the same thing, only, tidier in R, i.e. the osm_id and lat lon.

I guess if somebody has done this type of work previously they will have some ideas :)

(18 Jun '14, 19:49) Student

That last number looks suspiciously like the way ID for this building near Blanchardstown:

https://www.openstreetmap.org/way/23750977

The other one (25857740) is a building south of the Tallaght M50 junction, which would make sense if it's from the shapefile for Irish buildings.

Of the data that you've quoted, which do you consider to be the "osm_id"? I'd expect that QGIS will have an internal ID for the shapefile, which won't be in OSM.

(18 Jun '14, 23:08) SomeoneElse ♦

osm_id would be 25857740 and 23750977 in this example. The osm_id covers nodes, ways, relations. Yes I think QGIS apply the osm_id and it is not a generic id. There are lots of questions online about coverting the osm_id to lat and lon but the answers suggest using overpass api or osm api. I was hoping that there is a method of obtaining the latitude and longitude from the osm_id in the same way the apis do but without a necessity to indicate if it is a node, way, relation. Any assistance would be greatly appreciated.

(19 Jun '14, 08:23) Student
1

This is not possible because the ID is only unique within the same element type. Or in other words: There can and will be different elements with the same ID (e.g. a node, a way and a (now deleted) relation all having the ID 123). Therefore you have to know the element type a specific ID belongs to.

(19 Jun '14, 08:32) scai ♦

Thanks for the fast response. OK, I guess an if else statement in code might do the trick. Do you, or does anyone else know if it is possible to extract lat and lon from osm_id - with knowledge of whether it's a node or way - but without using an api. I mean how do the apis do it ? Can that be reproduced in R ?

(19 Jun '14, 08:43) Student
1

Are you aware that there is an OSM package for R? See http://osmar.r-forge.r-project.org/ and search for various blog posts about it.

(19 Jun '14, 08:59) SK53 ♦
1

thanks for the reply, yes I used osmar, it basically involves using the api to get the information. Was hoping to see what the api does and apply it in R, rather than using the api via an R package. Anyhow, if that's all that's available, I may just have to work with it. Thanks to all who have contributed, its been useful discussing it.

(19 Jun '14, 10:08) Student
showing 5 of 9 show 4 more comments

I found a work-around to this question. It gives you the lat and lon coordinates for the shape file which you can then bind with the shape file columns to create a new data set. Thanks to the r-sig-Geo email list and http://www.inside-r.org/packages/cran/rgdal/docs/ogrInfo.

library(rgdal)
shape<-readOGR("location of shape file/folder",layer="buildings")
names(shape)
[[1] "osm_id" "name"   "type"

lat <- coordinates(shape)[,1]

lon <- coordinates(shape)[,2]

a<-as.data.frame(shape$osm_id)

b<-as.data.frame(shape$name)

c<-as.data.frame(shape$type)

data <- cbind(lat,lon,a,b,c)

you can then name the columns if you so wish. you now have lat & lon coordinates for the osm_id

permanent link

answered 25 Jul '14, 12:45

Student's gravatar image

Student
26225
accept rate: 0%

edited 25 Jul '14, 15:16

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×55
×45
×10
×10

question asked: 18 Jun '14, 10:56

question was seen: 14,346 times

last updated: 25 Jul '14, 15:16

NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum