Find location with in 1 mile in postgresql query - postgresql

I have 2 tables 1 is for facility and 1 is for customer. both contained latitude longitude we want query to fetch customer's available with in 1 miles of facility. We don't want to use postgres function like ST_Distance. Any alternate query to achieve it.

Iterate java list of location and used haversine formula to calculate distance in miles
private double distance(double LatOne, double LonOne, double LatTwo, double LonTwo) {
LonOne = Math.toRadians(LonOne);
LonTwo = Math.toRadians(LonTwo);
LatOne = Math.toRadians(LatOne);
LatTwo = Math.toRadians(LatTwo);
double deltaLon = LonTwo - LonOne;
double deltaLat = LatTwo - LatOne;
double formula = Math.pow(Math.sin(deltaLat / 2), 2)+ Math.cos(LatOne) * Math.cos(LatTwo)* Math.pow(Math.sin(deltaLon / 2),2);
double fOutput = 2 * Math.asin(Math.sqrt(formula));
return (fOutput * 3956) ;
}

Related

How to calculate the percentage(%) in postgresql

I am not able to calculate the % in postgresql
Below the my code
v_total_repro_count = (select count(pm_task_run_bug_repro_id) from project.pm_task_run_bug_repro
where pm_task_run_bug_detail_id = 605)--5;
v_repro_count = (select count(pm_task_run_bug_repro_id) from project.pm_task_run_bug_repro
where execution_status = 'Reprod' and pm_task_run_bug_detail_id = 605)--3;
v_impact = select (5/3)*100; = answer = 100
v_impact = select (3/5)*100; = answer = 0
the answer getting 0 and 100 instead of 60%
Cast value to numeric
SELECT ROUND(( 3::NUMERIC/5::NUMERIC ) * 100);
If you want floating point results, you should use at least one float in your calculation, such that the entire expression is promoted to floating point:
select (3.0 / 5) * 100; -- returns 60

Convert geographical coordinates using measurements package

I am trying to convert some data with the indicated measurements package, but I'm not succeeding on it.
My data:
Long Lat
62ᵒ36.080 58ᵒ52.940
61ᵒ28.020 54ᵒ59.940
62ᵒ07.571 56ᵒ48.873
62ᵒ04.929 57ᵒ33.605
63ᵒ01.419 60ᵒ30.349
63ᵒ09.555 61ᵒ29.199
63ᵒ43.499 61ᵒ23.590
64ᵒ34.175 62ᵒ30.304
63ᵒ16.342 59ᵒ16.437
60ᵒ55.090 54ᵒ49.269
61ᵒ28.013 54ᵒ59.928
62ᵒ07.868 56ᵒ48.040
62ᵒ04.719 57ᵒ32.120
62ᵒ36.083 58ᵒ51.766
63ᵒ01.644 60ᵒ30.714
64ᵒ33.897 62ᵒ30.772
63ᵒ43.604 61ᵒ23.426
63ᵒ09.288 61ᵒ29.888
63ᵒ16.722 59ᵒ16.204
What I'm trying:
library(measurements)
library(readxl)
coord = read.table('coord_converter.txt', header = T, stringsAsFactors = F)
# change the degree symbol to a space
lat = gsub('°','', coord$Lat)
long = gsub('°','', coord$Long)
# convert from decimal minutes to decimal degrees
lat = measurements::conv_unit(lat, from = 'deg_dec_min', to = 'dec_deg')
long = measurements::conv_unit(long, from = 'deg_dec_min', to = 'dec_deg')
What I'm getting with this penultimate line:
Warning messages:
In split(as.numeric(unlist(strsplit(x, " "))) * c(3600, 60), f = rep(1:length(x), : NAs introduced by coercion
In as.numeric(unlist(strsplit(x, " "))) * c(3600, 60) : longer object length is not a multiple of shorter object length
In split.default(as.numeric(unlist(strsplit(x, " "))) * c(3600, : data length is not a multiple of split variable
Can someone point my mistake or make a suggestion of how to proceed?
Thank you!
I think the issue here was that after gsub call, degrees and minutes were not space delimited, as required by measurements::conv_unit.
For example, this works fine (for this reproducible example I also changed "ᵒ" to "°"):
library(measurements)
#read your data
txt <-
"Long Lat
62°36.080 58°52.940
61°28.020 54°59.940
62°07.571 56°48.873
62°04.929 57°33.605
63°01.419 60°30.349
63°09.555 61°29.199
63°43.499 61°23.590
64°34.175 62°30.304
63°16.342 59°16.437
60°55.090 54°49.269
61°28.013 54°59.928
62°07.868 56°48.040
62°04.719 57°32.120
62°36.083 58°51.766
63°01.644 60°30.714
64°33.897 62°30.772
63°43.604 61°23.426
63°09.288 61°29.888
63°16.722 59°16.204"
coord <- read.table(text = foo, header = TRUE, stringsAsFactors = F)
# change the degree symbol to a space
lat = gsub('°',' ', coord$Lat)
long = gsub('°',' ', coord$Long)
# convert from decimal minutes to decimal degrees
lat = measurements::conv_unit(lat, from = 'deg_dec_min', to = 'dec_deg')
long = measurements::conv_unit(long, from = 'deg_dec_min', to = 'dec_deg')
yields...
> cbind(long, lat)
long lat
[1,] "62.6013333333333" "58.8823333333333"
[2,] "61.467" "54.999"
[3,] "62.1261833333333" "56.81455"
[4,] "62.08215" "57.5600833333333"
[5,] "63.02365" "60.5058166666667"
[6,] "63.15925" "61.48665"
[7,] "63.7249833333333" "61.3931666666667"
[8,] "64.5695833333333" "62.5050666666667"
[9,] "63.2723666666667" "59.27395"
[10,] "60.9181666666667" "54.82115"
[11,] "61.4668833333333" "54.9988"
[12,] "62.1311333333333" "56.8006666666667"
[13,] "62.07865" "57.5353333333333"
[14,] "62.6013833333333" "58.8627666666667"
[15,] "63.0274" "60.5119"
[16,] "64.56495" "62.5128666666667"
[17,] "63.7267333333333" "61.3904333333333"
[18,] "63.1548" "61.4981333333333"
[19,] "63.2787" "59.2700666666667"

Swift 3 - To the power of (pow) function not working as expected

Please could somebody help me. I am trying to run a simple compounding calculation in Swift.
Formula I am trying to recreate:
T = P(1+r/n)^(n*t), where
T = Total, P = Starting amount, r = interest rate, n = number of times compounded and t = number of years
My code as follows:
import Darwin
var total: Double
var startingAmount: Double = 5000.00
var interestRate: Double = 0.05
var numberOfTimesCompounded: Double = 4.0
var numberOfYears: Double = 2.0
var totalYear1: Double
var toThePowerOf: Double
totalYear1 = startingAmount * (1 + interestRate / numberOfTimesCompounded)
toThePowerOf = numberOfTimesCompounded * number of years
total = pow(totalYear1,toThePowerOf)
The answer to the formula should be 5,522.43
In my code above, TotalYear1 = 5062.50 (which is correct) and toThePowerOf = 8.0 (which is correct) However, total shows = 4314398832739892000000.00 which clearly isn't right. Could anyone tell me what I am doing wrong with my calculation of total?
Many thanks
You've actually implemented T = (P(1+r/n))^(n*t), which doesn't even make dimensional sense.
startingAmount needs to be multiplied at the end, it can't be part of the pow:
totalYear1 = (1 + interestRate / numberOfTimesCompounded)
toThePowerOf = numberOfTimesCompounded * number of years // [sic]
total = startingAmount * pow(totalYear1,toThePowerOf)

Distance calculation in query with slick database

I am using slick with scala to find the distance of the user to the closes spot. But I'm not sure how to properly do this with slick. I've tried different ways, but I keep getting errors with symbols or not getting the appropriate answers. The SQL query that I'm looking to convert is:
SELECT *, 3956 * 2 * sin(sqrt( pow(sin((spot_lat -
abs(lat)) * Pi/180 / 2),2) + cos(spot_lat * Pi/180 ) * cos(abs(lat) * Pi/180) *
pow(sin((spot_lon – lon) * Pi/180 / 2), 2) )) as distance
FROM Spots
Where lat and lon have been given as inputs
First, define your database trig functions so that they can be used by slick:
val dbSin = SimpleFunction.unary[Double, Double]("sin")
val dbCos = SimpleFunction.unary[Double, Double]("cos")
val dbSqrt = SimpleFunction.unary[Double, Double]("sqrt")
val dbPow = SimpleFunction.binary[Double, Double, Double]("pow")
val dbAbs = SimpleFunction.unary[Double, Double]("abs")
When you use these functions in a slick query, it generates the SQL query with calls to the database function. Also, make sure that the trig functions are defined in the database you are using. Read more about it at: http://slick.typesafe.com/doc/2.1.0/userdefined.html
Now you can use these functions in your query:
import scala.math._
def distance(lat: Double, lon: Double) = globals.spots map { spot =>
3956 * 2 * dbSin(dbSqrt(dbPow(dbSin((spot.splot_lat - Pi / 180.0 / 2.0), 2) +
dbCos(spot.spot_lat * Pi / 180.0) * dbCos(dbAbs(lat) * Pi / 180.0) *
dbPow(dbSin((spot.spot_lon - lon) * Pi / 180.0 / 2.0), 2)))
}
Please double check that I copied the equation correctly.

Scaling Up a Number

How do I scale a number up to the nearest ten, hundred, thousand, etc...
Ex.
num = 11 round up to 20
num = 15 round up to 20
num = 115 round up to 200
num = 4334 round up to 5000
I guess this formula might work? Unless you have more examples to show.
power = floor(log10(n))
result = (floor(n/(10^power)) + 1) * 10^power
import math
exp = math.log10(num)
exp = math.floor(exp)
out = math.ceil(num/10**exp)
out = out * 10**exp
Convert the number to a decimal (i.e. 11 goes to 1.1, 115 goes to 1.15), then take the ceiling of the number, then multiply it back. Example:
public static int roundByScale(int toRound) {
int scale = (int)Math.pow(10.0, Math.floor(Math.log10(toRound)));
double dec = toRound / scale;
int roundDec = (int)Math.ceil(dec);
return roundDec * scale;
}
In this case, if you input 15, it will be divided by 10 to become 1.5, then rounded up to 2, then the method will return 2 * 10 which is 20.
public static int ceilingHighestPlaceValue(int toCeil)
{
int placeValue = Math.Pow(10,toCeil.ToString().Length()-1);
double temp = toCeil / placeValue;
return= ceil(temp) * placeValue;
}