private String getAddressReverseGeocodeURL(GeoPoint gp) { // - // http://nominatim.openstreetmap.org/reverse?format=xml&addressdetails=1&zoom=18&lat=46.17330&lon=21.29370 StringBuffer sb = new StringBuffer(); RTProperties rtp = this.getProperties(); String url = rtp.getString(PROP_reverseURL, null); if (!StringTools.isBlank(url)) { sb.append(url); } else { String host = rtp.getString(PROP_hostName, HOST_PRIMARY); sb.append("http://"); sb.append(host); if (host.indexOf("mapquest") >= 0) { sb.append("/nominatim/v1/reverse?"); } else { sb.append("/reverse?"); } } sb.append("format=xml&"); sb.append("limit=1&"); // sb.append("osm_type=W&"); sb.append("addressdetails=").append(rtp.getString(PROP_addressdetails, "1")).append("&"); // 0,1 sb.append("zoom=").append(rtp.getString(PROP_zoom, "18")).append("&"); // 0..18 sb.append("email=").append(this.getEmail()).append("&"); // required, per usage policy sb.append("lat=").append(gp.getLatitudeString(GeoPoint.SFORMAT_DEC_5, null)).append("&"); sb.append("lon=").append(gp.getLongitudeString(GeoPoint.SFORMAT_DEC_5, null)); return sb.toString(); }
/** ** Main entery point for debugging/testing */ public static void main(String argv[]) { RTConfig.setCommandLineArgs(argv); Print.setAllOutputToStdout(true); Print.setEncoding(ENCODING_UTF8); String accountID = RTConfig.getString(ARG_ACCOUNT, "demo"); GoogleGeocodeV2 gn = new GoogleGeocodeV2("google", null, null); /* reverse geocode */ if (RTConfig.hasProperty(ARG_REVGEOCODE)) { GeoPoint gp = new GeoPoint(RTConfig.getString(ARG_REVGEOCODE, null)); if (!gp.isValid()) { Print.logInfo("Invalid GeoPoint specified"); System.exit(1); } Print.logInfo("Reverse-Geocoding GeoPoint: " + gp); Print.sysPrintln( "RevGeocode = " + gn.getReverseGeocode(gp, null /*localeStr*/, false /*cache*/)); // Note: Even though the values are printed in UTF-8 character encoding, the // characters may not appear to be property displayed if the console display // does not support UTF-8. System.exit(0); } /* no options */ Print.sysPrintln("No options specified"); System.exit(1); }
/** ** Main entery point for debugging/testing */ public static void main(String argv[]) { RTConfig.setCommandLineArgs(argv); Print.setAllOutputToStdout(true); Print.setEncoding(ENCODING_UTF8); /* host */ String host = RTConfig.getString("host", null); if (!StringTools.isBlank(host)) { HOST_PRIMARY = host; } /* GeoPoint */ GeoPoint gp = new GeoPoint(RTConfig.getString("gp", null)); if (!gp.isValid()) { Print.logInfo("Invalid GeoPoint specified"); System.exit(1); } Print.logInfo("Reverse-Geocoding GeoPoint: " + gp); /* Reverse Geocoding */ Nominatim gn = new Nominatim("nominatim", null, RTConfig.getCommandLineProperties()); Print.sysPrintln("RevGeocode = " + gn.getReverseGeocode(gp, null /*localeStr*/)); }
// http://code.google.com/apis/maps/documentation/geocoding/index.html public GeoPoint getGeocode(String address, String country) { /* URL */ String url = this.getGeoPointGeocodeURL(address, country); Print.logDebug("Google GC URL: " + url); /* create XML document */ Document xmlDoc = GetXMLDocument(url, this.getGeocodeTimeout()); if (xmlDoc == null) { return null; } /* vars */ String errCode = null; GeoPoint geoPoint = null; /* parse "xml" */ Element kml = xmlDoc.getDocumentElement(); if (kml.getTagName().equalsIgnoreCase(TAG_kml)) { NodeList ResponseList = XMLTools.getChildElements(kml, TAG_Response); for (int g = 0; (g < ResponseList.getLength()); g++) { Element response = (Element) ResponseList.item(g); NodeList responseNodes = response.getChildNodes(); for (int n = 0; n < responseNodes.getLength(); n++) { Node responseNode = responseNodes.item(n); if (!(responseNode instanceof Element)) { continue; } Element responseElem = (Element) responseNode; String responseNodeName = responseElem.getNodeName(); if (responseNodeName.equalsIgnoreCase(TAG_name)) { // <name>1600 Amphitheatre Parkway, Mountain View, CA</name> } else if (responseNodeName.equalsIgnoreCase(TAG_Status)) { // <Status> ... </Status> NodeList statusNodes = responseElem.getChildNodes(); for (int st = 0; st < statusNodes.getLength(); st++) { Node statusNode = statusNodes.item(st); if (!(statusNode instanceof Element)) { continue; } Element statusElem = (Element) statusNode; String statusNodeName = statusElem.getNodeName(); if (statusNodeName.equalsIgnoreCase(TAG_code)) { errCode = StringTools.trim(GoogleGeocodeV2.GetNodeText(statusElem)); // expect "200" break; // we only care about the 'code' } } } else if (responseNodeName.equalsIgnoreCase(TAG_Placemark)) { // <Placemark> ... </Placemark> String id = responseElem.getAttribute(ATTR_id); if ((id != null) && id.equals("p1")) { NodeList placemarkNodes = responseElem.getChildNodes(); for (int pm = 0; (geoPoint == null) && (pm < placemarkNodes.getLength()); pm++) { Node placemarkNode = placemarkNodes.item(pm); if (!(placemarkNode instanceof Element)) { continue; } Element placemarkElem = (Element) placemarkNode; String placemarkNodeName = placemarkElem.getNodeName(); if (placemarkNodeName.equalsIgnoreCase(TAG_Point)) { NodeList pointNodes = placemarkElem.getChildNodes(); for (int ptn = 0; (geoPoint == null) && (ptn < pointNodes.getLength()); ptn++) { Node pointNode = pointNodes.item(ptn); if (!(pointNode instanceof Element)) { continue; } Element pointElem = (Element) pointNode; String pointNodeName = pointElem.getNodeName(); if (pointNodeName.equalsIgnoreCase(TAG_coordinates)) { String ll[] = StringTools.split(GoogleGeocodeV2.GetNodeText(pointElem), ','); if (ll.length >= 2) { double lon = StringTools.parseDouble(ll[0], 0.0); // longitude is first double lat = StringTools.parseDouble(ll[1], 0.0); if (GeoPoint.isValid(lat, lon)) { geoPoint = new GeoPoint(lat, lon); break; // we only care about the 'GeoPoint' } } } } } } } else { // Print.logInfo("Skipping Placemark ID = %s", id); } } } } } /* create address */ if (geoPoint != null) { // GeoPoint found return geoPoint; } /* check for Google reverse-geocode limit exceeded */ if ((errCode != null) && errCode.equals("620")) { Print.logError("!!!! Google Reverse-Geocode Limit Exceeded [Error 620] !!!!"); } /* no reverse-geocode available */ return null; }
/* encode GeoPoint into nearest address URI */ protected String getAddressReverseGeocodeURL(GeoPoint gp, String localStr) { StringBuffer sb = new StringBuffer(); GoogleSig sig = this.getSignature(); /* predefined URL */ String rgURL = this.getProperties().getString(PROP_reverseGeocodeURL, null); if (!StringTools.isBlank(rgURL)) { sb.append(rgURL); sb.append("&ll=") .append(gp.getLatitudeString(GeoPoint.SFORMAT_DEC_5, null)) .append(",") .append(gp.getLongitudeString(GeoPoint.SFORMAT_DEC_5, null)); String defURL = sb.toString(); if (sig == null) { return defURL; } else { String urlStr = sig.signURL(defURL); return (urlStr != null) ? urlStr : defURL; } } /* assemble URL */ sb.append(this.getAddressReverseGeocodeURI()); sb.append("output=xml"); sb.append("&oe=utf8"); /* latitude/longitude */ sb.append("&ll=") .append(gp.getLatitudeString(GeoPoint.SFORMAT_DEC_5, null)) .append(",") .append(gp.getLongitudeString(GeoPoint.SFORMAT_DEC_5, null)); /* sensor */ String sensor = this.getProperties().getString(PROP_sensor, null); if (!StringTools.isBlank(sensor)) { sb.append("&sensor=").append(sensor); } /* key */ String channel = this.getProperties().getString(PROP_channel, null); String auth = this.getAuthorization(); if (StringTools.isBlank(auth) || auth.startsWith("*")) { // invalid key } else if (auth.startsWith(CLIENT_ID_PREFIX)) { sb.append("&client=").append(auth); if (StringTools.isBlank(channel)) { channel = DBConfig.getServiceAccountID(null); } } else { sb.append("&key=").append(auth); } /* channel */ if (!StringTools.isBlank(channel)) { sb.append("&channel=").append(channel); } /* localization ("&hl=") */ if (!StringTools.isBlank(localStr)) { sb.append("&hl=").append(localStr); } /* return url */ String defURL = sb.toString(); if (sig == null) { return defURL; } else { String urlStr = sig.signURL(defURL); return (urlStr != null) ? urlStr : defURL; } }