From 791e78aa90d03de3c181f8fb88072c6b5f1d7d13 Mon Sep 17 00:00:00 2001 From: Jeremy Prevost Date: Tue, 29 Oct 2024 16:22:54 -0400 Subject: [PATCH 1/7] Holy heck this might work? --- app/controllers/record_controller.rb | 13 +++++++++++ app/views/record/_record_geo.html.erb | 33 +++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/app/controllers/record_controller.rb b/app/controllers/record_controller.rb index 9ac8ea3d..2ac5134f 100644 --- a/app/controllers/record_controller.rb +++ b/app/controllers/record_controller.rb @@ -16,10 +16,23 @@ def view # Manipulation of returned record would go here... @record = response&.data&.to_h&.dig('recordId') + @rectangle = bounding_box_to_coords end private + def bounding_box_to_coords + return unless geospatial_coordinates?(@record['locations']) + + raw_bbox = @record['locations'].select { |l| l if l['kind'] == 'Bounding Box' }.first + bbox = raw_bbox['geoshape'].sub('BBOX (', '').sub(')', '') + bbox_array = bbox.split(', ') + coords = [[bbox_array[2].to_f, bbox_array[0].to_f], [bbox_array[3].to_f, bbox_array[1].to_f]] + Rails.logger.info("Raw BBox: #{raw_bbox}") + Rails.logger.info("Rectangle: #{coords}") + coords + end + def validate_id! return if params[:id]&.strip.present? diff --git a/app/views/record/_record_geo.html.erb b/app/views/record/_record_geo.html.erb index aa247a62..04cf7632 100644 --- a/app/views/record/_record_geo.html.erb +++ b/app/views/record/_record_geo.html.erb @@ -79,6 +79,39 @@ <% end %> <% end %> + + <% content_for :additional_meta_tag do %> + + + + + <% end %> + +
+ <% end %> <% if @record['notes'].present? %> From 43e14ccd12c25eecf3ba38c9877ef9bcd1d85bbf Mon Sep 17 00:00:00 2001 From: Jeremy Prevost Date: Wed, 30 Oct 2024 11:03:58 -0400 Subject: [PATCH 2/7] Handle some edge cases - bbox is sometimes in a different field (ticket opened to understand why) - bbox sometimes has spaces and sometimes not so we handle both - convert strings to floats during initial string extraction rather than during coords array creation - if bbox_array is not 4 elements, don't use it - if we don't have a record at all, don't try to use it --- app/controllers/record_controller.rb | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/app/controllers/record_controller.rb b/app/controllers/record_controller.rb index 2ac5134f..f54baf7e 100644 --- a/app/controllers/record_controller.rb +++ b/app/controllers/record_controller.rb @@ -21,13 +21,34 @@ def view 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(')', '') - bbox_array = bbox.split(', ') - coords = [[bbox_array[2].to_f, bbox_array[0].to_f], [bbox_array[3].to_f, bbox_array[1].to_f]] + + # 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 From f6a5870c66954669209f597063273e6a07b33ccf Mon Sep 17 00:00:00 2001 From: Jeremy Prevost Date: Wed, 30 Oct 2024 11:19:06 -0400 Subject: [PATCH 3/7] Reorder record view to show map more prominently --- app/views/record/_record_geo.html.erb | 36 ++++++++++++++------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/app/views/record/_record_geo.html.erb b/app/views/record/_record_geo.html.erb index 04cf7632..9c5b239e 100644 --- a/app/views/record/_record_geo.html.erb +++ b/app/views/record/_record_geo.html.erb @@ -60,26 +60,9 @@ <% end %> <% end %> - <% if @record['subjects'].present? %> -

Subjects

-
    - <% deduplicate_subjects(@record['subjects'])&.each do |subject| %> -
  • <%= subject.join('; ') %>
  • - <% end %> -
- <% end %> - <% if geospatial_coordinates?(@record['locations']) %>

Geospatial coordinates

-
    - <% parse_nested_field(@record['locations']).each do |location| %> - <% if location['geoshape'].present? %> -
  • <%= "#{location['kind']}: #{location['geoshape']}" %>
  • - <% end %> - <% end %> -
- <% content_for :additional_meta_tag do %> + +

+

    + <% parse_nested_field(@record['locations']).each do |location| %> + <% if location['geoshape'].present? %> +
  • <%= "#{location['kind']}: #{location['geoshape']}" %>
  • + <% end %> + <% end %> +
+

+ <% end %> + + <% if @record['subjects'].present? %> +

Subjects

+
    + <% deduplicate_subjects(@record['subjects'])&.each do |subject| %> +
  • <%= subject.join('; ') %>
  • + <% end %> +
<% end %> <% if @record['notes'].present? %> From 399bfcbbea1633ccc310f249d72e4c7edc6f9ef0 Mon Sep 17 00:00:00 2001 From: Jeremy Prevost Date: Wed, 30 Oct 2024 15:08:35 -0400 Subject: [PATCH 4/7] This needs a refactor. Do not merge. yes, I know the tests are failing too. It needs cassette regen but wanted to share the idea before tuning it up --- app/controllers/record_controller.rb | 35 +------------------ app/helpers/record_helper.rb | 33 ++++++++++++++++++ app/models/timdex_search.rb | 20 +++++++++++ app/views/record/_record_geo.html.erb | 1 + app/views/search/_result_geo.html.erb | 50 +++++++++++++++++++++++++++ 5 files changed, 105 insertions(+), 34 deletions(-) diff --git a/app/controllers/record_controller.rb b/app/controllers/record_controller.rb index f54baf7e..14e3eecc 100644 --- a/app/controllers/record_controller.rb +++ b/app/controllers/record_controller.rb @@ -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? diff --git a/app/helpers/record_helper.rb b/app/helpers/record_helper.rb index 142ef5d6..2a86b92b 100644 --- a/app/helpers/record_helper.rb +++ b/app/helpers/record_helper.rb @@ -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) diff --git a/app/models/timdex_search.rb b/app/models/timdex_search.rb index c2dd9ee5..5520098a 100644 --- a/app/models/timdex_search.rb +++ b/app/models/timdex_search.rb @@ -67,6 +67,11 @@ class TimdexSearch < TimdexBase text url } + locations { + geoshape + kind + value + } notes { kind value @@ -199,6 +204,11 @@ class TimdexSearch < TimdexBase text url } + locations { + geoshape + kind + value + } notes { kind value @@ -325,6 +335,11 @@ class TimdexSearch < TimdexBase text url } + locations { + geoshape + kind + value + } notes { kind value @@ -461,6 +476,11 @@ class TimdexSearch < TimdexBase text url } + locations { + geoshape + kind + value + } notes { kind value diff --git a/app/views/record/_record_geo.html.erb b/app/views/record/_record_geo.html.erb index 9c5b239e..f52ec753 100644 --- a/app/views/record/_record_geo.html.erb +++ b/app/views/record/_record_geo.html.erb @@ -77,6 +77,7 @@
+ <% end %> +
style="height: 180px">
+ + + +

+

    + <% parse_nested_field(result_geo['locations']).each do |location| %> + <% if location['geoshape'].present? %> +
  • <%= "#{location['kind']}: #{location['geoshape']}" %>
  • + <% end %> + <% end %> +
+

+ <% else %> + Hallo + <% end %> + <% if result_geo['highlight'] %>
<%= render partial: 'search/highlights', locals: { result: result_geo } %> From 336210ba70c3c065c6e8c0c7b621d4e0bf171f8f Mon Sep 17 00:00:00 2001 From: Jeremy Prevost Date: Wed, 30 Oct 2024 15:20:16 -0400 Subject: [PATCH 5/7] ooops --- app/views/search/_result_geo.html.erb | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/views/search/_result_geo.html.erb b/app/views/search/_result_geo.html.erb index 55cc509e..bae04a43 100644 --- a/app/views/search/_result_geo.html.erb +++ b/app/views/search/_result_geo.html.erb @@ -67,8 +67,6 @@ <% end %>

- <% else %> - Hallo <% end %> <% if result_geo['highlight'] %> From edbb8a65195d75a0e7c97b324ad775528a899578 Mon Sep 17 00:00:00 2001 From: Jeremy Prevost Date: Thu, 31 Oct 2024 09:06:49 -0400 Subject: [PATCH 6/7] Initial refactor to extract some common logic --- app/views/record/_record_geo.html.erb | 10 -------- app/views/record/view.html.erb | 11 +++++++++ app/views/search/_result_geo.html.erb | 35 ++------------------------- app/views/search/results.html.erb | 11 +++++++++ app/views/shared/_map.html.erb | 21 ++++++++++++++++ 5 files changed, 45 insertions(+), 43 deletions(-) create mode 100644 app/views/shared/_map.html.erb diff --git a/app/views/record/_record_geo.html.erb b/app/views/record/_record_geo.html.erb index f52ec753..5b1e53fa 100644 --- a/app/views/record/_record_geo.html.erb +++ b/app/views/record/_record_geo.html.erb @@ -63,16 +63,6 @@ <% if geospatial_coordinates?(@record['locations']) %>

Geospatial coordinates

- <% content_for :additional_meta_tag do %> - - - - - <% end %>
+ <% end %> + <%= render('record_geo') %> <% else %> <%= render('record') %> diff --git a/app/views/search/_result_geo.html.erb b/app/views/search/_result_geo.html.erb index bae04a43..ab13564f 100644 --- a/app/views/search/_result_geo.html.erb +++ b/app/views/search/_result_geo.html.erb @@ -24,39 +24,8 @@ <% if geospatial_coordinates?(result_geo['locations']) %> - <% content_for :additional_meta_tag do %> - - - - - <% end %> -
style="height: 180px">
- - + + <%= render partial: 'shared/map', locals: { result_geo_counter: result_geo_counter, result_geo: result_geo} %>

    diff --git a/app/views/search/results.html.erb b/app/views/search/results.html.erb index 7509fdf1..fd53b133 100644 --- a/app/views/search/results.html.erb +++ b/app/views/search/results.html.erb @@ -1,5 +1,16 @@ <%= content_for(:title, results_page_title(@enhanced_query)) %> +<% content_for :additional_meta_tag do %> + + + + +<% end %> +
    <%= render partial: "shared/site_title" %> diff --git a/app/views/shared/_map.html.erb b/app/views/shared/_map.html.erb new file mode 100644 index 00000000..90194a9e --- /dev/null +++ b/app/views/shared/_map.html.erb @@ -0,0 +1,21 @@ +
    style="height: 180px">
    + + From f05efbc96c8c89ef5e15424e48d76ae1c9ad95ae Mon Sep 17 00:00:00 2001 From: Jeremy Prevost Date: Thu, 31 Oct 2024 14:02:46 -0400 Subject: [PATCH 7/7] Stimulize-it --- app/controllers/record_controller.rb | 1 - app/javascript/controllers/map_controller.js | 28 ++++++++++++++++++++ app/views/record/_record_geo.html.erb | 24 +---------------- app/views/search/_result_geo.html.erb | 2 +- app/views/shared/_map.html.erb | 20 -------------- 5 files changed, 30 insertions(+), 45 deletions(-) create mode 100644 app/javascript/controllers/map_controller.js diff --git a/app/controllers/record_controller.rb b/app/controllers/record_controller.rb index 14e3eecc..9ac8ea3d 100644 --- a/app/controllers/record_controller.rb +++ b/app/controllers/record_controller.rb @@ -16,7 +16,6 @@ def view # Manipulation of returned record would go here... @record = response&.data&.to_h&.dig('recordId') - @rectangle = bounding_box_to_coords(@record) end private diff --git a/app/javascript/controllers/map_controller.js b/app/javascript/controllers/map_controller.js new file mode 100644 index 00000000..eb70e2bd --- /dev/null +++ b/app/javascript/controllers/map_controller.js @@ -0,0 +1,28 @@ +// app/javascript/controllers/map_controller.js +import { Controller } from "@hotwired/stimulus" +// import L from 'leaflet' +export default class extends Controller { + static values = { + coords: Array, + id: String + } + connect() { + // Map logic goes here + // console.log(this.identifier) + // console.log(this.coordsValue) + // console.log(this.idValue) + + var map = L.map(this.idValue); + + L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', { + maxZoom: 19, + attribution: '© OpenStreetMap' + }).addTo(map); + + // create an orange rectangle + L.rectangle(this.coordsValue, {color: "#ff7800", weight: 1}).addTo(map); + + // // zoom the map to the rectangle bounds + map.fitBounds(this.coordsValue); + } +} diff --git a/app/views/record/_record_geo.html.erb b/app/views/record/_record_geo.html.erb index 5b1e53fa..e8c94ea0 100644 --- a/app/views/record/_record_geo.html.erb +++ b/app/views/record/_record_geo.html.erb @@ -64,29 +64,7 @@ <% if geospatial_coordinates?(@record['locations']) %>

    Geospatial coordinates

    -
    - - +
    data-map-id-value="map">

      <% parse_nested_field(@record['locations']).each do |location| %> diff --git a/app/views/search/_result_geo.html.erb b/app/views/search/_result_geo.html.erb index ab13564f..b52148a8 100644 --- a/app/views/search/_result_geo.html.erb +++ b/app/views/search/_result_geo.html.erb @@ -25,7 +25,7 @@ <% if geospatial_coordinates?(result_geo['locations']) %> - <%= render partial: 'shared/map', locals: { result_geo_counter: result_geo_counter, result_geo: result_geo} %> +
      style="height: 180px" data-controller="map" data-map-coords-value=<%= bounding_box_to_coords(result_geo).to_json %> data-map-id-value=<%= "map_#{result_geo_counter}" %>>

        diff --git a/app/views/shared/_map.html.erb b/app/views/shared/_map.html.erb index 90194a9e..8b137891 100644 --- a/app/views/shared/_map.html.erb +++ b/app/views/shared/_map.html.erb @@ -1,21 +1 @@ -
        style="height: 180px">
        -