From 2d099d61c9f854467d396c10830ce14b92552a52 Mon Sep 17 00:00:00 2001 From: Paul Pound Date: Tue, 3 Jul 2018 11:38:22 -0300 Subject: [PATCH] added ability to graph individual relationships --- islandora_herbarium_object.routing.yml | 8 +- islandora_herbarium_object.services.yml | 2 +- js/fdg.js | 244 ++++++++++++------------ src/Controller/GraphController.php | 7 +- src/DataParser/GeonamesParser.php | 73 ------- src/DataParser/IpniParser.php | 21 -- src/DataParser/NrcanParser.php | 8 +- src/DataParser/XmlDataParser.php | 22 --- src/Graph/FdgGraph.php | 66 ++----- src/Graph/GraphInterface.php | 16 ++ src/Graph/GraphParser.php | 53 +++++ 11 files changed, 224 insertions(+), 296 deletions(-) delete mode 100644 src/DataParser/GeonamesParser.php delete mode 100644 src/DataParser/IpniParser.php delete mode 100644 src/DataParser/XmlDataParser.php create mode 100644 src/Graph/GraphInterface.php create mode 100644 src/Graph/GraphParser.php diff --git a/islandora_herbarium_object.routing.yml b/islandora_herbarium_object.routing.yml index 2cbc51f..bab01b0 100644 --- a/islandora_herbarium_object.routing.yml +++ b/islandora_herbarium_object.routing.yml @@ -1,14 +1,18 @@ islandora_herbarium_object.graph: - path: '/islandora_herbarium_object/fdg' + path: '/islandora_herbarium_object/fdg/{scientificName}/{municipality}' defaults: _controller: '\Drupal\islandora_herbarium_object\Controller\GraphController::printFdgGraph' _title: 'Graph' + scientificName: '' + municipality: '' requirements: _permission: 'access content' islandora_herbarium_object.graph-json: - path: '/islandora_herbarium_object/fdg_json' + path: '/islandora_herbarium_object/fdg_json/{scientificName}/{municipality}' defaults: _controller: '\Drupal\islandora_herbarium_object\Controller\GraphController::printFdgData' _title: 'Graph Json' + scientificName: '' + municipality: '' requirements: _permission: 'access content' diff --git a/islandora_herbarium_object.services.yml b/islandora_herbarium_object.services.yml index 2ce2947..5408e72 100644 --- a/islandora_herbarium_object.services.yml +++ b/islandora_herbarium_object.services.yml @@ -4,4 +4,4 @@ services: islandora_herbarium_object.geoparser: class: Drupal\islandora_herbarium_object\DataParser\NrcanParser islandora_herbarium_object.fdggraph: - class: Drupal\islandora_herbarium_object\Graph\FdgGraph + class: Drupal\islandora_herbarium_object\Graph\FdgGraph diff --git a/js/fdg.js b/js/fdg.js index 26ffe00..17d97d6 100644 --- a/js/fdg.js +++ b/js/fdg.js @@ -1,124 +1,128 @@ -var svg = d3.select(".fdg"), - width = +svg.attr("width"), - height = +svg.attr("height"); - -var color = d3.scaleOrdinal(d3.schemeCategory20); - -var simulation = d3.forceSimulation() - .force("link", d3.forceLink().id(function (d) { - return d.id; - })) - .force("charge", d3.forceManyBody()) - .force("center", d3.forceCenter(width / 2, height / 2)); - -d3.json("/islandora_herbarium_object/fdg_json", function (error, graph) { - if (error) throw error; - - var link = svg.append("g") - .attr("class", "links") - .selectAll("line") - .data(graph.links) - .enter().append("line") - .attr("stroke-width", function (d) { - return Math.sqrt(d.value); +Drupal.behaviors.islandora_herbarium_object_graph = { + attach: function (context, settings) { + var svg = d3.select(".fdg"), + width = +svg.attr("width"), + height = +svg.attr("height"); + + var color = d3.scaleOrdinal(d3.schemeCategory20); + + var simulation = d3.forceSimulation() + .force("link", d3.forceLink().id(function (d) { + return d.id; + })) + .force("charge", d3.forceManyBody()) + .force("center", d3.forceCenter(width / 2, height / 2)); + var dataPath = "/" + settings.path.currentPath.replace('fdg', 'fdg_json'); + d3.json(dataPath, function (error, graph) { + if (error) throw error; + + var link = svg.append("g") + .attr("class", "links") + .selectAll("line") + .data(graph.links) + .enter().append("line") + .attr("stroke-width", function (d) { + return Math.sqrt(d.value); + }); + + var node = svg.append("g") + .attr("class", "nodes") + .selectAll("circle") + .data(graph.nodes) + .enter().append("circle") + .attr("r", 12) + .attr("fill", function (d) { + return color(d.group); + }) + .call(d3.drag() + .on("start", dragstarted) + .on("drag", dragged) + .on("end", dragended)); + + node.append("title") + .text(function (d) { + return d.id; + }); + + var uriElement = svg.append('g') + .attr('class', 'uri') + .attr('x', 8) + .attr('y', '.31em') + .selectAll('uri') + .data(graph.nodes) + .enter().append('a') + .attr("target", "_blank") + .attr('xlink:href', function (d) { + return d.uri + }) + .append('text') + .text(function (d) { + return d.name; + }) + .attr('font-size', 10) + .attr('font-weight', 'bolder'); + + + simulation + .nodes(graph.nodes) + .on("tick", ticked); + + simulation.force("link") + .links(graph.links); + + + function ticked() { + link + .attr("x1", function (d) { + return d.source.x; + }) + .attr("y1", function (d) { + return d.source.y; + }) + .attr("x2", function (d) { + return d.target.x; + }) + .attr("y2", function (d) { + return d.target.y; + }); + + node + .attr("cx", function (d) { + return d.x; + }) + .attr("cy", function (d) { + return d.y; + }); + + //textElement + // .attr("dx", function(d) {return d.x}) + //.attr("dy", function(d) { return d.y; }); + + uriElement + .attr("dx", function (d) { + return d.x + }) + .attr("dy", function (d) { + return d.y; + }); + } }); - var node = svg.append("g") - .attr("class", "nodes") - .selectAll("circle") - .data(graph.nodes) - .enter().append("circle") - .attr("r", 12) - .attr("fill", function (d) { - return color(d.group); - }) - .call(d3.drag() - .on("start", dragstarted) - .on("drag", dragged) - .on("end", dragended)); - - node.append("title") - .text(function (d) { - return d.id; - }); - - var uriElement = svg.append('g') - .attr('class', 'uri') - .attr('x', 8) - .attr('y', '.31em') - .selectAll('uri') - .data(graph.nodes) - .enter().append('a') - .attr("target", "_blank") - .attr('xlink:href', function (d) { - return d.uri - }) - .append('text') - .text(function (d) { - return d.name; - }) - .attr('font-size', 10) - .attr('font-weight', 'bolder'); - - - simulation - .nodes(graph.nodes) - .on("tick", ticked); - - simulation.force("link") - .links(graph.links); - - - function ticked() { - link - .attr("x1", function (d) { - return d.source.x; - }) - .attr("y1", function (d) { - return d.source.y; - }) - .attr("x2", function (d) { - return d.target.x; - }) - .attr("y2", function (d) { - return d.target.y; - }); - - node - .attr("cx", function (d) { - return d.x; - }) - .attr("cy", function (d) { - return d.y; - }); - - //textElement - // .attr("dx", function(d) {return d.x}) - //.attr("dy", function(d) { return d.y; }); - - uriElement - .attr("dx", function (d) { - return d.x - }) - .attr("dy", function (d) { - return d.y; - }); + function dragstarted(d) { + if (!d3.event.active) simulation.alphaTarget(0.3).restart(); + d.fx = d.x; + d.fy = d.y; + } + + function dragged(d) { + d.fx = d3.event.x; + d.fy = d3.event.y; + } + + function dragended(d) { + if (!d3.event.active) simulation.alphaTarget(0); + d.fx = null; + d.fy = null; + } } -}); - -function dragstarted(d) { - if (!d3.event.active) simulation.alphaTarget(0.3).restart(); - d.fx = d.x; - d.fy = d.y; -} - -function dragged(d) { - d.fx = d3.event.x; - d.fy = d3.event.y; -} - -function dragended(d) { - if (!d3.event.active) simulation.alphaTarget(0); - d.fx = null; - d.fy = null; } diff --git a/src/Controller/GraphController.php b/src/Controller/GraphController.php index 8048775..205a96c 100644 --- a/src/Controller/GraphController.php +++ b/src/Controller/GraphController.php @@ -15,7 +15,7 @@ class GraphController extends ControllerBase { * @return array * A renderable array. */ - public function printFdgGraph() { + public function printFdgGraph($scientificName = NULL, $municipality = NULL) { return [ '#theme' => 'fdg_graph', '#attached' => [ @@ -30,9 +30,12 @@ class GraphController extends ControllerBase { /** * 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->getData($scientificName, $municipality); $graphJson = $fdgGraph->parseData(); print $graphJson; exit(); diff --git a/src/DataParser/GeonamesParser.php b/src/DataParser/GeonamesParser.php deleted file mode 100644 index 7ac2874..0000000 --- a/src/DataParser/GeonamesParser.php +++ /dev/null @@ -1,73 +0,0 @@ -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); - } - -} diff --git a/src/DataParser/IpniParser.php b/src/DataParser/IpniParser.php deleted file mode 100644 index bf36161..0000000 --- a/src/DataParser/IpniParser.php +++ /dev/null @@ -1,21 +0,0 @@ -loadXML($this->data); - $this->getXmlNodeValue($xml_doc, 'nameComplete'); - $this->getXmlNodeValue($xml_doc, 'genusPart'); - $this->getXmlNodeValue($xml_doc, 'specificEpithet'); - } - -} diff --git a/src/DataParser/NrcanParser.php b/src/DataParser/NrcanParser.php index 0538b53..56c7c40 100644 --- a/src/DataParser/NrcanParser.php +++ b/src/DataParser/NrcanParser.php @@ -86,13 +86,17 @@ class NrcanParser extends DataParser { * * @param string $municipality * The municipality that we want to lookup. + * @param string $province + * Limit a municipality to a province. + * Defaults to 11 (Prince Edward Island). * * @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"; + public function getNrcanUrl($municipality, $province='11') { + $url = $this->nrcanUrlSearchPrefix . $municipality . '&province=' . + $province; $data = $this->getRemoteData($url, TRUE); $data_arr = json_decode($data, TRUE); $key = ''; diff --git a/src/DataParser/XmlDataParser.php b/src/DataParser/XmlDataParser.php deleted file mode 100644 index 74504d8..0000000 --- a/src/DataParser/XmlDataParser.php +++ /dev/null @@ -1,22 +0,0 @@ -dataArray[$element_name] = $xml_doc->getElementsByTagName($element_name)[0]->nodeValue; - } - -} diff --git a/src/Graph/FdgGraph.php b/src/Graph/FdgGraph.php index 1350c7d..e715d10 100644 --- a/src/Graph/FdgGraph.php +++ b/src/Graph/FdgGraph.php @@ -11,66 +11,26 @@ use Drupal\Core\Url; * * @package Drupal\islandora_herbarium_object\Graph */ -class FdgGraph { +class FdgGraph extends GraphParser { 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. */ - public function getData() { - $sparqlQuery = 'SELECT DISTINCT ?municipalityIRI ?scientificNameIRI ?name ?municipality where { ?s ?municipalityIRI . + public function getData($scientificName = NULL, $municipality = NULL) { + $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 ?municipalityIRI . ?s ?scientificNameIRI . - ?s ?name. - ?s ?municipality - }'; - + ?s ?name . + ?s ?municipality ' . + $scientificName . ' ' . $municipality . ' ' . + '}'; + // TODO: discover or store sparql endpoint somehow. $uri = 'http://localhost:8080/bigdata/namespace/islandora/sparql'; $options = ['query' => ['query' => $sparqlQuery]]; $url = Url::fromUri($uri, $options); diff --git a/src/Graph/GraphInterface.php b/src/Graph/GraphInterface.php new file mode 100644 index 0000000..db373e7 --- /dev/null +++ b/src/Graph/GraphInterface.php @@ -0,0 +1,16 @@ +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); + } + +}