|
|
|
@ -1,38 +1,45 @@ |
|
|
|
<?php |
|
|
|
<?php |
|
|
|
/** |
|
|
|
|
|
|
|
* Created by IntelliJ IDEA. |
|
|
|
|
|
|
|
* User: ppound |
|
|
|
|
|
|
|
* Date: 2018-05-07 |
|
|
|
|
|
|
|
* Time: 10:44 AM |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Drupal\islandora_herbarium_object\DataParser; |
|
|
|
namespace Drupal\islandora_herbarium_object\DataParser; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Drupal\Core\Entity\EntityInterface; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Provides storage and functions for NRCAN data. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
class NrcanParser extends DataParser { |
|
|
|
|
|
|
|
|
|
|
|
class NrcanParser extends DataParser |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
public $nrcanUrlSearchPrefix = 'http://geogratis.gc.ca/services/geoname/en/geonames?q='; |
|
|
|
public $nrcanUrlSearchPrefix = 'http://geogratis.gc.ca/services/geoname/en/geonames?q='; |
|
|
|
|
|
|
|
|
|
|
|
public $nrcanUrlPrefix = 'http://geogratis.gc.ca/services/geoname/en/geonames/'; |
|
|
|
public $nrcanUrlPrefix = 'http://geogratis.gc.ca/services/geoname/en/geonames/'; |
|
|
|
|
|
|
|
|
|
|
|
public function parseData() |
|
|
|
/** |
|
|
|
{ |
|
|
|
* {@inheritdoc} |
|
|
|
$arr = json_decode($this->data, TRUE); |
|
|
|
*/ |
|
|
|
if(empty($arr)) { |
|
|
|
public function parseData() { |
|
|
|
|
|
|
|
$arr = json_decode($this->data, TRUE); |
|
|
|
|
|
|
|
if (empty($arr)) { |
|
|
|
throw new \Exception('Error parsing geo json data.'); |
|
|
|
throw new \Exception('Error parsing geo json data.'); |
|
|
|
} |
|
|
|
} |
|
|
|
$this->data_arr = $arr; |
|
|
|
$this->dataArray = $arr; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function updateEntity($entity) { |
|
|
|
/** |
|
|
|
|
|
|
|
* {@inheritdoc} |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function updateEntity(EntityInterface $entity) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
$url = ''; |
|
|
|
$url = ''; |
|
|
|
$dwciri = $entity->get("field_dwciri_municipality")->getValue(); |
|
|
|
$dwciri = $entity->get("field_dwciri_municipality")->getValue(); |
|
|
|
if(empty($url = $dwciri[0]['value']) && !empty($entity->get("field_dwc_municipality")->getValue())) { |
|
|
|
if (empty($url = $dwciri[0]['value']) && !empty($entity->get("field_dwc_municipality") |
|
|
|
//throw new Exception("No URI provided, cannot look up location data."); |
|
|
|
->getValue())) { |
|
|
|
$value = array($this->getNrcanUrl($entity->get('field_dwc_municipality')->getValue()[0]['value'])); |
|
|
|
$value = [ |
|
|
|
$dwciri[] = ['value' => $value[0]]; |
|
|
|
$this->getNrcanUrl($entity->get('field_dwc_municipality') |
|
|
|
|
|
|
|
->getValue()[0]['value']), |
|
|
|
|
|
|
|
]; |
|
|
|
|
|
|
|
$dwciri[] = ['value' => $value[0]]; |
|
|
|
$url = $dwciri[0]['value']; |
|
|
|
$url = $dwciri[0]['value']; |
|
|
|
if(empty($url)) { |
|
|
|
if (empty($url)) { |
|
|
|
throw new \Exception("No Municipality URI or Municipality text provided, cannot look up taxon data."); |
|
|
|
throw new \Exception("No Municipality URI or Municipality text provided, cannot look up taxon data."); |
|
|
|
} |
|
|
|
} |
|
|
|
$entity->set('field_dwciri_municipality', $url); |
|
|
|
$entity->set('field_dwciri_municipality', $url); |
|
|
|
@ -40,49 +47,62 @@ class NrcanParser extends DataParser |
|
|
|
|
|
|
|
|
|
|
|
$this->getRemoteData($url); |
|
|
|
$this->getRemoteData($url); |
|
|
|
$this->parseData(); |
|
|
|
$this->parseData(); |
|
|
|
} catch (Exception $e) { |
|
|
|
} |
|
|
|
|
|
|
|
catch (Exception $e) { |
|
|
|
watchdog_exception('islandora_herbarium_object', $e, 'Error getting dwciri'); |
|
|
|
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', |
|
|
|
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'); |
|
|
|
['%msg' => $e->getMessage(), '%url' => $dwciri[0]['value']]), 'error'); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
$entity->set('field_dwc_municipality', $this->getValue('name')); |
|
|
|
$entity->set('field_dwc_municipality', $this->getValue('name')); |
|
|
|
// This parser assumes we are in canada |
|
|
|
// This parser assumes we are in canada. |
|
|
|
$entity->set('field_dwc_countrycode', 'CA'); |
|
|
|
$entity->set('field_dwc_countrycode', 'CA'); |
|
|
|
$entity->set('field_dwc_country', 'Canada'); |
|
|
|
$entity->set('field_dwc_country', 'Canada'); |
|
|
|
$entity->set('field_dwc_continent', 'North America'); |
|
|
|
$entity->set('field_dwc_continent', 'North America'); |
|
|
|
$entity->set('field_dwc_decimallatitude', $this->getValue('latitude')); |
|
|
|
$entity->set('field_dwc_decimallatitude', $this->getValue('latitude')); |
|
|
|
$entity->set('field_dwc_decimallongitude', $this->getValue('longitude')); |
|
|
|
$entity->set('field_dwc_decimallongitude', $this->getValue('longitude')); |
|
|
|
$provinceUri = $this->getvalue('province')['links']['self']['href']; |
|
|
|
$provinceUri = $this->getvalue('province')['links']['self']['href']; |
|
|
|
try { |
|
|
|
try { |
|
|
|
$provinceData = $this->getRemoteData($provinceUri, TRUE); |
|
|
|
$provinceData = $this->getRemoteData($provinceUri, TRUE); |
|
|
|
$p_arr = json_decode($provinceData, TRUE); |
|
|
|
$p_arr = json_decode($provinceData, TRUE); |
|
|
|
$province = $p_arr['description']; |
|
|
|
$province = $p_arr['description']; |
|
|
|
} |
|
|
|
} |
|
|
|
catch(Exception $e) { |
|
|
|
catch (Exception $e) { |
|
|
|
drupal_set_message(t('There was an error retrieving Province data. %msg.', |
|
|
|
drupal_set_message(t('There was an error retrieving Province data. %msg.', |
|
|
|
array('%msg'=> $e->getMessage())), 'error'); |
|
|
|
['%msg' => $e->getMessage()]), 'error'); |
|
|
|
} |
|
|
|
} |
|
|
|
$entity->set('field_dwc_stateprovince', $province); |
|
|
|
$entity->set('field_dwc_stateprovince', $province); |
|
|
|
$point = array( |
|
|
|
$point = [ |
|
|
|
'lon' => $this->getValue('longitude'), |
|
|
|
'lon' => $this->getValue('longitude'), |
|
|
|
'lat' => $this->getValue('latitude'), |
|
|
|
'lat' => $this->getValue('latitude'), |
|
|
|
); |
|
|
|
]; |
|
|
|
$geo_field_value = \Drupal::service('geofield.wkt_generator')->WktBuildPoint($point); |
|
|
|
$geo_field_value = \Drupal::service('geofield.wkt_generator') |
|
|
|
|
|
|
|
->WktBuildPoint($point); |
|
|
|
$entity->set('field_herb_geo_field', $geo_field_value); |
|
|
|
$entity->set('field_herb_geo_field', $geo_field_value); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function getNrcanUrl($municipality){ |
|
|
|
/** |
|
|
|
|
|
|
|
* Returns a URL string. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param string $municipality |
|
|
|
|
|
|
|
* The municipality that we want to lookup. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return string |
|
|
|
|
|
|
|
* URL String based on the nrcanUrlPrefix class variable plus a key that |
|
|
|
|
|
|
|
* represents the municipality. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function getNrcanUrl($municipality) { |
|
|
|
$url = $this->nrcanUrlSearchPrefix . $municipality . "&province=11"; |
|
|
|
$url = $this->nrcanUrlSearchPrefix . $municipality . "&province=11"; |
|
|
|
$data = $this->getRemoteData($url, TRUE); |
|
|
|
$data = $this->getRemoteData($url, TRUE); |
|
|
|
$data_arr = json_decode($data, TRUE); |
|
|
|
$data_arr = json_decode($data, TRUE); |
|
|
|
$key = ''; |
|
|
|
$key = ''; |
|
|
|
foreach($data_arr['items'] as $item) { |
|
|
|
foreach ($data_arr['items'] as $item) { |
|
|
|
if($item['name'] === $municipality){ |
|
|
|
if ($item['name'] === $municipality) { |
|
|
|
$key = $item['id']; |
|
|
|
$key = $item['id']; |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return $this->nrcanUrlPrefix . $key; |
|
|
|
return $this->nrcanUrlPrefix . $key; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|