I am working on a new project and whole day thinking & looking for the best way, how to save users' location to database.
I need to save their city, country. Here is a bit problem, because for example for Europeans this means city=Berlin, country=Germany.
But for US users it's like city=Los Angeles, country=California (state=USA).
So this is the problem with which I ma facing whole today.
My goal in this app is to find all users in the city according to their location. And also find the people, which are in their area of let's say 15 km/miles.
I plan to implement the app in RoR, PostgreSQL, the app will run probably on Heroku.
What is the best way to solve this "problem"? Could you give me please some advices, tips, whatever?
Thank you
You can use the geokit and geokit-rails gems to achieve that. See here for documentation: https://github.com/imajes/geokit-rails
Basically, it works like this: you save address data of your users and that address is looked up and mapped to a point in space (lat/lng) using a geocoding service (e.g. Google Maps or Yahoo Placefinder). These points can then be used to calculate distances etc.
An example:
class User < ActiveRecord::Base
# has the following db fields:
# - city
# - state
# - country
# - latitude
# - longitude
acts_as_mappable :default_units => :miles,
:default_formula => :sphere,
:lat_column_name => :latitude,
:lng_column_name => :longitude,
:auto_geocode => {:field => :full_address}
def full_address
[city, state, country].reject(&:blank).join(', ')
end
end
Then you can do the following:
# find all users in a city (this has nothing to do with geokit but is just a normal db lookup)
User.where(:city => 'New York')
# find all users that are within X miles of a place
User.find_within(300, 'Denver')
and much more, just see the documentation...
This example shows you how to use the geokit gem. This gem does no longer seem to be under active development. So maybe it would be worthwile to check out geocoder: https://github.com/alexreisner/geocoder
Related
I am using premium account (not sandbox) for data collection.
I want to collect:
All tweets in English that contain ‘china’ or ‘chinese’ that are user geolocated to US and not geolocated at tweet level, excluding all retweets
All tweets in English that contain ‘china’ or ‘chinese’ that are user geolocated to ‘Minnesota’ and not geolocated at tweet level, excluding all retweets
The code is as follows:
premium_search_args = load_credentials('twitter_API.yaml',
yaml_key ='search_tweets_premium_api', env_overwrite=False)
# keywords for the search
# key word 1
keywords = '(China OR Chinese) lang:en profile_country:US -place_country:US -is:retweet'
# key word 2
keywords = '(China OR Chinese) lang:en -place_country:US profile_region:"Minnesota" -is:retweet'
# define search rule
rule = gen_rule_payload(keywords,from_date='2019-12-01',
to_date='2019-12-10',results_per_call=500)
# create result stream and print before start
rs = ResultStream(rule_payload=rule, max_results=1250000,
**premium_search_args)
My problems are that:
For the first one, a large portion of the results I get didn’t satisfy the query. First, some don’t have Profile Geo enrichment, i.e. user.derived.locations attribute is not in the user object. Second, if it is, a lot don’t have country code US, i.e. they are identified to other countries.
For the second one, the result I get from this method is a smaller subset of the results I can get from 1). That is, when I filter all tweets user geolocated to Minnesota (by user.derived.locations.region) from profile_country:US, it gives a larger sample than using profile_region:“Minnesota”. A considerable amount of data is missing using this method.
I have tried several times but it seems that user geolocation operators don’t work exactly what I want. Does anyone has any idea why this is the case? I would very much appreciate any answers/suggestions/comments.
Thank you!
I would like to get all apps which are accessible to me. So I used the API
https://developers.podio.com/doc/applications/get-all-apps-5902728
There is a 'limit' parameter but there is no 'offset' parameter.
The max value of limit is 100.
Is there any other method to get all apps?
Any help will be appreciated.
You can still go through hierarchy Org => Space => App => Item
Get all organisations, which will also give all spaces
https://developers.podio.com/doc/organizations/get-organizations-22344
Get all apps per space
https://developers.podio.com/doc/applications/get-apps-by-space-22478
Small example in Ruby demonstrating how it works
orgs = Podio::Organization.find_all()
orgs.each do |org|
org.spaces.each do |space|
puts "Working in space id:#{space['space_id']} name:'#{space['name']}'"
apps = Podio::Application.find_all_for_space(space['space_id'])
apps.each do |app|
puts "\tApp id:#{app['app_id']} name:'#{app['config']['name']}'"
end
end
end
(Using Rails 3)
I have 2 models (Vehicle and Capabilities) in a has_many through association.
So Vehicle 1 can have Capability 1 (eg towing), Capability 2 (eg passenger), Capability 3 (eg flying), etc.
v = Vehicle.first
v.capabilities.pluck(:name) #=> will give something like ['towing', 'passenger', 'flying']
I want to find all vehicles which must not have a particular capability, eg all vehicles which cannot fly.
I have tried queries similar to this below but it still includes flying vehicles, I think mainly because the airplane also has other capabilities.
non_flying = Vehicle.includes(:capabilities).where('capabilities.id NOT IN (?)', [2,3])
non_flying.first.capabilities.pluck(:name) #=> will give something like ['towing'].
Note that the flying capability is not included, but I just do not want this vehicle returned at all. How would I write this?
If possible, I would rather not use meta_wheel or squeel gems, but any arel_table implementation is welcome unless there is a simpler solution.
Try this query
non_flying = Vehicle.all - Vehicle.includes(:capabilities).where('capabilities.id IN (?)', [2,3]).all
I ended up doing something similar to this, inspired by Thaha kp's answer.
# Get all flying vehicles first
subquery = Vehicle.joins(:capabilities).where("capabilities.id IN (?)", 3).pluck("vehicles.id")
# Then get all vehicles not in this flying vehicles array
non_flying = Vehicle.where('vehicles.id NOT IN (?)', subquery).all
I have a model in Rails called Statistics and every time someone visits my page, I log their IP in the model and save. This is how my model currently looks:
Statistics(id: integer, ip: string, ...);
An example record return would be:
#<Statistics id: 700, ip: "10.0.2.2", ...>
But when I enter the Rails Console, and try to locate the record:
ruby-1.9.2-p290 :175 > Statistics.find_all_by_ip("10.0.2.2")
Statistics Load (0.8ms) SELECT "statistics".* FROM "statistics" WHERE "statistics"."ip" = '10.0.2.2'
=> []
I always get an empty return..
Are they any quirks to Rails for IP addresses that I don't know of? I just can't seem to figure out why this wouldn't work.
My first assumption is that the IP address was stored using the value from an Addrinfo, whereas SQLite uses UTF. This assumes you're using SQLite, which you don't mention.
See my previous answer I gave covering this in some detail.
I have installed both geokit gem and geokit-rails plugin. I configured the plugin as shown here: http://github.com/andre/geokit-rails . Then I generated a new model - Location, which looks like this:
class Location < ActiveRecord::Base
acts_as_mappable :default_units => :kms
end
and a controller:
class TestController < ApplicationController
include GeoKit::Geocoders
include GeoKit::Mappable
def test1
#a=Geokit::Geocoders::YahooGeocoder.geocode 'Kaohsiung City, Taiwan'
#b=Location.find(:all, :origin => '100 Spear st, San Francisco, CA', :within => 5)
end
end
I also set up a Locations table with both lng and lat columns in my database and put my google key in /config/initializers/geokit_config.rb
And right now, altough #a is giving me correct results from YahooGeocoder (I can use GoogleGeocoder as well), #b is an empty array. I know #a is generated using Geokit gem and #b using Geokit-rails plugin, so the problem is definitely related to the plugin. When I use #c=IpGeocoder.geocode('85.128.202.178') I get "success: false" ... What am I doing wrong here?
Have you double checked your Location collection to ensure there exists a location within 5 kilometers of that street address? What happens if you do a similar lookup with an explicit latitude and longitude of a Location?
Re: #success=false, I'm having the same trouble with that IP (using IpGeocoder and MultiGeocoder).
>> #c=IpGeocoder.geocode('85.128.202.178')
=> #<Geokit::GeoLoc:0x1038df5f0 #success=false, #city="(Unknown City)", #province=nil, #street_address=nil, #lng=nil, #full_address=nil, #state=nil, #country_code="PL", #all=[#<Geokit::GeoLoc:0x1038df5f0 ...>], #lat=nil, #precision="unknown", #provider="hostip", #zip=nil>
>> #c=IpGeocoder.geocode('74.125.93.104')
=> #<Geokit::GeoLoc:0x1038d3c28 #success=true, #city="Manila", #province=nil, #street_address=nil, #lng=120.95, #full_address=nil, #state=nil, #country_code="PH", #all=[#<Geokit::GeoLoc:0x1038d3c28 ...>], #lat=14.5833, #precision="unknown", #provider="hostip", #zip=nil>