package votorola.a; // Copyright 2008-2011, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import com.google.gson.stream.*; import com.sun.jersey.api.uri.*; import java.io.*; import java.net.*; import java.sql.*; import java.util.logging.*; import votorola.a.*; import votorola.g.lang.*; import votorola.g.logging.*; import votorola.g.net.*; /** A geocoder based on the Google Geocoding API, version 3. It converts street addresses * to cartographic coordinates. * * @see http://code.google.com/apis/maps/documentation/geocoding/ */ public @ThreadSafe final class GoogleGeocoder { /** Constructs a GoogleGeocoder. * * @see #geocodeTable() */ public GoogleGeocoder( Geocode.Table _geocodeTable ) { geocodeTable = _geocodeTable; } // ------------------------------------------------------------------------------------ /** Geocodes a street address. * * @see Geocode#region() * @see Geocode#address() */ public Geocode geocode( final String region, final String address ) throws IOException, SQLException { final Geocode geocode = new Geocode( region, address, geocodeTable ); if( geocode.exists() ) return geocode; final URL url; try { final StringBuilder b = new StringBuilder(); b.append( "http://maps.googleapis.com/maps/api/geocode/json?address=" ); b.append( UriComponent.encode( address, UriComponent.Type.QUERY_PARAM )); b.append( "®ion=" ); b.append( UriComponent.encode( region, UriComponent.Type.QUERY_PARAM )); b.append( "&sensor=false" ); url = new URL( b.toString() ); } catch( MalformedURLException x ) { throw new RuntimeException( x ); } boolean areCoordinatesSet = false; logger.fine( "querying geocoder: " + url ); final HttpURLConnection http = (HttpURLConnection)( url.openConnection() ); URLConnectionX.connect( http ); try { final JsonReader in = new JsonReader( new InputStreamReader( http.getInputStream(), "UTF-8" )); // assumed encoding, or parse http.getContentType() for actual try { in.beginObject(); while( in.hasNext() ) { String name = in.nextName(); if( "status".equals( name )) { final String status = in.nextString(); if( !"OK".equals( status )) throw new Geocode.GeocodingException( "geocoder status=" + status + ": " + url ); } else if( "results".equals( name )) { in.beginArray(); if( in.hasNext() ) { in.beginObject(); while( in.hasNext() ) { if( !"geometry".equals( in.nextName() )) { in.skipValue(); continue; } in.beginObject(); while( in.hasNext() ) { if( !"location".equals( in.nextName() )) { in.skipValue(); continue; } in.beginObject(); double lat = Double.NaN; double lng = lat; while( in.hasNext() ) { name = in.nextName(); if( "lat".equals( name )) { lat = Math.toRadians( in.nextDouble() ); } else if( "lng".equals( name )) { lng = Math.toRadians( in.nextDouble() ); } else in.skipValue(); } if( lat != Double.NaN && lng != Double.NaN ) { geocode.setCoordinates( lat, lng ); areCoordinatesSet = true; } in.endObject(); while( in.hasNext() ) in.skipValue(); // skip others break; } in.endObject(); while( in.hasNext() ) in.skipValue(); // skip others break; } in.endObject(); while( in.hasNext() ) in.skipValue(); // additional results, not expected } in.endArray(); } else in.skipValue(); } in.endObject(); } finally { in.close(); } } finally{ http.disconnect(); } if( !areCoordinatesSet ) throw new Geocode.GeocodingException( "missing geocode location in response: " + url ); logger.info( "caching geocode: " + geocode ); geocode.write( geocodeTable ); return geocode; } /** The relational cache of geocoded residential addresses. * * @see votorola.a.VoteServer.Run#geocodeTable() */ public Geocode.Table geocodeTable() { return geocodeTable; } private final Geocode.Table geocodeTable; //// P r i v a t e /////////////////////////////////////////////////////////////////////// private static final Logger logger = LoggerX.i( GoogleGeocoder.class ); }