GithubHelp home page GithubHelp logo

jscience's People

Contributors

tarelli avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

jscience's Issues

UTM getLatitudeZone() sometimes returns wrong zone & possible fix for this

getLatitudeZone generates incorrect zone letters for some latitudes. At least, my version of your code converted to Kotlin does. I've fixed the issue on my side but you may want to do the same on your side. Here is my converted implementation:

https://github.com/jillesvangurp/geogeometry/blob/master/src/commonMain/kotlin/com/jillesvangurp/geo/utm.kt

The reason for the conversion is that I needed a Kotlin multiplatform implementation (i.e. without Java dependencies) for converting to/from UTM coordinates and have been looking for existing Java implementations to adapt. I started with another implementation before stumbling on yours:

https://gist.github.com/yazdipour/6231fcc7d1da8588601da2395dc3cb78 (this gist was linked in some forum post)

After converting that, I wrote some tests to ensure that it behaves correctly. This revealed some bugs with it where it generates wrong utm conversions for a small amount of coordinates. I suspect the convoluted formulas have a subtle bug somewhere.

This is why I started looking at your implementation, which looks a lot cleaner than the other one (thank you for this).

Interestingly, the test that I used also breaks for your implementation; but in a different way. This time, I was able to see a pattern.

The test randomly generates coordinates, converts them to utm, and then back again. I assert that the distance has to be less than 1m. In the cases it is wrong, it is very wrong by thousands of kilometers. On closer examination, I noticed the failing cases with your implementation all had the same latitude zone letter. I've pasted my test at the bottom, you might want to add a similar test.

However, the earlier Java implementation that I adapted had a much simpler/nicer implementation for calculating the latitude zone letter. After substituting your implementation for that one, all my tests now pass.

The following (Kotlin) code is what I ended up using. You might want to adapt this; it should be easy to convert back to Java.

fun getLatitudeZoneLetter(latLong: PointCoordinates): Char {
    val latitude = latLong.latitude
    return when {
        latitude < -72 -> 'C'
        latitude < -64 -> 'D'
        latitude < -56 -> 'E'
        latitude < -48 -> 'F'
        latitude < -40 -> 'G'
        latitude < -32 -> 'H'
        latitude < -24 -> 'J'
        latitude < -16 -> 'K'
        latitude < -8 -> 'L'
        latitude < 0 -> 'M'
        latitude < 8 -> 'N'
        latitude < 16 -> 'P'
        latitude < 24 -> 'Q'
        latitude < 32 -> 'R'
        latitude < 40 -> 'S'
        latitude < 48 -> 'T'
        latitude < 56 -> 'U'
        latitude < 64 -> 'V'
        latitude < 72 -> 'W'
        else -> 'X'
    }
}

Here's the kotlin code for the test that produces the error.

    @Test
    fun testLotsOfCoordinates() {
        fun Random.supportedUtmCoordinate(): DoubleArray {
            return doubleArrayOf(
                nextDouble(-180.0,180.0).roundDecimals(4),
                nextDouble(-80.0,84.0).roundDecimals(4)
            )
        }

        assertSoftly {
            repeat(10000) {
                Random.supportedUtmCoordinate().let { p ->
                    val toUTM = p.toUTM()
                    runCatching {
                        val convertedBack = toUTM.toWgs84()
                        withClue("${p.geoJson} -> ${convertedBack.geoJson} - $toUTM") {
                            convertedBack.let { out ->
                                out.distanceTo(p) shouldBeLessThan 1.0
                            }
                        }
                    }
                }
            }
        }
    }

is it me or is it a bug?

package jscience;

import static org.junit.Assert.assertEquals;

import javax.measure.quantity.Quantity;
import javax.measure.unit.BaseUnit;
import javax.measure.unit.Unit;

import org.jscience.physics.amount.Amount;
import org.junit.Before;
import org.junit.Test;

public class TestConversion {
    private Unit<Quantity> netto_unit;
    private Unit<Quantity> tax_unit;

    @Before
    public void setUp() {
        netto_unit = new BaseUnit<Quantity>("€ netto");
        tax_unit = netto_unit.times(0.2);
    }

    @Test
    public void conversionTest(){
        Amount<Quantity> netto = Amount.valueOf(100, netto_unit);
        Amount<Quantity> tax = netto.to(tax_unit);

        System.out.println("netto: " + netto);
        System.out.println("tax:   " + tax);

        assertEquals(20d, tax.doubleValue(tax_unit), 0.000000001d);
    }

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.