Skip to content

Commit

Permalink
Georeference scope tests (#with_locality), (#with_georgaphic_area)
Browse files Browse the repository at this point in the history
  • Loading branch information
TuckerJD committed Apr 16, 2014
1 parent 5c614da commit 9b212c2
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 64 deletions.
44 changes: 37 additions & 7 deletions app/models/georeference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,26 @@ def self.within_radius_of(geographic_item, distance)

def self.with_locality_like(string)
# return all Georeferences that are attached to a CollectingEvent that has a verbatim_locality that
# includes String somewhere TODO: (04/15/14) figure out how to use a 'join' here
# includes String somewhere
# Joins collecting_event.rb and matches %String% against verbatim_locality
# .where(id in CollectingEvent.where{verbatim_locality like "%var%"})

partial_ce = CollectingEvent.where("verbatim_locality like '%#{string}%'")
# where {id in CollectingEvent.where{verbatim_locality like "%var%"}}
with_locality_as(string, true)
end

partial_gr = []
partial_ce.each {|collecting_event|
partial_gr.push(Georeference.where(collecting_event_id: collecting_event.id).to_a)
}
partial_gr.flatten

def self.with_locality(string)
# return all Georeferences that are attached to a CollectingEvent that has a verbatim_locality that
# equals String somewhere
# Joins collecting_event.rb and matches %String% against verbatim_locality
# .where(id in CollectingEvent.where{verbatim_locality like "%var%"})

with_locality_as(string, false)
end

def self.with_geographic_area(geographic_area)
# returns all georeferences which have geographic_items which match geographic_areas
end

def error_box?
Expand Down Expand Up @@ -143,6 +152,27 @@ def error_box?

protected

def self.with_locality_as(string, like)
# return all Georeferences that are attached to a CollectingEvent that has a verbatim_locality that
# includes, or is equal to 'string' somewhere TODO: (04/15/14) figure out how to use a 'join' here
# Joins collecting_event.rb and matches %String% against verbatim_locality
# .where(id in CollectingEvent.where{verbatim_locality like "%var%"})

likeness = like ? '%' : ''
like = like ? 'like' : '='
query = "verbatim_locality #{like} '#{likeness}#{string}#{likeness}'"
# where(id in CollectingEvent.where{verbatim_locality like "%var%"})
partial_ce = CollectingEvent.where(query)

partial_gr = []
partial_ce.each {|collecting_event|
partial_gr.push(Georeference.where(collecting_event_id: collecting_event.id).to_a)
}
partial_gr.flatten

# where { geographic_item_id: where {CollectingEvent} }
end

def chk_obj_inside_err_geo_item
# case 1
retval = true
Expand Down
43 changes: 29 additions & 14 deletions spec/models/georeference_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,24 @@
# build some geo-references for testing using existing factories and geometries, something roughly like this
#@center_point = FactoryGirl.build()
@gr1 = FactoryGirl.create(:valid_georeference,
collecting_event: FactoryGirl.create(:valid_collecting_event),
geographic_item: FactoryGirl.create(:geographic_item_with_polygon, polygon: SHAPE_K)) # swap out the polygon with another shape if needed
collecting_event: FactoryGirl.create(:valid_collecting_event),
geographic_item: FactoryGirl.create(:geographic_item_with_polygon, polygon: SHAPE_K)) # swap out the polygon with another shape if needed

@gr2 = FactoryGirl.create(:valid_georeference_geo_locate)

@gr3 = FactoryGirl.create(:valid_georeference_verbatim_data)

@gr1.save!
@gr2.save!
@gr3.save!

}

after(:all) { GeographicItem.destroy_all }
after(:all) {
GeographicItem.destroy_all
Georeference.destroy_all
CollectingEvent.destroy_all
}

specify '.within_radius_of(geographic_item, distance)' do
#pending 'determination of what is intended'
Expand All @@ -280,37 +288,44 @@

expect(Georeference).to respond_to :within_radius_of

expect(@gr1.save).to be_true
expect(@gr2.save).to be_true
expect(Georeference.within_radius_of(@gr3.geographic_item, 112000)).to eq([@gr2, @gr3])
# TODO: (04/15/14) these have to be turned into ActiveRecord::Relationship
expect(Georeference.within_radius_of(@gr3.geographic_item, 112000).to_a).to eq([@gr2, @gr3])

end

specify '.with_locality_like(String)' do
# pending 'determination of what is intended'
# return all Georeferences that are attached to a CollectingEvent that has a verbatim_locality that includes String somewhere
# Joins collecting_event.rb and matches %String% against verbatim_locality

# .where(id in CollectingEvent.where{verbatim_locality like "%var%"})
expect(Georeference).to respond_to :with_locality_like
expect(@gr1.save!).to be_true
expect(@gr2.save!).to be_true
expect(@gr3.save!).to be_true
expect(Georeference.with_locality_like('Illinois')).to eq([@gr3])
expect(Georeference.with_locality_like('Locality ')).to eq([@gr1, @gr2])
# TODO: (04/15/14) these have to be turned into ActiveRecord::Relationship
expect(Georeference.with_locality_like('Illinois').to_a).to eq([@gr3])
pending 'construction of appropriate Georeference objects'
expect(Georeference.with_locality_like('Locality ').to_a).to eq([@gr1, @gr2])
expect(Georeference.wiht_locality_like('Saskatoon').to_a).to eq([])

end

specify '.with_locality(String)' do
pending 'determination of what is intended'
# return all Georeferences that are attached to a CollectingEvent that has a verbatim_locality = String
# Joins collecting_event.rb and matches String against verbatim_locality,
# .where(id in CollectingEvent.where{verbatim_locality = "var"})
expect(Georeference).to respond_to :with_locality
# TODO: (04/15/14) these have to be turned into ActiveRecord::Relationship
expect(Georeference.with_locality('Champaign Co., Illinois').to_a).to eq([@gr3])
pending 'construction of appropriate Georeference objects'
expect(Georeference.with_locality('Locality 1 for testing...').to_a).to eq([@gr1])
expect(Georeference.wiht_locality('Saskatoon').to_a).to eq([])

end

specify '.with_geographic_area(geographic_area)' do
pending 'determination of what is intended'
expect(Georeference).to respond_to :with_geographic_area
# TODO: (04/15/14) these have to be turned into ActiveRecord::Relationship
pending 'construction of appropriate Georeference objects'
# where{geograhic_item_id: geographic_area.id}

end
end

Expand Down
88 changes: 45 additions & 43 deletions spec/support/geo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -522,10 +522,12 @@ def generate_test_objects
outer_limits: @outer_limits.id
}

@debug_names.collect { |k, v| print " " + v.to_s + ": " + k.to_s }

puts @debug_names.invert[@p1]
my_debug = false

if my_debug
@debug_names.collect { |k, v| print " " + v.to_s + ": " + k.to_s }
puts @debug_names.invert[@p1]
end
end


Expand All @@ -545,26 +547,26 @@ def gen_wkt_files_1()
wkt = it[0].as_text
name = it[1]
case it[0].geometry_type.type_name
when 'Point'
f_type = f_point
when 'MultiPoint'
# MULTIPOINT ((3.0 -14.0 0.0), (6.0 -12.9 0.0)
f_type = $stdout
when /^Line[S]*/ #when 'Line' or 'LineString'
f_type = f_line
when 'MultiLineString'
# MULTILINESTRING ((-20.0 -1.0 0.0, -26.0 -6.0 0.0), (-21.0 -4.0 0.0, -31.0 -4.0 0.0))
f_type = $stdout
when 'Polygon'
f_type = f_poly
when 'MultiPolygon'
# MULTIPOLYGON (((28.0 2.3 0.0, 23.0 -1.7 0.0, 26.0 -4.8 0.0, 28.0 2.3 0.0))
f_type = $stdout
when 'GeometryCollection'
# GEOMETRYCOLLECTION (POLYGON ((-19.0 9.0 0.0, -9.0 9.0 0.0, -9.0 2.0 0.0, -19.0 2.0 0.0, -19.0 9.0 0.0)), POLYGON ((5.0 -1.0 0.0, -14.0 -1.0 0.0, -14.0 6.0 0.0, 5.0 6.0 0.0, 5.0 -1.0 0.0)), POLYGON ((-11.0 -1.0 0.0, -11.0 -5.0 0.0, -7.0 -5.0 0.0, -7.0 -1.0 0.0, -11.0 -1.0 0.0)), POLYGON ((-3.0 -9.0 0.0, -3.0 -1.0 0.0, -7.0 -1.0 0.0, -7.0 -9.0 0.0, -3.0 -9.0 0.0)), POLYGON ((-7.0 -9.0 0.0, -7.0 -5.0 0.0, -11.0 -5.0 0.0, -11.0 -9.0 0.0, -7.0 -9.0 0.0)))
f_type = $stdout
else
f_type = $stdout
when 'Point'
f_type = f_point
when 'MultiPoint'
# MULTIPOINT ((3.0 -14.0 0.0), (6.0 -12.9 0.0)
f_type = $stdout
when /^Line[S]*/ #when 'Line' or 'LineString'
f_type = f_line
when 'MultiLineString'
# MULTILINESTRING ((-20.0 -1.0 0.0, -26.0 -6.0 0.0), (-21.0 -4.0 0.0, -31.0 -4.0 0.0))
f_type = $stdout
when 'Polygon'
f_type = f_poly
when 'MultiPolygon'
# MULTIPOLYGON (((28.0 2.3 0.0, 23.0 -1.7 0.0, 26.0 -4.8 0.0, 28.0 2.3 0.0))
f_type = $stdout
when 'GeometryCollection'
# GEOMETRYCOLLECTION (POLYGON ((-19.0 9.0 0.0, -9.0 9.0 0.0, -9.0 2.0 0.0, -19.0 2.0 0.0, -19.0 9.0 0.0)), POLYGON ((5.0 -1.0 0.0, -14.0 -1.0 0.0, -14.0 6.0 0.0, 5.0 6.0 0.0, 5.0 -1.0 0.0)), POLYGON ((-11.0 -1.0 0.0, -11.0 -5.0 0.0, -7.0 -5.0 0.0, -7.0 -1.0 0.0, -11.0 -1.0 0.0)), POLYGON ((-3.0 -9.0 0.0, -3.0 -1.0 0.0, -7.0 -1.0 0.0, -7.0 -9.0 0.0, -3.0 -9.0 0.0)), POLYGON ((-7.0 -9.0 0.0, -7.0 -5.0 0.0, -11.0 -5.0 0.0, -11.0 -9.0 0.0, -7.0 -9.0 0.0)))
f_type = $stdout
else
f_type = $stdout
# ignore it for now
end
f_type.write("#{index}:#{wkt}: #{name}\n")
Expand All @@ -591,26 +593,26 @@ def gen_wkt_files()
wkt = it[0].as_text
name = it[1]
case it[0].geometry_type.type_name
when 'Point'
f_type = f_point
when 'MultiPoint'
# MULTIPOINT ((3.0 -14.0 0.0), (6.0 -12.9 0.0)
f_type = $stdout
when /^Line[S]*/ #when 'Line' or 'LineString'
f_type = f_line
when 'MultiLineString'
# MULTILINESTRING ((-20.0 -1.0 0.0, -26.0 -6.0 0.0), (-21.0 -4.0 0.0, -31.0 -4.0 0.0))
f_type = $stdout
when 'Polygon'
f_type = f_poly
when 'MultiPolygon'
# MULTIPOLYGON (((28.0 2.3 0.0, 23.0 -1.7 0.0, 26.0 -4.8 0.0, 28.0 2.3 0.0))
f_type = $stdout
when 'GeometryCollection'
# GEOMETRYCOLLECTION (POLYGON ((-19.0 9.0 0.0, -9.0 9.0 0.0, -9.0 2.0 0.0, -19.0 2.0 0.0, -19.0 9.0 0.0)), POLYGON ((5.0 -1.0 0.0, -14.0 -1.0 0.0, -14.0 6.0 0.0, 5.0 6.0 0.0, 5.0 -1.0 0.0)), POLYGON ((-11.0 -1.0 0.0, -11.0 -5.0 0.0, -7.0 -5.0 0.0, -7.0 -1.0 0.0, -11.0 -1.0 0.0)), POLYGON ((-3.0 -9.0 0.0, -3.0 -1.0 0.0, -7.0 -1.0 0.0, -7.0 -9.0 0.0, -3.0 -9.0 0.0)), POLYGON ((-7.0 -9.0 0.0, -7.0 -5.0 0.0, -11.0 -5.0 0.0, -11.0 -9.0 0.0, -7.0 -9.0 0.0)))
f_type = $stdout
else
f_type = $stdout
when 'Point'
f_type = f_point
when 'MultiPoint'
# MULTIPOINT ((3.0 -14.0 0.0), (6.0 -12.9 0.0)
f_type = $stdout
when /^Line[S]*/ #when 'Line' or 'LineString'
f_type = f_line
when 'MultiLineString'
# MULTILINESTRING ((-20.0 -1.0 0.0, -26.0 -6.0 0.0), (-21.0 -4.0 0.0, -31.0 -4.0 0.0))
f_type = $stdout
when 'Polygon'
f_type = f_poly
when 'MultiPolygon'
# MULTIPOLYGON (((28.0 2.3 0.0, 23.0 -1.7 0.0, 26.0 -4.8 0.0, 28.0 2.3 0.0))
f_type = $stdout
when 'GeometryCollection'
# GEOMETRYCOLLECTION (POLYGON ((-19.0 9.0 0.0, -9.0 9.0 0.0, -9.0 2.0 0.0, -19.0 2.0 0.0, -19.0 9.0 0.0)), POLYGON ((5.0 -1.0 0.0, -14.0 -1.0 0.0, -14.0 6.0 0.0, 5.0 6.0 0.0, 5.0 -1.0 0.0)), POLYGON ((-11.0 -1.0 0.0, -11.0 -5.0 0.0, -7.0 -5.0 0.0, -7.0 -1.0 0.0, -11.0 -1.0 0.0)), POLYGON ((-3.0 -9.0 0.0, -3.0 -1.0 0.0, -7.0 -1.0 0.0, -7.0 -9.0 0.0, -3.0 -9.0 0.0)), POLYGON ((-7.0 -9.0 0.0, -7.0 -5.0 0.0, -11.0 -5.0 0.0, -11.0 -9.0 0.0, -7.0 -9.0 0.0)))
f_type = $stdout
else
f_type = $stdout
# ignore it for now
end
f_type.write("#{index}:#{wkt}: #{name}\n")
Expand Down

0 comments on commit 9b212c2

Please sign in to comment.