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.
© 2016-2020 Tobias Zwick. This library is released under the terms of the GNU Lesser General Public License (LGPL).
Depending on which part of the API you use, you can only include what you need:
Class | Dependency | Description |
---|---|---|
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
}
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...
Note myNote = new NotesDao(osm).create(position, "My first note");
ChangesetInfo changeset = new ChangesetsDao(osm).comment(id, "Nice work!");
UserInfo user = new UserDao(osm).get(id);
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);
}
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.
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.