All Downloads are FREE. Search and download functionalities are using the official Maven repository.

geotrellis.proj4.util.UTM.scala Maven / Gradle / Ivy

Go to download

GeoTrellis is an open source geographic data processing engine for high performance applications.

There is a newer version: 0.10.3
Show newest version
package geotrellis.proj4.util

import geotrellis.proj4._

// Ported from javascript https://github.com/chrisveness/geodesy by Chris Veness (MIT license)
object UTM {
  def inValidZone(lat: Double): Boolean =
    (-80 <= lat && lat <= 84)

  /** Gets a UTM zone based on a LatLn coordinate.
    * @return Left(zone) if the zone is North and Right(zone) if the zone is right
    */
  def getZone(lon: Double, lat: Double): Either[Int, Int] = {
    if (!inValidZone(lat))
      throw new IllegalArgumentException("Outside UTM limits")

    var zone = (math.floor((lon + 180) / 6) + 1).toInt

    // handle Norway/Svalbard exceptions
    // grid zones are 8° tall; 0°N is offset 10 into latitude bands array
    val mgrsLatBands = "CDEFGHJKLMNPQRSTUVWXX" // X is repeated for 80-84°N
    val latBand = mgrsLatBands.charAt(math.floor(lat / 8 + 10).toInt)
    // adjust zone & central meridian for Norway
    if (zone==31 && latBand=='V' && lon >= 3) { zone += 1 }
    // adjust zone & central meridian for Svalbard
    if (zone==32 && latBand=='X' && lon <  9) { zone -= 1 }
    if (zone==32 && latBand=='X' && lon >= 9) { zone += 1 }
    if (zone==34 && latBand=='X' && lon < 21) { zone -= 1 }
    if (zone==34 && latBand=='X' && lon >=21) { zone += 1 }
    if (zone==36 && latBand=='X' && lon < 33) { zone -= 1 }
    if (zone==36 && latBand=='X' && lon >=33) { zone += 1 }

    if(lat >= 0) Left(zone) else Right(zone)
  }

  /** Retrieves a CRS for the UTM zone that contains the LatLng point */
  def getZoneCrs(lon: Double, lat: Double): CRS =
    getZone(lon, lat) match {
      case Left(zone) =>
        CRS.fromName(f"EPSG:326$zone%02d")
      case Right(zone) =>
        CRS.fromName(f"EPSG:327$zone%02d")
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy