Skip to content

Commit

Permalink
Cleaning up handling of communication problems in some methods that q…
Browse files Browse the repository at this point in the history
…uery Getty TGN, throwing source authority exceptions, more cleanup of handling source authorities with exceptions. Adding a stub for tdwg/bdq#287.
  • Loading branch information
chicoreus committed Aug 27, 2024
1 parent fc785f7 commit efb9018
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 81 deletions.
189 changes: 125 additions & 64 deletions src/main/java/org/filteredpush/qc/georeference/DwCGeoRefDQ.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,63 +185,70 @@ public static DQResponse<ComplianceValue> validationCountryFound(
sourceAuthority = GettyLookup.GETTY_TGN;
}

if (GEOUtil.isEmpty(country)) {
result.addComment("dwc:country is empty");
result.setResultState(ResultState.INTERNAL_PREREQUISITES_NOT_MET);
} else {
if (sourceAuthority.equalsIgnoreCase(GettyLookup.GETTY_TGN)) {
Boolean cached = GeoUtilSingleton.getInstance().getTgnCountriesEntry(country);
if (cached!=null) {
if (cached) {
result.addComment("the value provided for dwc:country [" + country + "] exists as a nation in the Getty Thesaurus of Geographic Names (TGN).");
try {
GeoRefSourceAuthority foo = new GeoRefSourceAuthority(sourceAuthority);
if (GEOUtil.isEmpty(country)) {
result.addComment("dwc:country is empty");
result.setResultState(ResultState.INTERNAL_PREREQUISITES_NOT_MET);
} else {
if (sourceAuthority.equalsIgnoreCase(GettyLookup.GETTY_TGN)) {
Boolean cached = GeoUtilSingleton.getInstance().getTgnCountriesEntry(country);
if (cached!=null) {
if (cached) {
result.addComment("the value provided for dwc:country [" + country + "] exists as a nation in the Getty Thesaurus of Geographic Names (TGN).");
result.setResultState(ResultState.RUN_HAS_RESULT);
result.setValue(ComplianceValue.COMPLIANT);
} else {
result.addComment("the value provided for dwc:country [" + country + "] is not a nation in the Getty Thesaurus of Geographic Names (TGN).");
result.setResultState(ResultState.RUN_HAS_RESULT);
result.setValue(ComplianceValue.NOT_COMPLIANT);
}
} else {
GettyLookup lookup = new GettyLookup();
if (lookup.lookupCountry(country)==null) {
result.addComment("Error looking up country in " + sourceAuthority);
result.setResultState(ResultState.EXTERNAL_PREREQUISITES_NOT_MET);
} else if (lookup.lookupCountry(country)) {
result.addComment("the value provided for dwc:country [" + country + "] exists as a nation in the Getty Thesaurus of Geographic Names (TGN).");
result.setResultState(ResultState.RUN_HAS_RESULT);
result.setValue(ComplianceValue.COMPLIANT);
GeoUtilSingleton.getInstance().addTgnCountry(country, true);
} else {
result.addComment("the value provided for dwc:country [" + country + "] is not a nation in the Getty Thesaurus of Geographic Names (TGN).");
result.setResultState(ResultState.RUN_HAS_RESULT);
result.setValue(ComplianceValue.NOT_COMPLIANT);
GeoUtilSingleton.getInstance().addTgnCountry(country, false);
}
}
} else if (sourceAuthority.equalsIgnoreCase("NaturalEarth")) {
// ne_10m_admin_0_countries
if (GEOUtil.isCountryKnown(country)) {
result.addComment("the value provided for dwc:country [" + country + "] exists as a country name in the NaturalEarth admin regions.");
result.setResultState(ResultState.RUN_HAS_RESULT);
result.setValue(ComplianceValue.COMPLIANT);
} else {
result.addComment("the value provided for dwc:country [" + country + "] is not a nation in the Getty Thesaurus of Geographic Names (TGN).");
result.addComment("the value provided for dwc:country [" + country + "] is not a country name in the NaturalEarth admin regions.");
result.setResultState(ResultState.RUN_HAS_RESULT);
result.setValue(ComplianceValue.NOT_COMPLIANT);
}
} else {
GettyLookup lookup = new GettyLookup();
if (lookup.lookupCountry(country)==null) {
result.addComment("Error looking up country in " + sourceAuthority);
result.setResultState(ResultState.EXTERNAL_PREREQUISITES_NOT_MET);
} else if (lookup.lookupCountry(country)) {
result.addComment("the value provided for dwc:country [" + country + "] exists as a nation in the Getty Thesaurus of Geographic Names (TGN).");
} else if (sourceAuthority.equalsIgnoreCase("datahub.io")) {
if (CountryLookup.countryExistsHasCode(country)) {
result.addComment("the value provided for dwc:country [" + country + "] exists as a country name in the datahub.io list of countries.");
result.setResultState(ResultState.RUN_HAS_RESULT);
result.setValue(ComplianceValue.COMPLIANT);
GeoUtilSingleton.getInstance().addTgnCountry(country, true);
} else {
result.addComment("the value provided for dwc:country [" + country + "] is not a nation in the Getty Thesaurus of Geographic Names (TGN).");
result.addComment("the value provided for dwc:country [" + country + "] is not a country name in the datahub.io list of countries.");
result.setResultState(ResultState.RUN_HAS_RESULT);
result.setValue(ComplianceValue.NOT_COMPLIANT);
GeoUtilSingleton.getInstance().addTgnCountry(country, false);
}
}
} else if (sourceAuthority.equalsIgnoreCase("NaturalEarth")) {
if (GEOUtil.isCountryKnown(country)) {
result.addComment("the value provided for dwc:country [" + country + "] exists as a country name in the NaturalEarth admin regions.");
result.setResultState(ResultState.RUN_HAS_RESULT);
result.setValue(ComplianceValue.COMPLIANT);
} else {
result.addComment("the value provided for dwc:country [" + country + "] is not a country name in the NaturalEarth admin regions.");
result.setResultState(ResultState.RUN_HAS_RESULT);
result.setValue(ComplianceValue.NOT_COMPLIANT);
}
} else if (sourceAuthority.equalsIgnoreCase("datahub.io")) {
if (CountryLookup.countryExistsHasCode(country)) {
result.addComment("the value provided for dwc:country [" + country + "] exists as a country name in the datahub.io list of countries.");
result.setResultState(ResultState.RUN_HAS_RESULT);
result.setValue(ComplianceValue.COMPLIANT);
} else {
result.addComment("the value provided for dwc:country [" + country + "] is not a country name in the datahub.io list of countries.");
result.setResultState(ResultState.RUN_HAS_RESULT);
result.setValue(ComplianceValue.NOT_COMPLIANT);
result.addComment("Unknown bdq:sourceAuthority");
result.setResultState(ResultState.EXTERNAL_PREREQUISITES_NOT_MET);
}
} else {
result.addComment("Unknown bdq:sourceAuthority");
result.setResultState(ResultState.EXTERNAL_PREREQUISITES_NOT_MET);
}
} catch (SourceAuthorityException e) {
result.addComment("Error with bdq:sourceAuthority: " + e.getMessage());
result.setResultState(ResultState.EXTERNAL_PREREQUISITES_NOT_MET);
}
return result;
}
Expand Down Expand Up @@ -3691,27 +3698,81 @@ public static DQResponse<AmendmentValue> amendmentCoordinatesTransposed(

return result;
}

/**
* Are the supplied geographic coordinates within a defined buffer of the center of the country?
*
* Provides: 287 ISSUE_COORDINATES_CENTEROFCOUNTRY
* Version: 2023-09-17
*
* @param decimalLatitude the provided dwc:decimalLatitude to evaluate as ActedUpon.
* @param decimalLongitude the provided dwc:decimalLongitude to evaluate as ActedUpon.
* @param countryCode the provided dwc:countryCode to evaluate as Consulted.
* @return DQResponse the response of type AmendmentValue to return
* @param sourceAuthority a {@link java.lang.String} object.
*/
@Amendment(label="ISSUE_COORDINATES_CENTEROFCOUNTRY", description="Are the supplied geographic coordinates within a defined buffer of the center of the country?")
@Provides("256e51b3-1e08-4349-bb7e-5186631c3f8e")
@ProvidesVersion("https://rs.tdwg.org/bdqcore/terms/256e51b3-1e08-4349-bb7e-5186631c3f8e/2024-08-20")
@Specification("EXTERNAL_PREREQUISITES_NOT_MET if the bdq:sourceAuthority is not available; INTERNAL_PREREQUISITES_NOT_MET if any of dwc:countryCode, dwc:decimalLatitude, dwc:decimalLongitude are EMPTY; POTENTIAL_ISSUE if the geographic coordinates are within the distance given by bdq:spatialBufferInMeters from the center (or one of the centers), of the bdq:sourceAuthority provides more than one per country code of the supplied dwc:countryCode as represented in the bdq:sourceAuthority; otherwise NOT_ISSUE.")
public static DQResponse<AmendmentValue> issueCoordinatesCenterofcountry(
@ActedUpon("dwc:decimalLatitude") String decimalLatitude,
@ActedUpon("dwc:decimalLongitude") String decimalLongitude,
@Consulted("dwc:countryCode") String countryCode,
@Parameter(name="bdq:spatialBufferInMeters") String spatialBufferInMeters,
@Parameter(name="bdq:sourceAuthority") String sourceAuthority
) {
DQResponse<AmendmentValue> result = new DQResponse<AmendmentValue>();

// TODO: Specification
// EXTERNAL_PREREQUISITES_NOT_MET if the bdq:sourceAuthority is not available;
// INTERNAL_PREREQUISITES_NOT_MET if any of dwc:countryCode,
// dwc:decimalLatitude, dwc:decimalLongitude are EMPTY; POTENTIAL_ISSUE if the
// geographic coordinates are within the distance given by
// bdq:spatialBufferInMeters from the center (or one of the centers), of the
// bdq:sourceAuthority provides more than one per country code of the supplied
// dwc:countryCode as represented in the bdq:sourceAuthority; otherwise
// NOT_ISSUE.

// Parameters
// bdq:spatialBufferInMeters default = "5000"
// bdq:sourceAuthority default = "GBIF Catalogue of Country Centroides"
// {[https://raw.githubusercontent.com/jhnwllr/catalogue-of-centroids/master/PCLI.tsv]}

if (GEOUtil.isEmpty(sourceAuthority)) {
sourceAuthority = "GBIF Catalogue of Country Centroides";
}
if (GEOUtil.isEmpty(spatialBufferInMeters)) {
sourceAuthority = "3000";
}

try {
GeoRefSourceAuthority sourceAuthorityObject = new GeoRefSourceAuthority(sourceAuthority);
if (sourceAuthorityObject.getAuthority().equals(EnumGeoRefSourceAuthority.INVALID)) {
throw new SourceAuthorityException("Invalid Source Authority");
}

if (GEOUtil.isEmpty(decimalLatitude)) {
result.setResultState(ResultState.INTERNAL_PREREQUISITES_NOT_MET);
result.addComment("The value provided for dwc:decimalLatitude is empty");
} else if (GEOUtil.isEmpty(decimalLongitude)) {
result.setResultState(ResultState.INTERNAL_PREREQUISITES_NOT_MET);
result.addComment("The value provided for dwc:decimalLongitude is empty");
} else if (GEOUtil.isEmpty(countryCode)) {
result.setResultState(ResultState.INTERNAL_PREREQUISITES_NOT_MET);
result.addComment("The value provided for dwc:countryCode is empty");
} else {
// TODO: Implementation


}
} catch (SourceAuthorityException e) {
result.setResultState(ResultState.EXTERNAL_PREREQUISITES_NOT_MET);
result.addComment("Error with specified Source Authority: " + e.getMessage());
}

return result;
}


// TODO: Implementation of VALIDATION_COUNTRY_FOUND is not up to date with current version: https://rs.tdwg.org/bdq/terms/69b2efdc-6269-45a4-aecb-4cb99c2ae134/2024-08-19 see line: 155
// TODO: Implementation of AMENDMENT_COORDINATES_FROM_VERBATIM is not up to date with current version: https://rs.tdwg.org/bdq/terms/3c2590c7-af8a-4eb4-af57-5f73ba9d1f8e/2024-08-19 see line: 389
// TODO: Implementation of VALIDATION_COUNTRY_NOTEMPTY is not up to date with current version: https://rs.tdwg.org/bdq/terms/6ce2b2b4-6afe-4d13-82a0-390d31ade01c/2023-09-17 see line: 743
// TODO: Implementation of AMENDMENT_COORDINATES_CONVERTED is not up to date with current version: https://rs.tdwg.org/bdq/terms/620749b9-7d9c-4890-97d2-be3d1cde6da8/2023-09-18 see line: 796
// TODO: Implementation of AMENDMENT_COUNTRYCODE_STANDARDIZED is not up to date with current version: https://rs.tdwg.org/bdq/terms/fec5ffe6-3958-4312-82d9-ebcca0efb350/2023-09-17 see line: 917
// TODO: Implementation of VALIDATION_COORDINATES_COUNTRYCODE_CONSISTENT is not up to date with current version: https://rs.tdwg.org/bdq/terms/adb27d29-9f0d-4d52-b760-a77ba57a69c9/2023-09-17 see line: 1019
// TODO: Implementation of AMENDMENT_MINDEPTH-MAXDEPTH_FROM_VERBATIM is not up to date with current version: https://rs.tdwg.org/bdq/terms/c5658b83-4471-4f57-9d94-bf7d0a96900c/2024-08-03 see line: 1139
// TODO: Implementation of VALIDATION_GEODETICDATUM_STANDARD is not up to date with current version: https://rs.tdwg.org/bdq/terms/7e0c0418-fe16-4a39-98bd-80e19d95b9d1/2023-09-17 see line: 1459
// TODO: Implementation of AMENDMENT_GEODETICDATUM_STANDARDIZED is not up to date with current version: https://rs.tdwg.org/bdq/terms/0345b325-836d-4235-96d0-3b5caf150fc0/2024-08-05 see line: 1518
// TODO: Implementation of VALIDATION_COUNTRY_COUNTRYCODE_CONSISTENT is not up to date with current version: https://rs.tdwg.org/bdq/terms/b23110e7-1be7-444a-a677-cdee0cf4330c/2023-09-18 see line: 1599
// TODO: Implementation of AMENDMENT_MINELEVATION-MAXELEVATION_FROM_VERBATIM is not up to date with current version: https://rs.tdwg.org/bdq/terms/2d638c8b-4c62-44a0-a14d-fa147bf9823d/2024-08-03 see line: 1701
// TODO: Implementation of AMENDMENT_COUNTRYCODE_FROM_COORDINATES is not up to date with current version: https://rs.tdwg.org/bdq/terms/8c5fe9c9-4ba9-49ef-b15a-9ccd0424e6ae/2024-08-18 see line: 1925
// TODO: Implementation of VALIDATION_GEODETICDATUM_NOTEMPTY is not up to date with current version: https://rs.tdwg.org/bdq/terms/239ec40e-a729-4a8e-ba69-e0bf03ac1c44/2023-09-18 see line: 2002
// TODO: Implementation of VALIDATION_DECIMALLONGITUDE_NOTEMPTY is not up to date with current version: https://rs.tdwg.org/bdq/terms/9beb9442-d942-4f42-8b6a-fcea01ee086a/2023-09-18 see line: 2168
// TODO: Implementation of VALIDATION_COUNTRYCODE_NOTEMPTY is not up to date with current version: https://rs.tdwg.org/bdq/terms/853b79a2-b314-44a2-ae46-34a1e7ed85e4/2023-09-18 see line: 2203
// TODO: Implementation of AMENDMENT_GEODETICDATUM_ASSUMEDDEFAULT is not up to date with current version: https://rs.tdwg.org/bdq/terms/7498ca76-c4d4-42e2-8103-acacccbdffa7/2024-08-18 see line: 2244
// TODO: Implementation of VALIDATION_MINDEPTH_INRANGE is not up to date with current version: https://rs.tdwg.org/bdq/terms/04b2c8f3-c71b-4e95-8e43-f70374c5fb92/2023-09-18 see line: 2316
// TODO: Implementation of VALIDATION_MINELEVATION_LESSTHAN_MAXELEVATION is not up to date with current version: https://rs.tdwg.org/bdq/terms/d708526b-6561-438e-aa1a-82cd80b06396/2023-09-18 see line: 2390
// TODO: Implementation of VALIDATION_COORDINATEUNCERTAINTY_INRANGE is not up to date with current version: https://rs.tdwg.org/bdq/terms/c6adf2ea-3051-4498-97f4-4b2f8a105f57/2023-09-18 see line: 2455
// TODO: Implementation of VALIDATION_DECIMALLATITUDE_NOTEMPTY is not up to date with current version: https://rs.tdwg.org/bdq/terms/7d2485d5-1ba7-4f25-90cb-f4480ff1a275/2023-09-18 see line: 2581
// TODO: Implementation of VALIDATION_MAXDEPTH_INRANGE is not up to date with current version: https://rs.tdwg.org/bdq/terms/3f1db29a-bfa5-40db-9fd1-fde020d81939/2023-09-18 see line: 2620
// TODO: Implementation of VALIDATION_STATEPROVINCE_FOUND is not up to date with current version: https://rs.tdwg.org/bdq/terms/4daa7986-d9b0-4dd5-ad17-2d7a771ea71a/2023-09-18 see line: 2693
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public enum EnumGeoRefSourceAuthority {
GADM_ADM1,
GETTY_TGN,
INVALID;

// TODO; add datahub.io
// TODO: add ne_10m_admin_0_countries

/**
* <p>getName.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ private void init() {
* @return true if the country is found as a sovereign nation level entity in TGN matching
* any form of the name, false if the country is not found as a sovereign nation lavel entity
* in TGN, null on an exception querying TGN.
* @throws SourceAuthorityException
*/
public Boolean lookupCountry(String country) {
public Boolean lookupCountry(String country) throws SourceAuthorityException {

Boolean retval = null;

Expand Down Expand Up @@ -103,7 +104,6 @@ public Boolean lookupCountry(String country) {
Unmarshaller unmarshaler = jc.createUnmarshaller();
Vocabulary response = (Vocabulary) unmarshaler.unmarshal(is);
System.out.println(response.getCount());
System.out.println(response.getCount());
if (response.getCount().compareTo(BigInteger.ONE)==0) {
// idiom for line above from BigInteger docs: (x.compareTo(y) <op> 0)
// one match
Expand All @@ -129,14 +129,13 @@ public Boolean lookupCountry(String country) {
retval = false;
}
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error(e.getMessage());
throw new SourceAuthorityException("Error interpreting json response returned from Getty TGN:" + e.getMessage());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error(e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error(e.getMessage());
throw new SourceAuthorityException("Error querying the Getty TGN:" + e.getMessage());
}
}
}
Expand All @@ -150,8 +149,9 @@ public Boolean lookupCountry(String country) {
* @param country preferred name of sovereign nation level entity to look up.
* @return true if the provided country matches a preferred name of a sovereign nation level
* entity in TGN, false if it does not, null on an error querying the TGN service.
* @throws SourceAuthorityException
*/
public Boolean lookupCountryExact(String country) {
public Boolean lookupCountryExact(String country) throws SourceAuthorityException {

Boolean retval = null;

Expand All @@ -178,8 +178,7 @@ public Boolean lookupCountryExact(String country) {
JAXBContext jc = JAXBContext.newInstance(Vocabulary.class);
Unmarshaller unmarshaler = jc.createUnmarshaller();
Vocabulary response = (Vocabulary) unmarshaler.unmarshal(is);
System.out.println(response.getCount());
System.out.println(response.getCount());
logger.debug(response.getCount());
if (response.getCount().compareTo(BigInteger.ONE)==0) {
String preferredTerm = response.getSubject().get(0).getPreferredTerm().getValue();
String cleanedPreferredTerm = preferredTerm.replaceAll("\\([A-Za-z ]+\\)$", "").trim();
Expand All @@ -193,14 +192,13 @@ public Boolean lookupCountryExact(String country) {
retval = false;
}
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error(e.getMessage());
throw new SourceAuthorityException("Error interpreting json response returned from Getty TGN:" + e.getMessage());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error(e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error(e.getMessage());
throw new SourceAuthorityException("Error querying the Getty TGN:" + e.getMessage());
}

}
Expand Down

0 comments on commit efb9018

Please sign in to comment.