Skip to content

Commit 399bfcb

Browse files
committed
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
1 parent f6a5870 commit 399bfcb

File tree

5 files changed

+105
-34
lines changed

5 files changed

+105
-34
lines changed

app/controllers/record_controller.rb

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,11 @@ def view
1616
# Manipulation of returned record would go here...
1717

1818
@record = response&.data&.to_h&.dig('recordId')
19-
@rectangle = bounding_box_to_coords
19+
@rectangle = bounding_box_to_coords(@record)
2020
end
2121

2222
private
2323

24-
# Converts a bounding box into a top left, bottom right set of coordinates
25-
def bounding_box_to_coords
26-
return unless @record.present?
27-
return unless geospatial_coordinates?(@record['locations'])
28-
29-
# Our preference is to use the `Bounding Box` kind
30-
raw_bbox = @record['locations'].select { |l| l if l['kind'] == 'Bounding Box' }.first
31-
32-
# If we had no `Bounding Box` kind, see if we have a `Geometry kind`
33-
if raw_bbox.blank?
34-
raw_bbox = @record['locations'].select { |l| l if l['kind'] == 'Geometry' }.first
35-
end
36-
37-
return unless raw_bbox.present?
38-
39-
# extract just the geo coordinates and remove the extra syntax
40-
bbox = raw_bbox['geoshape'].sub('BBOX (', '').sub(')', '')
41-
42-
# conver the string into an array of floats
43-
bbox_array = bbox.split(',').map!(&:strip).map!(&:to_f)
44-
45-
# Protect against unexpected data
46-
if bbox_array.count != 4
47-
Rails.logger.info("Unexpected Bounding Box: #{raw_bbox}")
48-
return
49-
end
50-
51-
coords = [[bbox_array[2], bbox_array[0]], [bbox_array[3], bbox_array[1]]]
52-
Rails.logger.info("Raw BBox: #{raw_bbox}")
53-
Rails.logger.info("Rectangle: #{coords}")
54-
coords
55-
end
56-
5724
def validate_id!
5825
return if params[:id]&.strip.present?
5926

app/helpers/record_helper.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,39 @@ def deduplicate_subjects(subjects)
146146
subjects.map { |subject| subject['value'].uniq(&:downcase) }.uniq { |values| values.map(&:downcase) }
147147
end
148148

149+
# Converts a bounding box into a top left, bottom right set of coordinates
150+
def bounding_box_to_coords(record)
151+
return unless record.present?
152+
return unless geospatial_coordinates?(record['locations'])
153+
154+
# Our preference is to use the `Bounding Box` kind
155+
raw_bbox = record['locations'].select { |l| l if l['kind'] == 'Bounding Box' }.first
156+
157+
# If we had no `Bounding Box` kind, see if we have a `Geometry kind`
158+
if raw_bbox.blank?
159+
raw_bbox = record['locations'].select { |l| l if l['kind'] == 'Geometry' }.first
160+
end
161+
162+
return unless raw_bbox.present?
163+
164+
# extract just the geo coordinates and remove the extra syntax
165+
bbox = raw_bbox['geoshape'].sub('BBOX (', '').sub(')', '')
166+
167+
# conver the string into an array of floats
168+
bbox_array = bbox.split(',').map!(&:strip).map!(&:to_f)
169+
170+
# Protect against unexpected data
171+
if bbox_array.count != 4
172+
Rails.logger.info("Unexpected Bounding Box: #{raw_bbox}")
173+
return
174+
end
175+
176+
coords = [[bbox_array[2], bbox_array[0]], [bbox_array[3], bbox_array[1]]]
177+
Rails.logger.info("Raw BBox: #{raw_bbox}")
178+
Rails.logger.info("Rectangle: #{coords}")
179+
coords
180+
end
181+
149182
private
150183

151184
def render_kind_value(list)

app/models/timdex_search.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ class TimdexSearch < TimdexBase
6767
text
6868
url
6969
}
70+
locations {
71+
geoshape
72+
kind
73+
value
74+
}
7075
notes {
7176
kind
7277
value
@@ -199,6 +204,11 @@ class TimdexSearch < TimdexBase
199204
text
200205
url
201206
}
207+
locations {
208+
geoshape
209+
kind
210+
value
211+
}
202212
notes {
203213
kind
204214
value
@@ -325,6 +335,11 @@ class TimdexSearch < TimdexBase
325335
text
326336
url
327337
}
338+
locations {
339+
geoshape
340+
kind
341+
value
342+
}
328343
notes {
329344
kind
330345
value
@@ -461,6 +476,11 @@ class TimdexSearch < TimdexBase
461476
text
462477
url
463478
}
479+
locations {
480+
geoshape
481+
kind
482+
value
483+
}
464484
notes {
465485
kind
466486
value

app/views/record/_record_geo.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<div id="map" style="height: 180px"></div>
7878
<script>
7979
var map = L.map('map');
80+
8081
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
8182
maxZoom: 19,
8283
attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'

app/views/search/_result_geo.html.erb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,56 @@
2121
</p>
2222
<% end %>
2323

24+
25+
<!-- We only care about geospatial locations for this, as place names are also subjects. -->
26+
<% if geospatial_coordinates?(result_geo['locations']) %>
27+
<% content_for :additional_meta_tag do %>
28+
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
29+
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
30+
crossorigin=""/>
31+
32+
<!-- Make sure you put this AFTER Leaflet's CSS -->
33+
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"
34+
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
35+
crossorigin=""></script>
36+
<% end %>
37+
<div id=<%= "map_#{result_geo_counter}" %> style="height: 180px"></div>
38+
39+
<script>
40+
var <%= "map_#{result_geo_counter}" %> = L.map('<%= "map_#{result_geo_counter}" %>');
41+
42+
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
43+
maxZoom: 19,
44+
attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
45+
}).addTo(<%= "map_#{result_geo_counter}" %>);
46+
47+
// define rectangle geographical bounds
48+
// -71.158693, -71.064796, 42.395972, 42.351993
49+
// var bounds = [[42.395972, -71.158693],[42.351993, -71.064796]];
50+
51+
console.log('#<%= bounding_box_to_coords(result_geo) %>');
52+
var bounds = <%= bounding_box_to_coords(result_geo) %>;
53+
54+
// create an orange rectangle
55+
L.rectangle(bounds, {color: "#ff7800", weight: 1}).addTo(<%= "map_#{result_geo_counter}" %>);
56+
57+
// zoom the map to the rectangle bounds
58+
<%= "map_#{result_geo_counter}" %>.fitBounds(bounds);
59+
</script>
60+
61+
<p>
62+
<ul>
63+
<% parse_nested_field(result_geo['locations']).each do |location| %>
64+
<% if location['geoshape'].present? %>
65+
<li><%= "#{location['kind']}: #{location['geoshape']}" %></li>
66+
<% end %>
67+
<% end %>
68+
</ul>
69+
</p>
70+
<% else %>
71+
Hallo
72+
<% end %>
73+
2474
<% if result_geo['highlight'] %>
2575
<div class="result-highlights">
2676
<%= render partial: 'search/highlights', locals: { result: result_geo } %>

0 commit comments

Comments
 (0)