I am getting the latitude and longitude from GPS device. They look like 21081686N,079030977E
If I manually convert them to 21°08'16.86"N, 79°03'09.77"E and check on Google maps, the location is almost correct.
How do I convert these values in java and convert them to decimal accurately? Any JAVA libraries?
Thanks,
You shouldn't need a library, but rather just break up the strings into their component parts. Perhaps this will help. Please note I'm not a Java programmer so this syntax could very well be wrong. (Perhaps retag your question to Java)
By decimal I presume you mean decimal degrees and not ddd.mmss
// Object for Lat/Lon pair
public class LatLonDecimal
{
public float lat = 0.0;
public float lon = 0.0;
}
// Convert string e.g. "21081686N,079030977E" to Lat/Lon pair
public LatLonDecimal MyClass::convert(String latlon)
{
String[] parts = latlon.split(",");
LatLonDecimal position = new LatLonDecimal();
position.lat = convertPart(parts[0]);
position.lon = convertPart(parts[1]);
return position;
}
// Convert substring e.g. "21081686N" to decimal angle
private float MyClass::convertPart(String angle)
{
while (angle.length() < 10)
angle = StringBuffer(angle).insert(0, "0").toString();
int deg = Integer.parseInt( angle.substring(0,2) );
int min = Integer.parseInt( angle.substring(3,4) );
int sec = Integer.parseInt( angle.substring(5,6) );
int sub = Integer.parseInt( angle.substring(7,8) );
String hem = angle.substring(9);
float value = deg + min / 60.0f + sec / 3600.0f + sub / 360000.0f;
float sign = (hem == "S") ? -1.0 : 1.0; // negative southern hemisphere latitudes
return sign * value;
}
Related
I am using webview2 with google maps api scripts. With 96 dpi (100% scale) the static map is inserted correctly into AutoCAD, the lat and long are calculated correcly based on the screen point. If I increase the display scale higher than 100%, the lat and long are calculated wrong based on the screen point, the static map is inserted not far from the right position in AutoCAD.
What would be the solution with higher dpi?
Here is the script I use
function getMapType() {
return map.getMapTypeId();
}
function getMapZoom() {
return map.getZoom();
}
function getMapCenter() {
var c = map.getCenter();
return c.lat() + "|" + c.lng();
}
function getMapProjection() {
projection = map.getProjection();
topRight = projection.fromLatLngToPoint(map.getBounds().getNorthEast());
bottomLeft = projection.fromLatLngToPoint(map.getBounds().getSouthWest());
scale = 1 << map.getZoom();
}
function getLatLongByScreenPoint(x, y) {
var c = projection.fromPointToLatLng(new google.maps.Point(x / scale + bottomLeft.x, y / scale + topRight.y));
return c.lat() + "|" + c.lng();
}
i want to let the player in bukkit minecraft 1.13 solve a equation for x,
i wrote a generator and its working fine. But there are fractions possible as answers so i thought i could get the players answer, check if its a fraction, convert it to a double, back to a string and see if the fraction in decimal equals the solution:
if(cmd.getName().equalsIgnoreCase("math")) {
Player p = (Player) sender;
if(mathPlayer.contains(p.getName())) {
String eing = args[0];
String eing2 = "";
String eingf = "";
double vergl = 0.0;
if(eing.contains("/")) {
eing2 = eing.replace(",", ".");
vergl = Double.parseDouble(eing2);
eingf = Double.toString(vergl).replace(".", ",");
} else {
eingf = eing;
}
int in = mathPlayer.indexOf(p.getName());
String ergeb = mathAnswer.get(in);
if(ergeb.contains(eingf)) {
World w = p.getWorld();
w.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 10, 1);
p.sendMessage("§8[§cMATH§8] §aCongratulations!");
} else {
p.sendMessage("§8[§cMATH§8] §7Wrong: Solution: " + ergeb);
}
mathPlayer.remove(in);
mathAnswer.remove(in);
} else {
String equation = genequation(p);
sender.sendMessage("§8[§cMATH§8] §7Solve for x: §a" + equation);
p.sendMessage("§8[§cMATH§8] §7Enter answer with /math <answer>!");
}
return false;
Thanks in advance
Looking through your code i believe that you have asked the user to enter a fraction and not a decimal. However, a fraction needs to be converted to decimal before you can check it.
Easiest way to do this I believe would be like so:
double partA = Double.parseDouble(args[0].substring(0, args[0].indexOf("/")));
double partB = Double.parseDouble(args[0].substring(args[0].indexOf("/") + 1))
double answer = partA / partB
This is approximate and typed on an ipad but them you would compare it to the answer of the question.
Also, but confused as why you return false at the end instead of true.
Edit:
Use ChatColor.COLOR_CHAR for the color symbol as that could cause issues later down the line
I know how to go the other way around. What I am looking for is, given a (x,y) coordinate in the pixel space (of the 1920x1080 image), how do I get the corresponding (if available) (x,y,z) (in meters) of the depth image. I realize that there are more pixels than voxels and it could be possible not to find any, but Microsoft's SDK has a CoordinateMapper class. This exposes the MapColorFrameToCameraSpace function. If I use that, I can get an array of points in the camera space (x,y,z) but I am unable to figure out how to extract the mapping for a specific pixel.
You probably need to use
CoordinateMapper.MapDepthFrameToColorSpace
Find the color space coordinates of all depth point.
Then, compare your pixel coordinate (x, y) with these color space coordinates. My solution is to find the closest point (might have better way), because the mapped coordinates are floats.
If you use C#, here is the code. Hope it helps!
private ushort GetDepthValueFromPixelPoint(KinectSensor kinectSensor, ushort[] depthData, float PixelX, float PixelY)
{
ushort depthValue = 0;
if (null != depthData)
{
ColorSpacePoint[] depP = new ColorSpacePoint[512 * 424];
kinectSensor.CoordinateMapper.MapDepthFrameToColorSpace(_depthData, depP);
int depthIndex = FindClosestIndex(depP, PixelX, PixelY);
if (depthIndex < 0)
Console.WriteLine("-1");
else
{
depthValue = _depthData[depthIndex];
Console.WriteLine(depthValue);
}
}
return depthValue;
}
private int FindClosestIndex(ColorSpacePoint[] depP, float PixelX, float PixelY)
{
int depthIndex = -1;
float closestPoint = float.MaxValue;
for (int j = 0; j < depP.Length; ++j)
{
float dis = DistanceOfTwoPoints(depP[j], PixelX, PixelY);
if (dis < closestPoint)
{
closestPoint = dis;
depthIndex = j;
}
}
return depthIndex;
}
private float DistanceOfTwoPoints(ColorSpacePoint colorSpacePoint, float PixelX, float PixelY)
{
float x = colorSpacePoint.X - PixelX;
float y = colorSpacePoint.Y - PixelY;
return (float)Math.Sqrt(x * x + y * y);
}
I'm trying to get a GPS coordinate given the current location, bearing and distance.
But I can't find any related to cocoa-touch.
I've found the formula here Get lat/long given current point, distance and bearing
Is there an easier method for iOS?
Thanks in advance.
Converted the code in the link to Objective-C.
- (double)degreesToRadians:(double)degrees {
return degrees * M_PI / 180;
}
- (double)radiansToDegrees:(double)radians {
return radians * 180/ M_PI;
}
- (CLLocationCoordinate2D)remoteCoordinate:(CLLocationCoordinate2D)localCoordinate withDistance:(double)distance withBearing:(double)bearing {
double earthRadius = 6378.1; // Radius of Earth in kilometres.
double rLat1 = [self degreesToRadians:localCoordinate.latitude]; // Convert latitude to radians
double rLon1 = [self degreesToRadians:localCoordinate.longitude]; // Convert longitude to radians
double rLat2 = asinl( sinl(rLat1) * cosl(distance / earthRadius) + cosl(rLat1) * sinl(distance / earthRadius) * cosl(bearing) );
double rLon2 = rLon1 + atan2l( sinl(bearing) * sinl(distance/earthRadius) * cosl(rLat1), cosl(distance/earthRadius) - sinl(rLat1) * sinl(rLat2) );
double dLat2 = [self radiansToDegrees:rLat2]; // Convert latitude to degrees
double dLon2 = [self radiansToDegrees:rLon2]; // Convert longuitude to degrees
return CLLocationCoordinate2DMake(dLat2, dLon2);
}
How can I convert a float to int while rounding up to the next integer? For example, 1.00001 would go to 2 and 1.9999 would go to 2.
float myFloat = 3.333
// for nearest integer rounded up (3.333 -> 4):
int result = (int)ceilf(myFloat );
// for nearest integer (3.4999 -> 3, 3.5 -> 4):
int result = (int)roundf(myFloat );
// for nearest integer rounded down (3.999 -> 3):
int result = (int)floor(myFloat);
// For just an integer value (for which you don't care about accuracy)
int result = (int)myFloat;
Use ceil function:
int intValue = (int)ceil(yourValue);
You can use following C methods to get the int values from different dataTypes.
extern float ceilf(float);
extern double ceil(double);
extern long double ceill(long double);
These functions return float, double and long double respectively. But the job of these function is to get ceil of or floor of the argument. As in http://en.wikipedia.org/wiki/Floor_and_ceiling_functions
Then you can cast the return value to desired type like.
int intVariable = (int)ceilf(floatValueToConvert);
Hope it is helpful.
if we have float value like 13.123 to convert it into integer like 13
Code
float floatnumber=13.123; //you can also use CGFloat instead of float
NSLog(#"%.f",floatnumber); // use for print in command prompt