1

enter image description here

I'm trying to get the road network in an area focusing on the smaller (1-2 lane ) roads using osm and node. Using the OSM overpass api, I used the following query:

const roadsOverpassQuery = `
[out:json];
(way[lanes=1](${bbox}); way[lanes=2](${bbox}); );
out;
`;

I was able to get many of the roads within a bounding box. I noticed that these roads when converted to geojson objects have nodes

 {
"_id": {
  "$oid": "643c3fd883215e96ece3be0a"
 {,
"type": "way",
"id": 5053100,
"nodes": [
  33979706,
  33979705
],
"tags": {
  "adot_name": "Tucson-Benson Highway",
  "bicycle": "yes",
  "hgv": "designated",
  "highway": "motorway",
  "lanes": "2",
  "maxspeed": "75 mph",
  "old_ref": "US 80 (old AZ 86)",
  "oneway": "yes",
  "ref": "I 10"
}

I passed these into the overpass api using

const overpassQuery = `
[out:json];
(
  ${nodeIds.map(id => `node(${id})`).join(';')};
);
out;

`;

These as geojson look like:

"features": [
  {
    "type": "Feature",
    "id": "node/33979705",
    "properties": {
      "highway": "motorway_junction",
      "ref": "292",
      "id": "node/33979705"
    },
    "geometry": {
      "type": "Point",
      "coordinates": [
        -110.5123291,
        31.9766748
      ]
    }
  },
  {
    "type": "Feature",
    "id": "node/33979706",
    "properties": {
      "id": "node/33979706"
    },
    "geometry": {
      "type": "Point",
      "coordinates": [
        -110.5141282,
        31.9773252
      ]
    }
  }
]

I notice that the geometry type is point. In other places I've seen roads made up of Linestrings:

{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
  [-122.483696, 37.833818],
  [-122.483482, 37.833174],
  [-122.483396, 37.8327],
  [-122.483568, 37.832056],
  [-122.48404, 37.831141],
  [-122.48404, 37.830497],
  [-122.483482, 37.82992],
  [-122.483568, 37.829548],
  [-122.48507, 37.828802],
  [-122.4861, 37.828387],
  [-122.486958, 37.828138],
  [-122.487001, 37.827885]
 ]
},
"properties": {
"name": "Example LineString"
}
}

How do I get the Linestring coordinates for the roads of interest?

2
  • According to this comment, you can use the geometry operator in a convert statement. Using the way ID in your question, the query would look like this: [out:json]; way(5053100); convert item ::=::,::geom=geom(),_osm_type=type(); out geom; Here's an example. Does that address your question? If so, I can write it up as an answer — if not, what am I missing?
    – jsejcksn
    Commented Apr 22, 2023 at 23:56
  • Than you , that looks interesting - Would you mind writing it as an answer? Commented Apr 23, 2023 at 0:27

1 Answer 1

3

According to this comment on the OSM help forum, you can use the geometry operator in a convert statement along with the geom output argument.

Using the way ID in your question, the query would look like this:

[out:json];
way(5053100);
convert item ::=::,::geom=geom(),_osm_type=type();
out geom;

Here's an example in a runnable snippet:

TS Playground

function fetchOverpassJson(query) {
  return fetch("https://overpass-api.de/api/interpreter", {
    body: new URLSearchParams([["data", query]]),
    method: "POST",
  }).then(res => res.json());
}

const query = `
[out:json];
way(5053100);
convert item ::=::,::geom=geom(),_osm_type=type();
out geom;
`;

fetchOverpassJson(query).then(console.log);

And here's the static output of the code above at the time I ran the query:

{
  "version": 0.6,
  "generator": "Overpass API 0.7.60 f4c14d41",
  "osm3s": {
    "timestamp_osm_base": "2023-04-23T14:58:09Z",
    "copyright": "The data included in this document is from www.openstreetmap.org. The data is made available under ODbL."
  },
  "elements": [
    {
      "type": "item",
      "id": 1,
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            -110.5141282,
            31.9773252
          ],
          [
            -110.5123291,
            31.9766748
          ]
        ]
      },
      "tags": {
        "adot_name": "Tucson-Benson Highway",
        "bicycle": "yes",
        "hgv": "designated",
        "highway": "motorway",
        "lanes": "2",
        "maxspeed": "75 mph",
        "old_ref": "US 80 (old AZ 86)",
        "oneway": "yes",
        "ref": "I 10",
        "_osm_type": "way"
      }
    }
  ]
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.