Browse Source

added iri field population based on scientificname and municipality fields

master
Paul Pound 9 years ago
parent
commit
9a143b0ecf
  1. 19
      src/DataParser/GbifParser.php
  2. 32
      src/DataParser/NrcanParser.php

19
src/DataParser/GbifParser.php

@ -11,6 +11,7 @@ namespace Drupal\islandora_herbarium_object\DataParser;
class GbifParser extends DataParser
{
public $gbifUrlPrefix = 'http://api.gbif.org/v1/species?name=';
public function parseData() {
$arr = json_decode($this->data, TRUE);
if(empty($arr['results'])) {
@ -22,11 +23,17 @@ class GbifParser extends DataParser
public function updateEntity($entity) {
try {
$taxoniri = $entity->get("field_dwciri_scientificname")->getValue();
if(empty($taxoniri[0]['value'])) {
throw new Exception("No URI provided, cannot look up taxon data.");
$url = '';
if(empty($url = $taxoniri[0]['value']) && !empty($entity->get('field_dwc_scientificname')->getValue())) {
$value = array($this->getGbifUrl($entity->get('field_dwc_scientificname')->getValue()[0]['value']));
$taxoniri[] = ['value' => $value[0]];
$url = $taxoniri[0]['value'];
if(empty($url)) {
throw new \Exception("No ScientificName URI or ScientificName text provided, cannot look up taxon data.");
}
$this->getRemoteData($taxoniri[0]['value']);
$entity->set('field_dwciri_scientificname', $url);
}
$this->getRemoteData($url);
$this->parseData();
} catch (Exception $e) {
watchdog_exception('islandora_herbarium_object', $e, 'Error getting dwciri');
@ -45,4 +52,8 @@ class GbifParser extends DataParser
$entity->set('field_dwc_vernacularname', $this->getValue('vernacularName'));
}
public function getGbifUrl($scientificName){
return $this->gbifUrlPrefix . $scientificName;
}
}

32
src/DataParser/NrcanParser.php

@ -11,6 +11,9 @@ namespace Drupal\islandora_herbarium_object\DataParser;
class NrcanParser extends DataParser
{
public $nrcanUrlSearchPrefix = 'http://geogratis.gc.ca/services/geoname/en/geonames?q=';
public $nrcanUrlPrefix = 'http://geogratis.gc.ca/services/geoname/en/geonames/';
public function parseData()
{
$arr = json_decode($this->data, TRUE);
@ -22,11 +25,20 @@ class NrcanParser extends DataParser
public function updateEntity($entity) {
try {
$url = '';
$dwciri = $entity->get("field_dwciri_municipality")->getValue();
if(empty($dwciri[0]['value'])) {
throw new Exception("No URI provided, cannot look up location data.");
if(empty($url = $dwciri[0]['value']) && !empty($entity->get("field_dwc_municipality")->getValue())) {
//throw new Exception("No URI provided, cannot look up location data.");
$value = array($this->getNrcanUrl($entity->get('field_dwc_municipality')->getValue()[0]['value']));
$dwciri[] = ['value' => $value[0]];
$url = $dwciri[0]['value'];
if(empty($url)) {
throw new \Exception("No Municipality URI or Municipality text provided, cannot look up taxon data.");
}
$entity->set('field_dwciri_municipality', $url);
}
$this->getRemoteData($dwciri[0]['value']);
$this->getRemoteData($url);
$this->parseData();
} catch (Exception $e) {
watchdog_exception('islandora_herbarium_object', $e, 'Error getting dwciri');
@ -59,4 +71,18 @@ class NrcanParser extends DataParser
$geo_field_value = \Drupal::service('geofield.wkt_generator')->WktBuildPoint($point);
$entity->set('field_herb_geo_field', $geo_field_value);
}
public function getNrcanUrl($municipality){
$url = $this->nrcanUrlSearchPrefix . $municipality . "&province=11";
$data = $this->getRemoteData($url, TRUE);
$data_arr = json_decode($data, TRUE);
$key = '';
foreach($data_arr['items'] as $item) {
if($item['name'] === $municipality){
$key = $item['id'];
break;
}
}
return $this->nrcanUrlPrefix . $key;
}
}

Loading…
Cancel
Save