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

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...

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, ...

Making dashboards more usable for LLMs

I’ve been looking into agentic workflows to act as an operations assistant for the SaaS I'm working on. A big part of that work is getting the assistant to make sense of all the alert and monitoring data that pours in every day. Passing a bunch of raw time-series data to an LLM generally doesn’t work that well. You need to tell the LLM to aggregate the data and give it the means to do so. Using aggregates will often lead to better insights from the LLM. This is a well-known fact to anyone who has tinkered with this (at least at the time of writing this). Humans, of course, like to build visualizations and dashboards to solve this issue (yes, yes, dashboards are often useless, but complaining about that is another blog post). LLMs can analyze them as well and in fact are pretty good at that, so the aggregate can be something both humans and LLMs can digest. I’ve been tinkering with the idea of appending some LLM-only content to a dashboard—for example, additional context, specific d...