use of org.opentripplanner.geocoder.GeocoderResult in project OpenTripPlanner by opentripplanner.
the class YahooGeocoder method geocode.
@Override
public GeocoderResults geocode(String address, Envelope bbox) {
if (appId == null)
throw new NullPointerException("appid not set");
String content = null;
try {
// make json request
URL googleGeocoderUrl = getYahooGeocoderUrl(address);
URLConnection conn = googleGeocoderUrl.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
StringBuilder sb = new StringBuilder(128);
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
reader.close();
content = sb.toString();
} catch (IOException e) {
LOG.error("Error parsing geocoder response", e);
return noGeocoderResult("Error parsing geocoder response");
}
YahooGeocoderResults yahooGeocoderResults = yahooJsonDeserializer.parseResults(content);
YahooGeocoderResultSet resultSet = yahooGeocoderResults.getResultSet();
List<YahooGeocoderResult> results = resultSet.getResults();
List<GeocoderResult> geocoderResults = new ArrayList<GeocoderResult>();
for (YahooGeocoderResult yahooGeocoderResult : results) {
double lat = yahooGeocoderResult.getLatDouble();
double lng = yahooGeocoderResult.getLngDouble();
String line1 = yahooGeocoderResult.getLine1();
String line2 = yahooGeocoderResult.getLine2();
String addressString = null;
if (line1 != null && !line1.trim().isEmpty()) {
addressString = line1 + ", " + line2;
} else {
addressString = line2;
}
geocoderResults.add(new GeocoderResult(lat, lng, addressString));
}
return new GeocoderResults(geocoderResults);
}
use of org.opentripplanner.geocoder.GeocoderResult in project OpenTripPlanner by opentripplanner.
the class BanoGeocoder method geocode.
/**
*/
@Override
public GeocoderResults geocode(String address, Envelope bbox) {
try {
URL banoUrl = getBanoGeocoderUrl(address, bbox);
URLConnection conn = banoUrl.openConnection();
InputStream in = conn.getInputStream();
FeatureCollection featureCollection = mapper.readValue(in, FeatureCollection.class);
in.close();
List<GeocoderResult> geocoderResults = new ArrayList<GeocoderResult>();
for (Feature feature : featureCollection.getFeatures()) {
GeoJsonObject geom = feature.getGeometry();
if (geom instanceof Point) {
Point p = (Point) geom;
GeocoderResult res = new GeocoderResult();
res.setLat(p.getCoordinates().getLatitude());
res.setLng(p.getCoordinates().getLongitude());
res.setDescription(feature.getProperties().get("label").toString());
/*
* Note: We also have here as properties a break-down of other details, such as
* the house number, street, city, postcode... Can be useful if needed.
*/
geocoderResults.add(res);
} else {
// Should not happen according to the API
}
}
return new GeocoderResults(geocoderResults);
} catch (IOException e) {
LOG.error("Error processing BANO geocoder results", e);
return new GeocoderResults(e.getLocalizedMessage());
}
}
use of org.opentripplanner.geocoder.GeocoderResult in project OpenTripPlanner by opentripplanner.
the class BanoGeocoderTest method testOnLine.
/**
* TODO -- This unit-test rely on an on-line API to be up and running, which may not be the case
* if a network connection is not active or the server is down.
*/
@Test
public void testOnLine() throws Exception {
BanoGeocoder banoGeocoder = new BanoGeocoder();
// The Presidential palace of the French Republic is not supposed to move often
Envelope bbox = new Envelope();
bbox.expandToInclude(2.25, 48.8);
bbox.expandToInclude(2.35, 48.9);
GeocoderResults results = banoGeocoder.geocode("55 Rue du Faubourg Saint-Honoré", bbox);
assert (results.getResults().size() >= 1);
boolean found = false;
for (GeocoderResult result : results.getResults()) {
if ("55 Rue du Faubourg Saint-Honoré 75008 Paris".equals(result.getDescription())) {
double dist = SphericalDistanceLibrary.distance(result.getLat(), result.getLng(), 48.870637, 2.316939);
assert (dist < 100);
found = true;
}
}
assert (found);
}
use of org.opentripplanner.geocoder.GeocoderResult in project OpenTripPlanner by opentripplanner.
the class GeocoderServerTest method testGeocodeValidAddress.
@Test
public void testGeocodeValidAddress() {
final double lat = 78.121;
final double lng = -43.237;
final String description = "121 elm street";
geocoderServer.geocoder = new Geocoder() {
@Override
public GeocoderResults geocode(String address, Envelope bbox) {
GeocoderResult result = new GeocoderResult(lat, lng, description);
return new GeocoderResults(Arrays.asList(result));
}
};
GeocoderResults results = geocoderServer.geocode("121 elm street", null);
for (GeocoderResult result : results.getResults()) {
// should only have one result
assertEquals("description matches", description, result.getDescription());
assertEquals(lat, result.getLat(), 0.001);
assertEquals(lng, result.getLng(), 0.001);
}
}
use of org.opentripplanner.geocoder.GeocoderResult in project OpenTripPlanner by opentripplanner.
the class GoogleGeocoder method geocode.
@Override
public GeocoderResults geocode(String address, Envelope bbox) {
String content = null;
try {
// make json request
URL googleGeocoderUrl = getGoogleGeocoderUrl(address);
URLConnection conn = googleGeocoderUrl.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
StringBuilder sb = new StringBuilder(128);
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
reader.close();
content = sb.toString();
} catch (IOException e) {
LOG.error("Error parsing Google geocoder response", e);
return noGeocoderResult("Error parsing geocoder response");
}
GoogleGeocoderResults googleGeocoderResults = googleJsonDeserializer.parseResults(content);
List<GoogleGeocoderResult> googleResults = googleGeocoderResults.getResults();
List<GeocoderResult> geocoderResults = new ArrayList<GeocoderResult>();
for (GoogleGeocoderResult googleGeocoderResult : googleResults) {
Geometry geometry = googleGeocoderResult.getGeometry();
Location location = geometry.getLocation();
Double lat = location.getLat();
Double lng = location.getLng();
String formattedAddress = googleGeocoderResult.getFormatted_address();
GeocoderResult geocoderResult = new GeocoderResult(lat, lng, formattedAddress);
geocoderResults.add(geocoderResult);
}
return new GeocoderResults(geocoderResults);
}
Aggregations