GeoDjango distance query weighted by rating - django-models

I am using GeoDjango/PostGIS and have a model called Business that has a physical location and a rating. I would like to run a query to find 'nearest' highly rated businesses. How do I do this? To make it more concrete, suppose given a location, I want to find businesses sorted by rating/(1+distance). What is the best way to go about this?
from django.contrib.gis.db import models
class Business(models.Model):
name = models.CharField(max_length=255)
rating = models.IntegerField()
address = models.PointField()

I don't think you can sort by min distance in geodjango, but you can filter by distance, and only get the ones near of your point, and then order by rating.
from django.contrib.gis.geos import *
pnt = Point(954158.1, 4215137.1, srid=32140)
pnt.buffer(23) #radius
business = Business.objects.filter(address__intersects=pnt).order_by('rating')
In postgis you could get what you are asking for with a simple query like this:
SELECT name, rating,
ST_Distance(the_geom,ST_GeomFromEWKT('SRID=4326;POINT(19.232 91.00)') AS minDist
FROM business ORDER BY minDist,rating;

Related

Power BI - TopN + Others on data from two tables

I am a bit stuck with a specific case in Power BI. Let's say that we have two tables. The first one contains the product ID and the product name, and the second one contains the product ID and a specific budget.
I want to create a piechart showing the topN + group others. I have made a dax formula which works for data in a single table, but not on two.
Here is the formula :
ProductTop =
VAR rankSiteImpressions = RANKX(ALL(Piechart); [Impressions ];;DESC)
return
IF(rankSiteImpressions<=3;Piechart[Site];"Others")
How can I apply this on data from two tables to get the top products by budget?
Many thanks,
RĂ©mi

Combining scores from multiple Neo4j queries

We have a movie graph. There are actors, directors, and movies. It's possible that a director has also played in another movie, or even the same movie. We give an actor a score for each movie he plays in. We also give a director a score for each of his movies. Now, we are looking to get a list of individuals and their scores, sorted from highest to lowest score. So these are the queries we currently have:
MATCH (p:Person)-[idr:IsDirectorOf]->(m:Movie)
RETURN p.name AS name, COUNT(idr) AS numberOfMoviesDirected
ORDER BY numberOfMoviesDirected DESC;
and
MATCH (p:Person)-[iai:IsActorIn]->(m:Movie)
RETURN p.name AS name, COUNT(iai) AS numberOfMoviesPlayed
ORDER BY numberOfMoviesPlayed DESC;
I want to combine these queries, to get the person who has the highest score from both queries together at the top. Also, please note that we may later need to add another query to the mix, so solutions that work for only two queries may not be the best.
You should try this query :
MATCH (p:Person)
RETURN p, size((p:Person)-[:IsDirectorOf]->(:Movie)) + size((p:Person)-[:IsActorIn]->(:Movie)) AS score
ORDER BY score DESC
And if you want to add additional score, just continue your query with a WITH.
Cheers
UPDATE
To respond to your comment, this is the query :
MATCH (p:Person)
WITH
max(size((p)-[:IsActorIn]->())) As maxActed,
max(size((p)-[:IsDirectorOf]->())) AS maxDirected
MATCH (p:Person)
RETURN
p,
size((p)-[:IsActorIn]->(:Movie)) / toFloat(maxActed) + size((p)-[:IsDirectorOf]->(:Movie)) / toFloat(maxDirected) AS score
ORDER BY score DESC

What's the most effective way of storing this data?

Need help figuring out a good way to store data effectively and efficiently
I'm using Parse (JavaScript SDK), here's an example of what I'm trying to store
Predictions of football (soccer) matches so an example of one match would be;
Team A v Team B
EventID = "abc"
Categories = ["League-1","Sunday-League"]
User123 predicts the score will be Team A 2-0 Team B -> so 2-0
User456 predicts the score will be Team A 1-3 Team B -> so 1-3
Each event has information attached to it like an eventId, several categories, start time, end time, a result and more
I need to record a score prediction per user for each event (usually 10 events at a time so a lot of predictions will be coming in)
I need to store these so I can cross reference the correct result against the user's prediction and award points based on their prediction, the teams in the match and the categories of the event but instead of adding to a total I need all the awarded points stored separately per category and per user so I can then filter based on predictions between set dates and certain categories e.g.
Team A v Team B
EventID = "abc"
Categories = ["League-1","Sunday-League"]
User123 prediction = 2-0
Actual result = 2-0
So now I need to award X points to User123 for Team A, Team B, "League-1", and "Sunday-League" and record it to the event date too.
I would suggest you create a table for games and a table for users and then an associative table to handle the many to many relationship. This is a pretty standard many to many relationship.

Many to Many Relationship in a Transactional System With Google Datastore/Python NDB

I am writting an application for a crop warehousing company, which purchases the harvested crop and stores it in one or more warehouses. Till now limitations of Google App Engine/Datastore was not posing problem to me. And I so deep into the project that it's very difficult to return.
I have to establish a many to many relationship between two Kinds which are transactional (means parent/ancestor queries) in nature. I am not sure what is the right approach to do this. This is the business logic:
Warehouse issues a purchase order for a specific quantity (PO). So we create an entity in PO Kind for each purchase order (PO).
Goods are received at the warehouse. So we create an entity in GRN Kind. (GRN=Goods Receiving Notes).
Here is many to many relationship.
One GRN may be required to complete one PO.
One GRN may be required to complete many POs.
Many GRNs may be required to complete one PO.
Many GRNs may be required to complete many POs.
This is a relevent snapshot of the code.
class Warehouse(ndb.Models):
name = ndb.TextProperty()
capacity = ndb.FloatProperty()
current_stock = ndb.FloatProperty()
class PurchaseOrder(ndb.Models):
quantity = ndb.FloatProperty()
remaining = ndb.FloatProperty()
is_complete = ndb.BooleanProperty()
grn = ndb.KeyProprty(repeated=True, kind=GRN)
class GRN(ndb.Models):
quantity = ndb.FloatProperty()
remaining = ndb.FloatProperty()
is_complete = ndb.BooleanProperty()
po = ndb.KeyProprty(repeated=True, kind=PurchaseOrder)
Entity Group Relationships
Warehouse -> GRN
Warehouse -> PO
To establish many - to - many relationship I hold all the related GRN Keys in a PO record and all related PO Keys in a GRN record. This is working fine for me.
But in case I have to edit a GRN or PO in the back date, then I am not able to handle the complication of cascaded changes which may impact other GRNs and POs.
Somewhere I read I should be using a third model to hold the many - to - many relationship, rather than storing the related keys. I am not able to visualize what that third table to should contain. *
I know my problem is very specific and I have not funished all the details. But just help me with the right approach for this kind of problem pattern. I will handle the rest. Or provide any link if this is already documented somewhere.
I would add a many-to-many relationship called GRNinstance that links a single GRN instance with a single PO:
class PurchaseOrder(ndb.Models):
quantity = ndb.FloatProperty()
remaining = ndb.FloatProperty()
is_complete = ndb.BooleanProperty()
class GRN(ndb.Models):
quantity = ndb.FloatProperty()
remaining = ndb.FloatProperty()
is_complete = ndb.BooleanProperty()
class GRNinstance(ndb.Models):
po = ndb.KeyProperty(kind=PurchaseOrder)
grn = ndb.KeyProperty(kind=GRN)
quantity = ndb.FloatProperty()
The sum of GRNinstance.quantity for a particular po should add up to PurchaseOrder.quantity, and the sum of GRNinstance.quantity for a particular grn should add up to GRN.quantity.
In fact, Dmitry's ComputedProperty could replace both PurchaseOrder.quantity and GRN.quantity (but that could be slow). Maybe is_complete could be a simple test:
is_complete = ndb.ComputedProperty(lambda self: self.remaining > 0)

Movement Paths & Spatial-Temporal Queries in SQL Server

Hey, so I'm trying to figure out the best way of storing movement paths and then afterwards how they might be queried.
Let me try to explain a bit more. Say I have many cars moving around on a map and I want to determine if and when they're in a convoy. If I store just the paths then I can see that they travelled along the same road, but not if they were there at the same time. I can store the start and end times but that will not take into account the changes in speed of the two vehicles. I can't think of any obvious way to store and achieve this so I thought I'd put the question out there in case there's something I'm missing before trying to implement a solution. So does anyone know anything I don't?
Thanks,
Andrew
Well it depends on what type of movement information you have.
If you have some tables setup like:
Vehicle (Id, Type, Capacity, ...)
MovementPoint(VehicleId, Latitude, Longitude, DateTime, AverageSpeed)
This would allow you to query if two cars going to the same point plus or minus 5 minutes like so:
Select * from Vehicle v INNER JOIN MovementPoint mp on mp.VehicleId = v.Id
WHERE v.Id = #FirstCarID
AND EXISTS
(
SELECT 1 FROM Vehicle v2 INNER JOIN MovementPoint mp2 on mp2.VehicleId = v2.Id
WHERE v2.Id = #SecondCarId
AND mp2.Latitude = mp.Latitude AND mp2.Longitude = mp.Longitude
AND mp2.DateTime BETWEEN DATEADD(minute,-5,mp.DateTime) AND DATEADD(minute,5,mp.DateTime)
)
You could also query for multiple points in common between multiple vehicles with specific time windows.
Also you could make the query check latitude and longitude values are within a certain radius of each other.

Resources