Why i cant use acos function - excel

I have a problem with this function. Excel return #name error.
=ACOS(COS(RADIANS(90-B3)) * COS(RADIANS(90-$G$3)) + SIN(RADIANS(90-B3)) * SIN(RADIANS(90-$G$3)) * COS(RADIANS(C3-$G$4))) * 6371

Related

PowerPivot: Function for distance between two points Error

I am currently using powerpivot and I added a column to create a calculated field for all rows. The purpose of the function is to find the distance between two zip codes by takin their Latitude and Longitude; this function always works in excel worksheets but when I attempt to use the same function in power query, it does not work. The reason why I'm using power query for this function is because the data set is over 4,000,000 rows...
I'm using the function below and it returns as "#ERROR"
=ACOS(COS(RADIANS(90-'Origin vs Destination w PKG'[Latitude]))*COS(RADIANS(90-'Origin vs Destination w PKG'[DLatitude]))+SIN(RADIANS(90-'Origin vs Destination w PKG'[Latitude]))*SIN(RADIANS(90-'Origin vs Destination w PKG'[DLatitude]))*COS(RADIANS('Origin vs Destination w PKG'[Longitude]-'Origin vs Destination w PKG'[DLongitude])))*3958.8
Above is the function. Does anyone know how to get the distance function between two coordinates to work in PowerPivot?
Thanks a lot
Assuming Table1 has 4 columns [Latitude_1], [Longitude_1], [Latitude_2], [Longitude_2] and the coordinates are in decimal [38.892456, -74.0247852] then code below generates the distance between the locations in miles (change 3959 to 6371 for kilometers) using the formula:
=ACOS(SIN(lat1)*SIN(lat2)+COS(lat1)*COS(lat2)*COS(lon2-lon1))*3959
which in powerquery code is:
= Number.Acos(Number.Sin(([Latitude_1] / 180) * Number.PI) * Number.Sin(([Latitude_2] / 180) * Number.PI) + Number.Cos(([Latitude_1] / 180) * Number.PI) * Number.Cos(([Latitude_2] / 180) * Number.PI) * Number.Cos( ([Longitude_2] / 180) * Number.PI-([Longitude_1] / 180) * Number.PI)) * 3959)
full code sample:
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "DistanceMiles", each Number.Acos(Number.Sin(([Latitude_1] / 180) * Number.PI) * Number.Sin(([Latitude_2] / 180) * Number.PI) + Number.Cos(([Latitude_1] / 180) * Number.PI) * Number.Cos(([Latitude_2] / 180) * Number.PI) * Number.Cos( ([Longitude_2] / 180) * Number.PI-([Longitude_1] / 180) * Number.PI)) * 3959),
in #"Added Custom"

Call Local Function in Postgres Query

I have a function which queries my database to find nearby users. However, my database stores the longitude and latitude of users, and to find the distance from the user, I need to call a function (distance) to check which users to return in the query. Is there a way to do what I have in the code (call my local distance function inside my querystring?)
exports.getNearby = function (longitude, latitude) {
var long1 = longitude;
var lat1 = latitude;
return new Promise(((resolve, reject) => {
const queryString = `SELECT ${userModel.userId}
FROM ${userModel.collectionName} where SELECT distance(${userLocationModel.latitude},
${userLocationModel.longitude}, ${lat1}, ${long1}) < 4`;
db.query(queryString, (err, res) => {
if (err) {
logger.log('error', ` getUser - ${userIdArr} - ${err}`);
reject(err);
} else {
resolve(res.rows[0]);
}
});
}));
};
function distance(lat1, lon1, lat2, lon2) { //returns in km
return 12742 * asin(sqrt(0.5 - cos((lat2 - lat1) * 0.01745329252)/2
+ cos(lat1 * 0.01745329252) * cos(lat2 * 0.01745329252) *
(1 - cos((lon2 - lon1) * 0.01745329252)) / 2));
}
Function written in Postgres (as suggested by #LaurenzAlbe)
CREATE FUNCTION distance(
lat1 double precision,
lng1 double precision,
lat2 double precision,
lng2 double precision
) RETURNS double precision AS
'SELECT 12742
* asin(
|/ (
0.5 - cos(
(lat2 - lat1) * 0.01745329252
)/2
+ cos(lat1 * 0.01745329252)
* cos(lat2 * 0.01745329252)
* (1 - cos(
(lon2 - lon1) * 0.01745329252
)) / 2
)
)'
LANGUAGE sql IMMUTABLE;
If you want to use the function in an SQL statement, you have to define it inside the database using CREATE FUNCTION.
For a function like that, I'd choose LANGUAGE sql.
Have you considered using the PostGIS extension if you want to do geographical data processing inside the database? It offers all imaginable functions out of the box.
Here is how your function could look as SQL function:
CREATE FUNCTION distance(
lat1 double precision,
lng1 double precision,
lat2 double precision,
lng2 double precision
) RETURNS double precision AS
'SELECT 12742
* asin(
|/ (
0.5 - cos(
(lat2 - lat1) * 0.01745329252
)/2
+ cos(lat1 * 0.01745329252)
* cos(lat2 * 0.01745329252)
* (1 - cos(
(lon2 - lon1) * 0.01745329252
)) / 2
)
)'
LANGUAGE sql IMMUTABLE;

How to set a different time zone for a SPECIFIC task in crontab?

I need to add a new task for crontab which will execute it in a different TZ than the local machine time.
I succeed to do it as you can see in the example below, but the problem is that from time i changed the TZ all the jobs after refers to the new TZ
* * * * * rubHello.sh
0 19 * * * runKuku.sh
CRON_TZ="Europe/Rome"
17 13 * * * /tmp/job1.sh
0 18 * * * /tmp/Job2.sh
in the example above, job1 runs in Europe/Rome as my request, but also job2
runs in that time which is NOT OK.
it there a way to tell crontab change the TZ for only specific task and get back to default crontab TZ for the next jobs after?
Thank you.
Try this (presuming you're in Istanbul), CRON_TZ variable should be set before your cron entry :
* * * * * rubHello.sh
0 19 * * * runKuku.sh
CRON_TZ="Europe/Rome"
17 13 * * * /tmp/job1.sh
CRON_TZ="Europe/Istanbul"
0 18 * * * /tmp/Job2.sh
OR
* * * * * rubHello.sh
0 19 * * * runKuku.sh
0 18 * * * /tmp/Job2.sh
CRON_TZ="Europe/Rome"
17 13 * * * /tmp/job1.sh
This should work, I believe:
* * * * * rubHello.sh
0 19 * * * runKuku.sh
17 13 * * * TZ="Europe/Rome" /tmp/job1.sh
0 18 * * * /tmp/Job2.sh

how to Calculate google map circle radius js to C#

I know how to use the javascript to calculate the radius by using the below code
var center = new google.maps.LatLng(3.2987599, 102.6872022);
var latLng = new google.maps.LatLng(3.0987599, 101.6872022);
var distanceInMetres = google.maps.geometry.spherical.computeDistanceBetween(center, latLng);
But how to convert the google.maps.geometry.spherical.computeDistanceBetween into C# function?
Distance between 2 points: (lat1,lon1) to (lat2,lon2)
distance = acos(
cos(lat1 * (PI()/180)) *
cos(lon1 * (PI()/180)) *
cos(lat2 * (PI()/180)) *
cos(lon2 * (PI()/180))
+
cos(lat1 * (PI()/180)) *
sin(lon1 * (PI()/180)) *
cos(lat2 * (PI()/180)) *
sin(lon2 * (PI()/180))
+
sin(lat1 * (PI()/180)) *
sin(lat2 * (PI()/180))
) * 3959
3959 is the Earth radius in Miles. Replace this value with
radius in KM, (or any other unit), to get results on the same unit.
You can verify your implementation by comparing to this worked example:
i have write the C# solution to calculate the distance to convert
var distanceInMetres = google.maps.geometry.spherical.computeDistanceBetween(center, latLng);
into C#. Below is the code i have using. 6371 is the radius of the Earth.
//Calculate distance earth between 2 coordinate point
double e = lat * (Math.PI / 180);
double f = lng * (Math.PI / 180);
double g = lat2 * (Math.PI / 180);
double h = lng2 * (Math.PI / 180);
double i =
(Math.Cos(e) * Math.Cos(g) * Math.Cos(f) * Math.Cos(h)
+ Math.Cos(e) * Math.Sin(f) * Math.Cos(g) * Math.Sin(h)
+ Math.Sin(e) * Math.Sin(g));
double j = Math.Acos(i);
double k = (6371 * j); //Distance in KM
The distance between 2 lat/long points can be calculated with the haversine formula, which is described here http://en.wikipedia.org/wiki/Haversine_formula
There also is another question here at stackoverflow about more or less the same issue: Calculate distance between two latitude-longitude points? (Haversine formula)

Cron : Setting alternative seconds

I posted a question the other day about setting alternative minutes in cron, and i was given a lovely simple answer.
0-59/2 * * * * first_script
1-59/2 * * * * second_script
This worked brilliantly, however i have seen realized that i need my scripts to run quicker than every minute.
I know cron doesn't support seconds, but you can bluff it by using sleep, like so
* * * * * /foo/bar/your_script
* * * * * sleep 15; /foo/bar/your_script
* * * * * sleep 30; /foo/bar/your_script
* * * * * sleep 45; /foo/bar/your_script
So i need to combine the both of these so that i can get them to run alternatively every 15 seconds for instance.
Any ideas?
Ended up with the following code to get my scripts to run in shorter intervals than 1 minute.
* * * * * /usr/bin/php -q /path/to/file/script1.php
* * * * * sleep 15; /usr/bin/php -q /path/to/file/script2.php
* * * * * sleep 30; /usr/bin/php -q /path/to/file/script1.php
* * * * * sleep 45; /usr/bin/php -q /path/to/file/script2.php

Resources