Batch Upload
From OpenStreetMap
Main idea is to upload GPX files directly from scripts, without having to use the web interface.
Contents |
Tools
Tar or Zip
The simplest way is just to zip up your gpx files and submit a single zip (or tar or tar.gz) file using the normal upload form. The only drawbacks with this are that you can only attach one description to the whole file set, and it will appear as a single trace in the upload log.
Perl
Uploading via the API
You can upload a GPX to the API like this:
use HTTP::Request::Common;
use LWP::UserAgent;
$ua=LWP::UserAgent->new;
$ua->credentials('www.openstreetmap.org:80','Web Password',$yourusername, $yourpassword);
$response=$ua->request(POST 'http://www.openstreetmap.org/api/0.5/gpx/create',
Content_Type => 'form-data',
Content => [ file =>[$filename],
description=>$description,
tags =>$tags,
public =>"1" ] );
if ($response->code==200) {
# yay, success
} else {
# boo, failure
}
from which a batch uploader can be elementarily constructed.
Doing it without the API
The old Perl batch uploaders mentioned below used to upload using the standard web form because there was, at the time, no API method.
This Perl script demonstrates how to automatically upload a GPX file to OpenStreetMap. This is useful since the current OSM API does not have a method for doing it.
I use the script to extract a GPX file from my PostgreSQL database and to upload it to OSM. I hope it is commented enough so you can make changes to suit your needs.
Remember to edit the script with your OSM login and password. Remember also that you can upload — along with the GPX file — the tags and public fields.
The script uses the LWP::UserAgent Perl library, that greatly simplify HTTP interactions like posting a form, receiving and storing a cookie, uploading a file.
moved to svn at /applications/utils/gps-tracks/gpx-batch-upload/ --Deelkar (talk) 19:17, 16 April 2007 (BST)
I uploaded a new version of the script into the talk page. This version is working with the new site and adds the GPL license. Deelkar, please move it to svn if you like. --Niccolo 20:59, 5 October 2007 (BST)
Java
It is only one java class (needs compiling) and it allows to pass all uploaded files to other command line apps (using xargs).
Feedback is welcome: Christof Dallermassl
Usage
java GpxUpload <description> <tags> <files*>
Osm username and password can be defined as system properties by -Dusername=<username> and -Dpassword=<password> or if not given, josm's preference file is read. Any messages are printed to stderror, only the filename that was sent successfully is printed to stdout, so you may use the output of this program in a pipe for other calls.
Examples
java GpxUpload "taking a ride in Graz, Austria" "graz austria" gpxfile.gpx
java GpxUpload "taking a ride in Graz, Austria" "graz austria" gpxfiles*.gpx | xargs -i mv '{}' /home/cdaller/targetdir
Download
http://svn.openstreetmap.org/applications/utils/gps-tracks/jgpxupload/
C/libcurl
The following source code works with glib+libcurl.
void osm_traces_upload_file(const char *user, const char *password, const char *file, const char *filename, const char *description, const char *tags, gboolean public)
{
CURL *curl;
CURLcode res;
char curl_error_buffer[CURL_ERROR_SIZE];
struct curl_slist *headers = NULL;
struct curl_httppost *post=NULL;
struct curl_httppost *last=NULL;
gchar *public_string;
char *base_url = "http://www.openstreetmap.org/api/0.4/gpx/create";
gchar *user_pass = get_login();
/* Init CURL */
curl = curl_easy_init();
/* Filling the form */
curl_formadd(&post, &last,
CURLFORM_COPYNAME, "description",
CURLFORM_COPYCONTENTS, description, CURLFORM_END);
curl_formadd(&post, &last,
CURLFORM_COPYNAME, "tags",
CURLFORM_COPYCONTENTS, tags, CURLFORM_END);
if (public) public_string = "1";
else public_string = "0";
curl_formadd(&post, &last,
CURLFORM_COPYNAME, "public",
CURLFORM_COPYCONTENTS, public_string, CURLFORM_END);
curl_formadd(&post, &last,
CURLFORM_COPYNAME, "file",
CURLFORM_FILE, file,
CURLFORM_FILENAME, filename,
CURLFORM_CONTENTTYPE, "text/xml", CURLFORM_END);
/* Prepare request */
/* As explained in http://wiki.openstreetmap.org/index.php/User:LA2 */
/* Expect: header seems to produce incompatibilites between curl and httpd */
headers = curl_slist_append(headers, "Expect: ");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
curl_easy_setopt(curl, CURLOPT_URL, base_url);
curl_easy_setopt(curl, CURLOPT_USERPWD, user_pass);
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &curl_error_buffer);
/* Execute request */
res = curl_easy_perform(curl);
if (res == CURLE_OK)
{
long code;
res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
if (res == CURLE_OK)
{
g_debug("received valid curl response: %ld", code);
if (code != 200) g_warning("failed to upload data: HTTP response is %ld", code);
}
else g_error("curl_easy_getinfo failed: %d", res);
else g_warning("curl request failed: %s", curl_error_buffer);
/* Memory */
g_free(user_pass); user_pass = NULL;
curl_formfree(post);
curl_easy_cleanup(curl);
}
CURL
curl -u user:password -H "Expect: " -F "file=@"file.gpx -F description=description -F tags=tags -F public=1 http://www.openstreetmap.org/api/0.5/gpx/create
file.gpx is your filename, description, tags, and public, as well as user and password must be changed.
Technics
Cf. API documentation and precisely how to create gpx file.
Categories: HOWTO | Perl | Java | Unix command examples | Import

