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/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/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 aa247a62..e8c94ea0 100644 --- a/app/views/record/_record_geo.html.erb +++ b/app/views/record/_record_geo.html.erb @@ -60,6 +60,22 @@ <% end %> <% end %> + + <% if geospatial_coordinates?(@record['locations']) %> +
+
+