Browse Source

refactored gbif and geonames and module file

master
Paul Pound 9 years ago
parent
commit
acd471d66b
  1. 47
      islandora_herbarium_object.module
  2. 2
      src/DataParser/DataParserInterface.php
  3. 26
      src/DataParser/GbifParser.php
  4. 29
      src/DataParser/GeonamesParser.php

47
islandora_herbarium_object.module

@ -31,51 +31,12 @@ function islandora_herbarium_object_help($route_name, RouteMatchInterface $route
function islandora_herbarium_object_entity_presave(Drupal\Core\Entity\EntityInterface $entity) function islandora_herbarium_object_entity_presave(Drupal\Core\Entity\EntityInterface $entity)
{ {
if ($entity->bundle() == 'islandora_herbarium_object') { if ($entity->bundle() == 'islandora_herbarium_object') {
try { // Get the geo data.
$dwciri = $entity->get("field_dwciri_municipality")->getValue();
$geo = \Drupal::service('islandora_herbarium_object.geoparser'); $geo = \Drupal::service('islandora_herbarium_object.geoparser');
$geo->getRemoteData($dwciri[0]['value']); $geo->updateEntity($entity);
$geo->parseData(); // Get the taxonomy data.
} catch (Exception $e) {
watchdog_exception('islandora_herbarium_object', $e, 'Error getting dwciri');
drupal_set_message(t('There was an error retrieving Municipality data. %msg. Verify the url is correct %ulr',
array('%msg'=> $e->getMessage(), '%url' => $dwciri[0]['value'])), 'error');
return;
}
$entity->set('field_dwc_municipality', $geo->getValue('name'));
$entity->set('field_dwc_countrycode', $geo->getValue('countryCode'));
$entity->set('field_dwc_country', $geo->getValue('countryName'));
$entity->set('field_dwc_continent', $geo->getValue('countinentCode'));
$entity->set('field_dwc_decimallatitude', $geo->getValue('lat'));
$entity->set('field_dwc_decimallongitude', $geo->getValue('lng'));
$entity->set('field_dwc_stateprovince', $geo->getValue('adminName1'));
$point = array(
'lon' => $geo->data_arr['lng'],
'lat' => $geo->data_arr['lat'],
);
$geo_field_value = \Drupal::service('geofield.wkt_generator')->WktBuildPoint($point);
$entity->set('field_herb_geo_field', $geo_field_value);
try {
$taxoniri = $entity->get("field_dwciri_scientificname")->getValue();
$taxon = \Drupal::service('islandora_herbarium_object.taxonparser'); $taxon = \Drupal::service('islandora_herbarium_object.taxonparser');
$taxon->getRemoteData($taxoniri[0]['value']); $taxon->updateEntity($entity);
$taxon->parseData();
} catch (Exception $e) {
watchdog_exception('islandora_herbarium_object', $e, 'Error getting dwciri');
drupal_set_message(t('There was an error retrieving Taxon data. %msg. Verify the url is correct %url',
array('%msg'=> $e->getMessage(), '%url' => $taxoniri[0]['value'])),'error');
return;
}
$entity->set('field_dwc_scientificname', $taxon->getValue('canonicalName'));
$name_arr = explode(' ', $taxon->getValue('canonicalName'));
$entity->set('field_dwc_specificepithet', $name_arr[1]);
$entity->set('field_dwc_kingdom', $taxon->getValue('kingdom'));
$entity->set('field_dwc_genus', $taxon->getValue('genus'));
$entity->set('field_dwc_phylum', $taxon->getValue('phylum'));
$entity->set('field_dwc_order', $taxon->getValue('order'));
$entity->set('field_dwc_family', $taxon->getValue('family'));
$entity->set('field_dwc_vernacularname', $taxon->getValue('vernacularName'));
$title = $taxon->getValue('canonicalName') . ' - ' . $geo->getValue('name'); $title = $taxon->getValue('canonicalName') . ' - ' . $geo->getValue('name');
if(!empty($title)) { if(!empty($title)) {

2
src/DataParser/DataParserInterface.php

@ -23,4 +23,6 @@ interface DataParserInterface
*/ */
public function getValue($key); public function getValue($key);
public function updateEntity($entity);
} }

26
src/DataParser/GbifParser.php

@ -19,4 +19,30 @@ class GbifParser extends DataParser
$this->data_arr = reset($arr['results']); $this->data_arr = reset($arr['results']);
} }
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.");
}
$this->getRemoteData($taxoniri[0]['value']);
$this->parseData();
} catch (Exception $e) {
watchdog_exception('islandora_herbarium_object', $e, 'Error getting dwciri');
drupal_set_message(t('There was an error retrieving Taxon data. %msg. Verify the url is correct %url',
array('%msg'=> $e->getMessage(), '%url' => $taxoniri[0]['value'])),'error');
return;
}
$entity->set('field_dwc_scientificname', $this->getValue('canonicalName'));
$name_arr = explode(' ', $this->getValue('canonicalName'));
$entity->set('field_dwc_specificepithet', $name_arr[1]);
$entity->set('field_dwc_kingdom', $this->getValue('kingdom'));
$entity->set('field_dwc_genus', $this->getValue('genus'));
$entity->set('field_dwc_phylum', $this->getValue('phylum'));
$entity->set('field_dwc_order', $this->getValue('order'));
$entity->set('field_dwc_family', $this->getValue('family'));
$entity->set('field_dwc_vernacularname', $this->getValue('vernacularName'));
}
} }

29
src/DataParser/GeonamesParser.php

@ -38,4 +38,33 @@ class GeonamesParser extends XmlDataParser
$this->getXmlNodeValue($xml_doc, 'east'); $this->getXmlNodeValue($xml_doc, 'east');
$this->getXmlNodeValue($xml_doc, 'south'); $this->getXmlNodeValue($xml_doc, 'south');
} }
public function updateEntity($entity) {
try {
$dwciri = $entity->get("field_dwciri_municipality")->getValue();
if(empty($dwciri[0]['value'])) {
throw new Exception("No URI provided, cannot look up location data.");
}
$this->getRemoteData($dwciri[0]['value']);
$this->parseData();
} catch (Exception $e) {
watchdog_exception('islandora_herbarium_object', $e, 'Error getting dwciri');
drupal_set_message(t('There was an error retrieving Municipality data. %msg. Verify the url is correct %url',
array('%msg'=> $e->getMessage(), '%url' => $dwciri[0]['value'])), 'error');
return;
}
$entity->set('field_dwc_municipality', $this->getValue('name'));
$entity->set('field_dwc_countrycode', $this->getValue('countryCode'));
$entity->set('field_dwc_country', $this->getValue('countryName'));
$entity->set('field_dwc_continent', $this->getValue('countinentCode'));
$entity->set('field_dwc_decimallatitude', $this->getValue('lat'));
$entity->set('field_dwc_decimallongitude', $this->getValue('lng'));
$entity->set('field_dwc_stateprovince', $this->getValue('adminName1'));
$point = array(
'lon' => $this->getValue('lng'),
'lat' => $this->getValue('lat'),
);
$geo_field_value = \Drupal::service('geofield.wkt_generator')->WktBuildPoint($point);
$entity->set('field_herb_geo_field', $geo_field_value);
}
} }

Loading…
Cancel
Save