Skip to content
This repository has been archived by the owner on Jun 11, 2019. It is now read-only.

Commit

Permalink
calculate country geolocation from areas
Browse files Browse the repository at this point in the history
  • Loading branch information
jnicho02 committed Sep 23, 2014
1 parent adc0e07 commit 3f08c3f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/controllers/countries_controller.rb
Expand Up @@ -10,7 +10,7 @@ def index
respond_to do |format|
format.html
format.xml
format.json { render :json => @countries }
format.json { render :json => @countries.as_json }
end
end

Expand Down Expand Up @@ -38,7 +38,7 @@ def show
respond_to do |format|
format.html
format.xml
format.json { render :json => @country }
format.json { render :json => @country.as_json }
format.kml { render "plaques/index" }
format.osm { render "plaques/index" }
format.csv { render "plaques/index" }
Expand Down
39 changes: 35 additions & 4 deletions app/models/country.rb
Expand Up @@ -21,12 +21,29 @@ class Country < ActiveRecord::Base
has_many :locations, :through => :areas
has_many :plaques, :through => :locations

@@latitude = nil
@@longitude = nil

include PlaquesHelper

def find_centre
if !geolocated?
@mean = find_mean(self.areas)
@@latitude = @mean.latitude
@@longitude = @mean.longitude
end
end

def geolocated?
return !(@@latitude == nil || @@longitude == nil || @@latitude == 51.475 && @@longitude == 0)
end

def latitude
52
@@latitude
end

def longitude
0
@@longitude
end

def zoom
Expand All @@ -47,11 +64,25 @@ def to_s
end

def as_json(options={})
super(options.merge(
find_centre
default_options = {
:only => [:name, :uri, :dbpedia_uri],
:include => { :areas => {:only => [:name], :methods => :uri}},
:methods => [:uri]
))
}
{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [@@longitude, @@latitude]
},
properties:
if options.size > 0
super(options)
else
super(default_options)
end
}
end

end

0 comments on commit 3f08c3f

Please sign in to comment.