Search in sources :

Example 6 with GeocoderResult

use of org.opentripplanner.geocoder.GeocoderResult 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 7 with GeocoderResult

use of org.opentripplanner.geocoder.GeocoderResult 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

GeocoderResult (org.opentripplanner.geocoder.GeocoderResult)7 GeocoderResults (org.opentripplanner.geocoder.GeocoderResults)7 ArrayList (java.util.ArrayList)5 IOException (java.io.IOException)4 URL (java.net.URL)4 URLConnection (java.net.URLConnection)4 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3 Envelope (com.vividsolutions.jts.geom.Envelope)2 Test (org.junit.Test)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 Geocoder (org.opentripplanner.geocoder.Geocoder)1