How to order by calculated property of included model in Sequelize - include

This is my code: (I get longitude and latitude as arguments)
var attributes = Object.keys(models.addresses.attributes),
distance = Sequelize.fn(
"distance",
longitude,
latitude,
Sequelize.col("longitude"),
Sequelize.col("latitude")
)
attributes.push([distance, 'distance'])
const cars = await models.cars.findAll({
include: [{
model: models.addresses,
// order: ['car_address->distance'],
as: 'car_address'
}],
order: 'distance',
...
As you can see I'm trying to order by distance between the given coordinate and the coordinate of the address found in the included car_address object (association is defined in the model). However, I cannot get this code to work.. I've tried calculating the distance within the findAll() function, I've tried every possibility of getting the distance attribute.

Related

Using nearest neighbour to find postcode to new postcodes found

I have a list of new postcodes and I'm trying to find the nearest postcode from an existing postcode file to attach to the new postcodes. I am using the below code but it seems to have duplicated some rows, please could I have some help resolving this...
My 2 dataframes are:
new_postcode_df which contains 92,590 rows, and columns:
Postcode e.g. "AB101BJ"
Latitude e.g. 57.146051
Longitude e.g. -2.107375
current_postcode_df which contains 1,738,339 rows, and columns:
Postcode e.g. "AB101AB"
Latitude e.g. 57.149606
Longitude e.g. -2.096916
my desired output is output_df
new_postcode e.g. "AB101BJ"
current_postcode e.g. "AB101AB"
My code is below:
new_postcode_df_gps = new_postcode_df[["lat", "long"]].values
current_postcode_df_gps = current_postcode_df[["Latitude", "Longitude"]].values
new_postcode_df_radians = np.radians(new_postcode_df_gps)
current_postcode_df_radians = np.radians(current_postcode_df_gps)
tree = BallTree(current_postcode_df_radians , leaf_size=15, metric='haversine')
distance, index = tree.query(new_postcode_df_radians, k=1)
earth_radius = 6371000
distance_in_meters = distance * earth_radius
current_postcode_df.Postcode_NS[index[:,0]]
my output is shown in the attached where you can see postcodes beginning with "GY" have been added near the top which should not be the case. Postcodes starting with "AB" should all be at the top.
The new dataframe has increase from 92,590 rows to 92,848 rows
Image of final output dataframe
Libraries I'm using are:
import pandas as pd
import numpy as np
from sklearn.neighbors import BallTree
new_postcode_df = pd.DataFrame({"Postcode":["AB101BJ", "AB101BL", "AB107FU"],
"Latitude":[57.146051, 57.148655, 57.119636],
"Longitude":[-2.107375, -2.097433, -2.147906]})
current_postcode_df = pd.DataFrame({"Postcode":["AB101AB", "AB101AF", "AB101AG"],
"Latitude":[57.149606, 57.148707, 57.149051],
"Longitude":[-2.096916, -2.097806, -2.097004]})
output_df = pd.DataFrame({"Postcode":["AB101RS", "AB129TS", "GY35HG"]})

How to find min and max value in a array of objects

I am new to D3 and Javascript community. I have an array of objects(countries) and each object consists of years represented by key and its value(incomes).
How to get the minimum and maximum value of incomes across all the objects and its key to set the color legend?
Please find the link https://observablehq.com/d/287af954b6aaf349 of the project, hope it would help to explain the question.
Thank you for reading the questions.
To get the global maximum, you can use
const max = d3.max(
countries.map(country =>
d3.max(Object.values(country))));
// 179000
What it does is iterate over the countries, grabbing from each country the maximum of all the values. Then, it takes the maximum of these maxima and returns that.
To also get the corresponding year, it becomes more complex. You can use Object.entries to get the key value pair, instead of just the value.
You can then manually loop over each country, updating the max value, and the corresponding year as you go
data.reduce((max, country) => {
const newMax = d3.max(Object.values(country));
if(newMax > max.value) {
return {
year: Object.entries(country).find(([year, value]) => value === newMax)[0],
value: newMax,
};
}
return max;
}, {value: 0});
// {year: "2040", value: 179000}
Another useful method backed in d3 is the d3.extent() function check this article

How to add filter result to select menu

I'm stuck with my first dashboard project with d3, dc and crossfilter. Cannot find a solution.
"ETCBTC","BUY","0.002325","1.04","0.00241800","0.00104","ETC"
"ETCBTC","SELL","0.002358","1.04","0.00245232","0.00000245","BTC"
"LTCETH","SELL","0.30239","0.006","0.00181434","0.00000181","ETH"
"LTCETH","SELL","0.30239","0.149","0.04505611","0.00004506","ETH"
I have different trading pairs in first column and from it i need to use only last pair BTC and ETH in this example.
I found the filter that helps me to do that.
The thing is I need to have BTC and ETH in my select menu which can apply filter.
function show_market_selector(ndx) {
var marketDim = ndx.dimension(dc.pluck("Market"));
var selectorMenu = marketDim.group();
function filterItems(query) {
return ndx.dimension(dc.pluck("Market")).filter(function(el) {
return el.toLowerCase().indexOf(query.toLowerCase()) > 0;
});
}
filterItems("BTC");
var select = dc.selectMenu("#market-selector")
.dimension(marketDim)
.group(selectorMenu);
select.title(function (d){
return "BTC";
});
}
Now I get all pair in group in this menu. But my target is just to have BTC and ETH in the select menu.
I hope someone can give me advice. Thank you.
I think it would be easier just to use the currency as your dimension key:
var currencyDim = ndx.dimension(d => d.Market.slice(3)),
currencyGroup = marketDim.group();
var select = dc.selectMenu("#market-selector")
.dimension(currencyDim)
.group(currencyGroup);
You don't really want to create a new dimension every time filterItems is called - dimensions are heavy-weight indices which are intended to be kept around.
The name of dimension.filter() is confusing - it's nothing like JavaScript's Array.prototype.filter(), which returns the matching rows. Instead, it's an imperative function which sets the current filter for that dimension (and changes what all the other dimensions see).
If you need a "from currency" dimension, that would be
var fromCurrencyDim = ndx.dimension(d => d.Market.slice(0,3))

How to get the total sum of a column in jqgrid

i have a two columns in jqgrid, ShopID and NetSales, and i would like to add Contribution Column which will be a calculated column. the formula is NetSales divided by Total. please see image for example.
i know how to get the Total using getCol like this var sumtotal = grid.jqGrid('getCol', 'NetSales', false, 'sum');, but dont know how to use it further for division. i have tried, but it didnt work. please help.
i
Commonly you have two ways to solve the problem
As mentioned into the note you should calculate the sum of the column before to put the data into the grid. If you have this value you can use a custom fomatter to calculate the percentage. In this case the calculated sum should be defined as global in the scope.
Direct calculation of this without using any jqGrid method - see below
Suppose you have a local data like this
mydata = [
{ShipId: 1, NetSales: 150000},
{ShipId: 2, NetSales: 200000},
...
]
You can easy do (no checks it is just idea)
var sum = 0;
$.each(mydata, function( i, row) {
sum += parseFloat(row.NetSales);
}
$.each(mydata, function( i, row) {
if (sum > 0 ) {
row.Contribution = parseFloat(row.NetSales)/sum*100;
}
}
Then put mydata in jqGrid directly without to do any calculation and use custom formatter to display the percentage.

Query REST API latitude and longitude

I want my users to query two slugs fields (latitude, longitude) and then the 2 slug fields get compared to find nearest distance within 1.5km radius and display the api according to the nearest safehouses.
For example: when the users add latitude, longitude in their query,
www.example.com/safeplace/?find=-37.8770,145.0442
This will show the nearest safeplaces within 1.5km
Here is my function
def distance(lat1, long1, lat2, long2):
R = 6371 # Earth Radius in Km
dLat = math.radians(lat2 - lat1) # Convert Degrees 2 Radians
dLong = math.radians(long2 - long1)
lat1 = math.radians(lat1)
lat2 = math.radians(lat2)
a = math.sin(dLat/2) * math.sin(dLat/2) + math.sin(dLong/2) *
math.sin(dLong/2) * math.cos(lat1) * math.cos(lat2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = R * c
return d
Here is my model
class Safeplace(models.Model):
establishment = models.CharField(max_length=250)
address = models.CharField(max_length=250)
suburb = models.CharField(max_length=250)
postcode = models.IntegerField()
state = models.CharField(max_length=250)
type = models.CharField(max_length=250)
latitude = models.DecimalField(decimal_places=6,max_digits=10)
longtitude = models.DecimalField(decimal_places=6,max_digits=10)
Is there a way to run a for loop in my database? I am currently working on Django SQLite. On Views.py, how can i implement the distance function with the user input in my rest api url to find the nearest safeplace and display as REST Api?
What you need is to run a comparison for loop in your views.py. It is pretty difficult to execute but I will try to explain step by step.
assuming you are using that distance(lat, lng, lat2, lng2) function and trying to find the distance within 2km for example.
In views.py
import pandas as pd
class someapiview(ListAPIView):
serializer_class = SafeplaceSerializer
### Now we are creating definition which sorts parameters lng and lat ###
def get_queryset(self):
queryset = Safeplace.Objects.all()
lat = float(self.query_params.get('lag', None)
lng = float(self.query_params.get('lng', None)
### Now, we are reading your api using pandas ###
df = pd.read_json('yourapi') ## yourapi is a url to ur api
obj = []
for x in range(0, len(df)):
latx = float(df['latitude'][x])
lngx = float(df['longitude'][x])
### Calculating distance ###
km = distance(lat, lng, latx, lngx)
if km <= 2:
obj.append(df['id'][x])
### Django auto generate primary key which usually calls id ###
### Now we are going to call those pk as a queryset ###
return Safeplace.objects.filter(pk__in=obj)
I used pandas to work around, the load time might be slow if you have lots of data. However, I think this does the job. Usually Geo Django provides an efficient system to deal with long and lat, however I am not very competent in Geo Django so I cannot really tell. But I Believe this is a good work around.
UPDATE :
you can query with "www.yourapi.com/safeplace?lat=x&lng=y"
I believe you know how to set urls

Resources