Search in sources :

Example 1 with GeocoderResults

use of org.opentripplanner.geocoder.GeocoderResults 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);
}
Also used : GeocoderResults(org.opentripplanner.geocoder.GeocoderResults) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection) BufferedReader(java.io.BufferedReader) GeocoderResult(org.opentripplanner.geocoder.GeocoderResult)

Example 2 with GeocoderResults

use of org.opentripplanner.geocoder.GeocoderResults 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());
    }
}
Also used : GeocoderResults(org.opentripplanner.geocoder.GeocoderResults) FeatureCollection(org.geojson.FeatureCollection) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Point(org.geojson.Point) IOException(java.io.IOException) GeoJsonObject(org.geojson.GeoJsonObject) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) Feature(org.geojson.Feature) URL(java.net.URL) URLConnection(java.net.URLConnection) GeocoderResult(org.opentripplanner.geocoder.GeocoderResult)

Example 3 with GeocoderResults

use of org.opentripplanner.geocoder.GeocoderResults 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);
}
Also used : GeocoderResults(org.opentripplanner.geocoder.GeocoderResults) Envelope(com.vividsolutions.jts.geom.Envelope) GeocoderResult(org.opentripplanner.geocoder.GeocoderResult) Test(org.junit.Test)

Example 4 with GeocoderResults

use of org.opentripplanner.geocoder.GeocoderResults 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);
    }
}
Also used : GeocoderResults(org.opentripplanner.geocoder.GeocoderResults) Envelope(com.vividsolutions.jts.geom.Envelope) Geocoder(org.opentripplanner.geocoder.Geocoder) GeocoderResult(org.opentripplanner.geocoder.GeocoderResult) Test(org.junit.Test)

Example 5 with GeocoderResults

use of org.opentripplanner.geocoder.GeocoderResults in project OpenTripPlanner by opentripplanner.

the class GeocoderServerTest method testGeocodeInvalidAddress.

@Test
public void testGeocodeInvalidAddress() {
    final String error = "uh oh";
    geocoderServer.geocoder = new Geocoder() {

        @Override
        public GeocoderResults geocode(String address, Envelope bbox) {
            return new GeocoderResults(error);
        }
    };
    GeocoderResults result = geocoderServer.geocode("121 elm street", null);
    assertEquals("error returned", error, result.getError());
}
Also used : GeocoderResults(org.opentripplanner.geocoder.GeocoderResults) Envelope(com.vividsolutions.jts.geom.Envelope) Geocoder(org.opentripplanner.geocoder.Geocoder) Test(org.junit.Test)

Aggregations

GeocoderResults (org.opentripplanner.geocoder.GeocoderResults)8 GeocoderResult (org.opentripplanner.geocoder.GeocoderResult)7 ArrayList (java.util.ArrayList)5 IOException (java.io.IOException)4 URL (java.net.URL)4 URLConnection (java.net.URLConnection)4 Envelope (com.vividsolutions.jts.geom.Envelope)3 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3 Test (org.junit.Test)3 Geocoder (org.opentripplanner.geocoder.Geocoder)2 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)1 InputStream (java.io.InputStream)1 Feature (org.geojson.Feature)1 FeatureCollection (org.geojson.FeatureCollection)1 GeoJsonObject (org.geojson.GeoJsonObject)1 Point (org.geojson.Point)1