.packageName <- "proj4"
.package.name <- "proj4"
# Copyright (c) 2007 by Simon Urbanek
#
# part of proj4 R package, license: GPL v2

project <- function(xy, proj, inverse=FALSE, degrees=TRUE, silent=FALSE, ellps.default="sphere") {
    proj <- .proj2char(proj, ellps.default=ellps.default)
    if (is.list(xy)) {
        if (length(xy)<2) stop("input must be at least 2-dimensional")
        if (length(xy)>2 && !silent) warning("more than two dimensions found, using first two")
        x <- xy[[1]]
        y <- xy[[2]]
    } else {
        d <- dim(xy)
        if (is.null(d) && length(xy)==2) {
            x <- xy[1]
            y <- xy[2]
        } else {
            if (length(d) != 2 || (d[1]!=2 && d[2]!=2))
                stop("input must be 2-dimensional")
            if (d[1]==2) {
                x <- xy[1,]
                y <- xy[2,]
            } else {
                x <- xy[,1]
                y <- xy[,2]
            }
        }
    }
    if (!is.numeric(x)) x <- as.numeric(x)
    if (!is.numeric(y)) y <- as.numeric(y)
    if (length(x) < length(y)) {
        if (!silent) warning("x is shorter than y, recycling")
        x <- rep(x, length.out=length(y))
    }
    if (length(y) < length(x)) {
        if (!silent) warning("y is shorter than x, recycling")
        y <- rep(y, length.out=length(x))
    }
    n <- length(x)
    f <- 0:0
    if (inverse) f <- f + 1:1
    if (degrees) f <- f + 2:2
    res <- .C("project", as.character(proj),                
              as.integer(n), x=as.double(x), y=as.double(y),
              f, NAOK=TRUE, PACKAGE=.package.name)
    if (is.list(xy))
        list(x=res$x, y=res$y)
    else
        cbind(res$x, res$y)
}
# Copyright (c) 2007 Simon Urbanek
#
# proj4 R package, License: GPL v2

# convert projection argument into PROJ.4 arguments string
# accepts either a named vector c(proj='merc',units='m'),
# a vector of parameters c('+proj=merc','+units=m')
# or a single string "+proj= +units=m"
#
# datum.default is added as "+datum=.." if not NA/NULL and +datum
# or +ellps doesn't exist in proj
# the same applies to ellps.default (in that order, i.e. datum has
# a higher precedence)
.proj2char <- function(proj, ellps.default=NA, datum.default=NA) {
    if (length(names(proj))) {
        proj <- paste('+',names(proj),'=',proj,sep='',collapse='\n')
        # remove spaces in all arguments
        proj <- gsub('\n',' ',gsub(' ','',proj))
    } else {
        if (length(proj) > 1)
            proj <- paste(as.character(proj), collapse=' ')
    }
    if (!is.character(proj)) proj <- as.character(proj)
    if (!is.null(datum.default) && !is.na(datum.default) && !length(grep("\\+datum=",proj)) && !length(grep("\\+ellps=",proj)))
        proj <- paste(proj," +datum=",datum.default,sep='')
    if (!is.null(ellps.default) && !is.na(ellps.default) && !length(grep("\\+datum=",proj)) && !length(grep("\\+ellps=",proj)))
        proj <- paste(proj," +ellps=",ellps.default,sep='')
    proj
}
# Copyright (c) 2007 by Simon Urbanek
#
# part of proj4 R package, license: GPL v2

ptransform <- function(data, src.proj, dst.proj, silent=TRUE) {
    x <- 0
    y <- 0
    z <- 0
    src.proj <- .proj2char(src.proj)
    dst.proj <- .proj2char(dst.proj)
    if (is.list(data)) {
        l <- length(data)
        if (l) {
            x <- data[[1]]
            if (l>1) {
                y <- data[[2]]
                if (l>2) {
                    z <- data[[3]]
                    if (l>3 && !silent) warning("more than three dimensions found, using first three")
                }
            }
        }
    } else {
        d <- dim(data)
        if (is.null(d)) {
            x <- data
        } else {
            if (length(d) != 2 || d[2] > 3)
                stop("input must be 2-dimensional with at most three columns")
            if (d[2] > 0) x <- data[,1]
            if (d[2] > 1) y <- data[,2]
            if (d[2] > 2) z <- data[,3]
        }
    }
    if (!is.numeric(x)) x <- as.numeric(x)
    if (!is.numeric(y)) y <- as.numeric(y)
    if (!is.numeric(z)) z <- as.numeric(z)
    n <- max(length(x),length(y),length(z))
    if (length(x) < n) {
        if (!silent) warning("x is shorter than some of the others, recycling")
        x <- rep(x, length.out=n)
    }
    if (length(y) < n) {
        if (!silent) warning("y is shorter than some of the others, recycling")
        y <- rep(y, length.out=n)
    }
    if (length(z) < n) {
        if (!silent) warning("z is shorter than some of the others, recycling")
        z <- rep(z, length.out=n)
    }
    res <- .C("transform",
              as.character(src.proj), as.character(dst.proj),
              as.integer(n),
              x=as.double(x), y=as.double(y), z=as.double(z),
              NAOK=TRUE, PACKAGE=.package.name)

    if (is.list(data))
        list(x=res$x, y=res$y, z=res$z)
    else
        cbind(res$x, res$y, res$z)
}
.First.lib <- function(libname, pkgname)
	library.dynam(.package.name, pkgname, libname)

.onLoad <- .First.lib

