Skip to content

Commit c3a7143

Browse files
authored
Merge pull request #694 from BioKIC/locations-endpoint
addresses location history
2 parents 652bd96 + bf27e1f commit c3a7143

File tree

1 file changed

+111
-68
lines changed

1 file changed

+111
-68
lines changed

neon/classes/OccurrenceHarvester.php

+111-68
Original file line numberDiff line numberDiff line change
@@ -944,10 +944,29 @@ private function setCollectionIdentifier(&$dwcArr,$sampleClass){
944944
}
945945

946946
private function setNeonLocationData(&$dwcArr, $locationName){
947-
$url = $this->neonApiBaseUrl.'/locations/'.urlencode($locationName).'?apiToken='.$this->neonApiKey;
947+
$url = $this->neonApiBaseUrl.'/locations/'.urlencode($locationName).'?history=true&apiToken='.$this->neonApiKey;
948948
//echo 'loc url: '.$url.'<br/>';
949949
$resultArr = $this->getNeonApiArr($url);
950+
951+
$eventDate = $dwcArr['eventDate'];
952+
$matchingIndex = null;
953+
950954
if(!$resultArr) return false;
955+
956+
foreach ($resultArr['locationHistory'] as $index => $location) {
957+
958+
$startDate = $location['locationStartDate'];
959+
$endDate = $location['locationEndDate'];
960+
961+
// Check if eventDate falls within the location history date range
962+
if ($eventDate >= $startDate && (empty($endDate) || $eventDate <= $endDate)) {
963+
$matchingIndex = $index;
964+
break;
965+
}
966+
}
967+
968+
if ($matchingIndex == null) $matchingIndex = 0;
969+
951970
if(isset($resultArr['locationType']) && $resultArr['locationType']){
952971
if($resultArr['locationType'] == 'SITE') $dwcArr['siteID'] = $resultArr['locationName'];
953972
elseif($resultArr['locationType'] == 'DOMAIN') $dwcArr['domainID'] = $resultArr['locationName'];
@@ -966,82 +985,106 @@ private function setNeonLocationData(&$dwcArr, $locationName){
966985
}
967986
}
968987

969-
if(!isset($dwcArr['decimalLatitude']) && isset($resultArr['locationDecimalLatitude']) && $resultArr['locationDecimalLatitude']){
970-
$dwcArr['decimalLatitude'] = $resultArr['locationDecimalLatitude'];
971-
}
972-
if(!isset($dwcArr['decimalLongitude']) && isset($resultArr['locationDecimalLongitude']) && $resultArr['locationDecimalLongitude']){
973-
$dwcArr['decimalLongitude'] = $resultArr['locationDecimalLongitude'];
974-
}
975-
if(!isset($dwcArr['verbatimCoordinates']) && isset($resultArr['locationUtmEasting']) && $resultArr['locationUtmEasting']){
976-
$dwcArr['verbatimCoordinates'] = trim($resultArr['locationUtmZone'].$resultArr['locationUtmHemisphere'].' '.$resultArr['locationUtmEasting'].'E '.$resultArr['locationUtmNorthing'].'N');
988+
$resultArr_history = $resultArr['locationHistory'][$matchingIndex];
989+
990+
if(!isset($dwcArr['decimalLatitude']) && isset($resultArr_history['locationDecimalLatitude']) && $resultArr_history['locationDecimalLatitude']){
991+
$dwcArr['decimalLatitude'] = $resultArr_history['locationDecimalLatitude'];
977992
}
978-
$elevMin = '';
979-
$elevMax = '';
980-
$elevUncertainty = '';
981-
if(isset($resultArr['locationElevation']) && $resultArr['locationElevation']){
982-
$elevMin = round($resultArr['locationElevation']);
993+
if(!isset($dwcArr['decimalLongitude']) && isset($resultArr_history['locationDecimalLongitude']) && $resultArr_history['locationDecimalLongitude']){
994+
$dwcArr['decimalLongitude'] = $resultArr_history['locationDecimalLongitude'];
983995
}
984-
if(isset($resultArr['elevation_uncertainty']) && $resultArr['elevation_uncertainty']){
985-
$elevUncertainty = $resultArr['elevation_uncertainty'];
996+
if(!isset($dwcArr['verbatimCoordinates']) && isset($resultArr_history['locationUtmEasting']) && $resultArr_history['locationUtmEasting']){
997+
$dwcArr['verbatimCoordinates'] = trim($resultArr_history['locationUtmZone'].$resultArr_history['locationUtmHemisphere'].' '.$resultArr_history['locationUtmEasting'].'E '.$resultArr_history['locationUtmNorthing'].'N');
986998
}
999+
1000+
$locPropArr_history = $resultArr_history['locationProperties'];
9871001
$locPropArr = $resultArr['locationProperties'];
988-
if($locPropArr){
1002+
1003+
if ($locPropArr || $locPropArr_history) {
9891004
$habitatArr = array();
990-
foreach($locPropArr as $propArr){
991-
if(!isset($dwcArr['georeferenceSources']) && $propArr['locationPropertyName'] == 'Value for Coordinate source'){
992-
$dwcArr['georeferenceSources'] = $propArr['locationPropertyValue'];
993-
}
994-
elseif(!isset($dwcArr['coordinateUncertaintyInMeters']) && $propArr['locationPropertyName'] == 'Value for Coordinate uncertainty'){
995-
$dwcArr['coordinateUncertaintyInMeters'] = $propArr['locationPropertyValue'];
996-
}
997-
elseif($propArr['locationPropertyName'] == 'Value for Minimum elevation'){
998-
$elevMin = round($propArr['locationPropertyValue']);
999-
}
1000-
elseif($propArr['locationPropertyName'] == 'Value for Maximum elevation'){
1001-
$elevMax = round($propArr['locationPropertyValue']);
1002-
}
1003-
elseif(!isset($dwcArr['country']) && $propArr['locationPropertyName'] == 'Value for Country'){
1004-
$countryValue = $propArr['locationPropertyValue'];
1005-
if($countryValue == 'unitedStates') $countryValue = 'United States';
1006-
elseif($countryValue == 'USA') $countryValue = 'United States';
1005+
$elevMin = '';
1006+
$elevMax = '';
1007+
$elevUncertainty = '';
1008+
$fullPropArr = array_merge($locPropArr_history, $locPropArr);
1009+
1010+
foreach ($fullPropArr as $propArr) {
1011+
$propName = $propArr['locationPropertyName'];
1012+
$propValue = $propArr['locationPropertyValue'];
1013+
1014+
if (!isset($dwcArr['georeferenceSources']) && $propName == 'Value for Coordinate source') {
1015+
$dwcArr['georeferenceSources'] = $propValue;
1016+
} elseif (!isset($dwcArr['coordinateUncertaintyInMeters']) && $propName == 'Value for Coordinate uncertainty') {
1017+
$dwcArr['coordinateUncertaintyInMeters'] = $propValue;
1018+
} elseif ($elevMin == '' && $propName == 'Value for Minimum elevation') {
1019+
$elevMin = round($propValue);
1020+
} elseif ($elevMax == '' && $propName == 'Value for Maximum elevation') {
1021+
$elevMax = round($propValue);
1022+
} elseif ($elevUncertainty == '' && $propName == 'Value for Elevation uncertainty') {
1023+
$elevUncertainty = round($propValue);
1024+
} elseif (!isset($dwcArr['country']) && $propName == 'Value for Country') {
1025+
$countryValue = ($propValue == 'unitedStates' || $propValue == 'USA') ? 'United States' : $propValue;
10071026
$dwcArr['country'] = $countryValue;
1008-
}
1009-
elseif(!isset($dwcArr['county']) && $propArr['locationPropertyName'] == 'Value for County'){
1010-
$dwcArr['county'] = $propArr['locationPropertyValue'];
1011-
}
1012-
elseif(!isset($dwcArr['geodeticDatum']) && $propArr['locationPropertyName'] == 'Value for Geodetic datum'){
1013-
$dwcArr['geodeticDatum'] = $propArr['locationPropertyValue'];
1014-
}
1015-
elseif(!isset($dwcArr['plotDim']) && $propArr['locationPropertyName'] == 'Value for Plot dimensions'){
1016-
$dwcArr['plotDim'] = ' (plot dimensions: '.$propArr['locationPropertyValue'].')';
1017-
}
1018-
elseif(!isset($habitatArr['landcover']) && strpos($propArr['locationPropertyName'],'Value for National Land Cover Database') !== false){
1019-
$habitatArr['landcover'] = $propArr['locationPropertyValue'];
1020-
}
1021-
elseif(!isset($habitatArr['aspect']) && $propArr['locationPropertyName'] == 'Value for Slope aspect'){
1022-
$habitatArr['aspect'] = 'slope aspect: '.$propArr['locationPropertyValue'];
1023-
}
1024-
elseif(!isset($habitatArr['gradient']) && $propArr['locationPropertyName'] == 'Value for Slope gradient'){
1025-
$habitatArr['gradient'] = 'slope gradient: '.$propArr['locationPropertyValue'];
1026-
}
1027-
elseif(!isset($habitatArr['soil']) && $propArr['locationPropertyName'] == 'Value for Soil type order'){
1028-
if($dwcArr['collid'] == 30 && !isset($dwcArr['identifications'])){
1029-
$dwcArr['identifications'][] = array('sciname' => $propArr['locationPropertyValue']);
1027+
} elseif (!isset($dwcArr['county']) && $propName == 'Value for County') {
1028+
$dwcArr['county'] = $propValue;
1029+
} elseif (!isset($dwcArr['geodeticDatum']) && $propName == 'Value for Geodetic datum') {
1030+
$dwcArr['geodeticDatum'] = $propValue;
1031+
} elseif (!isset($dwcArr['plotDim']) && $propName == 'Value for Plot dimensions') {
1032+
$dwcArr['plotDim'] = ' (plot dimensions: ' . $propValue . ')';
1033+
} elseif (!isset($habitatArr['landcover']) && strpos($propName, 'Value for National Land Cover Database') !== false) {
1034+
$habitatArr['landcover'] = $propValue;
1035+
} elseif (!isset($habitatArr['aspect']) && $propName == 'Value for Slope aspect') {
1036+
$habitatArr['aspect'] = 'slope aspect: ' . $propValue;
1037+
} elseif (!isset($habitatArr['gradient']) && $propName == 'Value for Slope gradient') {
1038+
$habitatArr['gradient'] = 'slope gradient: ' . $propValue;
1039+
} elseif (!isset($habitatArr['soil']) && $propName == 'Value for Soil type order') {
1040+
if ($dwcArr['collid'] == 30 && !isset($dwcArr['identifications'])) {
1041+
$dwcArr['identifications'][] = array('sciname' => $propValue);
1042+
}
1043+
$habitatArr['soil'] = 'soil type order: ' . $propValue;
1044+
} elseif (!isset($dwcArr['stateProvince']) && $propName == 'Value for State province') {
1045+
$stateStr = $propValue;
1046+
if (array_key_exists($stateStr, $this->stateArr)) {
1047+
$stateStr = $this->stateArr[$stateStr];
10301048
}
1031-
$habitatArr['soil'] = 'soil type order: '.$propArr['locationPropertyValue'];
1032-
}
1033-
elseif(!isset($dwcArr['stateProvince']) && $propArr['locationPropertyName'] == 'Value for State province'){
1034-
$stateStr = $propArr['locationPropertyValue'];
1035-
if(array_key_exists($stateStr, $this->stateArr)) $stateStr = $this->stateArr[$stateStr];
10361049
$this->setTimezone($stateStr);
10371050
$dwcArr['stateProvince'] = $stateStr;
1038-
}
1039-
}
1040-
if($habitatArr) $dwcArr['habitat'] = implode('; ',$habitatArr);
1041-
}
1042-
if($elevMin && !isset($dwcArr['minimumElevationInMeters'])) $dwcArr['minimumElevationInMeters'] = $elevMin;
1043-
if($elevMax && $elevMax != $elevMin && !isset($dwcArr['maximumElevationInMeters'])) $dwcArr['maximumElevationInMeters'] = $elevMax;
1044-
if($elevUncertainty) $dwcArr['verbatimElevation'] = trim($elevMin.' - '.$elevMax,' -').' ('.$elevUncertainty.')';
1051+
}
1052+
}
1053+
if ($habitatArr) {
1054+
if (isset($habitatArr['landcover'])) {
1055+
$landcover = $habitatArr['landcover'];
1056+
unset($habitatArr['landcover']);
1057+
array_unshift($habitatArr, $landcover);
1058+
}
1059+
$dwcArr['habitat'] = implode('; ', $habitatArr);
1060+
}
1061+
if($elevMin === '' && !isset($dwcArr['minimumElevationInMeters'])) {
1062+
$elevMin = round($resultArr_history['locationElevation']);
1063+
}
1064+
if ($elevMin !== '' && !isset($dwcArr['minimumElevationInMeters'])) {
1065+
$dwcArr['minimumElevationInMeters'] = $elevMin;
1066+
}
1067+
1068+
if ($elevMax && $elevMax != $elevMin && !isset($dwcArr['maximumElevationInMeters'])) {
1069+
$dwcArr['maximumElevationInMeters'] = $elevMax;
1070+
}
1071+
1072+
// new code if we wanted to use the verbatim elevation field in the future
1073+
// if ($elevMin !== '' || $elevMax !== '' || $elevUncertainty !== '') {
1074+
// $verbatimParts = [];
1075+
// if ($elevMin !== '') {
1076+
// $verbatimParts[] = $elevMin;
1077+
// }
1078+
// if ($elevMax !== '' && $elevMax != $elevMin) {
1079+
// $verbatimParts[] = $elevMax;
1080+
// }
1081+
// if (!empty($elevUncertainty)) {
1082+
// $verbatimParts[] = "($elevUncertainty)";
1083+
// }
1084+
// $dwcArr['verbatimElevation'] = implode(' - ', $verbatimParts);
1085+
// }
1086+
1087+
}
10451088

10461089
if(isset($resultArr['locationParent']) && $resultArr['locationParent']){
10471090
if($resultArr['locationParent'] != 'REALM'){

0 commit comments

Comments
 (0)