GeoNames Home | Postal Codes | Download / Webservice | About 

GeoNames Forum
  [Search] Search   [Recent Topics] Recent Topics   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Java API - reverse geocoding  XML
Forum Index -> General
Author Message
hannessmit



Joined: 07/06/2009 14:17:14
Messages: 18
Offline

Hello,

I can not find any reverse geocoding possibilities in the Java api for GeoNames. Am I right?

Suppose I want to retrieve a lon/lat nearby spot like a metro station:
using the http url i can use:

http://ws.geonames.org/findNearby?lat=52.373&lng=4.9&featureClass=S&featureCode=MTRO

--> gives the nearby metro station

But can I create this function also using the API?

Thanks!
hannessmit



Joined: 07/06/2009 14:17:14
Messages: 18
Offline

I didn't test this yet, but I think the following code can be easily added:

Code:
public static List<Toponym> findNearbyFeature(double latitude, double longitude, double radius, int maxRows, 
 String featureClass, String featureCode) 
 throws IOException,	Exception {
 		List<Toponym> places = new ArrayList<Toponym>();
 
 		String url = "/findNearby?";
 
 		url = url + "&lat=" + latitude;
 		url = url + "&lng=" + longitude;
 		
 		if (radius > 0) {
 			url = url + "&radius=" + radius;
 		}
 		if (maxRows > 0) {
 			url = url + "&maxRows=" + maxRows;
 		}
 		
 		if (featureClass != "") {
 			url = url + "&featureClass=" + maxRows;
 		}		
 		if (featureCode != "") {
 			url = url + "&featureCode=" + maxRows;
 		}
 		
 		url = addUserName(url);
 		url = addDefaultStyle(url);
 
 		SAXBuilder parser = new SAXBuilder();
 		Document doc = parser.build(connect(url));
 
 		Element root = doc.getRootElement();
 		for (Object obj : root.getChildren("geoname")) {
 			Element toponymElement = (Element) obj;
 			Toponym toponym = getToponymFromElement(toponymElement);
 			places.add(toponym);
 		}
 
 		return places;
 	}
 


Is this implementable?
marc



Joined: 08/12/2005 07:39:47
Messages: 4499
Offline

I would implement it like this:

Code:
	public static List<Toponym> findNearby(double latitude, double longitude,
 			FeatureClass featureClass, String[] featureCodes)
 			throws IOException, Exception {
 		List<Toponym> places = new ArrayList<Toponym>();
 
 		String url = "/findNearby?";
 
 		url += "&lat=" + latitude;
 		url += "&lng=" + longitude;
 		if (featureClass != null) {
 			url += "&featureClass=" + featureClass;
 		}
 		if (featureCodes != null && featureCodes.length > 0) {
 			for (String featureCode : featureCodes) {
 				url += "&featureCode=" + featureCode;
 			}
 		}
 		url = addUserName(url);
 		url = addDefaultStyle(url);
 
 		SAXBuilder parser = new SAXBuilder();
 		Document doc = parser.build(connect(url));
 
 		Element root = doc.getRootElement();
 		for (Object obj : root.getChildren("geoname")) {
 			Element toponymElement = (Element) obj;
 			Toponym toponym = getToponymFromElement(toponymElement);
 			places.add(toponym);
 		}
 
 		return places;
 	}

[WWW]
hannessmit



Joined: 07/06/2009 14:17:14
Messages: 18
Offline

Hi Marc,
After I posted this I also realised that multiple featurecodes can be useful. Will you implement this into the next geonames jar?
Thanks!
marc



Joined: 08/12/2005 07:39:47
Messages: 4499
Offline

Yes, it will be in the next release.

Marc

[WWW]
 
Forum Index -> General
Go to:   
Powered by JForum 2.1.5 © JForum Team