use of org.eol.globi.util.InvalidLocationException in project eol-globi-data by jhpoelen.
the class StudyImporterForHurlbert method importInteraction.
protected void importInteraction(Set<String> regions, Set<String> locales, Set<String> habitats, Record record, Study study, String preyTaxonName, String predatorName) throws StudyImporterException {
try {
Taxon predatorTaxon = new TaxonImpl(predatorName);
Specimen predatorSpecimen = nodeFactory.createSpecimen(study, predatorTaxon);
setBasisOfRecordAsLiterature(predatorSpecimen);
Taxon preyTaxon = new TaxonImpl(preyTaxonName);
String preyNameId = StringUtils.trim(columnValueOrNull(record, "Prey_Name_ITIS_ID"));
if (NumberUtils.isDigits(preyNameId)) {
preyTaxon.setExternalId(TaxonomyProvider.ITIS.getIdPrefix() + preyNameId);
}
Specimen preySpecimen = nodeFactory.createSpecimen(study, preyTaxon);
setBasisOfRecordAsLiterature(preySpecimen);
String preyStage = StringUtils.trim(columnValueOrNull(record, "Prey_Stage"));
if (StringUtils.isNotBlank(preyStage)) {
Term lifeStage = nodeFactory.getOrCreateLifeStage("HULBERT:" + StringUtils.replace(preyStage, " ", "_"), preyStage);
preySpecimen.setLifeStage(lifeStage);
}
String preyPart = StringUtils.trim(columnValueOrNull(record, "Prey_Part"));
if (StringUtils.isNotBlank(preyPart)) {
Term term = nodeFactory.getOrCreateBodyPart("HULBERT:" + StringUtils.replace(preyPart, " ", "_"), preyPart);
preySpecimen.setBodyPart(term);
}
Date date = addCollectionDate(record, study);
nodeFactory.setUnixEpochProperty(predatorSpecimen, date);
nodeFactory.setUnixEpochProperty(preySpecimen, date);
LocationImpl location = new LocationImpl(null, null, null, null);
String longitude = columnValueOrNull(record, "Longitude_dd");
String latitude = columnValueOrNull(record, "Latitude_dd");
if (NumberUtils.isNumber(latitude) && NumberUtils.isNumber(longitude)) {
try {
LatLng latLng = LocationUtil.parseLatLng(latitude, longitude);
String altitude = columnValueOrNull(record, "Altitude_mean_m");
Double altitudeD = NumberUtils.isNumber(altitude) ? Double.parseDouble(altitude) : null;
location = new LocationImpl(latLng.getLat(), latLng.getLng(), altitudeD, null);
} catch (InvalidLocationException e) {
getLogger().warn(study, "found invalid (lat,lng) pair: (" + latitude + "," + longitude + ")");
}
}
String locationRegion = columnValueOrNull(record, "Location_Region");
String locationSpecific = columnValueOrNull(record, "Location_Specific");
location.setLocality(StringUtils.join(Arrays.asList(locationRegion, locationSpecific), ":"));
Location locationNode = nodeFactory.getOrCreateLocation(location);
String habitat_type = columnValueOrNull(record, "Habitat_type");
List<Term> habitatList = Arrays.stream(StringUtils.split(StringUtils.defaultIfBlank(habitat_type, ""), ";")).map(StringUtils::trim).map(habitat -> new TermImpl(idForHabitat(habitat), habitat)).collect(Collectors.toList());
nodeFactory.addEnvironmentToLocation(locationNode, habitatList);
preySpecimen.caughtIn(locationNode);
predatorSpecimen.caughtIn(locationNode);
predatorSpecimen.ate(preySpecimen);
} catch (NodeFactoryException e) {
throw new StudyImporterException("failed to create interaction between [" + predatorName + "] and [" + preyTaxonName + "]", e);
}
}
use of org.eol.globi.util.InvalidLocationException in project eol-globi-data by jhpoelen.
the class InteractionListenerImpl method getOrCreateLocation.
private Location getOrCreateLocation(Study study, Map<String, String> link) throws IOException, NodeFactoryException {
LatLng centroid = null;
String[] latitudes = { DECIMAL_LATITUDE, StudyImporterForMetaTable.LATITUDE };
String latitude = getFirstValueForTerms(link, latitudes);
String[] longitudes = { DECIMAL_LONGITUDE, StudyImporterForMetaTable.LONGITUDE };
String longitude = getFirstValueForTerms(link, longitudes);
if (StringUtils.isNotBlank(latitude) && StringUtils.isNotBlank(longitude)) {
try {
centroid = LocationUtil.parseLatLng(latitude, longitude);
} catch (InvalidLocationException e) {
getLogger().warn(study, "found invalid location: [" + e.getMessage() + "]");
}
}
String localityId = link.get(LOCALITY_ID);
String localityName = link.get(LOCALITY_NAME);
if (centroid == null) {
if (StringUtils.isNotBlank(localityId)) {
centroid = getGeoNamesService().findLatLng(localityId);
}
}
LocationImpl location = null;
if (centroid != null) {
location = new LocationImpl(centroid.getLat(), centroid.getLng(), null, null);
if (StringUtils.isNotBlank(localityId)) {
location.setLocalityId(localityId);
}
if (StringUtils.isNotBlank(localityName)) {
location.setLocality(localityName);
}
}
return location == null ? null : nodeFactory.getOrCreateLocation(location);
}
use of org.eol.globi.util.InvalidLocationException in project eol-globi-data by jhpoelen.
the class LocationUtil method parseLatLng.
public static LatLng parseLatLng(String latitude, String longitude) throws InvalidLocationException {
LatLng point = null;
try {
double lat = Double.parseDouble(latitude);
double lng = Double.parseDouble(longitude);
if (!isValidLatitude(lat)) {
throw new InvalidLocationException("range of latitude [" + latitude + "] not valid");
} else if (!isValidLongitude(lng)) {
throw new InvalidLocationException("range of longitude [" + longitude + "] not valid");
}
if (isValidLatitude(lat) && isValidLongitude(lng)) {
point = new LatLng(lat, lng);
}
} catch (NumberFormatException ex) {
throw new InvalidLocationException("invalid (latitude, longitude) = (" + latitude + "," + longitude + ")", ex);
}
return point;
}
Aggregations