Commit 92734015 authored by Matija Obreza's avatar Matija Obreza
Browse files

Merge branch 'suggest-coordinates-for-country' into 'master'

Suggest coordinates that fit ORIGCTY

See merge request genesys-pgr/validator!14
parents c6188c3c c36a482d
...@@ -29,6 +29,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -29,6 +29,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.opencsv.CSVReader; import com.opencsv.CSVReader;
import com.vividsolutions.jts.geom.Coordinate;
/** /**
* @author Andrey Lugovskoy. * @author Andrey Lugovskoy.
...@@ -82,15 +83,27 @@ public class CountryProcessServiceImpl implements ProcessService { ...@@ -82,15 +83,27 @@ public class CountryProcessServiceImpl implements ProcessService {
outputHeaders.add(pos + 1, ApplicationUtils.HEADER_ORIGCTY_DISTANCE); outputHeaders.add(pos + 1, ApplicationUtils.HEADER_ORIGCTY_DISTANCE);
outputMapping.add(pos + 1, null); outputMapping.add(pos + 1, null);
} }
// Add extra header HEADER_DECLATITUDE_CHECK // Add extra header HEADER_DECLATITUDE_PARSED
{ {
final int pos = outputHeaders.indexOf(ApplicationUtils.HEADER_LATITUDE); final int pos = outputHeaders.indexOf(ApplicationUtils.HEADER_LATITUDE);
outputHeaders.add(pos + 1, ApplicationUtils.HEADER_DECLATITUDE_PARSED);
outputMapping.add(pos + 1, null);
}
// Add extra header HEADER_DECLATITUDE_CHECK
{
final int pos = outputHeaders.indexOf(ApplicationUtils.HEADER_DECLATITUDE_PARSED);
outputHeaders.add(pos + 1, ApplicationUtils.HEADER_DECLATITUDE_CHECK); outputHeaders.add(pos + 1, ApplicationUtils.HEADER_DECLATITUDE_CHECK);
outputMapping.add(pos + 1, null); outputMapping.add(pos + 1, null);
} }
// Add extra header HEADER_DECLONGITUDE_CHECK // Add extra header HEADER_DECLONGITUDE_PARSED
{ {
final int pos = outputHeaders.indexOf(ApplicationUtils.HEADER_LONGITUDE); final int pos = outputHeaders.indexOf(ApplicationUtils.HEADER_LONGITUDE);
outputHeaders.add(pos + 1, ApplicationUtils.HEADER_DECLONGITUDE_PARSED);
outputMapping.add(pos + 1, null);
}
// Add extra header HEADER_DECLONGITUDE_CHECK
{
final int pos = outputHeaders.indexOf(ApplicationUtils.HEADER_DECLONGITUDE_PARSED);
outputHeaders.add(pos + 1, ApplicationUtils.HEADER_DECLONGITUDE_CHECK); outputHeaders.add(pos + 1, ApplicationUtils.HEADER_DECLONGITUDE_CHECK);
outputMapping.add(pos + 1, null); outputMapping.add(pos + 1, null);
} }
...@@ -158,6 +171,8 @@ public class CountryProcessServiceImpl implements ProcessService { ...@@ -158,6 +171,8 @@ public class CountryProcessServiceImpl implements ProcessService {
String result = null; String result = null;
String distance = null; String distance = null;
String newlatitude = null;
String newlongitude = null;
// if ("101346".equals(nextLine[0])) { // if ("101346".equals(nextLine[0])) {
// LOG.info("!!! Got what we're looking for"); // LOG.info("!!! Got what we're looking for");
// } // }
...@@ -180,15 +195,24 @@ public class CountryProcessServiceImpl implements ProcessService { ...@@ -180,15 +195,24 @@ public class CountryProcessServiceImpl implements ProcessService {
if (origCty != null && !result.equals(origCty)) { if (origCty != null && !result.equals(origCty)) {
try { try {
distance = "" + (long) countryOfOriginService.distanceToBorder(longitude, latitude, origCty); double distanceToBorder = countryOfOriginService.distanceToBorder(longitude, latitude, origCty);
distance = "" + (long) distanceToBorder;
} catch (final Throwable e) { } catch (final Throwable e) {
distance = "ERROR: " + e.getMessage(); distance = "ERROR: " + e.getMessage();
} }
Coordinate fixed = fixCoordinates(longitude, latitude, origCty, true);
if (fixed != null) {
newlatitude = decimalFormat.format(fixed.y);
newlongitude = decimalFormat.format(fixed.x);
}
} }
try { try {
outputLine[outputHeaders.indexOf(ApplicationUtils.HEADER_DECLATITUDE_CHECK)] = decimalFormat.format(latitude); outputLine[outputHeaders.indexOf(ApplicationUtils.HEADER_DECLATITUDE_PARSED)] = decimalFormat.format(latitude);
outputLine[outputHeaders.indexOf(ApplicationUtils.HEADER_DECLONGITUDE_CHECK)] = decimalFormat.format(longitude); outputLine[outputHeaders.indexOf(ApplicationUtils.HEADER_DECLONGITUDE_PARSED)] = decimalFormat.format(longitude);
} catch (final Throwable e) { } catch (final Throwable e) {
LOG.error(e.getMessage(), e); LOG.error(e.getMessage(), e);
} }
...@@ -196,13 +220,18 @@ public class CountryProcessServiceImpl implements ProcessService { ...@@ -196,13 +220,18 @@ public class CountryProcessServiceImpl implements ProcessService {
LOG.error("Unparsable lat/lon from ({}, {})", declongitude, declatitude, e); LOG.error("Unparsable lat/lon from ({}, {})", declongitude, declatitude, e);
result = "Unparseable lat/lon: " + e.getMessage(); result = "Unparseable lat/lon: " + e.getMessage();
} catch (final Throwable e) { } catch (final Throwable e) {
LOG.error("Error parsing lat/lon: {}", e.getMessage()); LOG.error("Error parsing lat/lon: {}", e.getMessage(), e);
result = "ERROR: " + e.getMessage(); result = "ERROR: " + e.getMessage();
} }
} }
outputLine[outputHeaders.indexOf(ApplicationUtils.HEADER_ORIGCTY_CHECK)] = StringUtils.equals(origCty, result) ? ApplicationUtils.CHECK_PASSED : result; outputLine[outputHeaders.indexOf(ApplicationUtils.HEADER_ORIGCTY_CHECK)] = StringUtils.equals(origCty, result) ? ApplicationUtils.CHECK_PASSED : result;
outputLine[outputHeaders.indexOf(ApplicationUtils.HEADER_ORIGCTY_DISTANCE)] = distance; outputLine[outputHeaders.indexOf(ApplicationUtils.HEADER_ORIGCTY_DISTANCE)] = distance;
outputLine[outputHeaders.indexOf(ApplicationUtils.HEADER_DECLATITUDE_CHECK)] = newlatitude;
outputLine[outputHeaders.indexOf(ApplicationUtils.HEADER_DECLONGITUDE_CHECK)] = newlongitude;
lines.add(outputLine); lines.add(outputLine);
// if ("101346".equals(nextLine[0])) { // if ("101346".equals(nextLine[0])) {
// LOG.info("Written what we're looking for"); // LOG.info("Written what we're looking for");
...@@ -238,4 +267,64 @@ public class CountryProcessServiceImpl implements ProcessService { ...@@ -238,4 +267,64 @@ public class CountryProcessServiceImpl implements ProcessService {
// } // }
return lines; return lines;
} }
/**
* Check if coordinates fall into the country or try the common fixes (swap,
* flip) to make values fit.
*
* @param longitude Original longitude
* @param latitude Original latitude
* @param origCty Country code (required)
* @param swap Try swapping?
* @return
*/
protected Coordinate fixCoordinates(float longitude, float latitude, final String origCty, boolean swap) {
if (origCty == null) {
return null;
}
try {
// Check originals
float lon = longitude;
float lat = latitude;
String result = countryOfOriginService.getCountries(lon, lat, origCty, ApplicationUtils.ALLOWED_DISTANCE_MARGIN);
if (StringUtils.equals(result, origCty)) {
return new Coordinate(lon, lat);
}
// Flip only longitude
lon = -longitude;
lat = latitude;
result = countryOfOriginService.getCountries(lon, lat, origCty, ApplicationUtils.ALLOWED_DISTANCE_MARGIN);
if (StringUtils.equals(result, origCty)) {
return new Coordinate(lon, lat);
}
// Flip latitude & longitude
lon = -longitude;
lat = -latitude;
result = countryOfOriginService.getCountries(lon, lat, origCty, ApplicationUtils.ALLOWED_DISTANCE_MARGIN);
if (StringUtils.equals(result, origCty)) {
return new Coordinate(lon, lat);
}
// Flip only latitude
lon = longitude;
lat = -latitude;
result = countryOfOriginService.getCountries(lon, lat, origCty, ApplicationUtils.ALLOWED_DISTANCE_MARGIN);
if (StringUtils.equals(result, origCty)) {
return new Coordinate(lon, lat);
}
// Swap
if (swap) {
return fixCoordinates(latitude, longitude, origCty, false);
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
return null;
}
} }
...@@ -73,21 +73,21 @@ public class LandOrSeaProcessServiceImpl implements ProcessService { ...@@ -73,21 +73,21 @@ public class LandOrSeaProcessServiceImpl implements ProcessService {
final Map<String, Integer> sourceMapping = HeaderUtils.makeSourceMapping(headers, new String[] { ApplicationUtils.HEADER_LONGITUDE, ApplicationUtils.HEADER_LATITUDE }); final Map<String, Integer> sourceMapping = HeaderUtils.makeSourceMapping(headers, new String[] { ApplicationUtils.HEADER_LONGITUDE, ApplicationUtils.HEADER_LATITUDE });
// Add extra header HEADER_DECLATITUDE_CHECK // Add extra header HEADER_DECLATITUDE_PARSED
{ {
final int pos = outputHeaders.indexOf(ApplicationUtils.HEADER_LATITUDE); final int pos = outputHeaders.indexOf(ApplicationUtils.HEADER_LATITUDE);
outputHeaders.add(pos + 1, ApplicationUtils.HEADER_DECLATITUDE_CHECK); outputHeaders.add(pos + 1, ApplicationUtils.HEADER_DECLATITUDE_PARSED);
outputMapping.add(pos + 1, null); outputMapping.add(pos + 1, null);
} }
// Add extra header HEADER_DECLONGITUDE_CHECK // Add extra header HEADER_DECLONGITUDE_PARSED
{ {
final int pos = outputHeaders.indexOf(ApplicationUtils.HEADER_LONGITUDE); final int pos = outputHeaders.indexOf(ApplicationUtils.HEADER_LONGITUDE);
outputHeaders.add(pos + 1, ApplicationUtils.HEADER_DECLONGITUDE_CHECK); outputHeaders.add(pos + 1, ApplicationUtils.HEADER_DECLONGITUDE_PARSED);
outputMapping.add(pos + 1, null); outputMapping.add(pos + 1, null);
} }
// Add extra header HEADER_LANDORSEA_CHECK // Add extra header HEADER_LANDORSEA_CHECK
{ {
final int pos = Math.max(outputHeaders.indexOf(ApplicationUtils.HEADER_DECLONGITUDE_CHECK), outputHeaders.indexOf(ApplicationUtils.HEADER_DECLATITUDE_CHECK)); final int pos = Math.max(outputHeaders.indexOf(ApplicationUtils.HEADER_DECLONGITUDE_PARSED), outputHeaders.indexOf(ApplicationUtils.HEADER_DECLATITUDE_PARSED));
outputHeaders.add(pos + 1, ApplicationUtils.HEADER_LANDORSEA_CHECK); outputHeaders.add(pos + 1, ApplicationUtils.HEADER_LANDORSEA_CHECK);
outputMapping.add(pos + 1, null); outputMapping.add(pos + 1, null);
} }
...@@ -163,8 +163,8 @@ public class LandOrSeaProcessServiceImpl implements ProcessService { ...@@ -163,8 +163,8 @@ public class LandOrSeaProcessServiceImpl implements ProcessService {
result = landOrSeaService.classifyLocation(longitude, latitude, ApplicationUtils.ALLOWED_DISTANCE_MARGIN); result = landOrSeaService.classifyLocation(longitude, latitude, ApplicationUtils.ALLOWED_DISTANCE_MARGIN);
try { try {
outputLine[outputHeaders.indexOf(ApplicationUtils.HEADER_DECLATITUDE_CHECK)] = decimalFormat.format(latitude); outputLine[outputHeaders.indexOf(ApplicationUtils.HEADER_DECLATITUDE_PARSED)] = decimalFormat.format(latitude);
outputLine[outputHeaders.indexOf(ApplicationUtils.HEADER_DECLONGITUDE_CHECK)] = decimalFormat.format(longitude); outputLine[outputHeaders.indexOf(ApplicationUtils.HEADER_DECLONGITUDE_PARSED)] = decimalFormat.format(longitude);
} catch (final Throwable e) { } catch (final Throwable e) {
LOG.error(e.getMessage(), e); LOG.error(e.getMessage(), e);
} }
......
...@@ -82,8 +82,10 @@ public class ApplicationUtils { ...@@ -82,8 +82,10 @@ public class ApplicationUtils {
/** The Constant HEADER_LANDORSEA_CHECK. */ /** The Constant HEADER_LANDORSEA_CHECK. */
public static final String HEADER_LANDORSEA_CHECK = "LANDorSEA_check"; public static final String HEADER_LANDORSEA_CHECK = "LANDorSEA_check";
public static final String HEADER_DECLATITUDE_CHECK = "DECLATITUDE_parsed"; public static final String HEADER_DECLATITUDE_PARSED = "DECLATITUDE_parsed";
public static final String HEADER_DECLATITUDE_CHECK = "DECLATITUDE_check";
public static final String HEADER_DECLONGITUDE_CHECK = "DECLONGITUDE_parsed"; public static final String HEADER_DECLONGITUDE_PARSED = "DECLONGITUDE_parsed";
public static final String HEADER_DECLONGITUDE_CHECK = "DECLONGITUDE_check";
} }
...@@ -46,6 +46,8 @@ ...@@ -46,6 +46,8 @@
.x-ORIGCTY_distance, .x-ORIGCTY_distance,
.x-DECLATITUDE_parsed, .x-DECLATITUDE_parsed,
.x-DECLONGITUDE_parsed, .x-DECLONGITUDE_parsed,
.x-DECLATITUDE_check,
.x-DECLONGITUDE_check,
.x-LANDorSEA_check { .x-LANDorSEA_check {
background-color: #ffffca; background-color: #ffffca;
} }
...@@ -60,6 +62,8 @@ ...@@ -60,6 +62,8 @@
thead .x-ORIGCTY_distance, thead .x-ORIGCTY_distance,
thead .x-DECLATITUDE_parsed, thead .x-DECLATITUDE_parsed,
thead .x-DECLONGITUDE_parsed, thead .x-DECLONGITUDE_parsed,
thead .x-DECLATITUDE_check,
thead .x-DECLONGITUDE_check,
thead .x-LANDorSEA_check { thead .x-LANDorSEA_check {
background-color: Yellow; background-color: Yellow;
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment