Quick projection file (.prj) function for R

This is one of those things that is pretty easy to do, but I can never seem to find when I need it. It’s a quick little function in R to create a .prj projection file for ArcGIS, QGIS, etc. I find it useful when I’m batch producing a bunch of ASCIIs in R (like climate variable surfaces, which I often make using the dataframe2asc() command from the {SDMTools} package.

I’ve broken it down line-by-line here so it’s easy to set up for whatever projection you have come to love (even though they’re all wrong anyway). A quick trick is to use the Projection Utility in ESRI ArcGIS to generate an example .prj file and copy the settings into the function.

The example here incorporates a Lambert Conformal Conic projection centred on -95° West Longitude (about the middle of North America). It also happens to be the projection of all the spectacular present and future climate data that we provide for free in the AdaptWest Databasin!

# LCC Projection file function
make.prj <- function(x){
 proj.file <- file(x)
 "Projection LAMBERT",
 "Datum WGS84",
 "Spheroid WGS84",
 "Units METERS",
 "Zunits NO",
 "Xshift 0.0",
 "Yshift 0.0",
 "49 0 0.0 /* 1st standard parallel",
 "77 0 0.0 /* 2nd standard parallel",
 "-95 0 0.0 /* central meridian",
 "0 0 0.0 /* latitude of projection's origin",
 "0.0 /* false easting (meters)",
 "0.0 /* false northing (meters)"
 ), proj.file)

# Create some test data
# In the above LCC projection, XYs are centred on N. America
foo <- data.frame(
    x=seq(-990000,0,10000), KEEP.OUT.ATTRS=T),

# Make the ASCII (projected & unprojected)
library('SDMTools') # For quick data frame -> ascii conversion
dataframe2asc(foo, "projected.asc")
dataframe2asc(foo, "unprojected.asc")

# Make a projection file with the same name

Just as an unnecessary demonstration, I’ll drop the sample data into ArcGIS set up with a lame projection for North America. (Okay, I suppose it’s only lame because us Northern Hemisphere types think of north as up).


Dropped into an Albers Equal Area Conic projection centred on Europe shows that the projected ASCII (blue) drops where it should in North America while the un-projected ASCII (orange) falls on the raw coordinates. Hooray.

This entry was posted in R. Bookmark the permalink.

One Response to Quick projection file (.prj) function for R

  1. Pingback: R code: ClimateXX software output management (ASCII creation & projection files) | David R. Roberts

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s