Spatialite return XY coordinate in Double Precision - qgis

I'm trying to return the first vertice of polyline in QspatiaLite.
The function x() is used, but it returns integer coordinate value.
How to return the coordinate value in double precision?
This is my query and result:

One possibility is to use the printf function to format the output:
SELECT id, AsText(StartPoint(Geometry), printf("%.02f", ST_X(PointN(Geometry,1))) FROM shp_lane;

Related

Errors converting lat and long values to sf coordinates

I have a series of coordinates that I am trying to plot on simple maps. I have two columns containing latitude and longitude for each point, respectively. I am attempting to use the st_as_sf function to convert this data into sf points but I keep getting numerous errors, most recently
"Error in UseMethod("st_as_sf") :
no applicable method for 'st_as_sf' applied to an object of class "c('double', 'numeric')"
I have tried changing the class of the data to numeric, double, integer, etc. and I continue to get this error.
Below is an example of my data
point lat long
1 38.254 -76.712
2 38.123 -76.710
3 38.438 -76.699
4 38.254 -76.712
5 38.232 -76.733
st_as_sf(coords=c(lat, long), crs=4326)
You need to work on the arguments of sf::st_as_sf() function; in the first place must be your data object, most likely a data frame with columns named lat and long.
In the second place you specify coords, which should be a string vector of variable names, contained in your data object - so the names "lat" and "long" are expected enclosed in quotation marks (these are not R variables, but column names).
In the third place is expected a coordinate reference system to make sense of the coordinates provided in the second step; in case of WGS84 this will be 4326.
So consider this piece of code; it is formally correct, but places your points somewhere in Antarctica (should you flip the coordinates to long-lat the points would be placed in Washington, DC).
library(sf)
raw_pts <- data.frame(point = 1:5,
lat = c(38.254, 38.123, 38.438, 38.254, 38.232),
long = c(-76.712, -76.710, -76.699, -76.712, -76.733))
points <- st_as_sf(raw_pts, # first argument = data frame with coordinates
coords = c("lat", "long"), # name of columns, in quotation marks
crs = 4326) # coordinate reference system to make sense of the numbers
points
# Simple feature collection with 5 features and 1 field
# Geometry type: POINT
# Dimension: XY
# Bounding box: xmin: -76.733 ymin: 38.123 xmax: -76.699 ymax: 38.438
# Geodetic CRS: WGS 84
# point geometry
# 1 1 POINT (38.254 -76.712)
# 2 2 POINT (38.123 -76.71)
# 3 3 POINT (38.438 -76.699)
# 4 4 POINT (38.254 -76.712)
# 5 5 POINT (38.232 -76.733)

Logarithmic Interpolation and 0 values

I have a function that interpolates between two values on a logarithmic scale, so as to have more values around the lower end of my values.
public void logspace(double lower, double upper, double amount) {
double start = Math.log(Math.min(lower, upper));
double end = Math.log(Math.max(lower, upper));
double step = (end - start) / amount;
for(double level = start; level <= end; level += step) {
System.out.println(Math.exp(level));
}
}
However I do not know how to correctly handle if one of the values is 0. If one of the values if 0 then we end up with -Infinity as one of our boundaries, which produces unexpected results.
I can't simply use 0 in place of log(0) as this would result in 0 values between 0 and 1, as log(1) is 0.
How can I produce logarithmically spaced values between two arbitrary values?
You can use two different formulas depending on whether your value is above or below some arbitrary cutoff. With a little care those two formulas will have the same value at the cutoff point.
For example, we'll assume your data doesn't have any interesting values below 1.0. If we use the log scale for everything above 1.0, the lowest output will be 0.0. Then for everything below 1.0, we switch to a linear scale.
public double logscale(double value) {
if (value > 1.0)
return Math.log(value);
else
return value - 1.0;
}
You can improve the linear portion of the formula by using a multiplier to set the slope of the curve from that point.
It's also possible to use something other than linear below the inflection point, but I'll leave that as an exercise for the reader.

How to simplify a symbolic and numeric mixed expression in Matlab

I have a symbolic and numeric mixed expression:
(3145495418313256125*sin(11334310783410932061962315977/17437937757178560512000000000)*cos(theta))/85568392920039424
where theta is a symbolic variable. I want to simplify this expression such that all the numeric numbers and their math operation results are changed to double.
In terms of data types, you can't mix floating point and symbolic values. However, you can use variable precision arithmetic so that the values are represented in decimal form. Using vpa:
syms theta
y1 = (3145495418313256125*sin(11334310783410932061962315977/17437937757178560512000000000)*cos(theta))/85568392920039424
y2 = vpa(y1)
which returns
y2 =
22.24607614528243677915796931637*cos(theta)
The data type (class) of y2 is still sym. See the digits function to adjust the number of significant digits.
If you want to work in actual floating point you'll need to convert your symbolic expression into a function. You can automate that procedure by using the confusingly-named matlabFunction:
thetafun = matlabFunction(y1)
which returns a function using double precision variables:
thetafun =
#(theta)cos(theta).*2.224607614528244e1
The anonymous function thetafun can then be called just like any function, e.g., thetafun(0.5).
You can make use of coeffs command to achieve the desired:
f=2*cos(theta)/3+5*sin(theta)/19
c_f=coeffs(f);
fraction_c_f=double(c_f);
ans = [0.2632 0.6667]

Is there equivalent function to fminbnd for maximum value?

I am doing homework in Matlab, calculating numeric integration using different methods like simpson, etc. I need to find value n from formulas for error of method like this one for rectangle method
E = (b-a)/24 * max f''(x) * ((b-a)/n)^2
n = sqrt(((b-a)^3 * max f''(x)) / 24 * E)
problem is finding function for finding maximum value of f''(x) in range a to b.
I have only found fminbnd, which calculates minimum value of function in range. Is there function which would calculate maximum value?
Look for the minimum of -f''(x) (the negative of the function you want to maximize).

How can I cast variables in matlab with fixed floating point

Is it possible convert a double variable to a float (single in Matlab) with fixed floating point?
For example
x = 10.023213032130123021302130210331232132103312321
to
x = 10.0231
Thank you !
First convert to single:
X = single(Y)
And than apply round() to get fixed format:
X = round(10^N*X) / 10^N;
to get N digits behind the decimal point