R/geo_convert.R

# Geographic Convert
geo_convert = function(data, lon, lat, geo.in = 31980, geo.out = 4326)
{	
	f = paste("~", substitute(lon), "+", substitute(lat))	
	f = stats::as.formula(f)	
	## convert to sp object
	sp::coordinates(data) = f
	
	## 
	if(is.numeric(geo.in) & is.numeric(geo.out)) 
	{
		## adding a coordinate reference system <CRS>: EPSG
		sp::proj4string(data) = sp::CRS(paste0("+init=epsg:", geo.in))
		## 
		out = sp::spTransform(data, sp::CRS(paste0("+init=epsg:", geo.out)))
		
	} else if (!is.numeric(geo.in) & !is.numeric(geo.out)) {
		
		## adding a coordinate reference system <CRS>: PROJ4
		sp::proj4string(data) = sp::CRS(geo.in)
		
		out = sp::spTransform(data, sp::CRS(geo.out))
		
	} else {
		stop("geo.in/geo.out devem ser do mesmo tipo\n EPSG ou Proj4", call. = FALSE)
	}#end if
	
	return(out)
	
}#end function convertUTMtoWGS84
salah31416/toolbox documentation built on June 3, 2019, 6:59 p.m.