Browse Source

added ability to graph individual relationships

master
Paul Pound 9 years ago
parent
commit
2d099d61c9
  1. 8
      islandora_herbarium_object.routing.yml
  2. 26
      js/fdg.js
  3. 7
      src/Controller/GraphController.php
  4. 73
      src/DataParser/GeonamesParser.php
  5. 21
      src/DataParser/IpniParser.php
  6. 8
      src/DataParser/NrcanParser.php
  7. 22
      src/DataParser/XmlDataParser.php
  8. 66
      src/Graph/FdgGraph.php
  9. 16
      src/Graph/GraphInterface.php
  10. 53
      src/Graph/GraphParser.php

8
islandora_herbarium_object.routing.yml

@ -1,14 +1,18 @@
islandora_herbarium_object.graph: islandora_herbarium_object.graph:
path: '/islandora_herbarium_object/fdg' path: '/islandora_herbarium_object/fdg/{scientificName}/{municipality}'
defaults: defaults:
_controller: '\Drupal\islandora_herbarium_object\Controller\GraphController::printFdgGraph' _controller: '\Drupal\islandora_herbarium_object\Controller\GraphController::printFdgGraph'
_title: 'Graph' _title: 'Graph'
scientificName: ''
municipality: ''
requirements: requirements:
_permission: 'access content' _permission: 'access content'
islandora_herbarium_object.graph-json: islandora_herbarium_object.graph-json:
path: '/islandora_herbarium_object/fdg_json' path: '/islandora_herbarium_object/fdg_json/{scientificName}/{municipality}'
defaults: defaults:
_controller: '\Drupal\islandora_herbarium_object\Controller\GraphController::printFdgData' _controller: '\Drupal\islandora_herbarium_object\Controller\GraphController::printFdgData'
_title: 'Graph Json' _title: 'Graph Json'
scientificName: ''
municipality: ''
requirements: requirements:
_permission: 'access content' _permission: 'access content'

26
js/fdg.js

@ -1,17 +1,19 @@
var svg = d3.select(".fdg"), Drupal.behaviors.islandora_herbarium_object_graph = {
attach: function (context, settings) {
var svg = d3.select(".fdg"),
width = +svg.attr("width"), width = +svg.attr("width"),
height = +svg.attr("height"); height = +svg.attr("height");
var color = d3.scaleOrdinal(d3.schemeCategory20); var color = d3.scaleOrdinal(d3.schemeCategory20);
var simulation = d3.forceSimulation() var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function (d) { .force("link", d3.forceLink().id(function (d) {
return d.id; return d.id;
})) }))
.force("charge", d3.forceManyBody()) .force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2)); .force("center", d3.forceCenter(width / 2, height / 2));
var dataPath = "/" + settings.path.currentPath.replace('fdg', 'fdg_json');
d3.json("/islandora_herbarium_object/fdg_json", function (error, graph) { d3.json(dataPath, function (error, graph) {
if (error) throw error; if (error) throw error;
var link = svg.append("g") var link = svg.append("g")
@ -104,21 +106,23 @@ d3.json("/islandora_herbarium_object/fdg_json", function (error, graph) {
return d.y; return d.y;
}); });
} }
}); });
function dragstarted(d) { function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart(); if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x; d.fx = d.x;
d.fy = d.y; d.fy = d.y;
} }
function dragged(d) { function dragged(d) {
d.fx = d3.event.x; d.fx = d3.event.x;
d.fy = d3.event.y; d.fy = d3.event.y;
} }
function dragended(d) { function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0); if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null; d.fx = null;
d.fy = null; d.fy = null;
}
}
} }

7
src/Controller/GraphController.php

@ -15,7 +15,7 @@ class GraphController extends ControllerBase {
* @return array * @return array
* A renderable array. * A renderable array.
*/ */
public function printFdgGraph() { public function printFdgGraph($scientificName = NULL, $municipality = NULL) {
return [ return [
'#theme' => 'fdg_graph', '#theme' => 'fdg_graph',
'#attached' => [ '#attached' => [
@ -30,9 +30,12 @@ class GraphController extends ControllerBase {
/** /**
* Directly outputs the json for calls from javascript. * Directly outputs the json for calls from javascript.
*
* TODO: expose as a proper rest endpoint
*/ */
public function printFdgData() { public function printFdgData($scientificName = NULL, $municipality = NULL) {
$fdgGraph = \Drupal::service('islandora_herbarium_object.fdggraph'); $fdgGraph = \Drupal::service('islandora_herbarium_object.fdggraph');
$fdgGraph->getData($scientificName, $municipality);
$graphJson = $fdgGraph->parseData(); $graphJson = $fdgGraph->parseData();
print $graphJson; print $graphJson;
exit(); exit();

73
src/DataParser/GeonamesParser.php

@ -1,73 +0,0 @@
<?php
namespace Drupal\islandora_herbarium_object\DataParser;
/**
* Provides storage and functions for GeoName data.
*/
class GeonamesParser extends XmlDataParser {
/**
* {@inheritdoc}
*/
public function parseData() {
$xml_doc = new \DOMDocument();
if (empty($xml_doc->loadXML($this->data))) {
throw new \Exception('Failed loading data as xml');
}
$this->getXmlNodeValue($xml_doc, 'toponymName');
$this->getXmlNodeValue($xml_doc, 'name');
$this->getXmlNodeValue($xml_doc, 'lat');
$this->getXmlNodeValue($xml_doc, 'lng');
$this->getXmlNodeValue($xml_doc, 'geonameId');
$this->getXmlNodeValue($xml_doc, 'countryCode');
$this->getXmlNodeValue($xml_doc, 'countryName');
$this->getXmlNodeValue($xml_doc, 'fcodeName');
$this->getXmlNodeValue($xml_doc, 'toponymName');
$this->getXmlNodeValue($xml_doc, 'population');
$this->getXmlNodeValue($xml_doc, 'asciiName');
$this->getXmlNodeValue($xml_doc, 'elevation');
$this->getXmlNodeValue($xml_doc, 'continentCode');
$this->getXmlNodeValue($xml_doc, 'adminName1');
$this->getXmlNodeValue($xml_doc, 'timezone');
$this->getXmlNodeValue($xml_doc, 'west');
$this->getXmlNodeValue($xml_doc, 'north');
$this->getXmlNodeValue($xml_doc, 'east');
$this->getXmlNodeValue($xml_doc, 'south');
}
/**
* {@inheritdoc}
*/
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',
['%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 = [
'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);
}
}

21
src/DataParser/IpniParser.php

@ -1,21 +0,0 @@
<?php
namespace Drupal\islandora_herbarium_object\DataParser;
/**
* Provides storage and functions for IPNI data.
*/
abstract class IpniParser extends XmlDataParser {
/**
* {@inheritdoc}
*/
public function parseData() {
$xml_doc = new \DOMDocument();
$xml_doc->loadXML($this->data);
$this->getXmlNodeValue($xml_doc, 'nameComplete');
$this->getXmlNodeValue($xml_doc, 'genusPart');
$this->getXmlNodeValue($xml_doc, 'specificEpithet');
}
}

8
src/DataParser/NrcanParser.php

@ -86,13 +86,17 @@ class NrcanParser extends DataParser {
* *
* @param string $municipality * @param string $municipality
* The municipality that we want to lookup. * The municipality that we want to lookup.
* @param string $province
* Limit a municipality to a province.
* Defaults to 11 (Prince Edward Island).
* *
* @return string * @return string
* URL String based on the nrcanUrlPrefix class variable plus a key that * URL String based on the nrcanUrlPrefix class variable plus a key that
* represents the municipality. * represents the municipality.
*/ */
public function getNrcanUrl($municipality) { public function getNrcanUrl($municipality, $province='11') {
$url = $this->nrcanUrlSearchPrefix . $municipality . "&province=11"; $url = $this->nrcanUrlSearchPrefix . $municipality . '&province=' .
$province;
$data = $this->getRemoteData($url, TRUE); $data = $this->getRemoteData($url, TRUE);
$data_arr = json_decode($data, TRUE); $data_arr = json_decode($data, TRUE);
$key = ''; $key = '';

22
src/DataParser/XmlDataParser.php

@ -1,22 +0,0 @@
<?php
namespace Drupal\islandora_herbarium_object\DataParser;
/**
* Provides functions for simple XML data.
*/
abstract class XmlDataParser extends DataParser {
/**
* Retrieves the value of the first xml node matching element_name.
*
* @param \DOMDocument $xml_doc
* An xml document.
* @param string $element_name
* The xml node to lookup.
*/
public function getXmlNodeValue(\DOMDocument $xml_doc, $element_name) {
$this->dataArray[$element_name] = $xml_doc->getElementsByTagName($element_name)[0]->nodeValue;
}
}

66
src/Graph/FdgGraph.php

@ -11,66 +11,26 @@ use Drupal\Core\Url;
* *
* @package Drupal\islandora_herbarium_object\Graph * @package Drupal\islandora_herbarium_object\Graph
*/ */
class FdgGraph { class FdgGraph extends GraphParser {
protected $data; protected $data;
/**
* Constructor.
*/
public function __construct() {
$this->getData();
}
/**
* Parse the data retrieved from the triple store for use in a d3 graph.
*
* @return string
* A json encode string.
*/
public function parseData() {
$jsonArr = json_decode($this->data, TRUE);
$outputArr = [];
// $nodes = $links = new \Ds\Set();
$outputArr['nodes'] = [];
$outputArr['links'] = [];
$idArr = [];
foreach ($jsonArr['results']['bindings'] as $result) {
if (!array_key_exists($result['municipality']['value'], $idArr)) {
$outputArr['nodes'][] = [
'id' => $result['municipality']['value'],
'name' => $result['municipality']['value'],
'uri' => $result['municipalityIRI']['value'],
'group' => 1,
];
$idArr[$result['municipality']['value']] = '';
}
$outputArr['nodes'][] = [
'id' => $result['name']['value'],
'name' => $result['name']['value'],
'uri' => $result['scientificNameIRI']['value'],
'group' => 2,
];
$outputArr['links'][] = [
'target' => $result['municipality']['value'],
'source' => $result['name']['value'],
'type' => 'found',
];
}
return json_encode($outputArr);
}
/** /**
* Retrieve data from the triple store. * Retrieve data from the triple store.
*/ */
public function getData() { public function getData($scientificName = NULL, $municipality = NULL) {
$sparqlQuery = 'SELECT DISTINCT ?municipalityIRI ?scientificNameIRI ?name ?municipality where { ?s <dwciri:municipality> ?municipalityIRI . $scientificName = ($scientificName == NULL || $scientificName == 'NULL') ? '' :
'Filter (?name="' . $scientificName . '"^^xsd:string)';
$municipality = ($municipality == NULL || $municipality == 'NULL') ? '' :
'Filter (?municipality="' . $municipality . '"^^xsd:string)';
$sparqlQuery = 'SELECT DISTINCT ?municipalityIRI ?scientificNameIRI ?name ?municipality where {
?s <dwciri:municipality> ?municipalityIRI .
?s <dwciri:scientificName> ?scientificNameIRI . ?s <dwciri:scientificName> ?scientificNameIRI .
?s <dwc:scientificName> ?name. ?s <dwc:scientificName> ?name .
?s <dwc:municipality> ?municipality ?s <dwc:municipality> ?municipality ' .
}'; $scientificName . ' ' . $municipality . ' ' .
'}';
// TODO: discover or store sparql endpoint somehow.
$uri = 'http://localhost:8080/bigdata/namespace/islandora/sparql'; $uri = 'http://localhost:8080/bigdata/namespace/islandora/sparql';
$options = ['query' => ['query' => $sparqlQuery]]; $options = ['query' => ['query' => $sparqlQuery]];
$url = Url::fromUri($uri, $options); $url = Url::fromUri($uri, $options);

16
src/Graph/GraphInterface.php

@ -0,0 +1,16 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: ppound
* Date: 2018-06-29
* Time: 10:54 AM
*/
namespace Drupal\islandora_herbarium_object\Graph;
interface GraphInterface {
public function parseData();
}

53
src/Graph/GraphParser.php

@ -0,0 +1,53 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: ppound
* Date: 2018-06-29
* Time: 11:00 AM
*/
namespace Drupal\islandora_herbarium_object\Graph;
class GraphParser implements GraphInterface {
/**
* Parse the data retrieved from the triple store for use in a d3 graph.
*
* @return string
* A json encode string.
*/
public function parseData() {
$jsonArr = json_decode($this->data, TRUE);
$outputArr = [];
// $nodes = $links = new \Ds\Set();
$outputArr['nodes'] = [];
$outputArr['links'] = [];
$idArr = [];
foreach ($jsonArr['results']['bindings'] as $result) {
if (!array_key_exists($result['municipality']['value'], $idArr)) {
$outputArr['nodes'][] = [
'id' => $result['municipality']['value'],
'name' => $result['municipality']['value'],
'uri' => $result['municipalityIRI']['value'],
'group' => 1,
];
$idArr[$result['municipality']['value']] = '';
}
$outputArr['nodes'][] = [
'id' => $result['name']['value'],
'name' => $result['name']['value'],
'uri' => $result['scientificNameIRI']['value'],
'group' => 2,
];
$outputArr['links'][] = [
'target' => $result['municipality']['value'],
'source' => $result['name']['value'],
'type' => 'found',
];
}
return json_encode($outputArr);
}
}
Loading…
Cancel
Save