Skip to main content

ykj2wgs84

Viljelysuunnitteluprojektissa olennaisena osana ovat luonnollisesti pellot. Suomalaiset maanviljelijät saavat omat peltotietonsa koordinaatteineen maaseutuviraston GISistä. Teimme oman parsijan, joka parsii koordinaatit ja peltojen nimet mavin viljelijäpalvelusta. Koordinaatit ovat palvelussa YKJ tasokoordinaatistossa ja ne täytyy muuttaa karttapalveluja ja mobiiliosan GPS träkkeriä varten WGS84 koordinaateiksi.

Tein muunnosoperaatiota varten melko mielenkiintoisen metodin

public double[] ykj2wgs84(double E, double N) {

  double f = 1.0D / 297.0;
  double el = f / (2 - f);
  double a = 6378388.0;
  double A1 = (a / (1 + el))
    * (1 + (Math.pow(el, 2) / 4) + (Math.pow(el, 4) / 64));
  double E0 = 3500000.0;
  double lambda_0 = 27 * (Math.PI / 180);
  double k_0 = 1.0D;

  double eps = N / (A1 * k_0);
  double n = (E - E0) / (A1 * k_0);

  double h1 = ((((1.0D / 2.0) * el) - ((2.0 / 3.0) * Math.pow(el, 2))) + ((37.0 / 96.0) * Math
    .pow(el, 3))) - ((1.0D / 360.0) * Math.pow(el, 4));
  double h2 = (((1.0D / 48.0) * Math.pow(el, 2)) + ((1.0D / 15.0) * Math
    .pow(el, 3))) - ((437.0 / 1140.0) * Math.pow(el, 4));
  double h3 = ((17.0 / 480.0) * Math.pow(el, 3))
    - ((37.0 / 840.0) * Math.pow(el, 4));
  double h4 = (4397.0 / 161280.0) * Math.pow(el, 4);

  double r1 = h1 * Math.sin(2 * eps) * Math.cosh(2 * n);
  double r2 = h2 * Math.sin(4 * eps) * Math.cosh(4 * n);
  double r3 = h3 * Math.sin(6 * eps) * Math.cosh(6 * n);
  double r4 = h4 * Math.sin(8 * eps) * Math.cosh(8 * n);

  double s1 = h1 * Math.sinh(2 * n) * Math.cos(2 * eps);
  double s2 = h2 * Math.sinh(4 * n) * Math.cos(4 * eps);
  double s3 = h3 * Math.sinh(6 * n) * Math.cos(6 * eps);
  double s4 = h4 * Math.sinh(8 * n) * Math.cos(8 * eps);

  double eps_f = eps - (r1 + r2 + r3 + r4);
  double n_f = n - (s1 + s2 + s3 + s4);
  double e = Math.sqrt((2 * f) - Math.pow(f, 2));

  double beta = Math.asin(sech(n_f) * Math.sin(eps_f));
  double l = Math.asin(Math.tanh(n_f) / (Math.cos(beta)));

  double Q1 = asinh(Math.tan(beta));
  double Q = Q1 + (e * atanh(Math.tanh(Q1) * e));
  Q = Q1 + (e * atanh(Math.tanh(Q) * e));
  Q = Q1 + (e * atanh(Math.tanh(Q) * e));
  Q = Q1 + (e * atanh(Math.tanh(Q) * e));

  double phi = Math.atan(Math.sinh(Q));
  double lambda_r = lambda_0 + l;
  double lambda = lambda_r;

  double NN = a
    * Math.pow((1 - (Math.pow(e, 2) * Math.pow(Math.sin(phi), 2))),
      -1.0D / 2.0);
  double X0 = (NN + 50) * Math.cos(phi) * Math.cos(lambda);
  double Y0 = (NN + 50) * Math.cos(phi) * Math.sin(lambda);
  double Z0 = ((NN * (1 - Math.pow(e, 2))) + 50) * Math.sin(phi);

  double DX = -96.062;
  double DY = -82.428;
  double DZ = -121.754;
  double Rx = -4.801 * (1.0D / 3600.0) * (Math.PI / 180.0);
  double Ry = -0.345 * (1.0D / 3600.0) * (Math.PI / 180.0);
  double Rz = 1.376 * (1.0D / 3600.0) * (Math.PI / 180.0);
  double m = 1.496;

  double X1 = DX
    + ((1 + (m / 1000000)) * (((1 * X0) + (Rz * Y0)) - (Ry * Z0)));
  double Y1 = DY
    + ((1 + (m / 1000000)) * ((-Rz * X0) + (1 * Y0) + (Rx * Z0)));
  double Z1 = DZ
    + ((1 + (m / 1000000)) * (((Ry * X0) - (Rx * Y0)) + (1 * Z0)));

  lambda = Math.atan(Y1 / X1);
  double a_grs = 6378137.0;
  double f_grs = 1 / 298.2572;
  double e_grs = Math.pow(((2 * f_grs) - Math.pow(f_grs, 2)), 0.5);
  double phi_0 = Math.atan(Z1
    / ((1 - Math.pow(e_grs, 2)) * Math.pow(
      (Math.pow(X1, 2) + Math.pow(Y1, 2)), (0.5))));
  double phi_i = phi_0;
  double N_i = 0.0;
  double h_i = 0.0;
  for (int i = 0; i < 3; i++) {
   N_i = a_grs
     * Math.pow((1 - (Math.pow(e_grs, 2) * Math.pow(
       Math.sin(phi_i), 2))), (-0.5));
   if (Math.abs(phi_0) < (45.0 * (Math.PI / 180.0))) {
    h_i = ((Math.sqrt((Math.pow(X1, 2) + Math.pow(Y1, 2)))) / (Math
      .cos(phi_i))) - N_i;
   } else {
    h_i = (Z1 / (Math.sin(phi_i)))
      - ((1.0 - Math.pow(e_grs, 2)) * N_i);
   }
   phi_i = Math
     .atan((Z1 / Math.sqrt(Math.pow(X1, 2) + Math.pow(Y1, 2)))
       * (1.0D / (1.0D - ((Math.pow(e_grs, 2) * N_i) / (N_i + h_i)))));
  }

  return new double[] { phi_i * (180.0 / Math.PI),
    lambda * (180.0 / Math.PI) };
 }

Comments

Popular posts from this blog

RocksDB data recovery

I recently needed to do some maintenance on a RocksDB key-value store. The task was simple enough, just delete some keys as the db served as a cache and did not contain any permanent data. I used the RocksDB cli administration tool ldb to erase the keys. After running a key scan with it, I got this error Failed: Corruption: Snappy not supported or corrupted Snappy compressed block contents So a damaged database. Fortunately, there's a tool to fix it, and after running it, I had access to the db via the admin tool. All the data was lost though. Adding and removing keys worked fine but all the old keys were gone. It turned out that the corrupted data was all the data there was. The recovery tool made a backup folder, and I recovered the data by taking the files from the backup folder and manually changing the CURRENT file to point to the old MANIFEST file which is apparently how RocksDB knows which sst (table) files to use. I could not access the data with the admin tool, ...

I'm not a passionate developer

A family friend of mine is an airlane pilot. A dream job for most, right? As a child, I certainly thought so. Now that I can have grown-up talks with him, I have discovered a more accurate description of his profession. He says that the truth about the job is that it is boring. To me, that is not that surprising. Airplanes are cool and all, but when you are in the middle of the Atlantic sitting next to the colleague you have been talking to past five years, how stimulating can that be? When he says the job is boring, it is not a bad kind of boring. It is a very specific boring. The "boring" you would want as a passenger. Uneventful.  Yet, he loves his job. According to him, an experienced pilot is most pleased when each and every tiny thing in the flight plan - goes according to plan. Passengers in the cabin of an expert pilot sit in the comfort of not even noticing who is flying. As someone employed in a field where being boring is not exactly in high demand, this sounds pro...

PydanticAI + evals + LiteLLM pipeline

I gave a tech talk at a Python meetup titled "Overengineering an LLM pipeline". It's based on my experiences of building production-grade stuff with LLMs I'm not sure how overengineered it actually turned out. Experimental would be a better term as it is using PydanticAI graphs library, which is in its very early stages as of writing this, although arguably already better than some of the pipeline libraries. Anyway, here is a link to it. It is a CLI poker app where you play one hand against an LLM. The LLM (theoretically) gets better with a self-correcting mechanism based on the evaluation score from another LLM. It uses the annotated past games as an additional context to potentially improve its decision-making. https://github.com/juho-y/archipylago-poker