private FeatureCollection getResultFor(String uri) throws IOException { String result = execute(uri); if (result != null) { try { return new ObjectMapper().readValue(result, FeatureCollection.class); } catch (JsonParseException | JsonMappingException e) { throw new IOException("Cannot unmarshall " + result + ": " + e, e); } } return null; }
public List<NavigationPosition> getPositionsFor(String address) throws IOException { FeatureCollection collection = getResultFor("/api/?q=" + encodeUri(address) + "&limit=10"); if (collection == null) return null; return extractPositions(collection.getFeatures()); }
private String getDisplayName(Feature feature) { String result = getProperty(feature, "name"); if(result.length() > 0) result += ", "; String postcode = getProperty(feature, "postcode"); if(postcode.length() > 0) result += postcode; String city = getProperty(feature, "city"); if(city.length() > 0) result += " " + city; String state = getProperty(feature, "state"); if(state.length() > 0) result += ", " + state; String country = getProperty(feature, "country"); if(country.length() > 0) result += ", " + country; result = result.replaceAll(" {2}", " "); if(result.endsWith(", ")) result = result.substring(0, result.length() - 2); return result; } }
public String getAddressFor(NavigationPosition position) throws IOException { FeatureCollection collection = getResultFor("/reverse/?lon=" + position.getLongitude() + "&lat=" + position.getLatitude()); if (collection == null) return null; List<Feature> features = collection.getFeatures(); if (features.size() == 0) return null; Feature feature = features.get(0); GeoJsonObject geometry = feature.getGeometry(); if (!(geometry instanceof Point)) return null; return getDisplayName(feature); }
private String execute(String uri) throws IOException { String url = getPhotonUrl() + uri; Get get = new Get(url); String result = get.executeAsString(); if (get.isSuccessful()) return result; return null; }
private List<NavigationPosition> extractPositions(List<Feature> features) { List<NavigationPosition> result = new ArrayList<>(features.size()); for (Feature feature : features) { GeoJsonObject geometry = feature.getGeometry(); if (!(geometry instanceof Point)) continue; Point point = (Point) geometry; LngLatAlt lngLatAlt = point.getCoordinates(); String type = feature.getProperty("osm_key"); result.add(new SimpleNavigationPosition(lngLatAlt.getLongitude(), lngLatAlt.getLatitude(), null, getDisplayName(feature) + " (" + type + ")")); } return result; }
protected void initializeGeocodingServices() { AutomaticGeocodingService service = new AutomaticGeocodingService(getGeocodingServiceFacade()); getGeocodingServiceFacade().addGeocodingService(service); getGeocodingServiceFacade().setPreferredGeocodingService(service); getGeocodingServiceFacade().addGeocodingService(new GeoNamesService()); getGeocodingServiceFacade().addGeocodingService(new NominatimService()); getGeocodingServiceFacade().addGeocodingService(new PhotonService()); }