make 0/0 coordinates valid even if using non-WGS84 ellipsoid

This commit is contained in:
Andreas Schildbach 2013-06-14 17:37:19 +02:00
parent 871b08f13a
commit aaa2a6c810
2 changed files with 68 additions and 29 deletions

View file

@ -534,22 +534,25 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider
final String place = !"loc".equals(anyType) ? normalizeLocationName(pp.getAttributeValue(null, "locality")) : null; final String place = !"loc".equals(anyType) ? normalizeLocationName(pp.getAttributeValue(null, "locality")) : null;
final String name = normalizeLocationName(pp.getAttributeValue(null, "objectName")); final String name = normalizeLocationName(pp.getAttributeValue(null, "objectName"));
final String mapName = pp.getAttributeValue(null, "mapName"); final String mapName = XmlPullUtil.optAttr(pp, "mapName", null);
final float x = XmlPullUtil.optFloatAttr(pp, "x", 0);
final float y = XmlPullUtil.optFloatAttr(pp, "y", 0);
final int lat; final int lat;
final int lon; final int lon;
if (mapName == null || mapName.length() == 0) if (mapName == null || (x == 0 && y == 0))
{ {
lat = 0; lat = 0;
lon = 0; lon = 0;
} }
else if ("WGS84".equals(mapName)) else if ("WGS84".equals(mapName))
{ {
lat = Math.round(XmlPullUtil.floatAttr(pp, "y")); lat = Math.round(y);
lon = Math.round(XmlPullUtil.floatAttr(pp, "x")); lon = Math.round(x);
} }
else else
{ {
throw new IllegalStateException("unknown mapName: " + mapName); throw new IllegalStateException("unknown mapName=" + mapName + " x=" + x + " y=" + y);
} }
LocationType type; LocationType type;
@ -611,22 +614,25 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider
{ {
final int id = Integer.parseInt(pp.getAttributeValue(null, "stopID")); final int id = Integer.parseInt(pp.getAttributeValue(null, "stopID"));
final String mapName = pp.getAttributeValue(null, "mapName"); final String mapName = XmlPullUtil.optAttr(pp, "mapName", null);
final float x = XmlPullUtil.optFloatAttr(pp, "x", 0);
final float y = XmlPullUtil.optFloatAttr(pp, "y", 0);
final int lat; final int lat;
final int lon; final int lon;
if (mapName == null || mapName.length() == 0) if (mapName == null || (x == 0 && y == 0))
{ {
lat = 0; lat = 0;
lon = 0; lon = 0;
} }
else if ("WGS84".equals(mapName)) else if ("WGS84".equals(mapName))
{ {
lat = Math.round(XmlPullUtil.floatAttr(pp, "y")); lat = Math.round(y);
lon = Math.round(XmlPullUtil.floatAttr(pp, "x")); lon = Math.round(x);
} }
else else
{ {
throw new IllegalStateException("unknown mapName: " + mapName); throw new IllegalStateException("unknown mapName=" + mapName + " x=" + x + " y=" + y);
} }
final String place = normalizeLocationName(XmlPullUtil.attr(pp, "place")); final String place = normalizeLocationName(XmlPullUtil.attr(pp, "place"));
@ -701,21 +707,24 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider
XmlPullUtil.enter(pp, "itdOdvAssignedStops"); XmlPullUtil.enter(pp, "itdOdvAssignedStops");
while (XmlPullUtil.test(pp, "itdOdvAssignedStop")) while (XmlPullUtil.test(pp, "itdOdvAssignedStop"))
{ {
final String parsedMapName = pp.getAttributeValue(null, "mapName"); final String mapName = pp.getAttributeValue(null, "mapName");
if (parsedMapName != null && parsedMapName.length() != 0) final float x = XmlPullUtil.optFloatAttr(pp, "x", 0);
final float y = XmlPullUtil.optFloatAttr(pp, "y", 0);
if (!(mapName == null || mapName.length() == 0 || (x == 0 && y == 0)))
{ {
final int parsedLocationId = XmlPullUtil.intAttr(pp, "stopID"); final int parsedLocationId = XmlPullUtil.intAttr(pp, "stopID");
// final String parsedLongName = normalizeLocationName(XmlPullUtil.attr(pp, // final String parsedLongName = normalizeLocationName(XmlPullUtil.attr(pp,
// "nameWithPlace")); // "nameWithPlace"));
final String parsedPlace = normalizeLocationName(XmlPullUtil.attr(pp, "place")); final String parsedPlace = normalizeLocationName(XmlPullUtil.attr(pp, "place"));
final int parsedLon = Math.round(XmlPullUtil.floatAttr(pp, "x")); final int parsedLat = Math.round(y);
final int parsedLat = Math.round(XmlPullUtil.floatAttr(pp, "y")); final int parsedLon = Math.round(x);
XmlPullUtil.enter(pp, "itdOdvAssignedStop"); XmlPullUtil.enter(pp, "itdOdvAssignedStop");
final String parsedName = normalizeLocationName(pp.getText()); final String parsedName = normalizeLocationName(pp.getText());
XmlPullUtil.exit(pp, "itdOdvAssignedStop"); XmlPullUtil.exit(pp, "itdOdvAssignedStop");
if (!"WGS84".equals(parsedMapName)) if (!"WGS84".equals(mapName))
throw new IllegalStateException("unknown mapName: " + parsedMapName); throw new IllegalStateException("unknown mapName=" + mapName + " x=" + x + " y=" + y);
final Location newStation = new Location(LocationType.STATION, parsedLocationId, parsedLat, parsedLon, parsedPlace, final Location newStation = new Location(LocationType.STATION, parsedLocationId, parsedLat, parsedLon, parsedPlace,
parsedName); parsedName);
@ -1279,24 +1288,27 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider
StationDepartures assignedStationDepartures = findStationDepartures(result.stationDepartures, assignedStopId); StationDepartures assignedStationDepartures = findStationDepartures(result.stationDepartures, assignedStopId);
if (assignedStationDepartures == null) if (assignedStationDepartures == null)
{ {
final String mapName = pp.getAttributeValue(null, "mapName"); final String mapName = XmlPullUtil.optAttr(pp, "mapName", null);
final float x = XmlPullUtil.optFloatAttr(pp, "x", 0);
final float y = XmlPullUtil.optFloatAttr(pp, "y", 0);
final int lat; final int lat;
final int lon; final int lon;
if (mapName == null || (x == 0 && y == 0))
if (mapName == null || mapName.length() == 0)
{ {
lat = 0; lat = 0;
lon = 0; lon = 0;
} }
else if ("WGS84".equals(mapName)) else if ("WGS84".equals(mapName))
{ {
lat = Math.round(XmlPullUtil.floatAttr(pp, "y")); lat = Math.round(y);
lon = Math.round(XmlPullUtil.floatAttr(pp, "x")); lon = Math.round(x);
} }
else else
{ {
throw new IllegalStateException("unknown mapName: " + mapName); throw new IllegalStateException("unknown mapName=" + mapName + " x=" + x + " y=" + y);
} }
// final String name = normalizeLocationName(XmlPullUtil.attr(pp, "nameWO")); // final String name = normalizeLocationName(XmlPullUtil.attr(pp, "nameWO"));
assignedStationDepartures = new StationDepartures(new Location(LocationType.STATION, assignedStopId, lat, lon), assignedStationDepartures = new StationDepartures(new Location(LocationType.STATION, assignedStopId, lat, lon),
@ -1389,22 +1401,25 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider
if (name == null) if (name == null)
name = normalizeLocationName(pp.getAttributeValue(null, "name")); name = normalizeLocationName(pp.getAttributeValue(null, "name"));
final String mapName = pp.getAttributeValue(null, "mapName"); final String mapName = XmlPullUtil.optAttr(pp, "mapName", null);
final float x = XmlPullUtil.optFloatAttr(pp, "x", 0);
final float y = XmlPullUtil.optFloatAttr(pp, "y", 0);
final int lat; final int lat;
final int lon; final int lon;
if (mapName == null || mapName.length() == 0) if (mapName == null || (x == 0 && y == 0))
{ {
lat = 0; lat = 0;
lon = 0; lon = 0;
} }
else if ("WGS84".equals(mapName)) else if ("WGS84".equals(mapName))
{ {
lat = Math.round(XmlPullUtil.floatAttr(pp, "y")); lat = Math.round(y);
lon = Math.round(XmlPullUtil.floatAttr(pp, "x")); lon = Math.round(x);
} }
else else
{ {
throw new IllegalStateException("unknown mapName: " + mapName); throw new IllegalStateException("unknown mapName=" + mapName + " x=" + x + " y=" + y);
} }
return new Location(LocationType.STATION, id, lat, lon, place, name); return new Location(LocationType.STATION, id, lat, lon, place, name);

View file

@ -102,6 +102,21 @@ public final class XmlPullUtil
return pp.getAttributeValue(null, attrName).trim(); return pp.getAttributeValue(null, attrName).trim();
} }
public static String optAttr(final XmlPullParser pp, final String attrName, final String defaultValue)
{
final String attr = pp.getAttributeValue(null, attrName);
if (attr != null)
{
final String trimmedAttr = attr.trim();
if (trimmedAttr.length() > 0)
return trimmedAttr;
}
return defaultValue;
}
public static int intAttr(final XmlPullParser pp, final String attrName) public static int intAttr(final XmlPullParser pp, final String attrName)
{ {
return Integer.parseInt(pp.getAttributeValue(null, attrName).trim()); return Integer.parseInt(pp.getAttributeValue(null, attrName).trim());
@ -109,9 +124,9 @@ public final class XmlPullUtil
public static int optIntAttr(final XmlPullParser pp, final String attrName, final int defaultValue) public static int optIntAttr(final XmlPullParser pp, final String attrName, final int defaultValue)
{ {
final String attr = pp.getAttributeValue(null, attrName); final String attr = optAttr(pp, attrName, null);
if (attr != null) if (attr != null)
return Integer.parseInt(attr.trim()); return Integer.parseInt(attr);
else else
return defaultValue; return defaultValue;
} }
@ -121,6 +136,15 @@ public final class XmlPullUtil
return Float.parseFloat(pp.getAttributeValue(null, attrName).trim()); return Float.parseFloat(pp.getAttributeValue(null, attrName).trim());
} }
public static float optFloatAttr(final XmlPullParser pp, final String attrName, final float defaultValue)
{
final String attr = optAttr(pp, attrName, null);
if (attr != null)
return Float.parseFloat(attr);
else
return defaultValue;
}
public static void requireAttr(final XmlPullParser pp, final String attrName, final String requiredValue) public static void requireAttr(final XmlPullParser pp, final String attrName, final String requiredValue)
{ {
if (!requiredValue.equals(attr(pp, attrName))) if (!requiredValue.equals(attr(pp, attrName)))