ReverseGeocoder improvements

This commit is contained in:
Lennart Hensler committed 2012-01-10 16:25:04 +01:00
1 parent ec235cde9f
commit d51ff6cbe4
7 files changed
+177 -57

No files matched your search

@@ -1,6 +1,5 @@
package com.sap.sailing.domain.base;
import com.sap.sailing.domain.base.impl.NauticalMileDistance;
/**
* A distance which can be converted to various units of measure. Can be negative.
@@ -9,7 +8,58 @@ import com.sap.sailing.domain.base.impl.NauticalMileDistance;
*
*/
public interface Distance extends Comparable<Distance> {
static final Distance NULL = new NauticalMileDistance(0);
static final Distance NULL = new Distance() {
@Override
public int compareTo(Distance o) {
return getMeters() > o.getMeters() ? 1 : getMeters() == o.getMeters() ? 0 : -1;
}
@Override
public double getGeographicalMiles() {
return 0;
}
@Override
public double getSeaMiles() {
return 0;
}
@Override
public double getNauticalMiles() {
return 0;
}
@Override
public double getMeters() {
return 0;
}
@Override
public double getKilometers() {
return 0;
}
@Override
public double getCentralAngleDeg() {
return 0;
}
@Override
public double getCentralAngleRad() {
return 0;
}
@Override
public Distance scale(double factor) {
return this;
}
@Override
public Speed inTime(long milliseconds) {
return Speed.NULL;
}
};
double getGeographicalMiles();
@@ -1,7 +1,5 @@
package com.sap.sailing.domain.base;
import com.sap.sailing.domain.base.impl.DegreeBearingImpl;
import com.sap.sailing.domain.base.impl.KnotSpeedWithBearingImpl;
/**
* A speed, convertable in various units of measure. Can be negative.
@@ -10,7 +8,38 @@ import com.sap.sailing.domain.base.impl.KnotSpeedWithBearingImpl;
*
*/
public interface Speed extends Comparable<Speed> {
final static Speed NULL = new KnotSpeedWithBearingImpl(0, new DegreeBearingImpl(0));
final static Speed NULL = new Speed() {
@Override
public int compareTo(Speed o) {
return getMetersPerSecond() > o.getMetersPerSecond() ? 1 : getMetersPerSecond() == o.getMetersPerSecond() ? 0 : -1;
}
@Override
public double getKnots() {
return 0;
}
@Override
public double getMetersPerSecond() {
return 0;
}
@Override
public double getKilometersPerHour() {
return 0;
}
@Override
public double getBeaufort() {
return 0;
}
@Override
public Distance travel(TimePoint from, TimePoint to) {
return Distance.NULL;
}
};
double getKnots();
@@ -40,11 +40,25 @@ public class ReverseGeocoderTest {
try {
List<Placemark> placemarks = geocoder.getPlacemarkNear(latDeg, lngDeg, 20);
Assert.assertFalse(placemarks.isEmpty());
} catch (IOException e) {
Assert.fail(e.getMessage());
} catch (ParseException e) {
Assert.fail(e.getMessage());
}
}
@Test
public void getPlacemarkBestTest() {
double latDeg = 54.3231063453431;
double lngDeg = 10.12265682220459;
try {
Placemark p = geocoder.getPlacemarkBest(latDeg, lngDeg, 20, new Placemark.ByPopulation());
Assert.assertNotNull(p);
} catch (IOException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
} catch (ParseException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
@@ -1,5 +1,7 @@
package com.sap.sailing.geocoding;
import java.util.Comparator;
public interface Placemark {
String getName();
@@ -12,4 +14,23 @@ public interface Placemark {
float distanceFrom(double latDeg, double lngDeg);
String getCountryName();
/**
* Sorts the Placemarks by Population from low to high.
* @author Lennart Hensler (D054527)
*
*/
public class ByPopulation implements Comparator<Placemark> {
@Override
public int compare(Placemark p1, Placemark p2) {
return new Long(p1.getPopulation()).compareTo(p2.getPopulation());
}
}
public class ByDistance implements Comparator<Placemark> {
@Override
public int compare(Placemark p1, Placemark p2) {
return 0;
}
}
}
@@ -60,18 +60,53 @@ public class PlacemarkImpl implements Placemark {
return null;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((countryCode == null) ? 0 : countryCode.hashCode());
long temp;
temp = Double.doubleToLongBits(latDeg);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(lngDeg);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + (int) (population ^ (population >>> 32));
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
@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;
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PlacemarkImpl other = (PlacemarkImpl) obj;
if (countryCode == null) {
if (other.countryCode != null)
return false;
} else if (!countryCode.equals(other.countryCode))
return false;
if (Double.doubleToLongBits(latDeg) != Double.doubleToLongBits(other.latDeg))
return false;
if (Double.doubleToLongBits(lngDeg) != Double.doubleToLongBits(other.lngDeg))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (population != other.population)
return false;
if (type == null) {
if (other.type != null)
return false;
} else if (!type.equals(other.type))
return false;
return true;
}
}
@@ -33,15 +33,15 @@ public class ReverseGeocoderImpl implements ReverseGeocoder {
url.append("&lat=" + Double.toString(latDeg));
url.append("&lng=" + Double.toString(lngDeg));
url.append("&username=" + GEONAMES_USER);
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);
JSONArray geonames = (JSONArray) obj.get("geonames");
p = JSONToPlacemark((JSONObject) geonames.get(0));
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);
JSONArray geonames = (JSONArray) obj.get("geonames");
p = JSONToPlacemark((JSONObject) geonames.get(0));
return p;
}
@@ -76,7 +76,7 @@ public class ReverseGeocoderImpl implements ReverseGeocoder {
List<Placemark> placemarks = getPlacemarkNear(latDeg, lngDeg, radius);
Collections.sort(placemarks, comp);
return placemarks.get(0);
return placemarks.get(placemarks.size() - 1);
}
private Placemark JSONToPlacemark(JSONObject json) {
@@ -84,7 +84,7 @@ public class ReverseGeocoderImpl implements ReverseGeocoder {
String countryCode = (String) json.get("countryCode");
Double latDeg = null;
//Tries are necessary, becaus some lat or lng values delivered by geonames have no decimal places and are interpreted as Long
//Tries are necessary, because some latitude or longitude values delivered by Geonames have no decimal places and are interpreted as Long
//Casting a Long to a Double raises a ClassCastException
try {
latDeg = (Double) json.get("lat");
@@ -1,29 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="com.google.gdt.eclipse.suite.webapp">
<booleanAttribute key="com.google.gdt.eclipse.core.RUN_SERVER" value="false"/>
<stringAttribute key="com.google.gdt.eclipse.suiteMainTypeProcessor.PREVIOUSLY_SET_MAIN_TYPE_NAME" value="com.google.gwt.dev.GWTShell"/>
<booleanAttribute key="com.google.gdt.eclipse.suiteWarArgumentProcessor.IS_WAR_FROM_PROJECT_PROPERTIES" value="true"/>
<listAttribute key="com.google.gwt.eclipse.core.ENTRY_POINT_MODULES">
<listEntry value="com.sap.sailing.gwt.ui.Spectator"/>
</listAttribute>
<stringAttribute key="com.google.gwt.eclipse.core.URL" value="/gwt/AdminConsole.html"/>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="/com.sap.sailing.gwt.ui"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="4"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.ui.favoriteGroups">
<listEntry value="org.eclipse.debug.ui.launchGroup.debug"/>
<listEntry value="org.eclipse.debug.ui.launchGroup.run"/>
</listAttribute>
<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="com.google.gwt.eclipse.core.moduleClasspathProvider"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="com.google.gwt.dev.DevMode"/>
<<<<<<< HEAD
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="-war &quot;${project_loc:com.sap.sailing.gwt.ui}&quot; -noserver -remoteUI &quot;${gwt_remote_ui_server_port}:${unique_id}&quot; -logLevel INFO -codeServerPort 9997 -startupUrl /gwt/AdminConsole.html com.sap.sailing.gwt.ui.AdminConsole -startupUrl /gwt/LeaderboardEditing.html com.sap.sailing.gwt.ui.LeaderboardEditing -startupUrl /gwt/UserManagement.html com.sap.sailing.gwt.ui.UserManagement -startupUrl /gwt/Leaderboard.html com.sap.sailing.gwt.ui.Leaderboard -startupUrl /gwt/Spectator.html com.sap.sailing.gwt.ui.Spectator"/>
=======
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="-war &quot;${project_loc:com.sap.sailing.gwt.ui}&quot; -noserver -remoteUI &quot;${gwt_remote_ui_server_port}:${unique_id}&quot; -logLevel INFO -codeServerPort 9997 -startupUrl /gwt/AdminConsole.html com.sap.sailing.gwt.ui.AdminConsole -startupUrl /gwt/LeaderboardEditing.html com.sap.sailing.gwt.ui.LeaderboardEditing -startupUrl /gwt/UserManagement.html com.sap.sailing.gwt.ui.UserManagement -startupUrl /gwt/Leaderboard.html com.sap.sailing.gwt.ui.Leaderboard"/>
>>>>>>> origin/gwt
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="com.sap.sailing.gwt.ui"/>
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Xmx512m"/>
</launchConfiguration>