Java - convert MGRS coordinate to LatLon WGS - java

For my App I need compact code for converting between LatLon (WGS84) and MGRS.
JCoord.jar:
Looks great, but the version 1.1 jar is 0.5Mb in size. That is doubles my App for only perfoming a 2-way conversion of coordinates.
Openmap:
Isolating just the MGRSPoint.java (https://code.google.com/p/openmap/source/browse/src/openmap/com/bbn/openmap/proj/coords/MGRSPoint.java) from the rest is not easy.
GeographicLib:
This seems a good solution, but I could not find a Java source file.
Is it available for usage?
Nasa:
The Nasa code looks great, see http://worldwind31.arc.nasa.gov/svn/trunk/WorldWind/src/gov/nasa/worldwind/geom/coords/MGRSCoordConverter.java. Isolating just the MGRS conversion code was not easy.
GDAL:
Was implemented in another programming language.
IBM (via j-coordconvert.zip):
Is complact, suits well for the UTM conversion, but the MGRS conversion is described to be errorneous. Alas.
Is there a good (compact) Java source for converting between LatLon/wgs84 and MGRS?

Finally found a sufficient good answer. Berico, thank you!
https://github.com/Berico-Technologies/Geo-Coordinate-Conversion-Java
This source code isolates the NASA Java source code and adds 1 nice utility class.
Examples:
double lat = 52.202050;
double lon = 6.102050;
System.out.println( "To MGRS is " + Coordinates.mgrsFromLatLon( lat, lon));
And the other way around:
String mgrs = "31UCU 59248 14149";
double[] latlon = Coordinates.latLonFromMgrs( mgrs);

Related

Convert WGS84 lat/long to GGRS87

Hello I am using GMapsFX for my JavaFX application. I am getting longtitude and latitute from Google Maps with the API but I would like to get GGRS87 also.
In Greek it is called EGSA87. Ι have searched for that and found nothing so far. There might be a solution on the libraries of Geotools or proj4j but I don't have very good knowledge of java and especially I can't find the solutions in so big libraries like those.
I have found many sites to make calculations but how can I make a mathematical calculation myself? Or even a library to solve this problem?
You can use the GDAL library for it, but its setup is actually quite complicated. You would have to install GDAL on your system (it is in Debian packages) and then build the Java JNI bindings for it [1]. [2] is also quite useful in getting it to work.
Then, you could use the Java GDAL library:
<dependency>
<groupId>org.gdal</groupId>
<artifactId>gdal</artifactId>
</dependency>
and do something like:
final SpatialReference source = new SpatialReference();
source.ImportFromEPSG(4326); // EPSG code for WGS84, see www.epsg.io
final SpatialReference target = new SpatialReference();
target.ImportFromEPSG(4121); // EPSG code for GGRS87, see www.epsg.io
this.coordinateTransform = CoordinateTransformation.CreateCoordinateTransformation(source, target);
final Geometry geometry = ogr.CreateGeometryFromWkt(String.format("POINT (%f %f)", longitude, latitude));
geometry.Transform(coordinateTransform);
final double ggrsX = geometry.GetX();
final double ggrsY = geometry.GetY();
[1] https://trac.osgeo.org/gdal/wiki/GdalOgrInJavaBuildInstructionsUnix
[2] http://geoexamples.blogspot.com/2012/05/running-gdal-java.html

ApacheCommons: Weird results from ChiSquareTest

I am using the Apache Commons lib to calculate the p-value with the ChiSquareTest:
I use the method chiSquareTest(double[] expected, long[] observed); But the values I get back don't make sense to me. So I tried numerous ChiSquare Online Calculators to find out what this function calculates.
An example:
Group 1: {25,25}
Group 2: {30,20}
(Taken from Wikipedia, German Chi Square Test article)
P- values from:
http://www.quantpsy.org/chisq/chisq.htm and
http://vassarstats.net/newcs.html
P = 0.3149 and 0.31490284
0.42154642 and 0.4201
(with and without Yates Correction)
Apache Commons: 0.1489146731787664
Code:
ChiSquareTest tester = new ChiSquareTest();
long[] b = {25,25};
double[] a = {30,20};
tester.chiSquareTest(a,b);
Another thing I do not understand is the need to have a long and a double array. Why not two long arrays?
There are two functions in the lib:
chiSquareTest(double[] expected, long[] observed)
chiSquareTest(long[][] values)
The first one (which I used in the question above) computes the goodness of a fit. But I expected the result from the second one, the test of independence.
The answer was given to me on the Apache Commons user Mailinglist, I will add a link to the archive once it is there. But it is also written in the JavaDoc.
Update:
Mailinglist Archive

How can i send an query from java to lpsolve as String

Hi i formulated a linear programing problem using java
and i want to send it to be solved by lpsolve without the need to create each constraint seperatlly.
i want to send the entire block (which if i insert it to the ide works well) and get a result
so basically instead of using something like
problem.strAddConstraint("", LpSolve.EQ, 9);
problem.strAddConstraint("", LpSolve.LE, 5);
i want to just send as one string
min: 0*x11 + 0*x12 + 0*x13
x11 + x12 + x13= 9;
x12 + x12<5;
can it be done if so how?
LpSolve supports LP files as well as MPS files. Everything is thoroughly detailed in the API documentation (see http://lpsolve.sourceforge.net/5.5/).
You can do your job like this in java :
lp = LpSolve.readLP("model.lp", NORMAL, "test model");
LpSolve.solve(lp)
What is sad with file based approaches is that you will not be able to use warm start features. I would not suggest you to use such approach if you want to optimize successive similar problems.
Cheers

Rosin thresholding (Unimodal thresholding) JAVA

I have to do for my project many image filters (all you can imagine) in JAVA (I use JAVA JAI). I have done all except unimodal thresholding by Paul L. Rosin. I found only this document and implementation on c++. Unfortunately, I'm terrible in c ++. Can you help me please? Thanks!
Just use Catalano Framework. It's very easy and fast. There's a lot of examples in samples folder.
import Catalano.Imaging.Filters;
// If you want to use parallel processing. Change the namespace for:
// import Catalano.Imaging.Concurrent.Filters;
FastBitmap fb = new FastBitmap(bufferedImage);
fb.toGrayscale();
RosinThreshold rosin = new RosinThreshold();
rosin.applyInPlace(fb);

Creating graphs in Matlab and displaying them in a Java program

I have a requirement to produce graphs of matrices and display these graphs on a JSP. The project has been developed in Java and so far all my operations relating to matrices are being performed using the MatLabControl API
http://code.google.com/p/matlabcontrol/ .
I wanted to return the matrices produced by MATLAB (especially eigen value matrices and wavelets). MATLAB provides a function "im2java" that converts graph image from its MATLAB representation to a java.awt.Image. My code used to get the image data in MatlabControl was as follows:
public Image produceEigenValueGraph(final double [][] matrix) {
final double [][] maxEigenValueMatrix =
extractOutMaxEigenValues(matrix);
Image matlabPlotImage = null;
try {
MatlabNumericArray matLabEigenValueMatrix =
new MatlabNumericArray(maxEigenValueMatrix, null);
matLabTypeConverter.setNumericArray("eigen",
matLabEigenValueMatrix);
matLabProxy.setVariable("amountOfTime", matrix.length - 1);
matLabProxy.eval("time");
matLabProxy.eval("plot(time, eigen)");
matLabProxy.eval("frame=getframe");
final Object [] returnedMatlabArguements =
matLabProxy.returningEval("im2java(frame.cdata)", 1);
matlabPlotImage =
(Image)returnedMatlabArguements[0];
} catch (MatlabInvocationException mie) {
mie.printStackTrace();
}
return matlabPlotImage;
}
The code returns a nested exception:
Caused by: java.io.WriteAbortedException: writing aborted;
java.io.NotSerializableException: sun.awt.image.ToolkitImage
Which basically puts an end to any hope of the above code working, unless I am incorrect in my use.
N.B The code does produce a correct graph it fails to return it in java.awt.Image
My questions are:
-Is the above code the correct/only way to return images to a java program from Matlab?
-If it is what would be the best alternatives to using Matlab, Java API or otherwise?
Is this the line that causes the exception?
matlabPlotImage = (Image)returnedMatlabArguements[0];
In answer to your question
"-Is the above code the correct/only way to return images to a java program from Matlab?"
You can call java classes from Matlab so you could also use the java in a Matlab file and call that to replace
final Object [] returnedMatlabArguements = matLabProxy.returningEval("im2java(frame.cdata)", 1);
matlabPlotImage = (Image)returnedMatlabArguements[0];
The error is being thrown because Image is not serializeable. An option would be to save it as a file in some image format (jpg,png,tiff) using either matlab or java and return File instead of Image.
"-If it is what would be the best alternatives to using Matlab, Java API or otherwise?"
Mathworks provide a Java api to perform a number of linear algebra calculations that you could implement.
http://math.nist.gov/javanumerics/jama/#Package
Alternatively the Apache Commons Math project provide a wide range of linear algebraic functions as well as other tools. http://commons.apache.org/math/userguide/linear.html
I would check other posts for suggestions on graphing in java
constructing graphs in Java
Java Graphing Libraries for Web Applicattions?

Categories