How to find the longest railway bridge in Mexico with an Overpass query?

I know how to find all railway bridges in Mexico using Overpass. How do I get the longest?

First of all, you can’t find “bridges” directly. You are only finding tracks on bridges. They can be split for any reason, whether unintentional mistake, workaround error fixing (using different layer= , instead of correcting all layer= along it so that it only has one layer= ) , managing the length, or truly valid splitting necessary for some changing attributes.
Example from road bridges: Reynosa - Pharr is split into 3 highway= Way: ‪Puente Internacional Reynosa-Pharr‬ (‪709859228‬) | OpenStreetMap
There is a trick that you can use for other attributes, eg count_members() and id() , if you only want to return the greatest object, Overpass returns objects by id in ascending order only. You can set the ::id= to a negative of an evaluator, so that it returns bigger one which is having the “smallest” id. Then you can choose to only return 1 result.
Unfortunately id requires an integer. length() is a floating point decimal number. Overpass doesn’t have a floor( or ceiling( function to convert them. So the only option is out:csv , to sort them in a spreadsheet outside.
Furthermore for your case, an entire country may be too large. If you have to do it in Overpass, at least trying splitting the task by state. But still, avoid overloading, and consider other methods.
Listing way object lengths:

[out:csv(::id,"name","bridge:name",length)][timeout:25];
{{geocodeArea:TheState, Mexico}}->.ar;
way(area.ar)[bridge][railway=rail]; 
convert bdg ::id=id(),::=::,length=length();
out; 

Continuous bridge= lengths: Has problem with definition and identification. This query doesn’t work when there are “junctions” on bridges, or different bridges connected together by viaducts. Effectively, “islands” of [bridge][railway=rail] is being measured.

[out:csv(::id,allid,"name","bridge:name",length)][timeout:25];
{{geocodeArea:YourState, Mexico}}->.ar;
way(area.ar)[bridge][railway=rail]->.all;  
foreach.all->.this 
{
  if (! lrs_in(this.u(id()), visited_ids.set(t['ids'])))
  {
    .this;
    complete(10)->.thisall
    {
      node(w);
      way.all(bn);
    };
    make bdg allid=thisall.set(id()),name=thisall.u(t["name"]),"bridge:name"=thisall.u(t["bridge:name"]),length=thisall.sum(length());
    out;
    //.thisall out;
    make visited
      ids = lrs_union(thisall.set(id()), visited_ids.set(t['ids']))
      -> .visited_ids; 
  };
}; 

Counter modified from model example Counting roundabouts
This will still double-count double-track bridges. I can think about how to handle these issues later.

https://www.reddit.com/r/openstreetmap/comments/1dsvp33/counting_bridges_with_overpassturbo/
Used 90m to allow for generous inaccuracy. If you have a small subdivision to try in, uncomment the commented line, and comment or remove next line. I don’t know how slow it can get.

[out:csv(allid,length,"bridge:name",highway,ref,network,name,"boundary:name","crossedroads:id","crossedroads:highway","crossedroads:ref","crossedroads:name","crossedrwy:id","crossedrwy:railway","crossedrwy:ref","crossedrwy:name","crossedwwy:id","crossedwwy:waterway","crossedwwy:name","crossedcoast:id","crossedcoast_count","crossedsea:id","crossedsea:natural","crossedsea:name")][timeout:55]; 
//{{geocodeArea:Mexico}}->.ar;
//way(area.ar)[bridge][highway][footway!=sidewalk]->.all;  
way({{bbox}})[bridge][highway][footway!=sidewalk]->.all;  
foreach.all->.this 
{
  if (! lrs_in(this.u(id()), visited_ids.set(t['ids'])))
  {
    .this;
    complete(10)->.thisall
    {
      node(w);
      way.all(bn);
    };
    if (thisall.sum(length()) >= 90)
    {
      way(around.thisall:0)->.thisallcrossed;
      (.thisallcrossed; rel(bw.thisallcrossed)[type=multipolygon];)->.thisallcrossed;
      (.thisallcrossed; - .thisall;)->.thisallcrossed;
      way.thisallcrossed[highway][highway!=footway][highway!=cycleway][highway!=bridleway][highway!=path][!tunnel][highway!=platform]->.thisallcrossedroads;
      way.thisallcrossed[railway][railway!=platform][!tunnel]->.thisallcrossedrwy;
      way.thisallcrossed[waterway][waterway!=dam]->.thisallcrossedwwy;
      wr.thisallcrossed[natural=water][water!=river][water!=stream][water!=canal][water!=ditch][water!=drain][!tunnel]->.thisallcrossedwaters;
      way.thisallcrossed[natural=coastline]->.thisallcrossedcoast;
      rel(bw.thisallcrossed)->.thisallcrossedparent;
      (
        way.thisallcrossed[natural=bay]; 
        way.thisallcrossed[natural=strait];  
        rel.thisallcrossedparent[natural=bay]; 
        rel.thisallcrossedparent[natural=strait];         
      )->.thisallcrossedsea;
      node(w.thisall);
      is_in;
      area._[admin_level=6]->.thisalladmins;
      make bdg allid=thisall.set(id()),
      length=thisall.sum(length()),
      "bridge:name"=thisall.set(t["bridge:name"]),
      highway=thisall.set(t["highway"]),
      ref=thisall.set(t["ref"]),
      network=thisall.set(t["network"]),
      name=thisall.set(t["name"]),
      "boundary:name"=thisalladmins.set(t["name"]),
      "crossedroads:id"=thisallcrossedroads.set(id()),
      "crossedroads:highway"=thisallcrossedroads.set(t["highway"]),
      "crossedroads:ref"=thisallcrossedroads.set(t["ref"]),
      "crossedroads:name"=thisallcrossedroads.set(t["ref"]), 
      "crossedrwy:id"=thisallcrossedrwy.set(id()),
      "crossedrwy:railway"=thisallcrossedrwy.set(t["railway"]),
      "crossedrwy:ref"=thisallcrossedrwy.set(t["ref"]),
      "crossedrwy:name"=thisallcrossedrwy.set(t["name"]),
      "crossedwwy:id"=thisallcrossedwwy.set(id()),
      "crossedwwy:waterway"=thisallcrossedwwy.set(t["waterway"]), 
      "crossedwwy:name"=thisallcrossedwwy.set(t["name"]),
      "crossedcoast:id"=thisallcrossedcoast.set(id()),
      "crossedcoast_count"=thisallcrossedcoast.count(ways),
      "crossedwater:id"=thisallcrossedwater.set(type() + id()),
      "crossedwater:water"=thisallcrossedwater.set(t["water"]),  
      "crossedsea:id"=thisallcrossedsea.set(type() + id()),
      "crossedsea:natural"=thisallcrossedsea.set(t["natural"]), 
      "crossedsea:name"=thisallcrossedsea.set(t["name"]);
      out;
    };
    //.thisall out;
    make visited
      ids = lrs_union(thisall.set(id()), visited_ids.set(t['ids']))
      -> .visited_ids; 
  };
};

OK, thanks a lot. You are the best. In the case of “joined” bridges, how is the length parameter defined? Do you need the longer length? The only thing is that it cannot define the geocodeArea: by inserting the name of an Italian city it begins to return data for the whole of Italy.

The only thing missing is the elimination of double-lane bridges which are considered as two bridges. In your opinion, can this code be integrated by perhaps eliminating the smallest bridge if its centroid is less than 20 meters from another? Or is it an operation that can only be performed in postprocessing with QGis?

image

As for nearby bridges, I tried this code. But it gives me back all the bridges present and not those 15 meters away from each other.

[out:json][bbox:{{bbox}}][timeout:25];
(
way[bridge=yes];
)->.bridges1;
(
way[bridge=yes];
)->.bridges2;
(
way.bridges1(around.bridges2:15);
)->.bridgesNearBridges;
.bridgesNearBridges;
out geom meta;