Skip to content

westnordost/osmapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ab6e39d · Mar 7, 2021
Oct 27, 2020
Oct 26, 2020
Mar 7, 2021
Oct 27, 2020
Oct 27, 2020
Apr 9, 2016
Mar 7, 2021
Mar 7, 2021
Apr 24, 2016
Oct 26, 2020
Oct 26, 2020
Dec 2, 2019

Repository files navigation

osmapi

osmapi is a client for the OSM API 0.6.

It is well tested (test coverage over 90%) and being used by StreetComplete, thus actively maintained.

Note, the OSM API, particularly the part to download the map data, is intended only for editing the map. It's not made for pulling larger amounts of data or data analysis of certain map features. If this is what you intend to do, the Overpass API is what you will want to use. I created a basic Java client for the Overpass API here, it builts upon this library: osmapi-overpass.

Copyright and License

© 2016-2020 Tobias Zwick. This library is released under the terms of the GNU Lesser General Public License (LGPL).

Installation

Depending on which part of the API you use, you can only include what you need:

ClassDependencyDescription
CapabilitiesDao
de.westnordost:osmapi-core:1.4
Getting server capabilities
PermissionsDao
de.westnordost:osmapi-core:1.4
Getting user permissions
MapDataDao
de.westnordost:osmapi-map:1.5
Getting map data, querying single elements and their relations toward each other and uploading changes in changesets
MapDataHistoryDao
de.westnordost:osmapi-map:1.5
Getting the history and specific versions of elements
NotesDao
de.westnordost:osmapi-notes:1.4
Getting finding, creating, commenting on and solving notes
GpsTracesDao
de.westnordost:osmapi-traces:1.4
Getting, uploading, updating and deleting GPS traces and trackpoints
ChangesetsDao
de.westnordost:osmapi-changesets:1.5
Finding changesets, changeset discussion, subscription and data
UserDao
de.westnordost:osmapi-user:1.4
Getting user information
UserPreferencesDao
de.westnordost:osmapi-user:1.4
Managing user preferences

To include everything, add de.westnordost:osmapi:3.10 as a Maven dependency or download the jar from there. On Android, you need to exclude kxml2 from the dependencies since it is already built-in, like so:

	compile ('de.westnordost:osmapi:3.10')
	{
		exclude group: 'net.sf.kxml', module: 'kxml2' // already included in Android
	}

Basic Usage

Everything revolves around the OsmConnection, this is the class that talks to the Api. Specify where to reach the Api, how the client should identify itself towards the server etc. If you plan to make calls that can only be made by a logged in user, such as uploading map data, an OAuthConsumer (third parameter) needs to be specified.

	OsmConnection osm = new OsmConnection(
	                          "https://api.openstreetmap.org/api/0.6/",
	                          "my user agent", null);

You can call osm.makeRequest(...) yourself to talk with the RESTful Api and write your own ApiRequestWriter and ApiResponseReader to write/read the request. It is more convenient however to use the appropriate DAO to do that for you and return the data you are interested in. See the table above for which DAOs are available.

For example...

Create a note

	Note myNote = new NotesDao(osm).create(position, "My first note");

Comment a changeset

	ChangesetInfo changeset = new ChangesetsDao(osm).comment(id, "Nice work!");

Get user info

	UserInfo user = new UserDao(osm).get(id);

Download map data

	MapDataDao mapDao = new MapDataDao(osm);
	mapDao.getMap(boundingBox, myMapDataHandler);

myMapDataHandler implements MapDataHandler whose methods are called as the elements are parsed, think SAX parser. I.e. if you download 10MB of data, then the elements start arriving at the handler as the data comes in so that you can process them on the fly.

	/** This class is fed the map data. */
	public interface MapDataHandler
	{
		void handle(Bounds bounds);

		void handle(Node node);
		void handle(Way way);
		void handle(Relation relation);
	}

Combine with data processing library

Read this if you want to use this library in conjunction with a data processing library like Osmosis, osm4j or have your own map data structures already.

Troubleshooting

If you are getting the exception

sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target.

, try updating your Java SDK. Openstreetmap.org uses Let's Encrypt certificates which are not trusted in earlier versions of Java by default. Read more here.