Exemplo n.º 1
0
        /**
         * The function Convert_UTM_To_Geodetic converts UTM projection (zone, hemisphere, easting and northing) coordinates
         * to geodetic(latitude and  longitude) coordinates, according to the current ellipsoid parameters.  If any errors
         * occur, the error code(s) are returned by the function, otherwise UTM_NO_ERROR is returned.
         *
         * @param Zone       UTM zone.
         * @param Hemisphere The coordinate hemisphere, either {@link SharpEarth.avlist.AVKey#NORTH} or {@link
         *                   SharpEarth.avlist.AVKey#SOUTH}.
         * @param Easting    Easting (X) in meters.
         * @param Northing   Northing (Y) in meters.
         *
         * @return error code.
         */
        public long convertUTMToGeodetic(long Zone, String Hemisphere, double Easting, double Northing)
        {
            // TODO: arg checking
            long   Error_Code      = UTM_NO_ERROR;
            double Origin_Latitude = 0;
            double False_Easting   = 500000;
            double False_Northing  = 0;
            double Scale           = 0.9996;

            if ((Zone < 1) || (Zone > 60))
            {
                Error_Code |= UTM_ZONE_ERROR;
            }
            if (!Hemisphere.Equals(AVKey.SOUTH) && !Hemisphere.Equals(AVKey.NORTH))
            {
                Error_Code |= UTM_HEMISPHERE_ERROR;
            }
//        if ((Easting < MIN_EASTING) || (Easting > MAX_EASTING))    //removed check to enable reprojecting images
//            Error_Code |= UTM_EASTING_ERROR;                       //that extend into another zone
            if ((Northing < MIN_NORTHING) || (Northing > MAX_NORTHING))
            {
                Error_Code |= UTM_NORTHING_ERROR;
            }

            if (Error_Code == UTM_NO_ERROR)
            { /* no errors */
                if (Zone >= 31)
                {
                    Central_Meridian = ((6 * Zone - 183) * PI / 180.0 /*+ 0.00000005*/);
                }
                else
                {
                    Central_Meridian = ((6 * Zone + 177) * PI / 180.0 /*+ 0.00000005*/);
                }
                if (Hemisphere.Equals(AVKey.SOUTH))
                {
                    False_Northing = 10000000;
                }
                try
                {
                    TMCoord TM = TMCoord.fromTM(Easting, Northing,
                                                this.globe, Angle.fromRadians(Origin_Latitude), Angle.fromRadians(Central_Meridian),
                                                False_Easting, False_Northing, Scale);
                    Latitude  = TM.getLatitude().radians;
                    Longitude = TM.getLongitude().radians;

                    if ((Latitude < MIN_LAT) || (Latitude > MAX_LAT))
                    { /* Latitude out of range */
                        Error_Code |= UTM_NORTHING_ERROR;
                    }
                }
                catch (Exception e)
                {
                    Error_Code = UTM_TM_ERROR;
                }
            }
            return(Error_Code);
        }
Exemplo n.º 2
0
        /**
         * The function Convert_Geodetic_To_UTM converts geodetic (latitude and longitude) coordinates to UTM projection
         * (zone, hemisphere, easting and northing) coordinates according to the current ellipsoid and UTM zone override
         * parameters.  If any errors occur, the error code(s) are returned by the function, otherwise UTM_NO_ERROR is
         * returned.
         *
         * @param Latitude  Latitude in radians
         * @param Longitude Longitude in radians
         *
         * @return error code
         */
        public long convertGeodeticToUTM(double Latitude, double Longitude)
        {
            long   Lat_Degrees;
            long   Long_Degrees;
            long   temp_zone;
            long   Error_Code      = UTM_NO_ERROR;
            double Origin_Latitude = 0;
            double False_Easting   = 500000;
            double False_Northing  = 0;
            double Scale           = 0.9996;

            if ((Latitude < MIN_LAT) || (Latitude > MAX_LAT))
            { /* Latitude out of range */
                Error_Code |= UTM_LAT_ERROR;
            }
            if ((Longitude < -PI) || (Longitude > (2 * PI)))
            { /* Longitude out of range */
                Error_Code |= UTM_LON_ERROR;
            }
            if (Error_Code == UTM_NO_ERROR)
            { /* no errors */
                if (Longitude < 0)
                {
                    Longitude += (2 * PI) + 1.0e-10;
                }
                Lat_Degrees  = (long)(Latitude * 180.0 / PI);
                Long_Degrees = (long)(Longitude * 180.0 / PI);

                if (Longitude < PI)
                {
                    temp_zone = (long)(31 + ((Longitude * 180.0 / PI) / 6.0));
                }
                else
                {
                    temp_zone = (long)(((Longitude * 180.0 / PI) / 6.0) - 29);
                }
                if (temp_zone > 60)
                {
                    temp_zone = 1;
                }
                /* UTM special cases */
                if ((Lat_Degrees > 55) && (Lat_Degrees < 64) && (Long_Degrees > -1) && (Long_Degrees < 3))
                {
                    temp_zone = 31;
                }
                if ((Lat_Degrees > 55) && (Lat_Degrees < 64) && (Long_Degrees > 2) && (Long_Degrees < 12))
                {
                    temp_zone = 32;
                }
                if ((Lat_Degrees > 71) && (Long_Degrees > -1) && (Long_Degrees < 9))
                {
                    temp_zone = 31;
                }
                if ((Lat_Degrees > 71) && (Long_Degrees > 8) && (Long_Degrees < 21))
                {
                    temp_zone = 33;
                }
                if ((Lat_Degrees > 71) && (Long_Degrees > 20) && (Long_Degrees < 33))
                {
                    temp_zone = 35;
                }
                if ((Lat_Degrees > 71) && (Long_Degrees > 32) && (Long_Degrees < 42))
                {
                    temp_zone = 37;
                }

                if (UTM_Override != 0)
                {
                    if ((temp_zone == 1) && (UTM_Override == 60))
                    {
                        temp_zone = UTM_Override;
                    }
                    else if ((temp_zone == 60) && (UTM_Override == 1))
                    {
                        temp_zone = UTM_Override;
                    }
                    else if (((temp_zone - 1) <= UTM_Override) && (UTM_Override <= (temp_zone + 1)))
                    {
                        temp_zone = UTM_Override;
                    }
                    else
                    {
                        Error_Code = UTM_ZONE_OVERRIDE_ERROR;
                    }
                }
                if (Error_Code == UTM_NO_ERROR)
                {
                    if (temp_zone >= 31)
                    {
                        Central_Meridian = (6 * temp_zone - 183) * PI / 180.0;
                    }
                    else
                    {
                        Central_Meridian = (6 * temp_zone + 177) * PI / 180.0;
                    }
                    Zone = (int)temp_zone;
                    if (Latitude < 0)
                    {
                        False_Northing = 10000000;
                        Hemisphere     = AVKey.SOUTH;
                    }
                    else
                    {
                        Hemisphere = AVKey.NORTH;
                    }

                    try
                    {
                        TMCoord TM = TMCoord.fromLatLon(Angle.fromRadians(Latitude), Angle.fromRadians(Longitude),
                                                        this.globe, this.UTM_a, this.UTM_f, Angle.fromRadians(Origin_Latitude),
                                                        Angle.fromRadians(Central_Meridian), False_Easting, False_Northing, Scale);
                        Easting  = TM.getEasting();
                        Northing = TM.getNorthing();

                        if ((Easting < MIN_EASTING) || (Easting > MAX_EASTING))
                        {
                            Error_Code = UTM_EASTING_ERROR;
                        }
                        if ((Northing < MIN_NORTHING) || (Northing > MAX_NORTHING))
                        {
                            Error_Code |= UTM_NORTHING_ERROR;
                        }
                    }
                    catch (Exception e)
                    {
                        Error_Code = UTM_TM_ERROR;
                    }
                }
            }
            return(Error_Code);
        }