geospatial searching

This commit is contained in:
2017-12-17 22:26:09 +01:00
parent de231092a2
commit 5f9ea11d77
9 changed files with 83 additions and 1792 deletions
+4
View File
@@ -38,6 +38,10 @@
.alert-notice
@extend .alert-info
.form-inline > *
margin: 5px
body.admin
background: repeating-linear-gradient(45deg, yellow, yellow 50px, black 50px, black 100px)
@@ -5,7 +5,22 @@ class Network::SignatureCollectionsController < ApplicationController
# GET /network/signature_collections
# GET /network/signature_collections.json
def index
@query, @location, @distance = params[:query], params[:location], params[:distance]
@network_signature_collections = Network::SignatureCollection.where(:event_date_start.gte => DateTime.now)
@network_signature_collections = @network_signature_collections.or(location_zip_s: /#{@query}/) if @query
@network_signature_collections = @network_signature_collections.or(location_name: /#{@query}/) if @query
@network_signature_collections = @network_signature_collections.or(location_address: /#{@query}/) if @query
distance_from_location
end
def distance_from_location
return unless @location && @distance
location_coordinates = Geocoder.coordinates(@location)
return unless location_coordinates # only filter when coordinates found
@network_signature_collections = @network_signature_collections.geo_near(location_coordinates.reverse).max_distance(@distance.to_f/111)
end
# GET /network/signature_collections/1
@@ -1,8 +1,30 @@
class Network::SignatureCollection
include Mongoid::Document
include Geocoder::Model::Mongoid
field :location_name, type: String
field :location_zip, type: Integer
field :location_zip_s, type: String
field :location_address, type: String
field :event_date_start, type: Time
field :event_date_end, type: Time
field :coordinates, type: Array
geocoded_by :address
before_save :update_location # auto-fetch coordinates
before_save :stringify_zip # auto-fetch coordinates
def address
"#{location_zip} #{location_name}, Switzerland"
end
def update_location
geocode
end
def stringify_zip
self.location_zip_s = self.location_zip.to_s
end
index({coordinates: "2d"})
end
@@ -1,6 +1,21 @@
h1 = title t('.title')
#signature_collections_table
input.search.form-control.string placeholder="#{t('.search')}"
css:
.form-inline > * {
margin: 5px;
}
form
.form-inline
input.form-control placeholder="#{t('.search')}" name="query" value="#{@query}"
input.form-control placeholder="#{t('.location')}" name="location" value="#{@location}"
select.form-control name="distance"
- [5, 10, 20, 50, 100, 150, 200].each do | d |
- if d == @distance.to_i
option value="#{d}" selected="selected" #{d} Km
- else
option value="#{d}" #{d} Km
input.btn.btn-primary type="submit" value="#{t('.search')}"
table.table
thead
tr
@@ -30,10 +45,3 @@ h1 = title t('.title')
br
= link_to t('.new'), new_network_signature_collection_path
javascript:
var options = {
valueNames: [ 'location_name', 'location_zip', 'location_address', 'event_date_start', 'event_date_end' ]
};
var signature_collections = new List('signature_collections_table', options);
+1
View File
@@ -119,6 +119,7 @@ de:
new: Neue Sammlung erfasssen
title: Unterschriftensammlungen
search: Suche
location: Ort
routes:
initiative_text: initiativtext
arguments: argumentarium
+2 -1
View File
@@ -7,8 +7,9 @@
# Mayor.create(name: 'Emanuel', city: cities.first)
@cities = CSV.read Rails.root.join('db', 'plz_ch.csv')
@cities.sample(122).each do |city|
tmp_date = DateTime.now + rand(50)
Network::SignatureCollection.create(location_zip: city.first, location_name: city.last, location_address: Faker::Address.street_address, event_date_start: tmp_date, event_date_end: tmp_date+rand(10).to_f/10)
end
# Network::SignatureCollection.where(location: {"$near" => x.location, '$maxDistance' => 1.fdiv(111.12)}).map &:location_name
-1758
View File
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long