【问题标题】:transfer public class to abstract class in Java将公共类转移到Java中的抽象类
【发布时间】:2014-08-01 10:24:01
【问题描述】:

我几乎是 Java 编程新手。我想在 MATSim 中实现以下代码(它是一个用于城市规划目的的模拟程序)。此代码是关于将 WGS84(经度和纬度)转换为 OSGB36(英国国家电网)。

https://gist.github.com/schoenobates/3497544#file-osgb-java

MATSim 库有一个接口(如下),可以将转换代码放入其中并用于视觉模拟。

我已经尝试过很多次来处理错误,但它对我不起作用。请您给我一些关于如何做到这一点的想法。

package org.matsim.core.utils.geometry;

import org.matsim.api.core.v01.Coord;


/**
 * A simple interface to convert coordinates from one coordinate system to
 * another one.
 *
 * @author mrieser
 */
public interface CoordinateTransformation {

    /**
     * Transforms the given coordinate from one coordinate system to the other.
     *
     * @param coord The coordinate to transform.
     * @return The transformed coordinate.
     */
    public Coord transform(Coord coord);

}

谢谢你,Somayeh

好的,错误在这里:

我做了以下:

这给了我两个语法错误:

double[] eastANDnorth(double lat, double lon) { 表示花括号和逗号; 和 return new double(OSGB36N, OSGB36E); for double

package org.matsim.core.utils.geometry.transformations;

import org.matsim.api.core.v01.Coord;
import org.matsim.core.utils.geometry.CoordImpl;
import org.matsim.core.utils.geometry.CoordinateTransformation;


public class WGS84toOSGB36 implements CoordinateTransformation {

    @Override
    public Coord transform(Coord coord) {

        // WGS84 ELLIPSOID
        double WGS84_A = 6378137;
        double WGS84_B = 6356752.314245;
        double WGS84_E2 = ((WGS84_A * WGS84_A) - (WGS84_B * WGS84_B) / (WGS84_A * WGS84_A));

        // NstGrid scale factor on central meridian
        final double F0 = 0.9996012717;

        // Airy 1830 major & minor semi-axes - note .909 not .910

        final double AIRY_A = 6377563.396;
        final double AIRY_B = 6356256.909;

        // NatGrid true origin
        final double LAT0 = Math.toRadians(49); //Phi0
        final double LON0 = Math.toRadians(-2); //Lamda0

        // northing & easting of true origin, metres
        final double N0 = -100000;
        final double E0 = 400000;

        // eccentricity squared 
        final double E2 = ((AIRY_A * AIRY_A) - (AIRY_B * AIRY_B)) / (AIRY_A *AIRY_A);

        final double N = (AIRY_A - AIRY_B) / (AIRY_A + AIRY_B);
        final double N2 = N * N;
        final double N3 = N * N * N;

        final double TX = -446.448;
        final double TY = 125.157;
        final double TZ = -542.060;
        final double RX = Math.toRadians(-0.1502 / 3600);
        final double RY = Math.toRadians(-0.2470 / 3600);
        final double RZ = Math.toRadians(-0.8421 / 3600);
        final double S = 20.4894 / 1e6 + 1;

/*----*/double[] eastANDnorth(double lat, double lon) {/* error in this line */

            // -- 1: convert polar to cartesian coordinates (using ellipse 1)

            // WGS84 ellipsoid
            double sinPhi = Math.sin(lat);
            double cosPhi = Math.cos(lat);
            double sinLambda = Math.sin(lon);
            double cosLambda = Math.cos(lon);
            double H = 24.7; // for the moment

            double nu = WGS84_A / Math.sqrt(1 - WGS84_E2 * sinPhi * sinPhi);

            double x1 = (nu + H) * cosPhi * cosLambda;
            double y1 = (nu + H) * cosPhi * sinLambda;
            double z1 = ((1-WGS84_E2) * nu + H) * sinPhi;

            // -- 2: apply helmert transform using appropriate params
            double x2 = TX + x1 * S - y1 * RZ + z1 * RY;
            double y2 = TY + x1 * RZ + y1 * S - z1 * RX;
            double z2 = TZ - x1 * RY + y1 * RX + z1 * S;

            // -- 3: convert cartesian to polar coordinates (using ellipse 2)
            double precision = 4 / AIRY_A;

            double p = Math.sqrt(x2 * x2 + y2 * y2);
            double phi = Math.atan2(z2, p * (1 - E2)), phiP = 2 * Math.PI;
            while (Math.abs(phi - phiP) > precision) {
                nu = AIRY_A / Math.sqrt(1 - E2 * Math.sin(phi) * Math.sin(phi));
                phiP = phi;
                phi = Math.atan2(z2 + E2 * nu * Math.sin(phi), p);
            }

            double lambda = Math.atan2(y2, x2);

            // -- 4: now we're in OSGB, get the EN coords
            double cosLat = Math.cos(phi), sinLat = Math.sin(phi);
            nu = AIRY_A * F0 / Math.sqrt(1 - E2 * sinLat * sinLat);              
            double rho = AIRY_A * F0 * (1 - E2) / Math.pow(1 - E2 * sinLat * sinLat, 1.5);
            double eta2 = nu / rho - 1;

            double Ma = (1 + N + (5 / 4) * N2 + (5 / 4) * N3) * (phi - LAT0);
            double Mb = (3 * N + 3 * N * N + (21 / 8) * N3) * Math.sin(phi - LAT0) * Math.cos(phi + LAT0);
            double Mc = ((15 / 8) * N2 + (15 / 8) * N3) * Math.sin(2 * (phi - LAT0)) * Math.cos(2 * (phi + LAT0));
            double Md = (35 / 24) * N3 * Math.sin(3 * (phi - LAT0)) * Math.cos(3 * (phi + LAT0));
            double M = AIRY_B * F0 * (Ma - Mb + Mc - Md);              // meridional arc

            double cos3lat = cosLat * cosLat * cosLat;
            double cos5lat = cos3lat * cosLat * cosLat;
            double tan2lat = Math.tan(phi) * Math.tan(phi);
            double tan4lat = tan2lat * tan2lat;

            double I = M + N0;
            double II = (nu / 2) * sinLat * cosLat;
            double III = (nu / 24) * sinLat * cos3lat * (5 - tan2lat + 9 * eta2);
            double IIIA = (nu / 720) * sinLat * cos5lat * (61 - 58 * tan2lat + tan4lat);
            double IV = nu * cosLat;
            double V = (nu / 6) * cos3lat * (nu / rho - tan2lat);
            double VI = (nu / 120) * cos5lat * (5 - 18 * tan2lat + tan4lat + 14 * eta2 - 58 * tan2lat * eta2);

            double dLon = lambda - LON0;
            double dLon2 = dLon * dLon, dLon3 = dLon2 * dLon, dLon4 = dLon3 * dLon, dLon5 = dLon4 * dLon, dLon6 = dLon5 * dLon;

            double OSGB36N = I + II * dLon2 + III * dLon4 + IIIA * dLon6;
            double OSGB36E = E0 + IV * dLon + V * dLon3 + VI * dLon5;

/*--------*/return new double(OSGB36N, OSGB36E);/* error in this line */
        }
            return new CoordImpl(OSGB36N, OSGB36E);
    }
}

【问题讨论】:

  • 你的公开课在哪里?
  • 不清楚你要做什么,“实现以下代码”应该是什么意思,你取得了什么进展,或者出了什么问题。
  • 嗯,这只是一个界面。您需要此接口的实现类。不确定,它对 MATsim 是如何工作的,但通常应该有一个 provider 来实现这个接口
  • 那么这段代码有什么问题或者出错了?
  • 为什么要在另一个方法中间定义一个方法?

标签: java interface abstract-class


【解决方案1】:

您正在尝试使用此表达式创建一个数组:

new double(OSGB36N, OSGB36E)

但是,这是无效的 java 语法。你应该把它改成

new double[]{ OSGB36N, OSGB36E }

您还试图在另一个方法中声明一个方法,即 eastANDnorthtransform 中,这在 java 中无效。

【讨论】:

  • 感谢您的建议 Fabian,我可以解决我的问题。我将双数组更改为: double lat1 = coord.getX();双纬度 = Math.toRadians(lat1);双 lon1 = coord.getY();双 lon = Math.toRadians(lon1);你是最有用的。 - 索玛耶
  • 我还牢记您在详细阐述我的问题时的提示。谢谢你对我的耐心。索玛耶
猜你喜欢
  • 2018-08-25
  • 1970-01-01
  • 2015-08-02
  • 1970-01-01
  • 1970-01-01
  • 2016-06-28
  • 1970-01-01
  • 2022-01-24
  • 2015-07-08
相关资源
最近更新 更多