I'd like to check if the country name provided belongs to Europe.
Does anyone know of a list of european countries that can be used in a Ruby project?
I'd like to do something like this:
spain = Country.named('Spain')
spain.parent # => 'Europe'
japan = Country.named('Japan')
japan.parent # => 'Asia'
https://github.com/jim/carmen/ lets me list subregions of a country, but not a country's parent.
Try that gem if you really need whole gem for this. You should be able to do:
Country.find_all_countries_by_region('Europe')
to get countries in Europe.
Check out the Wikipedia article for a complete list. http://en.wikipedia.org/wiki/List_of_European_countries
Related
I am trying to group records based on a value from a column, so I can use it to display the information elsewhere. At the moment I have this working if I specify the values in the column -
#city_count = People.select('city,count(*)').where("city in ('london', 'paris')").group(:city).count
This works fine if I want a list of people in London and Paris but if the city list also has Sydney, New York, Rio etc I don't want to keep adding the extra cities to the 'city in', I would like this to just find the people selected by each city.
Does anyone know the best way of doing this? Also if it can include NULL values as well.
Just use:
#city_count = People.group(:city).count
to get counts for all cities. This will include an entry for nil.
A more efficient way would be to use the distinct and count methods together.
#city_counts = Person.distinct.count(:city)
That way the work is done in the db instead of in Ruby.
I have latitude and longitude of an address and I want to search all the airports, Railway stations, Bus Stand and cafe etc popular places. I'm using these gems in my application :
gem 'geocoder'
gem 'gmaps4rails'
gem 'geokit-rails
Now Please suggest me how can I solve this problem?
We can pass the search parameters in type (these search strings are suggested by google for places)
lat="30.987674"
lng="-45.098753"
doc = JSON.parse(open("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=#{lat},#{lng}&radius=50&type=['cafe']&name=cruise&key=xxxxxx").read)
cords=[]
doc['results'].each do |r|
tmp={}
tmp = r['geometry']['location']
tmp["name"] = r['name']
cords<< r['geometry']['location']
end
Here cords is the array of hashes with relative information about places i.e. cafes.
See : https://developers.google.com/places/web-service/search
https://developers.google.com/places/supported_types
I would actually use the near function of geocoder, assuming you use ActiveRecord.
We made a simple query which gets some cities:
SELECT * FROM `allCountries` WHERE name='Moscow' and `country_code` = 'RU'
Here is the result of this query:
For example, for another city we get a result with 4-7 rows.
How to get all areas/regions for a country and then get all cities for this area/region?
P.S.: Please be careful. We are not interested in an API site and database fetch. Thanks!
Background
In Geonames you have feature_classes and feature_codes which discriminate the location type. You can find detailed description of the code in the Geonames website. As in your snapshot, P.PPLC means "City (populated place) which is capital of a political entity" and S.HLC means "building (spot) hotel".
Also, every geoname have properties to identify the location in the "hierarchy" inside a country; this properties are country_code, admin1_code, admin2_code, admin3_code, admin4_code. Note that not all properties are used for every given geoname, since this depends on the political organization of a country.
Find all city inside an administrative level
To find all city inside an area (i.e. administrative level), you must first search the geoname for that admin level, in order to have the admin codes useful to filter the city query.
To find an admin level, you must first execute a query like:
SELECT *
FROM `allCountries`
WHERE `country_code` = 'RU'
AND `feature_class`='A'
AND `feature_code`='ADM1'
Note that the query filter out only the first admin levels (feature_code='ADM1'), but you can find admin level of any depth by changing it to:
SELECT *
FROM `geonames`
WHERE `country_code` = 'RU'
AND `feature_class`='A'
AND `feature_code` LIKE 'ADM_'
Now, select one record from this result set and you it to search for the cities, by using the "hierarchy" codes of that level. You should use something like (mutatis mutandis):
SELECT *
FROM `geonames`
WHERE `country_code` = "RU"
AND `feature_class`='P'
AND `feature_code` LIKE 'PPL%'
AND `admin1_code`="<admin1>"
AND `admin2_code`="<admin2>"
AND `admin3_code`="<admin3>"
AND `admin4_code`="<admin4>"
Beware of NULL admin codes, which you need to strip out from the SQL (the whole "AND ..." clause).
Of course, you can do the original "Moscow" search inside this filtered set.
The answer for your question is pretty long, but this code snippet may help you a little bit. These queries obtain all hierarchy information about given geonameid (it's plpython inside postgres).
get_geoname = plpy.prepare("SELECT geonameid, asciiname, country, admin1, admin2 FROM all_countries where geonameid=$1",
["integer"])
get_country_name = plpy.prepare("SELECT name as country from country_info where code = upper($1)", ["varchar"])
get_admin1 = plpy.prepare("SELECT asciiname, name FROM admin1 where code = $1", ["text"])
get_admin2 = plpy.prepare("SELECT asciiname, name FROM admin2 where code = $1", ["text"])
I'm looking for a way to display my facets in a grouped list. For example i have some users and a facet to filter by country, this gives me:
Country
Holland (5)
England (2)
Egypt (5)
Rwanda (2)
And what i would like to have is:
Europe
Holland (5)
England (2)
Africa
Egypt (5)
Rwanda (2)
I'm using the Tire gem in a Rails application, my models and relations are like this: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-option_groups_from_collection_for_select
I've googled for an example on this for hours, just can't find anything for nested /grouped facets what makes sense to me in Elasticsearch. Hope someone can help me in the right direction! Many thanks in advance!
Daniel
You can probably handle this by indexing the data in two ways and then just parse the outout. Use the "object" version to apply filters, and get your parsing version as a facet to display filters.
For example:
"mydocument":{
"attributes":[
"location":{
{"continent":"europe",
"country":"england"
},
"fur_style":"long"
],
"facets":[
{"location":"Europe##england"},
{"fur_style":"long"
]
}
when you get your data back, you'll have:
"facets":[
{"location":"Europe##england",
"total":5},
{"location":"africa##egypt",
"total":7}
{"fur_style":"long",
"total":3}
etc etc
]
In your application, you just have to loop through and break apart the terms using the ## delimiter (or whatever you want it to be).
I have a destination model with following attributes :-
1) Continent 2) Country 3) City
I am doing something like this in the view :-
-#destinations.each do |d|
=d.continent
=d.country
=d.city
I the result to be grouped together i.e :- I want all the cities which are in a country together and all the country which are in a continent. I want to display something like this :-
Asia
India
New delhi
Lucknow
China
Beijing
Europe
Greece
How to I go about achieving this thing?
Look into the group_by method of the Enumerable class.
The idea would be to try something like
#destinations.group_by(&:continent).each do |continent, ds_per_continent|
# print continent
- ds_per_continent.group_by(&:country).each do |country, ds_per_country|
# print country
- ds_per_country.each do |destination|
# print destination.city