import tim.prune.data.Field;
import tim.prune.data.Latitude;
import tim.prune.data.Longitude;
-import tim.prune.data.Timestamp;
+import tim.prune.data.TimestampUtc;
/**
* Class to try to match data with field names,
*/
private static boolean isHeaderRow(String[] inValues)
{
- // Loop over values looking for a Latitude value
+ // Loop over values seeing if any are mostly numeric
if (inValues != null)
{
- for (int v=0; v<inValues.length; v++)
+ for (String value : inValues)
{
- Latitude lat = new Latitude(inValues[v]);
- if (lat.isValid()) {return false;}
+ if (fieldLooksNumeric(value)) {return false;}
}
}
- // No valid Latitude value found so presume header
+ // No (mostly) numeric values found so presume header
return true;
}
+ /**
+ * See if a field looks numeric or not by comparing the number of numeric vs non-numeric characters
+ * @param inValue field value to check
+ * @return true if there are more numeric characters than not
+ */
+ private static boolean fieldLooksNumeric(String inValue)
+ {
+ if (inValue == null) {
+ return false;
+ }
+ final int numChars = inValue.length();
+ if (numChars < 3) {return false;} // Don't care about one or two character values
+ // Loop through characters seeing which ones are numeric and which not
+ int numNums = 0;
+ for (int i=0; i<numChars; i++)
+ {
+ char currChar = inValue.charAt(i);
+ if (currChar >= '0' && currChar <= '9') {numNums++;}
+ }
+ // Return true if more than half the characters are numeric
+ return numNums > (numChars/2);
+ }
+
/**
* Try to guess the fields for the given values from the file
* @param inValues array of values from first non-blank line of file
fields[f] = Field.TIMESTAMP;
continue;
}
+ // check for tracksegment
+ if (!checkArrayHasField(fields, Field.NEW_SEGMENT) && fieldLooksLikeSegment(value, isHeader))
+ {
+ fields[f] = Field.NEW_SEGMENT;
+ continue;
+ }
+ // check for waypoint type
+ if (!checkArrayHasField(fields, Field.WAYPT_TYPE) && fieldLooksLikeWaypointType(value, isHeader))
+ {
+ fields[f] = Field.WAYPT_TYPE;
+ continue;
+ }
}
}
// Fill in the rest of the fields using just custom fields
else if (!checkArrayHasField(fields, Field.LONGITUDE)) {
fields[f] = Field.LONGITUDE;
}
- else {
- customFieldNum++;
- fields[f] = new Field(customPrefix + (customFieldNum));
+ else
+ {
+ // Can we use the field name given?
+ Field customField = null;
+ if (isHeader && inValues[f] != null && inValues[f].length() > 0) {
+ customField = new Field(inValues[f]);
+ }
+ // Find an unused field number
+ while (customField == null || checkArrayHasField(fields, customField))
+ {
+ customFieldNum++;
+ customField = new Field(customPrefix + (customFieldNum));
+ }
+ fields[f] = customField;
}
}
}
+
// Do a final check to make sure lat and long are in there
if (!checkArrayHasField(fields, Field.LATITUDE)) {
fields[0] = Field.LATITUDE;
else if (!checkArrayHasField(fields, Field.LONGITUDE)) {
fields[1] = Field.LONGITUDE;
}
+ // Longitude _could_ have overwritten latitude in position 1
+ if (!checkArrayHasField(fields, Field.LATITUDE)) {
+ fields[0] = Field.LATITUDE;
+ }
return fields;
}
String upperValue = inValue.toUpperCase();
return (upperValue.equals("ALTITUDE")
|| upperValue.equals("ALT")
+ || upperValue.equals("HMSL") // height above mean sea level
|| upperValue.equals(I18nManager.getText("fieldname.altitude").toUpperCase()));
}
else
{
// must be at least 7 characters long
if (inValue.length() < 7) {return false;}
- Timestamp stamp = new Timestamp(inValue);
+ TimestampUtc stamp = new TimestampUtc(inValue);
return stamp.isValid();
}
}
+
+ /**
+ * Check whether the given String looks like a track segment field
+ * @param inValue value from file
+ * @param inIsHeader true if this is a header line, false for data
+ * @return true if it could be a track segment
+ */
+ private static boolean fieldLooksLikeSegment(String inValue, boolean inIsHeader)
+ {
+ if (inValue == null || inValue.equals("")) {return false;}
+ if (inIsHeader)
+ {
+ String upperValue = inValue.toUpperCase();
+ // This is a header line so look for english or local text
+ return upperValue.equals("SEGMENT")
+ || upperValue.equals(I18nManager.getText("fieldname.newsegment").toUpperCase());
+ }
+ else
+ {
+ // can't reliably identify it just using the value
+ return false;
+ }
+ }
+
+ /**
+ * Check whether the given String looks like a waypoint type
+ * @param inValue value from file
+ * @param inIsHeader true if this is a header line, false for data
+ * @return true if it could be a waypoint type
+ */
+ private static boolean fieldLooksLikeWaypointType(String inValue, boolean inIsHeader)
+ {
+ if (inValue == null || inValue.equals("")) {return false;}
+ if (inIsHeader)
+ {
+ String upperValue = inValue.toUpperCase();
+ // This is a header line so look for english or local text
+ return (upperValue.equals("TYPE")
+ || upperValue.equals(I18nManager.getText("fieldname.waypointtype").toUpperCase()));
+ }
+ else
+ {
+ // can't reliably identify it just using the value
+ return false;
+ }
+ }
}