Using Product and Location Models, how to find "deals" near locations (Rails 3.1.1) - ruby

I have a Product Model having deals of stores (another Model Store) in whole city. Now if someone selects particular store I want my view to display deals of all stores in geographically nearby areas of that store (say within range of 3 miles).
One way would be finding all deals on zipcode basis. But wondering if there is any better way to do this. Maybe some gem..
Thanks.

Use geokit gem: http://geokit.rubyforge.org/ . Example:
Store.find(:all, :origin =>[37.792,-122.393], :within=>10)
If works with relational database. However, it is not optimized like Geo spatial databases.

What you're looking for is a spatial database. You can achieve this with Postgres via PostGIS. I'd also highly recommend using GeoServer or MapServer as a front-end to PostGIS. You're going to want to do some serious reading on GIS in general. This is not a topic to cover in a single answer. You may want to spend some time poking around the OSGeo site.
If you're feeling trendy, you can use MongoDB's spatial indexes. This is probably what I would recommend if you're looking for a quick fix. FourSquare actually runs entirely on MongoDB's spatial functionality. It's what they use to find people close-by. So with Mongo you could find nearby deals with something like
db.deals.find({
loc: {
$near: [YOUR_X, YOUR_Y],
$maxDistance : DEAL_DISTANCE
}
});
This will return all deals that are within DEAL_DISTANCE of your coordinates.

Related

Umbraco 8 - Get Children Of Node Using ContentAtXPath() Method

I've been refactoring an existing Umbraco project to use more performant querying when getting back document data as everything was previously being returned using LINQ. I have been using a combination of Umbraco's querying via XPaths and Examine.
I am currently stumped on trying to get child documents using the Umbraco.ContentAtXPath() method. What I would like to do is get child document based on a path I parse to the method. This is what I have currently:
IEnumerable<IPublishedContent> umbracoPages = Umbraco.ContentAtXPath("//* [#isDoc]/descendant::/About [#isDoc]");
Running this returns a "Object reference not set to an instance of an object." error and unable to see exactly where I'm going wrong (new to this form of querying in Umbraco).
Ideally, I'd like to enhance the querying to also carry out sorting using the non-LINQ approach, as demonstrated here.
Up until Umbraco 8, content was cached in an XML file, which made XPath perfect for querying content efficiently. In v8, however, the so called "NuCache" is not file based nor XML based, so the XPath query support is only there for ... well... Old times sake, I guess? Either way it's probably not going to be super efficient and (I'd advise) not something to "aim for". That said I of course don't know what you are changing from (Linq can be a lot of things) :-/
It certainly depends on how big your dataset is.
As Umbraco has moved away from the XML backed cache, you should look into Linq queries against your content models. Make sure you use ModelsBuilder to generate the models.
On a small dataset Linq will be much quicker than examine. On a large dataset Examine/Lucene will be much more steady on performance.
Querying NuCache is pretty fast in Umbraco 8, only beaten by an Examine search.
Assuming you're using Models Builder, and your About page is a child of Home page, you could use:
var homePage = (HomePage) Model.Root();
var aboutPage = homePage?.Children<AboutPage>().FirstOrDefault();
var umbracoPages = aboutPage.Children();
Where HomePage is your home page Document Type Alias and AboutPage is your About page Document Type alias.

Kohana ORM Caching/Caching design approach

This questions is related to Kohana ORM AND Caching module. I use version 3.2 if it matters. I tried to research trust me, but I really couldn't find some good answer... so here it is:
What are the correct ways to use ORM::cached() and ORM::serialize() and ORM::$reload_on_wakeup?
I've seen many 2-line code examples but never anything really solid on the userguide/api...
What is the difference between enabling Cache module and 'caching' => true in Kohana::init?
Anyone has any recommended approach for the following specific situations? I have a catalogue page that upon profiling, I realized two very expensive actions:
I queried database each time for a currency model for each item, when the currency information can really be reused.
I queried database each time for each item's inventory item, this is an expensive query, which I wish I can cache until inventory level changes.
References that I found but couldn't answer fully my questions:
http://forum.kohanaframework.org/discussion/1782/tip-for-caching-orm-objects/p1
http://forum.kohanaframework.org/discussion/10600/does-kohana-orm-and-cache-work-together/p1
Just found your question, maybe too late, but maybe is useful to others:
cached, will force the Query builder to cache the DB query. It uses the KOhana:cache method (file cache) I am trying to find a workaround for this.
enables caching for the file search as says in the Kohana/Core.php file: Whether to use internal caching for [Kohana::find_file], does not apply to [Kohana::cache]. Set by [Kohana::init]
Enable caching true to speed up the file search, and enable the cache module, I am working on a way to cache the queries of DB using the instance used by the module. That would be better than using the file cache. Maybe I am missing something but stuck there right now.

Adviced on how to array a mongodb document

I am building an API using Codeigniter and MongoDB.
I got some questions about how to "model" the mongoDB.
A user should have basic data like name and user should also be able to
follow other users. Like it is now each user document keeps track of all people
that is following him and all that he is following. This is done by using arrays
of user _ids.
Like this:
"following": [323424,2323123,2312312],
"followers": [355656,5656565,5656234234,23424243,234246456],
"fullname": "James Bond"
Is this a good way? Perhaps the user document should only contain ids of peoples that the user is following and not who is following him? I can imaging that keeping potentially thousands of ids (for followers) in an array will make the document to big?
All input is welcome!
The max-document size is currently limited to 16MB (v1.8.x and up), this is pretty big. But i still think, that it would be ok in this case to move the follower-relations to an own collection -- you never know how big your project gets.
However: i would recommend using database references for storing the follower-relations: it's way easier to resolve the user from a database reference. Have a look at:
http://www.mongodb.org/display/DOCS/Database+References

how to get Route directions between two points on a map to draw driving directions?

is there a way to use core location or google or some other api to provide two points on the map and get the resulting set of latitude/longitude pairs for the route?
i have seen in this tutorial Drawing polyines or routes on a MKMapView that how to draw driving direction using core graphics...but in the sample code it has route.csv which has predefined set of longitudes and latitudes.....how do i make pair of longitudes and latitudes so that i can draw driving directions.
Companies invest millions of dollars into developing routing algorithms, so I very much doubt that you'll be able to get it for free anywhere (I'm happy to be wrong, though). For example, the Google Maps JavaScript API supports routing, so I guess you could hack that up. The results wouldn't be fast or pretty, mind you.
If you have a limited use of the directions request (less than a certain amount per day) and always display results on a Google map, you do qualify for the free Google Directions API.
https://developers.google.com/maps/documentation/directions/
Read the license part thoroughly to know the limits. You should easily be able to request a json response that will b easier to parse than an xml one (plus Apple added json parser in iOS).
For example
http://maps.googleapis.com/maps/api/directions/json?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown,MA|Lexington,MA&sensor=false
If you are over the limit, you should look into their business licenses.
Old question but MTDirectionsKit was recently open sourced and it works great:
MTDirectionsKit
with Google API you can get directions from two points, the only problem is you just can use it once a day if you don't pay.
Search how to get the key API from google, there are a lots of videos on youtube talking about it.
Furthemore, i got a library which hepls you to draw the line between points this one https://github.com/jd-alexander/Google-Directions-Android
After you implemented it in build.gradle(Module:app), implement the library in the class like
public class LocationMapActivity extends AppCompatActivity implements RoutingListener
will appear a error to implemets the methods (obligatory to implement them but It's not necessary to use them).
I just did this:
public void route(){
Routing routing = new Routing.Builder()
.travelMode(/* Travel Mode */)
.withListener(/* Listener that delivers routing results.*/)
.waypoints(/*waypoints*/)
.key(/*api key for quota management*/)
.build();
routing.execute();
}
More info in the link.

How to quickly find a sharepoint document library by id?

Given the SPList.ID and a site collection (or an SPWeb with subwebs), how do I quickly find the document library with the given ID?
I can recursively enumerate through all webs and perform a web.Lists[guid] on each one of them, but there might be thousands of subwebs in my case, and I'm looking for a realtime solution.
If there is no way to do this quickly, any other suggestions on how to uniquely identify a document library? I could store the full path (url), but the identification will be publicly visible and I don't feel very comfortable giving away our exact SharePoint document structure like that. Should I resort to maintaining a manual ID <-> library mapping in a separate list?
I vote for the manual ID -> URL pair matching in a top-level, well-known list that's visible only to the elevated privileges account.
Since you are storing the ListID somewhere, you may also store the WebId. Lists are opened by the context SPWeb always, so if you go to:
http://toplevel/_layouts/ListGeneralSettings.aspx?ID={GUID1} // OK
http://toplevel/sub1/_layouts/ListGeneralSettings.aspx?ID={GUID1} // Wont Work (same Guid)
Having the WebId and ListId you can simply:
using(SPWeb subweb = (new SPSite("http://url")).OpenWeb(new Guid("{000...}")))
{
SPList list = subweb.Lists.GetList(new Guid("{111...}"), true);
// list logic
}
MS does not support this :)...
But take a look at this for giggles: http://weblogs.sqlteam.com/jhermiz/archive/2007/08/15/60288.aspx
If you have MOSS Search available, then it might help, depending on the lag you have between these lists getting created and needing to search for them. You could probably map list id as a managed property and do a quick search for list objects with the id in question.
For lots of classes of problems it seems like search is the fastest way to rip through huge sets of data. In fact if this approach worked for you, you really wouldn't even need to know the site collection up front. Don't have access to any of my MOSS environments at the moment, so can't verify this will work though.

Resources