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

I want my program to have the street's and building's data in xml format.

But I have no idea how to start this project. I am new to using api's and reading about the api does not tell me how do I actually write api call in my c++ code and retrieve the data. What sources should I refer to write a simple program to say :

                              "retrieve bounding box xml file in osm map"

All queries are on wiki page, but where to write this queries and required setup for starting with this api usage is evasive.

asked 24 Dec '12, 06:25

Anubha's gravatar image

Anubha
31336
accept rate: 0%

edited 24 Dec '12, 06:27

Implementing the API call in your project is very programming/scripting language-specific and hasn't much to do with OSM.

(24 Dec '12, 11:57) scai ♦

This is a minimal working example in plain C/C++ with only minimal error checking and no response processing:

#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <arpa/inet.h>
#include <sys/socket.h>

const std::string host  = "81.19.81.199"; // IP of overpass.osm.rambler.ru
const int port          = 80;
const std::string query = "GET /cgi/interpreter?data=node%5Bname%3DGielgen%5D%3Bout%3B HTTP/1.1\r\n"
                          "Host: overpass.osm.rambler.ru\r\n"
                          "User-Agent: SteveC\r\n"
                          "Accept: */*\r\n"
                          "Connection: close\r\n"
                          "\r\n";

int main(int argc, char* argv[]) {
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == -1) {
        perror("error opening socket"); return -1;
    }

    struct sockaddr_in sin;
    sin.sin_port = htons(port);
    sin.sin_addr.s_addr = inet_addr(host.c_str());
    sin.sin_family = AF_INET;

    if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
        perror("error connecting to host"); return -1;
    }

    const int query_len = query.length() + 1; // trailing '\0'
    if (send(sock, query.c_str(), query_len, 0) != query_len) {
        perror("error sending query"); return -1;
    }

    const int buf_size = 1024 * 1024;
    while (true) {
        std::vector<char> buf(buf_size, '\0');
        const int recv_len = recv(sock, &buf[0], buf_size - 1, 0);

        if (recv_len == -1) {
            perror("error receiving response"); return -1;
        } else if (recv_len == 0) {
            std::cout << std::endl; break;
        } else {
            std::cout << &buf[0];
        }
    }

    return 0;
}

Example output:

HTTP/1.1 200 OK
Server: nginx/1.0.9
Date: Mon, 24 Dec 2012 11:45:34 GMT
Content-Type: application/osm3s+xml
Connection: close
Content-Length: 1893
Access-Control-Allow-Origin: *

<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="Overpass API">
<note>The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.</note>
<meta osm_base="2012-12-24T11:44:01Z"/>

  <node id="371597317" lat="50.7412721" lon="7.1927120">
    <tag k="is_in" v="Bonn,Regierungsbezirk Köln,Nordrhein-Westfalen,Bundesrepublik Deutschland,Europe"/>
    <tag k="name" v="Gielgen"/>
    <tag k="place" v="suburb"/>
  </node>

[cropped]

</osm>

I strongly suggest using a third-party library for the network/HTTP code and you will probably also want to use a third-party library for parsing the XML/JSON/whatever response.

A much simpler option is to use a scripting language like Python, Ruby, or - if you like camels - Perl.

permanent link

answered 24 Dec '12, 11:51

scai's gravatar image

scai ♦
33.3k21309459
accept rate: 23%

1

Thank you again (again as you replied in stackOverflow), I ran the code, and it worked, its first time I connected to an internet server. Will make project in which user will be able to ride a car in any part of world he/she wishes, buildings will be mostly random.

(24 Dec '12, 12:35) Anubha

Ah, I didn't remember your username :)

(24 Dec '12, 13:40) scai ♦

An API call is done via HTTP, so your program needs to open a network connection. Libraries for C++ that do so are discussed for example here.

permanent link

answered 24 Dec '12, 07:08

Roland%20Olbricht's gravatar image

Roland Olbricht
6.7k36489
accept rate: 36%

1
(24 Dec '12, 07:20) cartinus

thanks, any code samples with simple osm api calls would be really helpful.

(24 Dec '12, 08:14) Anubha

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:

×710
×483
×202
×18
×10

question asked: 24 Dec '12, 06:25

question was seen: 11,888 times

last updated: 24 Dec '12, 13:40

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