Skip to content

Commit

Permalink
Merge pull request #2095 from MushroomObserver/search-has-lat-long
Browse files Browse the repository at this point in the history
Replace observation search param `has_location` with `has_public_lat_lng`, fix search instructions
  • Loading branch information
nimmolo committed Apr 15, 2024
2 parents 2be0832 + 5352d83 commit e698ddf
Show file tree
Hide file tree
Showing 59 changed files with 303 additions and 318 deletions.
6 changes: 3 additions & 3 deletions app/classes/api2/observation_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def query_params
help: 1),
gps_hidden: parse(:boolean, :gps_hidden, help: 1),
has_images: parse(:boolean, :has_images),
has_location: parse(:boolean, :has_location),
has_public_lat_lng: parse(:boolean, :has_public_lat_lng),
has_name: parse(:boolean, :has_name, help: :min_rank),
has_comments: parse(:boolean, :has_comments, limit: true),
has_specimen: parse(:boolean, :has_specimen),
Expand All @@ -84,7 +84,7 @@ def create_params
when: parse(:date, :date) || Time.zone.today,
place_name: @location,
lat: @latitude,
long: @longitude,
lng: @longitude,
alt: @altitude,
specimen: @has_specimen,
is_collection_location: parse(:boolean, :is_collection_location,
Expand All @@ -111,7 +111,7 @@ def update_params
place_name: parse(:place_name, :set_location, limit: 1024,
not_blank: true),
lat: @latitude,
long: @longitude,
lng: @longitude,
alt: @altitude,
specimen: parse(:boolean, :set_has_specimen),
is_collection_location: parse(:boolean, :set_is_collection_location,
Expand Down
8 changes: 4 additions & 4 deletions app/classes/labels.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ def add_gps
return unless @obs.lat.present? || @obs.alt.present?

label("Coordinates")
@para << format_lat_long
@para << format_lat_lng
@para << format_alt
@para.line_break
end

def format_lat_long
def format_lat_lng
loc = @obs.location
if @obs.lat.present?
"#{format_lat(@obs.lat)} #{format_long(@obs.long)}"
"#{format_lat(@obs.lat)} #{format_lng(@obs.lng)}"
elsif loc.present?
n = format_lat(loc.north)
s = format_lat(loc.south)
Expand Down Expand Up @@ -182,7 +182,7 @@ def format_lat(val)
val.negative? ? "#{-val}°S" : "#{val}°N"
end

def format_long(val)
def format_lng(val)
val = val.round(1) unless coordinates_visible?
val.negative? ? "#{-val}°W" : "#{val}°E"
end
Expand Down
10 changes: 5 additions & 5 deletions app/classes/mappable/box_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
# be useful on a map.
# delta_lat:: Returns north_south_distance * DELTA.
# delta_lng:: Returns east_west_distance * DELTA.
# lat_long_close?:: Determines if a given lat/long coordinate is within,
# lat_lng_close?:: Determines if a given lat/long coordinate is within,
# or close to, a bounding box.
# contains?(lat, lng):: Does box contain the given latititude and longitude
# contains_lat?
# contains_long?
# contains_lng?

module Mappable
module BoxMethods
Expand Down Expand Up @@ -101,14 +101,14 @@ def straddles_180_deg?
end

def contains?(lat, lng)
contains_lat?(lat) && contains_long?(lng)
contains_lat?(lat) && contains_lng?(lng)
end

def contains_lat?(lat)
(south..north).cover?(lat)
end

def contains_long?(lng)
def contains_lng?(lng)
return (west...east).cover?(lng) unless straddles_180_deg?

(lng >= west) || (lng <= east)
Expand Down Expand Up @@ -145,7 +145,7 @@ def delta_lng
# Determines if a given lat/long coordinate is within, or close to, a
# bounding box. Method is used to decide if an obs lat/lng is "dubious"
# with respect to the observation's assigned Location.
def lat_long_close?(pt_lat, pt_lng)
def lat_lng_close?(pt_lat, pt_lng)
loc = Box.new(north: north, south: south, east: east, west: west)
expanded = loc.expand(delta_lat, delta_lng)
expanded.contains?(pt_lat, pt_lng)
Expand Down
5 changes: 1 addition & 4 deletions app/classes/mappable/collapsible_collection_of_objects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@
# collection = Mappable::CollapsibleCollectionOfObjects.new(query.results)
#
###############################################################################
#
# GOTCHA: Observations have .long, Google map points need .lng
#

module Mappable
class CollapsibleCollectionOfObjects
Expand Down Expand Up @@ -139,7 +136,7 @@ def init_sets(objects)
if obj.location? && mappable
add_box_set(loc, [obj], MAX_PRECISION)
elsif obj.observation?
if (!will_be_grouped || !loc) && obj.lat # && !obj.lat_long_dubious?
if (!will_be_grouped || !loc) && obj.lat # && !obj.lat_lng_dubious?
add_point_set(obj, [obj], MAX_PRECISION)
elsif loc && mappable
add_box_set(loc, [obj], MAX_PRECISION)
Expand Down
5 changes: 1 addition & 4 deletions app/classes/mappable/map_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
# The title and caption are accessors, so they can be set in the map helper.
# The objects are an accessor, because they get stripped before sending to JS.
#
# GOTCHA: Observations have .long, Google Map points need .lng.
# MapSet must provide .lng to be interoperable with other points.
#
module Mappable
class MapSet
attr_reader :north, :south, :east, :west, :is_point, :is_box,
Expand All @@ -48,7 +45,7 @@ def init_objects_and_derive_extents
if obj.location? && !Location.is_unknown?(obj.name)
update_extents_with_box(obj)
elsif obj.observation?
if obj.lat && !obj.lat_long_dubious?
if obj.lat && !obj.lat_lng_dubious?
update_extents_with_point(obj)
elsif (loc = obj.location) &&
!Location.is_unknown?(loc.name)
Expand Down
12 changes: 5 additions & 7 deletions app/classes/mappable/minimal_observation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

module Mappable
class MinimalObservation
attr_accessor :id, :lat, :lng, :location_id # , :long
attr_accessor :id, :lat, :lng, :location_id

# The observation has the attr `long`, but the MapSet needs to provide `lng`
def initialize(id, lat, long, location_or_id)
def initialize(id, lat, lng, location_or_id)
@id = id
@lat = lat
@lng = long
# @long = long # Not sure if anyone needs `long`
@lng = lng
case location_or_id
when Integer, String
@location_id = location_or_id.to_i
Expand Down Expand Up @@ -41,8 +39,8 @@ def observation?
true
end

def lat_long_dubious?
lat && location && !location.lat_long_close?(lat, lng)
def lat_lng_dubious?
lat && location && !location.lat_lng_close?(lat, lng)
end
end
end
2 changes: 1 addition & 1 deletion app/classes/pattern_search/observation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Observation < Base

# booleanish
has_comments: [:has_comments, :parse_yes],
has_location: [:has_location, :parse_boolean],
has_public_lat_lng: [:has_public_lat_lng, :parse_boolean],
has_name: [:has_name, :parse_boolean],
has_notes: [:has_notes, :parse_boolean],
images: [:has_images, :parse_boolean],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Finds all observations that the current logged in user
# can see due to their being a project admin for a project
# that another user trusts.
class ProjectLatLongs
class ProjectLatLngs
attr_accessor :query

def initialize
Expand Down Expand Up @@ -53,7 +53,7 @@ def attribute(table_name, field)
def add_project
query.project(attribute(:observations, :id),
attribute(:observations, :lat),
attribute(:observations, :long))
attribute(:observations, :lng))
end

def add_conditions
Expand Down
12 changes: 6 additions & 6 deletions app/classes/query/image_with_observations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def parameter_declarations
old_by?: :string,
herbaria?: [:string],
is_collection_location?: :boolean,
has_location?: :boolean,
has_public_lat_lng?: :boolean,
has_name?: :boolean,
has_comments?: { boolean: [true] },
has_sequences?: { boolean: [true] },
Expand Down Expand Up @@ -53,7 +53,7 @@ def initialize_association_parameters

def initialize_boolean_parameters
initialize_is_collection_location_parameter
initialize_has_location_parameter
initialize_has_public_lat_lng_parameter
initialize_has_name_parameter
initialize_has_notes_parameter
add_join(:observations, :comments) if params[:has_comments]
Expand All @@ -69,11 +69,11 @@ def initialize_is_collection_location_parameter
)
end

def initialize_has_location_parameter
def initialize_has_public_lat_lng_parameter
add_boolean_condition(
"observations.location_id IS NOT NULL",
"observations.location_id IS NULL",
params[:has_location]
"observations.lat IS NOT NULL AND observations.gps_hidden IS FALSE",
"observations.lat IS NULL OR observations.gps_hidden IS TRUE",
params[:has_public_lat_lng]
)
end

Expand Down
12 changes: 6 additions & 6 deletions app/classes/query/location_with_observations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def parameter_declarations
herbaria?: [:string],
confidence?: [:float],
is_collection_location?: :boolean,
has_location?: :boolean,
has_public_lat_lng?: :boolean,
has_name?: :boolean,
has_comments?: { boolean: [true] },
has_sequences?: { boolean: [true] },
Expand Down Expand Up @@ -64,7 +64,7 @@ def initialize_association_parameters

def initialize_boolean_parameters
initialize_is_collection_location_parameter
initialize_has_location_parameter
initialize_has_public_lat_lng_parameter
initialize_has_name_parameter
initialize_has_notes_parameter
add_has_notes_fields_condition(params[:has_notes_fields])
Expand All @@ -80,11 +80,11 @@ def initialize_is_collection_location_parameter
)
end

def initialize_has_location_parameter
def initialize_has_public_lat_lng_parameter
add_boolean_condition(
"observations.location_id IS NOT NULL",
"observations.location_id IS NULL",
params[:has_location]
"observations.lat IS NOT NULL AND observations.gps_hidden IS FALSE",
"observations.lat IS NULL OR observations.gps_hidden IS TRUE",
params[:has_public_lat_lng]
)
end

Expand Down
20 changes: 10 additions & 10 deletions app/classes/query/modules/bounding_box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def add_bounding_box_conditions_for_observations
params[:east] && params[:west]

cond1, cond2 = bounding_box_conditions
cond0 = lat_long_plausible
cond0 = lat_lng_plausible
cond1 = cond1.join(" AND ")
cond2 = cond2.join(" AND ")
@where << "IF(locations.id IS NULL OR #{cond0}, #{cond1}, #{cond2})"
Expand All @@ -24,17 +24,17 @@ def add_bounding_box_conditions_for_observations

# ----------------------------------------------------------------------------

def lat_long_plausible
def lat_lng_plausible
# Condition which returns true if the observation's lat/long is plausible.
# (should be identical to Mappable::BoxMethods.lat_long_close?)
# (should be identical to Mappable::BoxMethods.lat_lng_close?)
%(
observations.lat >= locations.south*1.2 - locations.north*0.2 AND
observations.lat <= locations.north*1.2 - locations.south*0.2 AND
if(locations.west <= locations.east,
observations.long >= locations.west*1.2 - locations.east*0.2 AND
observations.long <= locations.east*1.2 - locations.west*0.2,
observations.long >= locations.west*0.8 + locations.east*0.2 + 72 OR
observations.long <= locations.east*0.8 + locations.west*0.2 - 72
observations.lng >= locations.west*1.2 - locations.east*0.2 AND
observations.lng <= locations.east*1.2 - locations.west*0.2,
observations.lng >= locations.west*0.8 + locations.east*0.2 + 72 OR
observations.lng <= locations.east*0.8 + locations.west*0.2 - 72
)
)
end
Expand All @@ -54,8 +54,8 @@ def bounding_box_normal(north, south, east, west)
# point location inside target box
"observations.lat >= #{south}",
"observations.lat <= #{north}",
"observations.long >= #{west}",
"observations.long <= #{east}"
"observations.lng >= #{west}",
"observations.lng <= #{east}"
], [
# box entirely within target box
"locations.south >= #{south}",
Expand All @@ -73,7 +73,7 @@ def bounding_box_straddling_date_line(north, south, east, west)
# point location inside target box
"observations.lat >= #{south}",
"observations.lat <= #{north}",
"(observations.long >= #{west} OR observations.long <= #{east})"
"(observations.lng >= #{west} OR observations.lng <= #{east})"
], [
# box entirely within target box
"locations.south >= #{south}",
Expand Down
12 changes: 6 additions & 6 deletions app/classes/query/name_with_observations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def parameter_declarations
herbaria?: [:string],
confidence?: [:float],
is_collection_location?: :boolean,
has_location?: :boolean,
has_public_lat_lng?: :boolean,
has_name?: :boolean,
has_sequences?: { boolean: [true] },
has_notes_fields?: [:string],
Expand Down Expand Up @@ -56,7 +56,7 @@ def initialize_association_parameters

def initialize_boolean_parameters
initialize_is_collection_location_parameter
initialize_has_location_parameter
initialize_has_public_lat_lng_parameter
initialize_has_name_parameter
initialize_has_notes_parameter
add_has_notes_fields_condition(params[:has_notes_fields])
Expand All @@ -72,11 +72,11 @@ def initialize_is_collection_location_parameter
)
end

def initialize_has_location_parameter
def initialize_has_public_lat_lng_parameter
add_boolean_condition(
"observations.location_id IS NOT NULL",
"observations.location_id IS NULL",
params[:has_location]
"observations.lat IS NOT NULL AND observations.gps_hidden IS FALSE",
"observations.lat IS NULL OR observations.gps_hidden IS TRUE",
params[:has_public_lat_lng]
)
end

Expand Down
12 changes: 6 additions & 6 deletions app/classes/query/observation_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def parameter_declarations

# boolean
has_comments?: { boolean: [true] },
has_location?: :boolean,
has_public_lat_lng?: :boolean,
has_name?: :boolean,
has_notes?: :boolean,
has_sequences?: { boolean: [true] },
Expand Down Expand Up @@ -111,7 +111,7 @@ def initialize_species_lists_parameter

def initialize_boolean_parameters
initialize_is_collection_location_parameter
initialize_has_location_parameter
initialize_has_public_lat_lng_parameter
initialize_has_name_parameter
initialize_has_notes_parameter
add_has_notes_fields_condition(params[:has_notes_fields])
Expand All @@ -127,11 +127,11 @@ def initialize_is_collection_location_parameter
)
end

def initialize_has_location_parameter
def initialize_has_public_lat_lng_parameter
add_boolean_condition(
"observations.location_id IS NOT NULL",
"observations.location_id IS NULL",
params[:has_location]
"observations.lat IS NOT NULL AND observations.gps_hidden IS FALSE",
"observations.lat IS NULL OR observations.gps_hidden IS TRUE",
params[:has_public_lat_lng]
)
end

Expand Down
Loading

0 comments on commit e698ddf

Please sign in to comment.