blacklight-maps 0.3.3 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +2 -0
  3. data/.rubocop.yml +99 -0
  4. data/.solr_wrapper.yml +7 -0
  5. data/.travis.yml +19 -16
  6. data/Gemfile +40 -18
  7. data/README.md +22 -11
  8. data/Rakefile +35 -32
  9. data/app/assets/images/blacklight/maps.svg +1 -0
  10. data/app/assets/javascripts/blacklight-maps.js +1 -4
  11. data/app/assets/javascripts/blacklight-maps/blacklight-maps-browse.js +21 -11
  12. data/app/assets/stylesheets/blacklight_maps/{blacklight-maps.css.scss → blacklight-maps.scss} +1 -2
  13. data/app/assets/stylesheets/blacklight_maps/{default.css.scss → default.scss} +7 -10
  14. data/app/helpers/blacklight/blacklight_maps_helper_behavior.rb +81 -86
  15. data/app/helpers/blacklight_maps_helper.rb +2 -0
  16. data/app/views/catalog/_document_maps.html.erb +1 -1
  17. data/app/views/catalog/{_index_map.html.erb → _index_mapview.html.erb} +1 -1
  18. data/app/views/catalog/_show_maplet_default.html.erb +8 -9
  19. data/app/views/catalog/map.html.erb +2 -2
  20. data/blacklight-maps.gemspec +27 -26
  21. data/config/locales/blacklight-maps-zh.yml +20 -0
  22. data/config/locales/blacklight-maps.ar.yml +21 -0
  23. data/config/locales/blacklight-maps.de.yml +21 -0
  24. data/config/locales/blacklight-maps.en.yml +1 -1
  25. data/config/locales/blacklight-maps.es.yml +21 -0
  26. data/config/locales/blacklight-maps.fr.yml +20 -0
  27. data/config/locales/blacklight-maps.hu.yml +21 -0
  28. data/config/locales/blacklight-maps.it.yml +20 -0
  29. data/config/locales/blacklight-maps.nl.yml +21 -0
  30. data/config/locales/blacklight-maps.pt-BR.yml +21 -0
  31. data/config/locales/blacklight-maps.sq.yml +21 -0
  32. data/config/routes.rb +3 -2
  33. data/lib/blacklight/maps.rb +8 -14
  34. data/lib/blacklight/maps/controller.rb +27 -0
  35. data/lib/blacklight/maps/engine.rb +17 -19
  36. data/lib/blacklight/maps/export.rb +112 -93
  37. data/lib/blacklight/maps/geometry.rb +30 -18
  38. data/lib/blacklight/maps/maps_search_builder.rb +26 -0
  39. data/lib/blacklight/maps/render_constraints_override.rb +63 -30
  40. data/lib/blacklight/maps/version.rb +3 -1
  41. data/lib/generators/blacklight_maps/install_generator.rb +56 -13
  42. data/lib/generators/blacklight_maps/templates/search_history_controller.rb +7 -0
  43. data/{solr_conf → lib/generators/blacklight_maps/templates/solr}/conf/schema.xml +0 -0
  44. data/{solr_conf → lib/generators/blacklight_maps/templates/solr}/conf/solrconfig.xml +0 -0
  45. data/lib/railties/blacklight_maps.rake +13 -9
  46. data/spec/controllers/catalog_controller_spec.rb +20 -10
  47. data/spec/fixtures/sample_solr_documents.yml +955 -852
  48. data/spec/helpers/blacklight_maps_helper_spec.rb +61 -106
  49. data/spec/lib/blacklight/maps/export_spec.rb +109 -143
  50. data/spec/lib/blacklight/maps/geometry_spec.rb +34 -21
  51. data/spec/lib/blacklight/maps/maps_search_builder_spec.rb +42 -0
  52. data/spec/lib/blacklight/maps/render_constraints_override_spec.rb +42 -69
  53. data/spec/spec_helper.rb +19 -21
  54. data/spec/system/index_view_spec.rb +127 -0
  55. data/spec/system/initial_view_spec.rb +28 -0
  56. data/spec/system/map_view_spec.rb +50 -0
  57. data/spec/system/show_view_maplet_spec.rb +78 -0
  58. data/spec/test_app_templates/lib/generators/test_app_generator.rb +8 -14
  59. data/vendor/assets/images/layers-2x.png +0 -0
  60. data/vendor/assets/images/layers.png +0 -0
  61. data/vendor/assets/images/marker-icon-2x.png +0 -0
  62. data/vendor/assets/images/marker-icon.png +0 -0
  63. data/vendor/assets/images/marker-shadow.png +0 -0
  64. data/vendor/assets/javascripts/leaflet.js.erb +13922 -0
  65. data/vendor/assets/javascripts/leaflet.markercluster.js +3 -0
  66. data/vendor/assets/stylesheets/MarkerCluster.Default.css +60 -0
  67. data/vendor/assets/stylesheets/MarkerCluster.css +14 -0
  68. data/vendor/assets/stylesheets/leaflet.css +635 -0
  69. metadata +100 -89
  70. data/config/jetty.yml +0 -7
  71. data/lib/blacklight/maps/controller_override.rb +0 -33
  72. data/spec/features/initial_view_spec.rb +0 -21
  73. data/spec/features/maps_spec.rb +0 -252
  74. data/spec/lib/blacklight/maps/controller_override_spec.rb +0 -34
  75. data/spec/test_app_templates/Gemfile.extra +0 -5
@@ -0,0 +1,20 @@
1
+ fr:
2
+ blacklight:
3
+
4
+ maps:
5
+ interactions:
6
+ bbox_search: 'Afficher les éléments qui coupent avec ce cadre de sélection'
7
+ placename_search: 'Afficher les éléments de cet emplacement'
8
+ item: 'article'
9
+ point_search: 'Afficher les éléments de cet emplacement'
10
+ search_ctrl_cue: 'Rechercher tous les éléments dans la fenêtre de carte actuelle'
11
+ title: 'Carte'
12
+ leader: 'Cliquez sur un marqueur pour rechercher des éléments à partir de cet emplacement, ou utilisez le 🔍 pour rechercher tous les éléments dans la fenêtre de carte actuelle.'
13
+
14
+ search:
15
+ filters:
16
+ coordinates:
17
+ bbox: 'Boîte de délimitation'
18
+ point: 'Coordonnés'
19
+ view:
20
+ maps: 'Carte'
@@ -0,0 +1,21 @@
1
+ hu:
2
+ blacklight:
3
+
4
+ maps:
5
+ interactions:
6
+ bbox_search: 'Tekintse meg azokat az elemeket, amelyek keresztezik ezt a határolódobozt'
7
+ placename_search: 'Tekintse meg az ezen a helyen található elemeket'
8
+ item: 'tétel'
9
+ point_search: 'Tekintse meg az ezen a helyen található elemeket'
10
+ search_ctrl_cue: 'Az összes elem keresése az aktuális térkép ablakban'
11
+ title: 'Térkép'
12
+ leader_html: "Kattintson a jelölőre az adott helyről elemek kereséséhez, vagy használja a 🔍 gombot az összes elem kereséséhez az aktuális térkép ablakban."
13
+
14
+ search:
15
+ filters:
16
+ coordinates:
17
+ bbox: 'Határoló doboz'
18
+ point: 'Koordináták'
19
+ view:
20
+ maps: 'Térkép'
21
+
@@ -0,0 +1,20 @@
1
+ it:
2
+ blacklight:
3
+
4
+ maps:
5
+ interactions:
6
+ bbox_search: 'Visualizza gli elementi che si intersecano con questo rettangolo di selezione'
7
+ placename_search: 'Visualizza articoli da questa posizione'
8
+ item: 'articolo'
9
+ point_search: 'Visualizza articoli da questa posizione'
10
+ search_ctrl_cue: 'Cerca tutti gli elementi nella finestra della mappa corrente'
11
+ title: 'Mappa'
12
+ leader: 'Fai clic su un marcatore per cercare elementi da quella posizione oppure usa 🔍 per cercare tutti gli elementi nella finestra della mappa corrente.'
13
+
14
+ search:
15
+ filters:
16
+ coordinates:
17
+ bbox: 'Rettangolo di selezione'
18
+ point: 'Coordinate'
19
+ view:
20
+ maps: 'Mappa'
@@ -0,0 +1,21 @@
1
+ nl:
2
+ blacklight:
3
+
4
+ maps:
5
+ interactions:
6
+ bbox_search: 'Bekijk items die kruisen met dit selectiekader'
7
+ placename_search: 'Bekijk items van deze locatie'
8
+ item: 'item'
9
+ point_search: 'Bekijk items van deze locatie'
10
+ search_ctrl_cue: 'Zoek naar alle items in het huidige kaartvenster'
11
+ title: 'Kaart'
12
+ leader_html: "Klik op een markering om naar items op die locatie te zoeken of gebruik de 🔍 knop om te zoeken naar alle items in het huidige kaartvenster."
13
+
14
+ search:
15
+ filters:
16
+ coordinates:
17
+ bbox: 'Begrenzende doos'
18
+ point: 'Coördinaten'
19
+ view:
20
+ maps: 'Kaart'
21
+
@@ -0,0 +1,21 @@
1
+ pt-BR:
2
+ blacklight:
3
+
4
+ maps:
5
+ interactions:
6
+ bbox_search: 'Exibir itens que se cruzam com esta caixa delimitadora'
7
+ placename_search: 'Ver itens deste local'
8
+ item: 'item'
9
+ point_search: 'Ver itens deste local'
10
+ search_ctrl_cue: 'Pesquise todos os itens na janela atual do mapa'
11
+ title: 'Mapa'
12
+ leader_html: "Clique em um marcador para procurar itens desse local ou use o 🔍 para procurar todos os itens na janela atual do mapa."
13
+
14
+ search:
15
+ filters:
16
+ coordinates:
17
+ bbox: 'Caixa delimitadora'
18
+ point: 'Coordenadas'
19
+ view:
20
+ maps: 'Mapa'
21
+
@@ -0,0 +1,21 @@
1
+ sq:
2
+ blacklight:
3
+
4
+ maps:
5
+ interactions:
6
+ bbox_search: 'Shikoni artikujt që kryqëzohen me këtë kuti kufizuese'
7
+ placename_search: 'Shikoni artikujt nga ky vendndodhje'
8
+ item: 'artikull'
9
+ point_search: 'Shikoni artikujt nga ky vendndodhje'
10
+ search_ctrl_cue: 'Kërkoni për të gjithë artikujt brenda dritares aktuale të hartës'
11
+ title: 'Hartë'
12
+ leader_html: "Klikoni në një shënues për të kërkuar artikuj nga ai vend, ose përdorni 🔍 butoni për të kërkuar të gjitha artikujt brenda dritares aktuale të hartës."
13
+
14
+ search:
15
+ filters:
16
+ coordinates:
17
+ bbox: 'Kuti kufizuese'
18
+ point: 'Koordinon'
19
+ view:
20
+ maps: 'Hartë'
21
+
@@ -1,4 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Rails.application.routes.draw do
2
- get 'map', :to => 'catalog#map', :as => 'map'
4
+ get 'map', to: 'catalog#map', as: 'map'
3
5
  end
4
-
@@ -1,25 +1,19 @@
1
- require "blacklight/maps/version"
1
+ # frozen_string_literal: true
2
+
3
+ require 'blacklight/maps/version'
2
4
 
3
5
  module Blacklight
4
6
  module Maps
5
- require 'blacklight/maps/controller_override'
7
+ require 'blacklight/maps/controller'
6
8
  require 'blacklight/maps/render_constraints_override'
7
9
  require 'blacklight/maps/engine'
8
10
  require 'blacklight/maps/export'
9
11
  require 'blacklight/maps/geometry'
12
+ require 'blacklight/maps/maps_search_builder'
10
13
 
11
- def self.inject!
12
- CatalogController.send(:include, BlacklightMaps::ControllerOverride)
13
- CatalogController.send(:include, BlacklightMaps::RenderConstraintsOverride)
14
- CatalogController.send(:helper, BlacklightMaps::RenderConstraintsOverride) unless
15
- CatalogController.helpers.is_a?(BlacklightMaps::RenderConstraintsOverride)
16
-
17
- # inject into SearchHistory and SavedSearches so spatial queries display properly
18
- SearchHistoryController.send(:helper, BlacklightMaps::RenderConstraintsOverride) unless
19
- SearchHistoryController.helpers.is_a?(BlacklightMaps::RenderConstraintsOverride)
20
- SavedSearchesController.send(:helper, BlacklightMaps::RenderConstraintsOverride) unless
21
- SavedSearchesController.helpers.is_a?(BlacklightMaps::RenderConstraintsOverride)
14
+ # returns the full path to the blacklight plugin installation
15
+ def self.root
16
+ @root ||= File.expand_path(File.dirname(File.dirname(File.dirname(__FILE__))))
22
17
  end
23
-
24
18
  end
25
19
  end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BlacklightMaps
4
+ module Controller
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ helper BlacklightMaps::RenderConstraintsOverride
9
+ end
10
+
11
+ def map
12
+ (@response, @document_list) = search_service.search_results
13
+ params[:view] = 'maps'
14
+ respond_to do |format|
15
+ format.html
16
+ end
17
+ end
18
+
19
+ ##
20
+ # BlacklightMaps override: update to look for spatial query params
21
+ # Check if any search parameters have been set
22
+ # @return [Boolean]
23
+ def has_search_parameters?
24
+ params[:coordinates].present? || super
25
+ end
26
+ end
27
+ end
@@ -1,33 +1,31 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'blacklight'
2
- require 'leaflet-rails'
3
- require 'leaflet-markercluster-rails'
4
4
 
5
5
  module Blacklight
6
6
  module Maps
7
7
  class Engine < Rails::Engine
8
8
  # Set some default configurations
9
- Blacklight::Configuration.default_values[:view].maps.geojson_field = "geojson"
10
- Blacklight::Configuration.default_values[:view].maps.placename_property = "placename"
11
- Blacklight::Configuration.default_values[:view].maps.coordinates_field = "coordinates"
12
- Blacklight::Configuration.default_values[:view].maps.search_mode = "placename" # or 'coordinates'
13
- Blacklight::Configuration.default_values[:view].maps.spatial_query_dist = 0.5
14
- Blacklight::Configuration.default_values[:view].maps.placename_field = "placename_field"
15
- Blacklight::Configuration.default_values[:view].maps.coordinates_facet_field = "coordinates_facet_field"
16
- Blacklight::Configuration.default_values[:view].maps.facet_mode = "geojson" # or 'coordinates'
17
- Blacklight::Configuration.default_values[:view].maps.tileurl = "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
18
- Blacklight::Configuration.default_values[:view].maps.mapattribution = 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>'
19
- Blacklight::Configuration.default_values[:view].maps.maxzoom = 18
20
- Blacklight::Configuration.default_values[:view].maps.show_initial_zoom = 5
9
+ initializer 'blacklight-maps.default_config' do |_app|
10
+ Blacklight::Configuration.default_values[:view].maps.geojson_field = 'geojson_ssim'
11
+ Blacklight::Configuration.default_values[:view].maps.placename_property = 'placename'
12
+ Blacklight::Configuration.default_values[:view].maps.coordinates_field = 'coordinates_srpt'
13
+ Blacklight::Configuration.default_values[:view].maps.search_mode = 'placename' # or 'coordinates'
14
+ Blacklight::Configuration.default_values[:view].maps.spatial_query_dist = 0.5
15
+ Blacklight::Configuration.default_values[:view].maps.placename_field = 'subject_geo_ssim'
16
+ Blacklight::Configuration.default_values[:view].maps.coordinates_facet_field = 'coordinates_ssim'
17
+ Blacklight::Configuration.default_values[:view].maps.facet_mode = 'geojson' # or 'coordinates'
18
+ Blacklight::Configuration.default_values[:view].maps.tileurl = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
19
+ Blacklight::Configuration.default_values[:view].maps.mapattribution = 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>'
20
+ Blacklight::Configuration.default_values[:view].maps.maxzoom = 18
21
+ Blacklight::Configuration.default_values[:view].maps.show_initial_zoom = 5
22
+ end
21
23
 
22
24
  # Add our helpers
23
- initializer 'blacklight-maps.helpers' do |app|
25
+ initializer 'blacklight-maps.helpers' do |_app|
24
26
  ActionView::Base.send :include, BlacklightMapsHelper
25
27
  end
26
28
 
27
- config.to_prepare do
28
- Blacklight::Maps.inject!
29
- end
30
-
31
29
  # This makes our rake tasks visible.
32
30
  rake_tasks do
33
31
  Dir.chdir(File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))) do
@@ -1,5 +1,6 @@
1
- module BlacklightMaps
1
+ # frozen_string_literal: true
2
2
 
3
+ module BlacklightMaps
3
4
  # This class provides the ability to export a response document to GeoJSON.
4
5
  # The export is formated as a GeoJSON FeatureCollection, where the features
5
6
  # consist of an array of Point features. For more on the GeoJSON
@@ -8,146 +9,164 @@ module BlacklightMaps
8
9
  class GeojsonExport
9
10
  include BlacklightMaps
10
11
 
11
- # controller is a Blacklight CatalogController object passed by a helper
12
- # action is the controller action
13
- # response_docs is passed by a helper, and is either:
12
+ # @param controller [CatalogController]
13
+ # @param action [Symbol] the controller action
14
+ # @param response_docs [Array || SolrDocument] either:
14
15
  # - index view, map view: an array of facet values
15
16
  # - show view: the document object
16
- # options is an optional hash of possible configuration options
17
- def initialize(controller, action, response_docs, options={})
17
+ # @param options [Hash] optional hash of configuration options
18
+ def initialize(controller, action, response_docs, options = {})
18
19
  @controller = controller
19
20
  @action = action
20
21
  @response_docs = response_docs
21
22
  @options = options
23
+ @features = []
22
24
  end
23
25
 
24
- # build the GeoJSON FeatureCollection
26
+ # builds the GeoJSON FeatureCollection
25
27
  def to_geojson
26
- geojson_docs = { type: 'FeatureCollection',
27
- features: build_geojson_features }
28
- geojson_docs.to_json
28
+ { type: 'FeatureCollection', features: build_geojson_features }.to_json
29
29
  end
30
30
 
31
31
  private
32
32
 
33
- def blacklight_maps_config
33
+ def maps_config
34
34
  @controller.blacklight_config.view.maps
35
35
  end
36
36
 
37
37
  def geojson_field
38
- blacklight_maps_config.geojson_field
38
+ maps_config.geojson_field
39
39
  end
40
40
 
41
41
  def coordinates_field
42
- blacklight_maps_config.coordinates_field
42
+ maps_config.coordinates_field
43
43
  end
44
44
 
45
- def search_mode
46
- blacklight_maps_config.search_mode
45
+ def build_geojson_features
46
+ if @action == :index || @action == :map
47
+ build_index_features
48
+ elsif @action == :show
49
+ build_show_features
50
+ end
51
+ @features
47
52
  end
48
53
 
49
- def facet_mode
50
- blacklight_maps_config.facet_mode
54
+ # build GeoJSON features array for index and map views
55
+ def build_index_features
56
+ @response_docs.each do |geofacet|
57
+ @features << if maps_config.facet_mode == 'coordinates'
58
+ build_feature_from_coords(geofacet.value, geofacet.hits)
59
+ else
60
+ build_feature_from_geojson(geofacet.value, geofacet.hits)
61
+ end
62
+ end
51
63
  end
52
64
 
53
- def placename_property
54
- blacklight_maps_config.placename_property
65
+ # build GeoJSON features array for show view
66
+ def build_show_features
67
+ doc = @response_docs
68
+ return unless doc[geojson_field] || doc[coordinates_field]
69
+
70
+ if doc[geojson_field]
71
+ build_features_from_geojson(doc[geojson_field])
72
+ elsif doc[coordinates_field]
73
+ build_features_from_coords(doc[coordinates_field])
74
+ end
55
75
  end
56
76
 
57
- # build GeoJSON features array
58
- # determine how to build GeoJSON feature based on config and controller#action
59
- def build_geojson_features
60
- features = []
61
- case @action
62
- when "index", "map"
63
- @response_docs.each do |geofacet|
64
- if facet_mode == "coordinates"
65
- features.push(build_feature_from_coords(geofacet.value, geofacet.hits))
66
- else
67
- features.push(build_feature_from_geojson(geofacet.value, geofacet.hits))
68
- end
69
- end
70
- when "show"
71
- doc = @response_docs
72
- return unless doc[geojson_field] || doc[coordinates_field]
73
- if doc[geojson_field]
74
- doc[geojson_field].uniq.each do |loc|
75
- features.push(build_feature_from_geojson(loc))
76
- end
77
- elsif doc[coordinates_field]
78
- doc[coordinates_field].uniq.each do |coords|
79
- features.push(build_feature_from_coords(coords))
80
- end
81
- end
77
+ def build_features_from_geojson(geojson_field_values)
78
+ return unless geojson_field_values
79
+
80
+ geojson_field_values.uniq.each do |loc|
81
+ @features << build_feature_from_geojson(loc)
82
82
  end
83
- features
84
83
  end
85
84
 
86
- # build blacklight-maps GeoJSON feature from GeoJSON-formatted data
85
+ def build_features_from_coords(coordinates_field_values)
86
+ return unless coordinates_field_values
87
+
88
+ coordinates_field_values.uniq.each do |coords|
89
+ @features << build_feature_from_coords(coords)
90
+ end
91
+ end
92
+
93
+ # build GeoJSON feature from incoming GeoJSON data
87
94
  # turn bboxes into points for index view so we don't get weird mix of boxes and markers
95
+ # @param loc [Hash]
96
+ # @param hits [Integer]
88
97
  def build_feature_from_geojson(loc, hits = nil)
89
- geojson_hash = JSON.parse(loc).deep_symbolize_keys
90
- if @action != "show" && geojson_hash[:bbox]
91
- geojson_hash[:geometry][:coordinates] = Geometry::Point.new(Geometry::BoundingBox.new(geojson_hash[:bbox]).find_center).normalize_for_search
92
- geojson_hash[:geometry][:type] = "Point"
93
- geojson_hash.delete(:bbox)
98
+ geojson = JSON.parse(loc).deep_symbolize_keys
99
+ if @action != :show && geojson[:bbox]
100
+ bbox = Geometry::BoundingBox.new(geojson[:bbox])
101
+ geojson[:geometry][:coordinates] = Geometry::Point.new(bbox.find_center).normalize_for_search
102
+ geojson[:geometry][:type] = 'Point'
103
+ geojson.delete(:bbox)
94
104
  end
95
- geojson_hash[:properties] ||= {}
96
- geojson_hash[:properties][:hits] = hits.to_i if hits
97
- geojson_hash[:properties][:popup] = render_leaflet_popup_content(geojson_hash, hits)
98
- geojson_hash
105
+ geojson[:properties] ||= {}
106
+ geojson[:properties][:hits] = hits.to_i if hits
107
+ geojson[:properties][:popup] = render_leaflet_popup_content(geojson, hits)
108
+ geojson
99
109
  end
100
110
 
101
- # build blacklight-maps GeoJSON feature from coordinate data
111
+ # build GeoJSON feature from incoming raw coordinate data
102
112
  # turn bboxes into points for index view so we don't get weird mix of boxes and markers
113
+ # @param coords [String]
114
+ # @param hits [Integer]
103
115
  def build_feature_from_coords(coords, hits = nil)
104
- geojson_hash = {type: "Feature", geometry: {}, properties: {}}
105
- if coords.scan(/[\s]/).length == 3 # bbox
106
- if @action != "show"
107
- geojson_hash[:geometry][:type] = "Point"
108
- geojson_hash[:geometry][:coordinates] = Geometry::Point.new(Geometry::BoundingBox.from_lon_lat_string(coords).find_center).normalize_for_search
109
- else
110
- coords_array = coords.split(' ').map { |v| v.to_f }
111
- geojson_hash[:bbox] = coords_array
112
- geojson_hash[:geometry][:type] = "Polygon"
113
- geojson_hash[:geometry][:coordinates] = [[[coords_array[0],coords_array[1]],
114
- [coords_array[2],coords_array[1]],
115
- [coords_array[2],coords_array[3]],
116
- [coords_array[0],coords_array[3]],
117
- [coords_array[0],coords_array[1]]]]
118
- end
119
- elsif coords.match(/^[-]?[\d]*[\.]?[\d]*[ ,][-]?[\d]*[\.]?[\d]*$/) # point
120
- geojson_hash[:geometry][:type] = "Point"
121
- if coords.match(/,/)
122
- coords_array = coords.split(',').reverse
123
- else
124
- coords_array = coords.split(' ')
125
- end
126
- geojson_hash[:geometry][:coordinates] = coords_array.map { |v| v.to_f }
116
+ geojson = { type: 'Feature', properties: {} }
117
+ if coords =~ /ENVELOPE/ # bbox
118
+ geojson.merge!(build_bbox_feature_from_coords(coords))
119
+ elsif coords =~ /^[-]?[\d]*[\.]?[\d]*[ ,][-]?[\d]*[\.]?[\d]*$/ # point
120
+ geojson[:geometry] = build_point_geometry(coords)
127
121
  else
128
122
  Rails.logger.error("This coordinate format is not yet supported: '#{coords}'")
129
123
  end
130
- geojson_hash[:properties] = { popup: render_leaflet_popup_content(geojson_hash, hits) } if geojson_hash[:geometry][:coordinates]
131
- geojson_hash[:properties][:hits] = hits.to_i if hits
132
- geojson_hash
124
+ geojson[:properties] = { popup: render_leaflet_popup_content(geojson, hits) } if geojson[:geometry][:coordinates]
125
+ geojson[:properties][:hits] = hits.to_i if hits
126
+ geojson
127
+ end
128
+
129
+ # @param coords [String]
130
+ def build_bbox_feature_from_coords(coords)
131
+ geojson = { geometry: {} }
132
+ bbox = Geometry::BoundingBox.from_wkt_envelope(coords)
133
+ if @action != :show
134
+ geojson[:geometry][:type] = 'Point'
135
+ geojson[:geometry][:coordinates] = Geometry::Point.new(bbox.find_center).normalize_for_search
136
+ else
137
+ coords_array = bbox.to_a
138
+ geojson[:bbox] = coords_array
139
+ geojson[:geometry][:type] = 'Polygon'
140
+ geojson[:geometry][:coordinates] = bbox.geojson_geometry_array
141
+ end
142
+ geojson
143
+ end
144
+
145
+ # @param coords [String]
146
+ def build_point_geometry(coords)
147
+ geometry = { type: 'Point' }
148
+ coords_array = coords =~ /,/ ? coords.split(',').reverse : coords.split(' ')
149
+ geometry[:coordinates] = coords_array.map(&:to_f)
150
+ geometry
133
151
  end
134
152
 
135
- # Render to string the partial for each individual doc.
153
+ # Render to string the partial for each individual feature.
136
154
  # For placename searching, render catalog/map_placename_search partial,
137
- # full geojson hash is passed to the partial for easier local customization
155
+ # pass the full geojson hash to the partial for easier local customization
138
156
  # For coordinate searches (or features with only coordinate data),
139
157
  # render catalog/map_coordinate_search partial
140
- def render_leaflet_popup_content(geojson_hash, hits=nil)
141
- if search_mode == 'placename' && geojson_hash[:properties][placename_property.to_sym]
142
- @controller.render_to_string partial: 'catalog/map_placename_search',
143
- locals: { geojson_hash: geojson_hash, hits: hits }
158
+ # @param geojson [Hash]
159
+ # @param hits [Integer]
160
+ def render_leaflet_popup_content(geojson, hits = nil)
161
+ if maps_config.search_mode == 'placename' &&
162
+ geojson[:properties][maps_config.placename_property.to_sym]
163
+ partial = 'catalog/map_placename_search'
164
+ locals = { geojson_hash: geojson, hits: hits }
144
165
  else
145
- @controller.render_to_string partial: 'catalog/map_spatial_search',
146
- locals: { coordinates: geojson_hash[:bbox].presence || geojson_hash[:geometry][:coordinates],
147
- hits: hits }
166
+ partial = 'catalog/map_spatial_search'
167
+ locals = { coordinates: geojson[:bbox].presence || geojson[:geometry][:coordinates], hits: hits }
148
168
  end
169
+ @controller.render_to_string(partial: partial, locals: locals)
149
170
  end
150
-
151
171
  end
152
-
153
172
  end