Skip to content

Commit

Permalink
This needs a refactor. Do not merge.
Browse files Browse the repository at this point in the history
yes, I know the tests are failing too. It needs cassette regen but wanted to share the idea before tuning it up
  • Loading branch information
JPrevost committed Oct 30, 2024
1 parent f6a5870 commit 399bfcb
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 34 deletions.
35 changes: 1 addition & 34 deletions app/controllers/record_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,11 @@ def view
# Manipulation of returned record would go here...

@record = response&.data&.to_h&.dig('recordId')
@rectangle = bounding_box_to_coords
@rectangle = bounding_box_to_coords(@record)
end

private

# Converts a bounding box into a top left, bottom right set of coordinates
def bounding_box_to_coords
return unless @record.present?
return unless geospatial_coordinates?(@record['locations'])

# Our preference is to use the `Bounding Box` kind
raw_bbox = @record['locations'].select { |l| l if l['kind'] == 'Bounding Box' }.first

# If we had no `Bounding Box` kind, see if we have a `Geometry kind`
if raw_bbox.blank?
raw_bbox = @record['locations'].select { |l| l if l['kind'] == 'Geometry' }.first
end

return unless raw_bbox.present?

# extract just the geo coordinates and remove the extra syntax
bbox = raw_bbox['geoshape'].sub('BBOX (', '').sub(')', '')

# conver the string into an array of floats
bbox_array = bbox.split(',').map!(&:strip).map!(&:to_f)

# Protect against unexpected data
if bbox_array.count != 4
Rails.logger.info("Unexpected Bounding Box: #{raw_bbox}")
return
end

coords = [[bbox_array[2], bbox_array[0]], [bbox_array[3], bbox_array[1]]]
Rails.logger.info("Raw BBox: #{raw_bbox}")
Rails.logger.info("Rectangle: #{coords}")
coords
end

def validate_id!
return if params[:id]&.strip.present?

Expand Down
33 changes: 33 additions & 0 deletions app/helpers/record_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,39 @@ def deduplicate_subjects(subjects)
subjects.map { |subject| subject['value'].uniq(&:downcase) }.uniq { |values| values.map(&:downcase) }
end

# Converts a bounding box into a top left, bottom right set of coordinates
def bounding_box_to_coords(record)
return unless record.present?
return unless geospatial_coordinates?(record['locations'])

# Our preference is to use the `Bounding Box` kind
raw_bbox = record['locations'].select { |l| l if l['kind'] == 'Bounding Box' }.first

# If we had no `Bounding Box` kind, see if we have a `Geometry kind`
if raw_bbox.blank?
raw_bbox = record['locations'].select { |l| l if l['kind'] == 'Geometry' }.first
end

return unless raw_bbox.present?

# extract just the geo coordinates and remove the extra syntax
bbox = raw_bbox['geoshape'].sub('BBOX (', '').sub(')', '')

# conver the string into an array of floats
bbox_array = bbox.split(',').map!(&:strip).map!(&:to_f)

# Protect against unexpected data
if bbox_array.count != 4
Rails.logger.info("Unexpected Bounding Box: #{raw_bbox}")
return
end

coords = [[bbox_array[2], bbox_array[0]], [bbox_array[3], bbox_array[1]]]
Rails.logger.info("Raw BBox: #{raw_bbox}")
Rails.logger.info("Rectangle: #{coords}")
coords
end

private

def render_kind_value(list)
Expand Down
20 changes: 20 additions & 0 deletions app/models/timdex_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class TimdexSearch < TimdexBase
text
url
}
locations {
geoshape
kind
value
}
notes {
kind
value
Expand Down Expand Up @@ -199,6 +204,11 @@ class TimdexSearch < TimdexBase
text
url
}
locations {
geoshape
kind
value
}
notes {
kind
value
Expand Down Expand Up @@ -325,6 +335,11 @@ class TimdexSearch < TimdexBase
text
url
}
locations {
geoshape
kind
value
}
notes {
kind
value
Expand Down Expand Up @@ -461,6 +476,11 @@ class TimdexSearch < TimdexBase
text
url
}
locations {
geoshape
kind
value
}
notes {
kind
value
Expand Down
1 change: 1 addition & 0 deletions app/views/record/_record_geo.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<div id="map" style="height: 180px"></div>
<script>
var map = L.map('map');

L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
Expand Down
50 changes: 50 additions & 0 deletions app/views/search/_result_geo.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,56 @@
</p>
<% end %>


<!-- We only care about geospatial locations for this, as place names are also subjects. -->
<% if geospatial_coordinates?(result_geo['locations']) %>
<% content_for :additional_meta_tag do %>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
crossorigin=""/>

<!-- Make sure you put this AFTER Leaflet's CSS -->
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
crossorigin=""></script>
<% end %>
<div id=<%= "map_#{result_geo_counter}" %> style="height: 180px"></div>

<script>
var <%= "map_#{result_geo_counter}" %> = L.map('<%= "map_#{result_geo_counter}" %>');

L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(<%= "map_#{result_geo_counter}" %>);

// define rectangle geographical bounds
// -71.158693, -71.064796, 42.395972, 42.351993
// var bounds = [[42.395972, -71.158693],[42.351993, -71.064796]];

console.log('#<%= bounding_box_to_coords(result_geo) %>');
var bounds = <%= bounding_box_to_coords(result_geo) %>;

// create an orange rectangle
L.rectangle(bounds, {color: "#ff7800", weight: 1}).addTo(<%= "map_#{result_geo_counter}" %>);

// zoom the map to the rectangle bounds
<%= "map_#{result_geo_counter}" %>.fitBounds(bounds);
</script>

<p>
<ul>
<% parse_nested_field(result_geo['locations']).each do |location| %>
<% if location['geoshape'].present? %>
<li><%= "#{location['kind']}: #{location['geoshape']}" %></li>
<% end %>
<% end %>
</ul>
</p>
<% else %>
Hallo
<% end %>
<% if result_geo['highlight'] %>
<div class="result-highlights">
<%= render partial: 'search/highlights', locals: { result: result_geo } %>
Expand Down

0 comments on commit 399bfcb

Please sign in to comment.