0

I want to create CSV data with different OpenStreetMap tag counts for selected cities using Overpass API. Unfortunately, the "Counting Objects" section of the Overpass API Documentation is still empty. Also, this count function documentation only shows how to count nodes, ways, relations or a combination for a single tag query.

How can an output containing several columns for different tag counts be achieved?

I found this helpful example for counting pharmacies.

And this is what my query currently looks like:

// Define fields for CSV output
[out:csv(name, "node[railway=station]", "node[highway=bus_stop]")];

// Select area
{{geocodeArea:München}}->.munich;

// Query stations and bus stops
node(area.munich)[railway=station];
//node(area.regio)[highway=bus_stop]; // How can this count be added separately?

// Count stations
make count name = munich.set(t["name"]),
           "node[railway=station]" = count(nodes);

// Output CSV
out; 

If I uncomment the node(area.regio)[highway=bus_stop]... line, only the bus stop count is contained in the output, but should appear in an additional column next to the railway station count.

1 Answer 1

0

Each query part must be saved as a variable that then has to be used as a prefix for count(). Then, a separate out; is necessary for each CSV row.

// Define fields for CSV output
[out:csv(feature, number)];

// Select area
{{geocodeArea:München}}->.munich; 

// Query stations and bus stops
node(area.munich)[railway=station]->.railstations;
node(area.munich)[highway=bus_stop]->.busstops; 

// Count railway stations
make count feature = "railway=station",
           number = railstations.count(nodes);

// Output first CSV row
out;

// Count bus stops
make count feature = "highway=bus_stop",
           number = busstops.count(nodes);

// Output second CSV row
out; 

Here's another example that creates CSV data for three different cities and uses a foreach loop:

// Define fields for CSV output
[out:csv(city, "railway stations", "bus stops")];

// Select area
(
  {{geocodeArea:Berlin}};
  {{geocodeArea:Hamburg}};
  {{geocodeArea:München}};
);

// Query stations and bus stops for each city
foreach->.c(
  node(area.c)[railway=station]->.railwaystations;
  node(area.c)[highway=bus_stop]->.busstops;
  
  // Count stations
  make count city = c.set(t["name"]),
           "railway stations" = railwaystations.count(nodes),
           "bus stops" = busstops.count(nodes);
  
  // Output CSV row
  out; 
);

See the original answer in the OSM community forum.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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