diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-05-07 15:39:42 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-05-07 15:39:42 +1200 |
commit | 953852e6e984f8a639cf1a5ac4116fff755d538a (patch) | |
tree | 1902beb226bfa2ba7b60b20a310b376e8e86ce76 /src/org/traccar/geocode/NominatimReverseGeocoder.java | |
parent | 6a07fc479021dbf61143d432285c573faf38d6b9 (diff) | |
download | trackermap-server-953852e6e984f8a639cf1a5ac4116fff755d538a.tar.gz trackermap-server-953852e6e984f8a639cf1a5ac4116fff755d538a.tar.bz2 trackermap-server-953852e6e984f8a639cf1a5ac4116fff755d538a.zip |
Add Gisgraphy reverse geocoder
Diffstat (limited to 'src/org/traccar/geocode/NominatimReverseGeocoder.java')
-rw-r--r-- | src/org/traccar/geocode/NominatimReverseGeocoder.java | 49 |
1 files changed, 40 insertions, 9 deletions
diff --git a/src/org/traccar/geocode/NominatimReverseGeocoder.java b/src/org/traccar/geocode/NominatimReverseGeocoder.java index c9393da42..911e0fa71 100644 --- a/src/org/traccar/geocode/NominatimReverseGeocoder.java +++ b/src/org/traccar/geocode/NominatimReverseGeocoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2014 - 2015 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,8 +15,12 @@ */ package org.traccar.geocode; +import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; +import javax.json.Json; +import javax.json.JsonArray; +import javax.json.JsonObject; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -26,23 +30,50 @@ import org.w3c.dom.Document; public class NominatimReverseGeocoder implements ReverseGeocoder { private final String url; + + public NominatimReverseGeocoder() { + this("http://nominatim.openstreetmap.org/reverse"); + } public NominatimReverseGeocoder(String url) { - this.url = url + "?format=xml&lat=%f&lon=%f&zoom=18&addressdetails=0"; + this.url = url + "?format=json&lat=%f&lon=%f&zoom=18&addressdetails=1"; } @Override - public String getAddress(double latitude, double longitude) { + public String getAddress(AddressFormat format, double latitude, double longitude) { try { - + Address address = new Address(); URLConnection conn = new URL(String.format(url, latitude, longitude)).openConnection(); - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - DocumentBuilder builder = factory.newDocumentBuilder(); - Document doc = builder.parse(conn.getInputStream()); - - return doc.getFirstChild().getLastChild().getFirstChild().getNodeValue(); + JsonObject json = Json.createReader(new InputStreamReader(conn.getInputStream())).readObject().getJsonObject("address"); + + if (json.containsKey("house_number")) { + address.setHouse(json.getString("house_number")); + } + if (json.containsKey("road")) { + address.setStreet(json.getString("road")); + } + if (json.containsKey("village")) { + address.setSettlement(json.getString("village")); + } + if (json.containsKey("city")) { + address.setSettlement(json.getString("city")); + } + if (json.containsKey("state_district")) { + address.setDistrict(json.getString("state_district")); + } + if (json.containsKey("state")) { + address.setState(json.getString("state")); + } + if (json.containsKey("country_code")) { + address.setCountry(json.getString("country_code").toUpperCase()); + } + if (json.containsKey("postcode")) { + address.setPostcode(json.getString("postcode")); + } + + return format.format(address); } catch(Exception error) { Log.warning(error); |