Overpass API: search for intersections of named roads

Please help me to make right overpass request.

I’m trying to create overpass request, that returns list of intersections near selected coordinate.
Task is look for closest intersections near selected point, to mark point as “near intersection between street First and street Second” if it is possible.
It must be intersections only between named/signed roads and road level must be not too small - it must be highway, street or road, not small inner service way or path.

I found (and slightly modified) this code, but it is far from ideal - it returns even points where one street cutted to two different segments (with identical street names for every segment). Construction of second part of this code is not clear for me, so I don’t understand, how to modify it to exclude points between parts of same street.

Also, I want to get coordinates and names for all ways for found intersections. Not list of other object features.

[bbox:{{bbox}}];
(way[highway="motorway"][name~"."];way[highway="trunk"][name~"."];way[highway="primary"][name~"."];way[highway="secondary"][name~"."];way[highway="tertiary"][name~"."];way[highway="residential"][name~"."];way[highway="motorway"][ref~"."];way[highway="trunk"][ref~"."];way[highway="primary"][ref~"."];way[highway="secondary"][ref~"."];way[highway="tertiary"][ref~"."];way[highway="residential"][ref~"."];)->.n1;
foreach.n1(
    (.n1; - ._;)->.n2;
  	node(w._)->.n3;
  	node(w.n2)->.n2;
  	node.n3.n2;
  	out;
  );

http://overpass-turbo.eu/s/tQo

You could try the following query. As it is rather expensive to compute, please keep the bounding box small and don’t integrate this kind of query in any app which is getting called by many users!!


[bbox:{{bbox}}];

way[highway~"^(motorway|trunk|primary|secondary|tertiary|residential)$"]
   [~"^(name|ref)$"~"."] -> .allways;

foreach.allways -> .currentway(
    (.allways; - .currentway;)->.otherways_unfiltered;
    way.otherways_unfiltered(if:t[name] != currentway.u(t[name])) -> .otherways;
  	node(w.currentway)->.currentwaynodes;
  	node(w.otherways)->.otherwaynodes;
  	node.currentwaynodes.otherwaynodes;   // intersection
    // output intersection
  	out;
    // output ways at intersection
  /*
    way(bn);
    out geom;
  */
  );

If possible, just download the respective area, and do the calculation on your own as post processing step:


[bbox:{{bbox}}];

way[highway~"^(motorway|trunk|primary|secondary|tertiary|residential)$"]
   [~"^(name|ref)$"~"."] ;

out geom;

Thank you.

I fixed one minor issue: if we detect difference between ways not by name but also by ref, we must get intersections between ways with different names OR different ref. Also, I added executon limits:

[bbox:{{bbox}}][timeout:300][maxsize];

way[highway~"^(motorway|trunk|primary|secondary|tertiary|residential)$"]
   [~"^(name|ref)$"~"."] -> .allways;

foreach.allways -> .currentway(
    (.allways; - .currentway;)->.otherways_unfiltered;
    way.otherways_unfiltered(if:t[name] != currentway.u(t[name]) || t[ref] != currentway.u(t[ref])) -> .otherways;
  	node(w.currentway)->.currentwaynodes;
  	node(w.otherways)->.otherwaynodes;
  	node.currentwaynodes.otherwaynodes;   // intersection
    // output intersection
  	out;
    // output ways at intersection
  /*
    way(bn);
    out geom;
  */
  );

http://overpass-turbo.eu/s/tSj

Please keep in mind that this query is expensive and consumes quite some resources on the server. Unless you’re running on your own instance I’d strongly recommend to use a much lower timeout value.

Couple of years late… but looking for the same.

When executing the above code I get several errors about NAME and REF not being defined (pasted at bottom).

How do I pass to that code the LAT/LNG of which I need the nearest intersection?

Thanks in advance,
Federico.

Errors:
Error: line 8: parse error: Put quotation marks around “name” if it should be a constant and not a tag evaluation.

Error: line 8: parse error: Token “name” has not been recognized as statement

Error: line 8: parse error: t[…] needs an argument

Error: line 8: parse error: Put quotation marks around “name” if it should be a constant and not a tag evaluation.

Error: line 8: parse error: Token “name” has not been recognized as statement

Error: line 8: parse error: t[…] needs an argument

Error: line 8: parse error: Put quotation marks around “ref” if it should be a constant and not a tag evaluation.

Error: line 8: parse error: Token “ref” has not been recognized as statement

Error: line 8: parse error: t[…] needs an argument

Error: line 8: parse error: Put quotation marks around “ref” if it should be a constant and not a tag evaluation.

Error: line 8: parse error: Token “ref” has not been recognized as statement

Error: line 8: parse error: t[…] needs an argument