Search in sources :

Example 6 with GeocoderResults

use of org.opentripplanner.geocoder.GeocoderResults 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);
}
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 7 with GeocoderResults

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

the class GeocoderGeoZoneCropper method geocode.

@Override
public GeocoderResults geocode(String address, Envelope bbox) {
    GeocoderResults retval = decorated.geocode(address, bbox);
    if (retval.getResults() != null) {
        List<GeocoderResult> results = new ArrayList<GeocoderResult>(retval.getCount());
        for (GeocoderResult result : retval.getResults()) {
            if (result.getLat() > minLat && result.getLng() > minLon && result.getLat() < maxLat && result.getLng() < maxLon)
                results.add(result);
        }
        retval.setResults(results);
    }
    return retval;
}
Also used : GeocoderResults(org.opentripplanner.geocoder.GeocoderResults) ArrayList(java.util.ArrayList) GeocoderResult(org.opentripplanner.geocoder.GeocoderResult)

Example 8 with GeocoderResults

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

the class NominatimGeocoder method geocode.

@Override
public GeocoderResults geocode(String address, Envelope bbox) {
    String content = null;
    try {
        // make json request
        URL nominatimGeocoderUrl = getNominatimGeocoderUrl(address, bbox);
        URLConnection conn = nominatimGeocoderUrl.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 nominatim geocoder response", e);
        return noGeocoderResult("Error parsing geocoder response");
    }
    List<NominatimGeocoderResult> nominatimResults = nominatimJsonDeserializer.parseResults(content);
    List<GeocoderResult> geocoderResults = new ArrayList<GeocoderResult>();
    for (NominatimGeocoderResult nominatimGeocoderResult : nominatimResults) {
        Double lat = nominatimGeocoderResult.getLatDouble();
        Double lng = nominatimGeocoderResult.getLngDouble();
        String displayName = nominatimGeocoderResult.getDisplay_name();
        GeocoderResult geocoderResult = new GeocoderResult(lat, lng, displayName);
        geocoderResults.add(geocoderResult);
    }
    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)

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