Skip to content

Commit bf27e1f

Browse files
committed
addresses location history
- addresses the fact that sampling locations with the same locationID have moved in geographic space over time = include history in the call to the locations endpoint of the API and pull values specific to the locationID *at the collection date*
1 parent 7d8d45a commit bf27e1f

File tree

1 file changed

+109
-66
lines changed

1 file changed

+109
-66
lines changed

neon/classes/OccurrenceHarvester.php

+109-66
Original file line numberDiff line numberDiff line change
@@ -948,10 +948,29 @@ private function setCollectionIdentifier(&$dwcArr,$sampleClass){
948948
}
949949

950950
private function setNeonLocationData(&$dwcArr, $locationName){
951-
$url = $this->neonApiBaseUrl.'/locations/'.urlencode($locationName).'?apiToken='.$this->neonApiKey;
951+
$url = $this->neonApiBaseUrl.'/locations/'.urlencode($locationName).'?history=true&apiToken='.$this->neonApiKey;
952952
//echo 'loc url: '.$url.'<br/>';
953953
$resultArr = $this->getNeonApiArr($url);
954+
955+
$eventDate = $dwcArr['eventDate'];
956+
$matchingIndex = null;
957+
954958
if(!$resultArr) return false;
959+
960+
foreach ($resultArr['locationHistory'] as $index => $location) {
961+
962+
$startDate = $location['locationStartDate'];
963+
$endDate = $location['locationEndDate'];
964+
965+
// Check if eventDate falls within the location history date range
966+
if ($eventDate >= $startDate && (empty($endDate) || $eventDate <= $endDate)) {
967+
$matchingIndex = $index;
968+
break;
969+
}
970+
}
971+
972+
if ($matchingIndex == null) $matchingIndex = 0;
973+
955974
if(isset($resultArr['locationType']) && $resultArr['locationType']){
956975
if($resultArr['locationType'] == 'SITE') $dwcArr['siteID'] = $resultArr['locationName'];
957976
elseif($resultArr['locationType'] == 'DOMAIN') $dwcArr['domainID'] = $resultArr['locationName'];
@@ -970,82 +989,106 @@ private function setNeonLocationData(&$dwcArr, $locationName){
970989
}
971990
}
972991

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

10501093
if(isset($resultArr['locationParent']) && $resultArr['locationParent']){
10511094
if($resultArr['locationParent'] != 'REALM'){

0 commit comments

Comments
 (0)