Search in sources :

Example 1 with InvalidLocationException

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);
    }
}
Also used : TsvParser(com.univocity.parsers.tsv.TsvParser) DateUtil(org.eol.globi.util.DateUtil) StringUtils(org.apache.commons.lang.StringUtils) CitationUtil(org.globalbioticinteractions.dataset.CitationUtil) Arrays(java.util.Arrays) Record(com.univocity.parsers.common.record.Record) Term(org.eol.globi.domain.Term) Specimen(org.eol.globi.domain.Specimen) Location(org.eol.globi.domain.Location) Date(java.util.Date) TermImpl(org.eol.globi.domain.TermImpl) LocationImpl(org.eol.globi.domain.LocationImpl) HashMap(java.util.HashMap) StudyImpl(org.eol.globi.domain.StudyImpl) HashSet(java.util.HashSet) TaxonImpl(org.eol.globi.domain.TaxonImpl) Map(java.util.Map) LatLng(org.eol.globi.geo.LatLng) TaxonomyProvider(org.eol.globi.domain.TaxonomyProvider) Taxon(org.eol.globi.domain.Taxon) InvalidLocationException(org.eol.globi.util.InvalidLocationException) DateTime(org.joda.time.DateTime) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) TsvParserSettings(com.univocity.parsers.tsv.TsvParserSettings) List(java.util.List) NumberUtils(org.apache.commons.lang3.math.NumberUtils) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) Study(org.eol.globi.domain.Study) StringEscapeUtils(org.apache.commons.lang.StringEscapeUtils) ArrayUtils(org.apache.commons.lang.ArrayUtils) InputStream(java.io.InputStream) InvalidLocationException(org.eol.globi.util.InvalidLocationException) Taxon(org.eol.globi.domain.Taxon) TaxonImpl(org.eol.globi.domain.TaxonImpl) Term(org.eol.globi.domain.Term) Date(java.util.Date) TermImpl(org.eol.globi.domain.TermImpl) Specimen(org.eol.globi.domain.Specimen) StringUtils(org.apache.commons.lang.StringUtils) LocationImpl(org.eol.globi.domain.LocationImpl) LatLng(org.eol.globi.geo.LatLng) Location(org.eol.globi.domain.Location)

Example 2 with InvalidLocationException

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);
}
Also used : InvalidLocationException(org.eol.globi.util.InvalidLocationException) LocationImpl(org.eol.globi.domain.LocationImpl) LatLng(org.eol.globi.geo.LatLng)

Example 3 with InvalidLocationException

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;
}
Also used : InvalidLocationException(org.eol.globi.util.InvalidLocationException) LatLng(org.eol.globi.geo.LatLng)

Aggregations

LatLng (org.eol.globi.geo.LatLng)3 InvalidLocationException (org.eol.globi.util.InvalidLocationException)3 LocationImpl (org.eol.globi.domain.LocationImpl)2 Record (com.univocity.parsers.common.record.Record)1 TsvParser (com.univocity.parsers.tsv.TsvParser)1 TsvParserSettings (com.univocity.parsers.tsv.TsvParserSettings)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Arrays (java.util.Arrays)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 ArrayUtils (org.apache.commons.lang.ArrayUtils)1 StringEscapeUtils (org.apache.commons.lang.StringEscapeUtils)1 StringUtils (org.apache.commons.lang.StringUtils)1 NumberUtils (org.apache.commons.lang3.math.NumberUtils)1