Skip to content

Commit

Permalink
Refactor Spatial
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Mar 27, 2014
1 parent 31ef091 commit 4428bad
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 91 deletions.
51 changes: 2 additions & 49 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Rest
include Gremlin
include Batch
include Extensions
include Spatial
include Clean
extend Forwardable

Expand All @@ -66,8 +67,6 @@ class Rest

def initialize(options = ENV['NEO4J_URL'] || {})
@connection = Connection.new(options)

@spatial ||= Spatial.new(@connection)
end

alias_method :list_indexes, :list_node_indexes
Expand Down Expand Up @@ -99,53 +98,7 @@ def get_relationship_start_node(rel)

def get_relationship_end_node(rel)
get_node(rel["end"])
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

def add_editable_layer(layer, format, node_property_name)
@spatial.add_editable_layer(layer, format, node_property_name)
end

def get_layer(layer)
@spatial.get_layer(layer)
end

def add_geometry_to_layer(layer, geometry)
@spatial.add_geometry_to_layer(layer, geometry)
end

def edit_geometry_from_layer(layer, geometry, node)
@spatial.edit_geometry_from_layer(layer, geometry, node)
end

def add_node_to_layer(layer, node)
@spatial.add_node_to_layer(layer, node)
end

def find_geometries_in_bbox(layer, minx, maxx, miny, maxy)
@spatial.find_geometries_in_bbox(layer, minx, maxx, miny, maxy)
end

def find_geometries_within_distance(layer, pointx, pointy, distance)
@spatial.find_geometries_within_distance(layer, pointx, pointy, distance)
end

def create_spatial_index(name, type = nil, lat = nil, lon = nil)
@spatial.create_spatial_index(name, type, lat, lon)
end

def add_node_to_spatial_index(index, id)
@spatial.add_node_to_spatial_index(index, id)
end
end

end
end
18 changes: 9 additions & 9 deletions lib/neography/rest/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,11 @@ def remove_from_index_path(klass, index, key_or_id, value_or_id = nil, id = nil)
# Spatial

def batch_get_spatial
get Spatial.index_path
get "/ext/SpatialPlugin"
end

def batch_add_point_layer(layer, lat = nil, lon = nil)
post Spatial.add_simple_point_layer_path do
post "/ext/SpatialPlugin/graphdb/addSimplePointLayer" do
{
:layer => layer,
:lat => lat || "lat",
Expand All @@ -250,7 +250,7 @@ def batch_add_point_layer(layer, lat = nil, lon = nil)
end

def batch_add_editable_layer(layer, format = "WKT", node_property_name = "wkt")
post Spatial.add_editable_layer_path do
post "/ext/SpatialPlugin/graphdb/addEditableLayer" do
{
:layer => layer,
:format => format,
Expand All @@ -260,15 +260,15 @@ def batch_add_editable_layer(layer, format = "WKT", node_property_name = "wkt")
end

def batch_get_layer(layer)
post Spatial.get_layer_path do
post "/ext/SpatialPlugin/graphdb/getLayer" do
{
:layer => layer
}
end
end

def batch_add_geometry_to_layer(layer, geometry)
post Spatial.add_geometry_to_layer_path do
post "/ext/SpatialPlugin/graphdb/addGeometryWKTToLayer" do
{
:layer => layer,
:geometry => geometry
Expand All @@ -277,7 +277,7 @@ def batch_add_geometry_to_layer(layer, geometry)
end

def batch_edit_geometry_from_layer(layer, geometry, node)
post Spatial.edit_geometry_from_layer_path do
post "/ext/SpatialPlugin/graphdb/updateGeometryFromWKT" do
{
:layer => layer,
:geometry => geometry,
Expand All @@ -287,7 +287,7 @@ def batch_edit_geometry_from_layer(layer, geometry, node)
end

def batch_add_node_to_layer(layer, node)
post Spatial.add_node_to_layer_path do
post "/ext/SpatialPlugin/graphdb/addNodeToLayer" do
{
:layer => layer,
:node => get_id(node)
Expand All @@ -296,7 +296,7 @@ def batch_add_node_to_layer(layer, node)
end

def batch_find_geometries_in_bbox(layer, minx, maxx, miny, maxy)
post Spatial.find_geometries_in_bbox_path do
post "/ext/SpatialPlugin/graphdb/findGeometriesInBBox" do
{
:layer => layer,
:minx => minx,
Expand All @@ -308,7 +308,7 @@ def batch_find_geometries_in_bbox(layer, minx, maxx, miny, maxy)
end

def batch_find_geometries_within_distance(layer, pointx, pointy, distance)
post Spatial.find_geometries_within_distance_path do
post "/ext/SpatialPlugin/graphdb/findGeometriesWithinDistance" do
{
:layer => layer,
:pointX => pointx,
Expand Down
49 changes: 16 additions & 33 deletions lib/neography/rest/spatial.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,13 @@
module Neography
class Rest
class Spatial
extend Neography::Rest::Paths
module Spatial
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"
add_path :create_index, "/index/node"
add_path :add_to_index, "/index/node/:index"

def initialize(connection)
@connection ||= connection
end

def index
@connection.get(index_path)

def get_spatial
@connection.get("/ext/SpatialPlugin")
end

def add_point_layer(layer, lat, lon)
def add_point_layer(layer, lat = nil, lon = nil)
options = {
:body => {
:layer => layer,
Expand All @@ -34,7 +17,7 @@ def add_point_layer(layer, lat, lon)
:headers => json_content_type.merge({'Accept' => 'application/json;charset=UTF-8'})
}

@connection.post(add_simple_point_layer_path, options)
@connection.post("/ext/SpatialPlugin/graphdb/addSimplePointLayer", options)
end

def add_editable_layer(layer, format = "WKT", node_property_name = "wkt")
Expand All @@ -47,7 +30,7 @@ def add_editable_layer(layer, format = "WKT", node_property_name = "wkt")
:headers => json_content_type.merge({'Accept' => 'application/json;charset=UTF-8'})
}

@connection.post(add_editable_layer_path, options)
@connection.post("/ext/SpatialPlugin/graphdb/addEditableLayer", options)
end

def get_layer(layer)
Expand All @@ -57,7 +40,7 @@ def get_layer(layer)
}.to_json,
:headers => json_content_type.merge({'Accept' => 'application/json;charset=UTF-8'})
}
@connection.post(get_layer_path, options)
@connection.post("/ext/SpatialPlugin/graphdb/getLayer", options)
end

def add_geometry_to_layer(layer, geometry)
Expand All @@ -68,7 +51,7 @@ def add_geometry_to_layer(layer, geometry)
}.to_json,
:headers => json_content_type.merge({'Accept' => 'application/json;charset=UTF-8'})
}
@connection.post(add_geometry_to_layer_path, options)
@connection.post("/ext/SpatialPlugin/graphdb/addGeometryWKTToLayer", options)
end

def edit_geometry_from_layer(layer, geometry, node)
Expand All @@ -80,7 +63,7 @@ def edit_geometry_from_layer(layer, geometry, node)
}.to_json,
:headers => json_content_type.merge({'Accept' => 'application/json;charset=UTF-8'})
}
@connection.post(edit_geometry_from_layer_path, options)
@connection.post("/ext/SpatialPlugin/graphdb/updateGeometryFromWKT", options)
end

def add_node_to_layer(layer, node)
Expand All @@ -91,7 +74,7 @@ def add_node_to_layer(layer, node)
}.to_json,
:headers => json_content_type.merge({'Accept' => 'application/json;charset=UTF-8'})
}
@connection.post(add_node_to_layer_path, options)
@connection.post("/ext/SpatialPlugin/graphdb/addNodeToLayer", options)
end

def find_geometries_in_bbox(layer, minx, maxx, miny, maxy)
Expand All @@ -105,7 +88,7 @@ def find_geometries_in_bbox(layer, minx, maxx, miny, maxy)
}.to_json,
:headers => json_content_type.merge({'Accept' => 'application/json;charset=UTF-8'})
}
@connection.post(find_geometries_in_bbox_path, options)
@connection.post("/ext/SpatialPlugin/graphdb/findGeometriesInBBox", options)
end

def find_geometries_within_distance(layer, pointx, pointy, distance)
Expand All @@ -118,10 +101,10 @@ def find_geometries_within_distance(layer, pointx, pointy, distance)
}.to_json,
:headers => json_content_type.merge({'Accept' => 'application/json;charset=UTF-8'})
}
@connection.post(find_geometries_within_distance_path, options)
@connection.post("/ext/SpatialPlugin/graphdb/findGeometriesWithinDistance", options)
end

def create_spatial_index(name, type, lat, lon)
def create_spatial_index(name, type = nil, lat = nil, lon = nil)
options = {
:body => {
:name => name,
Expand All @@ -134,7 +117,7 @@ def create_spatial_index(name, type, lat, lon)
}.to_json,
:headers => json_content_type.merge({'Accept' => 'application/json;charset=UTF-8'})
}
@connection.post(create_index_path, options)
@connection.post("/index/node", options)
end

def add_node_to_spatial_index(index, id)
Expand All @@ -146,7 +129,7 @@ def add_node_to_spatial_index(index, id)
}.to_json,
:headers => json_content_type.merge({'Accept' => 'application/json;charset=UTF-8'})
}
@connection.post(add_to_index_path(:index => index), options)
@connection.post("/index/node/%{index}" % {:index => index}, options)
end

end
Expand Down

0 comments on commit 4428bad

Please sign in to comment.