Browse Source

added nrcan parser for locations

master
Paul Pound 9 years ago
parent
commit
d53edb661e
  1. 22
      islandora_herbarium_object.module
  2. 2
      islandora_herbarium_object.services.yml
  3. 8
      src/DataParser/DataParser.php
  4. 5
      src/DataParser/DataParserInterface.php
  5. 62
      src/DataParser/NrcanParser.php

22
islandora_herbarium_object.module

@ -32,17 +32,27 @@ function islandora_herbarium_object_entity_presave(Drupal\Core\Entity\EntityInte
{
if ($entity->bundle() == 'islandora_herbarium_object') {
// Get the geo data.
$geo = \Drupal::service('islandora_herbarium_object.geoparser');
$geo->updateEntity($entity);
try {
$geo = \Drupal::service('islandora_herbarium_object.geoparser');
$geo->updateEntity($entity);
} catch (Exception $e) {
drupal_set_message(t('There was an error retrieving Geo data. %msg.',
array('%msg'=> $e->getMessage())), 'error');
}
// Get the taxonomy data.
$taxon = \Drupal::service('islandora_herbarium_object.taxonparser');
$taxon->updateEntity($entity);
try {
$taxon = \Drupal::service('islandora_herbarium_object.taxonparser');
$taxon->updateEntity($entity);
} catch(Exception $e) {
drupal_set_message(t('There was an error retrieving Taxon data. %msg.',
array('%msg'=> $e->getMessage())), 'error');
}
$title = $taxon->getValue('canonicalName') . ' - ' . $geo->getValue('name');
if(!empty($title)) {
if($title != ' - ') {
$entity->setTitle($title);
}
}
}

2
islandora_herbarium_object.services.yml

@ -2,4 +2,4 @@ services:
islandora_herbarium_object.taxonparser:
class: Drupal\islandora_herbarium_object\DataParser\GbifParser
islandora_herbarium_object.geoparser:
class: Drupal\islandora_herbarium_object\DataParser\GeonamesParser
class: Drupal\islandora_herbarium_object\DataParser\NrcanParser

8
src/DataParser/DataParser.php

@ -14,14 +14,18 @@ abstract class DataParser implements DataParserInterface
public $data;
public $data_arr = array();
public function getRemoteData($uri) {
public function getRemoteData($uri, $returnAsVariable = FALSE) {
$client = \Drupal::httpClient();
$request = $client->get(trim($uri));
$data = $request->getBody()->getContents();
if(empty($data)) {
throw new Exception("Empty dataset");
}
$this->data = $data;
if(!$returnAsVariable) {
$this->data = $data;
} else {
return $data;
}
}
public function getValue($key) {

5
src/DataParser/DataParserInterface.php

@ -16,11 +16,6 @@ interface DataParserInterface
public function parseData();
/**
* Gets the value from an array based on it's key, elimnates warnings if key does not exist.
* @param $arr
* @param $key
*/
public function getValue($key);
public function updateEntity($entity);

62
src/DataParser/NrcanParser.php

@ -0,0 +1,62 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: ppound
* Date: 2018-05-07
* Time: 10:44 AM
*/
namespace Drupal\islandora_herbarium_object\DataParser;
class NrcanParser extends DataParser
{
public function parseData()
{
$arr = json_decode($this->data, TRUE);
if(empty($arr)) {
throw new \Exception('Error parsing geo json data.');
}
$this->data_arr = $arr;
}
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'));
// This parser assumes we are in canada
$entity->set('field_dwc_countrycode', 'CA');
$entity->set('field_dwc_country', 'Canada');
$entity->set('field_dwc_continent', 'North America');
$entity->set('field_dwc_decimallatitude', $this->getValue('latitude'));
$entity->set('field_dwc_decimallongitude', $this->getValue('longitude'));
$provinceUri = $this->getvalue('province')['links']['self']['href'];
try {
$provinceData = $this->getRemoteData($provinceUri, TRUE);
$p_arr = json_decode($provinceData, TRUE);
$province = $p_arr['description'];
}
catch(Exception $e) {
drupal_set_message(t('There was an error retrieving Province data. %msg.',
array('%msg'=> $e->getMessage())), 'error');
}
$entity->set('field_dwc_stateprovince', $province);
$point = array(
'lon' => $this->getValue('longitude'),
'lat' => $this->getValue('latitude'),
);
$geo_field_value = \Drupal::service('geofield.wkt_generator')->WktBuildPoint($point);
$entity->set('field_herb_geo_field', $geo_field_value);
}
}
Loading…
Cancel
Save