[Proj4j] Albers again

Fred Pospeschil f.w.pospeschil.t.r at charter.net
Thu Jun 10 20:59:33 EDT 2010


Martin,

Now I need some help with the inverse projection.

After many days messing with the Albers I have found where the problems are but they do not make any sense.  In the projection for the ellipsoids, the following line caused the returned longitude to always be either +/-90 which is what it is supposed to do.  My problem is: why is it there at all?

// line 1 lpphi = lpphi < 0. ? -ProjectionMath.HALFPI : ProjectionMath.HALFPI;

When I commented it out the elliptical code gave me very good results over all 64K tests.

Just to keep things interesting, when working with the sphere, the following line of code returned a NaN for all latitudes between -89 and 16 degrees.  From 17 thru 89 degrees it returned values from 1.4964... through 0.2351....

       lpphi = Math.asin(lpphi); // line 2 returns NaN

When I replaced that line with the code used for the ellipsoids:

    lpphi = (c - lpphi * lpphi) / n;
    lpphi = phi1_(lpphi, e, one_es));

the code for the sphere returned very accurate results.

Although I am getting good results, this does not seem to be the way to do it.  This just not make any sense.  Over to you for resolution.

Fred

/*
Copyright 2006 Jerry Huxtable

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/*
 * This file was semi-automatically converted from the public-domain USGS PROJ source.
 */
package org.osgeo.proj4j.proj;

import java.awt.geom.*;

import org.osgeo.proj4j.ProjectionMath;
import org.osgeo.proj4j.Projection;
import org.osgeo.proj4j.ProjectionException;

public class AlbersProjection extends Projection {

 private final static double EPS10 = 1.e-10;
 private final static double TOL7 = 1.e-7;
 private double ec;
 private double n;
 private double c;
 private double dd;
 private double n2;
 private double rho0;
 private double phi1;
 private double phi2;
 private double[] en;

 private final static int N_ITER = 15;
 private final static double EPSILON = 1.0e-7;
 private final static double TOL = 1.0e-10;

 //protected double projectionLatitude1 = MapMath.degToRad(45.5);
 //protected double projectionLatitude2 = MapMath.degToRad(29.5);

 public AlbersProjection() {
  minLatitude = Math.toRadians(0);
  maxLatitude = Math.toRadians(80);
  projectionLatitude1 = ProjectionMath.degToRad(45.5);
  projectionLatitude2 = ProjectionMath.degToRad(29.5);
  initialize();
 }

 private static double phi1_(double qs, double Te, double Tone_es) {
  int i;
  double Phi, sinpi, cospi, con, com, dphi;

  Phi = Math.asin (.5 * qs);
  if (Te < EPSILON)
   return( Phi );
  i = N_ITER;
  do {
   sinpi = Math.sin (Phi);
   cospi = Math.cos (Phi);
   con = Te * sinpi;
   com = 1. - con * con;
   dphi = .5 * com * com / cospi * (qs / Tone_es -
      sinpi / com + .5 / Te * Math.log ((1. - con) /
      (1. + con)));
   Phi += dphi;
  } while (Math.abs(dphi) > TOL && --i != 0);
  return( i != 0 ? Phi : Double.MAX_VALUE );
 }

 public Point2D.Double project(double lplam, double lpphi, Point2D.Double out) {
  double rho;
  if ((rho = c - (!spherical ? n * ProjectionMath.qsfn(Math.sin(lpphi), e, one_es) : n2 * Math.sin(lpphi))) < 0.)
   throw new ProjectionException("F");
  rho = dd * Math.sqrt(rho);
  out.x = rho * Math.sin( lplam *= n );
  out.y = rho0 - rho * Math.cos(lplam);
  return out;
 }

 public Point2D.Double projectInverse(double xyx, double xyy, Point2D.Double out)
    {
  double rho;
  if ((rho = ProjectionMath.distance(xyx, xyy = rho0 - xyy)) != 0)
      {
   double lpphi, lplam;
   if (n < 0.)
        {
    rho = -rho;
    xyx = -xyx;
    xyy = -xyy;
     }
   lpphi =  rho / dd;
   if (!spherical)
        {
    lpphi = (c - lpphi * lpphi) / n;
    if (Math.abs(ec - Math.abs(lpphi)) > TOL7)
          {
     if ((lpphi = phi1_(lpphi, e, one_es)) == Double.MAX_VALUE)
      throw new ProjectionException("I");
      }
        else
     lpphi = lpphi < 0. ? -ProjectionMath.HALFPI : ProjectionMath.HALFPI;
     } // end if (!spherical)
      else if (Math.abs(out.y = (c - lpphi * lpphi) / n2) <= 1.)
       lpphi = Math.asin(lpphi); // line 2 returns NaN
   else
// line 1 lpphi = lpphi < 0. ? -ProjectionMath.HALFPI : ProjectionMath.HALFPI;
     lplam = Math.atan2(xyx, xyy) / n;
     out.x = lplam;
     out.y = lpphi;
    } // end if ((rho = ProjectionMath
    else
      {
   out.x = 0.;
   out.y = n > 0. ? ProjectionMath.HALFPI : - ProjectionMath.HALFPI;
    }
  return out;
   } // end projectInverse

 public void initialize() {
  super.initialize();
  double cosphi, sinphi;
  boolean secant;

  phi1 = projectionLatitude1;
  phi2 = projectionLatitude2;

  if (Math.abs(phi1 + phi2) < EPS10)
   throw new IllegalArgumentException("-21");
  n = sinphi = Math.sin(phi1);
  cosphi = Math.cos(phi1);
  secant = Math.abs(phi1 - phi2) >= EPS10;
  //spherical = es > 0.0;
  if (!spherical) {
   double ml1, m1;

   if ((en = ProjectionMath.enfn(es)) == null)
    throw new IllegalArgumentException("0");
   m1 = ProjectionMath.msfn(sinphi, cosphi, es);
   ml1 = ProjectionMath.qsfn(sinphi, e, one_es);
   if (secant) { /* secant cone */
    double ml2, m2;

    sinphi = Math.sin(phi2);
    cosphi = Math.cos(phi2);
    m2 = ProjectionMath.msfn(sinphi, cosphi, es);
    ml2 = ProjectionMath.qsfn(sinphi, e, one_es);
    n = (m1 * m1 - m2 * m2) / (ml2 - ml1);
   }
   ec = 1. - .5 * one_es * Math.log((1. - e) /
    (1. + e)) / e;
   c = m1 * m1 + n * ml1;
   dd = 1. / n;
   rho0 = dd * Math.sqrt(c - n * ProjectionMath.qsfn(Math.sin(projectionLatitude),
    e, one_es));
  } else {
   if (secant) n = .5 * (n + Math.sin(phi2));
   n2 = n + n;
   c = cosphi * cosphi + n2 * sinphi;
   dd = 1. / n;
   rho0 = dd * Math.sqrt(c - n2 * Math.sin(projectionLatitude));
  }
 }

 /**
  * Returns true if this projection is equal area
  */
 public boolean isEqualArea() {
  return true;
 }

 public boolean hasInverse() {
  return true;
 }

 /**
  * Returns the ESPG code for this projection, or 0 if unknown.
  */
 public int getEPSGCode() {
  return 9822;
 }

 public String toString() {
  return "Albers Equal Area";
 }

/*
 public String toString() {
  return "Lambert Equal Area Conic";
 }
*/
}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/proj4j/attachments/20100610/9a8a351a/attachment.html


More information about the Proj4j mailing list