1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
package tim.prune.load;
import tim.prune.I18nManager;
import tim.prune.data.Field;
import tim.prune.data.FieldCustom;
import tim.prune.data.Latitude;
import tim.prune.data.Longitude;
import tim.prune.data.TimestampUtc;
/**
* Class to try to match data with field names,
* using a variety of guessing techniques
*/
public abstract class FieldGuesser
{
/**
* Try to guess whether the given line is a header line or data
* @param inValues array of values from first non-blank line of file
* @return true if it looks like a header row, false if it looks like data
*/
private static boolean isHeaderRow(String[] inValues)
{
// Loop over values seeing if any are mostly numeric
if (inValues != null)
{
for (String value : inValues)
{
if (fieldLooksNumeric(value)) {return false;}
}
}
// 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
* @return array of fields which hopefully match
*/
public static Field[] guessFields(String[] inValues)
{
// Guess whether it's a header line or not
boolean isHeader = isHeaderRow(inValues);
// make array of Fields
int numFields = inValues.length;
Field[] fields = new Field[numFields];
// Loop over fields to try to guess the main ones
for (int f=0; f<numFields; f++)
{
if (inValues[f] != null) {
String value = inValues[f].trim();
// check for latitude
if (!checkArrayHasField(fields, Field.LATITUDE) && fieldLooksLikeLatitude(value, isHeader))
{
fields[f] = Field.LATITUDE;
}
// check for longitude
else if (!checkArrayHasField(fields, Field.LONGITUDE) && fieldLooksLikeLongitude(value, isHeader))
{
fields[f] = Field.LONGITUDE;
}
// check for altitude
else if (!checkArrayHasField(fields, Field.ALTITUDE) && fieldLooksLikeAltitude(value, isHeader))
{
fields[f] = Field.ALTITUDE;
}
// check for waypoint name
else if (!checkArrayHasField(fields, Field.WAYPT_NAME) && fieldLooksLikeName(value, isHeader))
{
fields[f] = Field.WAYPT_NAME;
}
// check for timestamp
else if (!checkArrayHasField(fields, Field.TIMESTAMP) && fieldLooksLikeTimestamp(value, isHeader))
{
fields[f] = Field.TIMESTAMP;
}
// check for tracksegment
else if (!checkArrayHasField(fields, Field.NEW_SEGMENT) && fieldLooksLikeSegment(value, isHeader))
{
fields[f] = Field.NEW_SEGMENT;
}
// check for waypoint type
else if (!checkArrayHasField(fields, Field.WAYPT_TYPE) && fieldLooksLikeWaypointType(value, isHeader))
{
fields[f] = Field.WAYPT_TYPE;
}
}
}
// Fill in the rest of the fields using just custom fields
// Could try to guess other fields (waypoint type, segment) or unguessed altitude, name, but keep simple for now
String customPrefix = I18nManager.getText("fieldname.prefix") + " ";
int customFieldNum = 0;
for (int f=0; f<numFields; f++) {
if (fields[f] == null)
{
// Make sure lat and long are filled in if not already
if (!checkArrayHasField(fields, Field.LATITUDE)) {
fields[f] = Field.LATITUDE;
}
else if (!checkArrayHasField(fields, Field.LONGITUDE)) {
fields[f] = Field.LONGITUDE;
}
else
{
// Can we use the field name given?
Field customField = null;
if (isHeader && inValues[f] != null && inValues[f].length() > 0) {
customField = new FieldCustom(inValues[f]);
}
// Find an unused field number
while (customField == null || checkArrayHasField(fields, customField))
{
customFieldNum++;
customField = new FieldCustom(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;
}
/**
* Check whether the given field array has the specified field
* @param inFields field array to look through
* @param inCheckField field to look for
* @return true if Field is contained within the array
*/
private static boolean checkArrayHasField(Field[] inFields, Field inCheckField)
{
for (Field field : inFields)
{
if (field != null && field.equals(inCheckField)) {
return true;
}
}
// not found
return false;
}
/**
* Check whether the given String looks like a Latitude value
* @param inValue value from file
* @param inIsHeader true if this is a header line, false for data
* @return true if it could be latitude
*/
public static boolean fieldLooksLikeLatitude(String inValue, boolean inIsHeader)
{
if (inValue == null || inValue.equals("")) {return false;}
if (inIsHeader)
{
// This is a header line so look for english or local text
String upperValue = inValue.toUpperCase();
return (upperValue.equals("LATITUDE")
|| upperValue.equals(I18nManager.getText("fieldname.latitude").toUpperCase()));
}
else
{
// Note this will also catch longitudes too if they're within range
return couldBeCoordinateString(inValue) && Latitude.make(inValue) != null;
}
}
/**
* Check whether the given String looks like a Longitude value
* @param inValue value from file
* @param inIsHeader true if this is a header line, false for data
* @return true if it could be longitude
*/
public static boolean fieldLooksLikeLongitude(String inValue, boolean inIsHeader)
{
if (inValue == null || inValue.equals("")) {return false;}
if (inIsHeader)
{
// This is a header line so look for english or local text
String upperValue = inValue.toUpperCase();
return (upperValue.equals("LONGITUDE")
|| upperValue.equals(I18nManager.getText("fieldname.longitude").toUpperCase()));
}
else
{
// Note this will also catch latitudes too
return couldBeCoordinateString(inValue) && Longitude.make(inValue) != null;
}
}
/**
* @return true if this string could represent a lat/long coordinate
*/
public static boolean couldBeCoordinateString(String inValue)
{
final String value = inValue == null ? "" : inValue.trim().toUpperCase();
if (value.length() < 2) {
return false;
}
int totalLetters = 0, totalNumbers = 0;
boolean prevCharWasLetter = false;
for (int i=0; i<value.length(); i++) {
char c = value.charAt(i);
if (Character.isAlphabetic(c)) {
if (prevCharWasLetter) {return false;} // no consecutive letters allowed
totalLetters++;
prevCharWasLetter = true;
}
else {
prevCharWasLetter = false;
}
if (Character.isDigit(c)) {
totalNumbers++;
}
}
return totalLetters < 3 && totalNumbers > 1;
}
/**
* Check whether the given String looks like an Altitude value
* @param inValue value from file
* @param inIsHeader true if this is a header line, false for data
* @return true if it could be altitude
*/
private static boolean fieldLooksLikeAltitude(String inValue, boolean inIsHeader)
{
if (inValue == null || inValue.equals("")) {return false;}
if (inIsHeader)
{
// This is a header line so look for english or local text
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
{
// Look for a number less than 100000
try
{
int intValue = Integer.parseInt(inValue);
return (intValue > 0 && intValue < 100000);
}
catch (NumberFormatException nfe) {}
return false;
}
}
/**
* Check whether the given String looks like a waypoint name
* @param inValue value from file
* @param inIsHeader true if this is a header line, false for data
* @return true if it could be a name
*/
private static boolean fieldLooksLikeName(String inValue, boolean inIsHeader)
{
if (inValue == null || inValue.equals("")) {return false;}
if (inIsHeader)
{
// This is a header line so look for english or local text
String upperValue = inValue.toUpperCase();
return (upperValue.equals("NAME")
|| upperValue.equals("LABEL")
|| upperValue.equals(I18nManager.getText("fieldname.waypointname").toUpperCase()));
}
else
{
// Look for at least two letters in it
int numLetters = 0;
for (int i=0; i<inValue.length(); i++)
{
char currChar = inValue.charAt(i);
if (Character.isLetter(currChar)) {
numLetters++;
}
// Not interested if it contains ":" or "."
if (currChar == ':' || currChar == '.') {return false;}
}
return numLetters >= 2;
}
}
/**
* Check whether the given String looks like a timestamp
* @param inValue value from file
* @param inIsHeader true if this is a header line, false for data
* @return true if it could be a timestamp
*/
private static boolean fieldLooksLikeTimestamp(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("TIMESTAMP")
|| upperValue.equals("TIME")
|| upperValue.equals(I18nManager.getText("fieldname.timestamp").toUpperCase()));
}
else
{
// must be at least 7 characters long
if (inValue.length() < 7) {return false;}
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;
}
}
}
|