Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Spatial Features #146

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "everyman/neo4jphp",
"name": "tomcorbett/neo4jphp",
"type": "library",
"description": "Wrapper for the Neo4j graph database REST interface",
"keywords": ["neo4j","graph","database"],
"homepage": "http://github.com/jadell/neo4jphp/wiki",
"license": "MIT",
"authors": [
{"name": "Josh Adell", "email": "[email protected]", "homepage": "http://joshadell.com"}
{"name": "Josh Adell", "email": "[email protected]", "homepage": "http://joshadell.com"},
{"name": "Thomas Corbett", "email": "[email protected]"}
],
"require": {
"php": ">=5.3.0",
Expand All @@ -19,5 +20,6 @@
},
"autoload": {
"psr-0": {"Everyman\\Neo4j":"lib/"}
}
},
"version": "1.0.1-beta"
}
81 changes: 81 additions & 0 deletions lib/Everyman/Neo4j/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Client
const CapabilityGremlin = 'gremlin';
const CapabilityLabel = 'label';
const CapabilityTransactions = 'transactions';
const CapabilitySpatialPlugin = 'spatial';

protected $transport = null;
protected $entityMapper = null;
Expand Down Expand Up @@ -504,6 +505,12 @@ public function hasCapability($capability)
return $info['extensions']['GremlinPlugin']['execute_script'];
}
return false;

case self::CapabilitySpatialPlugin:
if (isset($info['extensions']['SpatialPlugin'])) {
return true;
}
return false;

default:
return false;
Expand Down Expand Up @@ -726,6 +733,80 @@ public function searchIndex(Index $index, $key, $value)
return $this->runCommand($command);
}

/**
* Create a new simple point layer
*
* @param string $layer
* @param string $lat
* @param string $lon
* @return \Everyman\Neo4j\SpatialLayer\SimplePointLayer
*/
public function makeSimplePointLayer($layer, $lat = null, $lon = null)
{
$simplePointLayer = new SpatialLayer\SimplePointLayer($this, $layer, $lat, $lon);
return $simplePointLayer;
}

/**
* Save the given layer
*
* @param SpatialLayer $layer
* @return boolean
*/
public function saveLayer(SpatialLayer $layer)
{
return $this->runCommand(new Command\CreateSimplePointLayer($this, $layer));
}

/**
* Add an entity to a Layer
*
* @param SpatialLayer $layer
* @param PropertyContainer $entity
* @return boolean
*/
public function addToLayer(SpatialLayer $layer, PropertyContainer $entity)
{
// @todo implement batching support
return $this->runCommand(new Command\AddToLayer($this, $layer, $entity));
}

/**
* Search for nodes within a specified distance of X and Y
*
* @param \Everyman\Neo4j\SpatialLayer $layer
* @param float $pointX
* @param float $pointY
* @param float $distance
* @return array
*/
public function findNodesWithinDistance(SpatialLayer $layer, $pointX, $pointY, $distance)
{
return $this->runCommand(new Command\FindNodesWithinDistance($this, $layer, $pointX, $pointY, $distance));
}

/**
* Search for nodes within a specified bounding box area
*
* @param \Everyman\Neo4j\SpatialLayer $layer
* @param type $pointX
* @param type $pointY
* @param type $distance
* @return type
*/
public function findNodesInBBox(SpatialLayer $layer, $minX, $maxX, $minY, $maxY)
{
return $this->runCommand(new Command\FindNodesInBBox($this, $layer, $minX, $maxX, $minY, $maxY));
}









/**
* Set the cache to use
*
Expand Down
88 changes: 88 additions & 0 deletions lib/Everyman/Neo4j/Command/AddToLayer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
namespace Everyman\Neo4j\Command;

use Everyman\Neo4j\Command,
Everyman\Neo4j\Client,
Everyman\Neo4j\Exception,
Everyman\Neo4j\PropertyContainer,
Everyman\Neo4j\SpatialLayer;

/**
* Add an entity to an a spatial layer
*/
class AddToLayer extends Command
{
protected $layer = null;
protected $entity = null;
protected $key = null;
protected $value = null;

/**
* Set the layer to drive the command
*
* @param Client $client
* @param SpatialLayer $layer
* @param PropertyContainer $entity
* @param string $key
* @param string $value
*/
public function __construct(Client $client, SpatialLayer $layer, PropertyContainer $entity)
{
parent::__construct($client);
$this->layer = $layer;
$this->entity = $entity;
}

/**
* Return the data to pass
*
* @return mixed
*/
protected function getData()
{
if (!$this->entity || !$this->entity->hasId()) {
throw new Exception('No entity to add to layer specified');
}

return array(
'node' => $this->getTransport()->getEndpoint().'/'. 'node' .'/'.$this->entity->getId(),
'layer' => $this->layer->getName()
);
}

/**
* Return the transport method to call
*
* @return string
*/
protected function getMethod()
{
return 'post';
}

/**
* Return the path to use
*
* @return string
*/
protected function getPath()
{
return '/ext/SpatialPlugin/graphdb/addNodeToLayer';
}

/**
* Use the results
*
* @param integer $code
* @param array $headers
* @param array $data
* @return integer on failure
*/
protected function handleResult($code, $headers, $data)
{
if ((int)($code / 100) != 2) {
$this->throwException('Unable to add entity to spatial layer', $code, $headers, $data);
}
return true;
}
}
104 changes: 104 additions & 0 deletions lib/Everyman/Neo4j/Command/CreateSimplePointLayer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
namespace Everyman\Neo4j\Command;

use Everyman\Neo4j\Command,
Everyman\Neo4j\Client,
Everyman\Neo4j\SpatialLayer\SimplePointLayer;

/**
* Create a SimplePointLayer
*/
class CreateSimplePointLayer extends Command
{
protected $simplePointLayer = null;

/**
* Set the simplePointLayer to drive the command
*
* @param Client $client
* @param Node $node
*/
public function __construct(Client $client, SimplePointLayer $simplePointLayer)
{
parent::__construct($client);
$this->simplePointLayer = $simplePointLayer;
}

protected function getData()
{
return array(
'layer' => $this->getName(),
'lat' => $this->getLat(),
'lon' => $this->getLon(),
);
}

/**
* Return the name of the layer
*
* @return string
*/
protected function getName()
{
return $this->simplePointLayer->getName() ?: null;
}

/**
*
* @return string
*/
protected function getLat()
{
return $this->simplePointLayer->getLat() ?: null;
}

/**
*
* @return string
*/
protected function getLon()
{
return $this->simplePointLayer->getLon() ?: null;
}

/**
* Return the transport method to call
*
* @return string
*/
protected function getMethod()
{
return 'post';
}

/**
* Return the path to use
*
* @return string
*/
protected function getPath()
{
return '/ext/SpatialPlugin/graphdb/addSimplePointLayer';
}

/**
* Use the results
*
* @param integer $code
* @param array $headers
* @param array $data
* @return boolean true on success
* @throws Exception on failure
*/
protected function handleResult($code, $headers, $data)
{
if ((int)($code / 100) != 2) {
$this->throwException('Unable to create SimplePointLayer', $code, $headers, $data);
}

//$nodeId = $this->getEntityMapper()->getIdFromUri($headers['Location']);
//$this->node->setId($nodeId);
//$this->getEntityCache()->setCachedEntity($this->node);
return true;
}
}
Loading