11 changed files with 224 additions and 296 deletions
@ -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' |
||||||
|
|||||||
@ -1,124 +1,128 @@ |
|||||||
var svg = d3.select(".fdg"), |
Drupal.behaviors.islandora_herbarium_object_graph = { |
||||||
width = +svg.attr("width"), |
attach: function (context, settings) { |
||||||
height = +svg.attr("height"); |
var svg = d3.select(".fdg"), |
||||||
|
width = +svg.attr("width"), |
||||||
var color = d3.scaleOrdinal(d3.schemeCategory20); |
height = +svg.attr("height"); |
||||||
|
|
||||||
var simulation = d3.forceSimulation() |
var color = d3.scaleOrdinal(d3.schemeCategory20); |
||||||
.force("link", d3.forceLink().id(function (d) { |
|
||||||
return d.id; |
var simulation = d3.forceSimulation() |
||||||
})) |
.force("link", d3.forceLink().id(function (d) { |
||||||
.force("charge", d3.forceManyBody()) |
return d.id; |
||||||
.force("center", d3.forceCenter(width / 2, height / 2)); |
})) |
||||||
|
.force("charge", d3.forceManyBody()) |
||||||
d3.json("/islandora_herbarium_object/fdg_json", function (error, graph) { |
.force("center", d3.forceCenter(width / 2, height / 2)); |
||||||
if (error) throw error; |
var dataPath = "/" + settings.path.currentPath.replace('fdg', 'fdg_json'); |
||||||
|
d3.json(dataPath, function (error, graph) { |
||||||
var link = svg.append("g") |
if (error) throw error; |
||||||
.attr("class", "links") |
|
||||||
.selectAll("line") |
var link = svg.append("g") |
||||||
.data(graph.links) |
.attr("class", "links") |
||||||
.enter().append("line") |
.selectAll("line") |
||||||
.attr("stroke-width", function (d) { |
.data(graph.links) |
||||||
return Math.sqrt(d.value); |
.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") |
function dragstarted(d) { |
||||||
.attr("class", "nodes") |
if (!d3.event.active) simulation.alphaTarget(0.3).restart(); |
||||||
.selectAll("circle") |
d.fx = d.x; |
||||||
.data(graph.nodes) |
d.fy = d.y; |
||||||
.enter().append("circle") |
} |
||||||
.attr("r", 12) |
|
||||||
.attr("fill", function (d) { |
function dragged(d) { |
||||||
return color(d.group); |
d.fx = d3.event.x; |
||||||
}) |
d.fy = d3.event.y; |
||||||
.call(d3.drag() |
} |
||||||
.on("start", dragstarted) |
|
||||||
.on("drag", dragged) |
function dragended(d) { |
||||||
.on("end", dragended)); |
if (!d3.event.active) simulation.alphaTarget(0); |
||||||
|
d.fx = null; |
||||||
node.append("title") |
d.fy = null; |
||||||
.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; |
|
||||||
} |
} |
||||||
|
|||||||
@ -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); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -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'); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -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; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -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(); |
||||||
|
|
||||||
|
} |
||||||
@ -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…
Reference in new issue