Skip to content

Commit

Permalink
switch from querying solr using rectangle to envelope syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
mejackreed committed May 28, 2015
1 parent cb2bd65 commit 42cf848
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/geoblacklight.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "geoblacklight/engine"

module Geoblacklight
require 'geoblacklight/bounding_box'
require 'geoblacklight/catalog_helper_override'
require 'geoblacklight/config'
require 'geoblacklight/constants'
Expand Down
44 changes: 44 additions & 0 deletions lib/geoblacklight/bounding_box.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module Geoblacklight
##
# Transforms and parses a bounding box for various formats
class BoundingBox
##
# @param [String, Integer, Float] west
# @param [String, Integer, Float] south
# @param [String, Integer, Float] east
# @param [String, Integer, Float] north
def initialize(west, south, east, north)
@west = west
@south = south
@east = east
@north = north
# TODO: check for valid Geometry and raise if not
end

##
# Returns a bounding box in ENVELOPE syntax
# @return [String]
def to_envelope
"ENVELOPE(#{west}, #{east}, #{north}, #{south})"
end

##
# Create a Geoblacklight::BoundingBox from a Solr rectangle syntax
# @param [String] bbox as "W S E N"
# @return [Geoblacklight::BoundingBox]
def self.from_rectangle(rectangle)
rectangle_array = rectangle.split(' ')
fail Geoblacklight::Exceptions::WrongBoundingBoxFormat, 'Bounding box should be a string in Solr rectangle syntax e.g."W S E N"' if rectangle_array.count != 4
new(
rectangle_array[0],
rectangle_array[1],
rectangle_array[2],
rectangle_array[3]
)
end

private

attr_reader :west, :south, :east, :north
end
end
2 changes: 2 additions & 0 deletions lib/geoblacklight/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ def message
end
class WrongDownloadFormat < StandardError
end
class WrongBoundingBoxFormat < StandardError
end
end
end
20 changes: 18 additions & 2 deletions lib/geoblacklight/search_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,27 @@ def initialize(processor_chain, scope)
def add_spatial_params(solr_params)
if blacklight_params[:bbox]
solr_params[:bq] ||= []
solr_params[:bq] = ["#{Settings.GEOMETRY_FIELD}:\"IsWithin(#{blacklight_params[:bbox]})\"^10"]
solr_params[:bq] = ["#{Settings.GEOMETRY_FIELD}:\"IsWithin(#{envelope_bounds})\"^10"]
solr_params[:fq] ||= []
solr_params[:fq] << "#{Settings.GEOMETRY_FIELD}:\"Intersects(#{blacklight_params[:bbox]})\""
solr_params[:fq] << "#{Settings.GEOMETRY_FIELD}:\"Intersects(#{envelope_bounds})\""
end
solr_params
rescue Geoblacklight::Exceptions::WrongBoundingBoxFormat
# TODO: Potentially delete bbox params here so that its not rendered as search param
solr_params
end

##
# @return [String]
def envelope_bounds
bounding_box.to_envelope
end

##
# Returns a Geoblacklight::BoundingBox built from the blacklight_params
# @return [Geoblacklight::BoundingBox]
def bounding_box
Geoblacklight::BoundingBox.from_rectangle(blacklight_params[:bbox])
end
end
end
2 changes: 1 addition & 1 deletion spec/features/search_bar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

feature 'search bar' do
scenario 'present on a spatial search' do
visit catalog_index_path(bbox: '25, 3, 75, 35')
visit catalog_index_path(bbox: '25 3 75 35')
expect(page).to have_css '#search-navbar'
end
scenario 'present on a text search' do
Expand Down
27 changes: 27 additions & 0 deletions spec/lib/geoblacklight/bounding_box_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'spec_helper'

describe Geoblacklight::BoundingBox do
describe '#initialize' do
it 'handles multiple input types as arguments' do
expect(Geoblacklight::BoundingBox.new('1', '1', '1', '1')).to be_an Geoblacklight::BoundingBox
expect(Geoblacklight::BoundingBox.new(1, 2, 3, 3)).to be_an Geoblacklight::BoundingBox
expect(Geoblacklight::BoundingBox.new(1.1, 2.1, 3.1, 3.1)).to be_an Geoblacklight::BoundingBox
end
end
describe '#to_envelope' do
let(:example_box) { Geoblacklight::BoundingBox.new(-160, -80, 120, 70) }
it 'creates an envelope syntax version of the bounding box' do
expect(example_box.to_envelope).to eq 'ENVELOPE(-160, 120, 70, -80)'
end
end
describe '#from_rectangle' do
let(:example_box) { Geoblacklight::BoundingBox.from_rectangle('-160 -80 120 70') }
it 'parses and creates a Geoblacklight::BoundingBox from a Solr lat-lon' do
expect(example_box).to be_an Geoblacklight::BoundingBox
expect(example_box.to_envelope).to eq 'ENVELOPE(-160, 120, 70, -80)'
end
it 'checks for valididity' do
expect { Geoblacklight::BoundingBox.from_rectangle('-160 -80 120') }.to raise_error Geoblacklight::Exceptions::WrongBoundingBoxFormat
end
end
end
19 changes: 16 additions & 3 deletions spec/lib/geoblacklight/search_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,23 @@
end

it 'should return a spatial search if bbox is given' do
params = { :bbox => '123' }
params = { bbox: '-180 -80 120 80' }
subject.with(params)
expect(subject.add_spatial_params(solr_params)[:fq].to_s).to include("Intersects")
expect(subject.add_spatial_params(solr_params)[:fq].to_s).to include('Intersects')
end
end
describe '#envelope_bounds' do
it 'calls to_envelope on the bounding box' do
bbox = double('bbox', to_envelope: 'test')
expect(subject).to receive(:bounding_box).and_return bbox
expect(subject.envelope_bounds).to eq 'test'
end
end
describe '#bounding_box' do
it 'creates a bounding box from a Solr lat-lon rectangle format' do
params = { bbox: '-120 -80 120 80' }
subject.with(params)
expect(subject.bounding_box).to be_an Geoblacklight::BoundingBox
end
end
end

0 comments on commit 42cf848

Please sign in to comment.