diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index 05da35c..57fa869 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -28,6 +28,7 @@ require 'neography/rest/batch' require 'neography/rest/clean' require 'neography/rest/transactions' +require 'neography/rest/spatial' require 'neography/errors' @@ -69,6 +70,7 @@ def initialize(options = ENV['NEO4J_URL'] || {}) @batch = Batch.new(@connection) @clean = Clean.new(@connection) @transactions = Transactions.new(@connection) + @spatial = Spatial.new(@connection) end # meta-data @@ -452,6 +454,16 @@ def batch(*args) def batch_not_streaming(*args) @batch.not_streaming(*args) end + + # spatial + + def get_spatial + @spatial.index + end + + def add_point_layer(layer, lat = nil, lon = nil) + @spatial.add_point_layer(layer, lat, lon) + end # clean database diff --git a/lib/neography/rest/spatial.rb b/lib/neography/rest/spatial.rb new file mode 100644 index 0000000..a746e7b --- /dev/null +++ b/lib/neography/rest/spatial.rb @@ -0,0 +1,54 @@ +module Neography + class Rest + class Spatial + extend Neography::Rest::Paths + include Neography::Rest::Helpers + + add_path :index, "/ext/SpatialPlugin" + add_path :add_simple_point_layer, "/ext/SpatialPlugin/graphdb/addSimplePointLayer" + add_path :add_editable_layer, "/ext/SpatialPlugin/graphdb/addEditableLayer" + add_path :get_layer, "/ext/SpatialPlugin/graphdb/getLayer" + add_path :add_geometry_to_layer, "/ext/SpatialPlugin/graphdb/addGeometryWKTToLayer" + add_path :edit_geometry_from_layer, "/ext/SpatialPlugin/graphdb/updateGeometryFromWKT" + add_path :add_node_to_layer, "/ext/SpatialPlugin/graphdb/addNodeToLayer" + add_path :find_geometries_in_bbox, "/ext/SpatialPlugin/graphdb/findGeometriesInBBox" + add_path :find_geometries_within_distance,"/ext/SpatialPlugin/graphdb/findGeometriesWithinDistance" + + def initialize(connection) + @connection = connection + end + + def index + @connection.get(index_path) + end + + def add_point_layer(layer, lat, lon) + options = { + :body => { + :layer => layer, + :lat => lat || "lat", + :lon => lon || "lon" + }.to_json, + :headers => json_content_type.merge({'Accept' => 'application/json;charset=UTF-8'}) + } + + @connection.post(add_simple_point_layer_path, options) + end + + def add_editable_layer(layer, format = "WKT", node_property_name = "wkt") + options = { + :body => { + :layer => name, + :format => format, + :nodePropertyName => node_property_name + }.to_json, + :headers => json_content_type.merge({'Accept' => 'application/json;charset=UTF-8'}) + } + + @connection.post(add_editable_layer_path, options) + end + + + end + end +end \ No newline at end of file diff --git a/spec/integration/rest_spatial_spec.rb b/spec/integration/rest_spatial_spec.rb new file mode 100644 index 0000000..e1d37af --- /dev/null +++ b/spec/integration/rest_spatial_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +describe Neography::Rest do + before(:each) do + @neo = Neography::Rest.new + end + + describe "find the spatial plugin" do + it "can get a description of the spatial plugin" do + si = @neo.get_spatial + si.should_not be_nil + si["graphdb"]["addEditableLayer"].should_not be_nil + end + end + + describe "add a point layer" do + it "can add a simple point layer" do + pl = @neo.add_point_layer("restaurants") + pl.should_not be_nil + pl.first["data"]["layer"].should == "restaurants" + pl.first["data"]["geomencoder_config"].should == "lon:lat" + end + + it "can add a simple point layer with lat and long" do + pl = @neo.add_point_layer("coffee_shops", "latitude", "longitude") + pl.should_not be_nil + pl.first["data"]["layer"].should == "coffee_shops" + pl.first["data"]["geomencoder_config"].should == "longitude:latitude" + end + + end + +end \ No newline at end of file