Implemented ReverseGeocoder methods and added some tests

This commit is contained in:
Lennart Hensler committed 2012-01-10 14:18:50 +01:00
1 parent f08632a43d
commit ac21a7c200
40 files changed
+102 -289

No files matched your search

@@ -1,15 +1,18 @@
package com.sap.sailing.geocoding;
import java.io.IOException;
import java.util.Comparator;
import java.util.List;
import org.json.simple.parser.ParseException;
import com.sap.sailing.geocoding.impl.ReverseGeocoderImpl;
public interface ReverseGeocoder {
final ReverseGeocoder INSTANCE = new ReverseGeocoderImpl();
final String GEONAMES_USER = "sailtracking";
Placemark getPlacemark(double latDeg, double lngDeg);
Placemark getPlacemark(double latDeg, double lngDeg) throws IOException, ParseException;
List<Placemark> getPlacemarkNear(double latDeg, double lngDeg, float radius);
@@ -59,5 +59,19 @@ public class PlacemarkImpl implements Placemark {
// TODO Get country name from CountryCodeFactoryImpl with countryCode
return null;
}
@Override
public boolean equals(Object obj) {
boolean equals = true;
if (equals = (obj instanceof Placemark)) {
Placemark p = (Placemark) obj;
equals = name.equals(p.getName()) && countryCode.equals(p.getCountryCode())
&& Double.compare(latDeg, p.getLatDeg()) == 0 && Double.compare(lngDeg, p.getLngDeg()) == 0
&& type.equals(p.getType()) && population == p.getPopulation();
}
return equals;
}
}
@@ -24,16 +24,9 @@ public class ReverseGeocoderImpl implements ReverseGeocoder {
private final String NEARBY_PLACE_SERVICE = "http://api.geonames.org/findNearbyPlaceNameJSON?";
private int maxRows = 500;
public static void main(String[] args) {
ReverseGeocoder g = ReverseGeocoder.INSTANCE;
Placemark p = g.getPlacemark(55, 10);
System.out.println(p.getName());
}
@Override
public Placemark getPlacemark(double latDeg, double lngDeg) {
public Placemark getPlacemark(double latDeg, double lngDeg) throws IOException, ParseException {
Placemark p = null;
StringBuilder url = new StringBuilder(NEARBY_PLACE_SERVICE);
@@ -41,25 +34,13 @@ public class ReverseGeocoderImpl implements ReverseGeocoder {
url.append("&lng=" + Double.toString(lngDeg));
url.append("&username=" + GEONAMES_USER);
try {
URL request = new URL(url.toString());
URLConnection connection = request.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject) parser.parse(in);
// JSONObject obj = (JSONObject) parser.parse(new FileReader("C:\\JSON Test\\Geonames Single JSON"));
JSONArray geonames = (JSONArray) obj.get("geonames");
p = JSONToPlacemark((JSONObject) geonames.get(0));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return p;
}