[R-sig-Geo] Import custom projection in R

Roger Bivand Roger.Bivand at nhh.no
Tue Aug 27 10:53:07 CEST 2013


On Tue, 27 Aug 2013, Pan wrote:

> Dear all,
>
> I'm quite new to spatial data processing in R and I'm getting entangled in projection systems, I would appreciate your support to find a solution. I need to import my custom projection in R (my.proj), create projected spatial objects (I usedSpatialPointsDataFrame {sp}) and export them as
> shapefiles (I usedwritePointsShape {maptools}). As I said, the first step is particularly tricky to me!
>
> I tried to import the projection both asmy.proj.shp andmy.proj.prj 
> usingreadOGR() {rgdal} but the result was just the same: it didn't work!
>
> As you see below I've been trying different solutions to make the 
> function work but I still didn't understand what I'm supposed to assign 
> to dsn and layer:

No, reading (and re-reading) the help pages and their references may help. 
You have not explained why you need to read your "custom" projection. You 
could simply enter it as a PROJ.4 string. You appear to think that you 
should read a shapefile (including its optional *.prj file component) to 
retreive the string. If you don't need to do this, and can write it as a 
string in R, do that - CRS() of your string with rgdal loaded will check 
for obvious errors. If you have a representation that OGR recognises, such 
as a shapefile, you can say for example:

library(rgdal)
dsn <- system.file("vectors", package = "rgdal")[1]
# to access the vector examples provided
OGRSpatialRef(dsn, "cities")

Here, the dsn= argument is the directory containing the shapefile 
components, and cities is a layer; here, you can check that there are 
files ending in "prj", and for files starting with "cities":

list.files(path=dsn, pattern="prj$")
list.files(path=dsn, pattern="^cities")

Do check that the files are where you think they are, the most frequent 
cause of the error message you see being that they are somewhere else. 
Once you are done, use writeOGR(), not writePointShape() which is 
deprecated and which discards your coordinate reference system anyway, so 
defeating the point of your exercise.

Hope this clarifies,

Roger

>
> inputdir<-"C:/my working directory/"
>
> library(rgdal)
>
>  > Test<-readOGR(dsn=paste(inputdir),layer="my.proj")
> Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv) :
>    Cannot open file
>
>  > Test<-readOGR(dsn=paste(inputdir,"my.proj",sep=""),layer="my.proj.shp")
> Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv) :
>    Cannot open file
>
>  > Test<-readOGR(dsn="my.proj",layer="my.proj.shp")
> Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv) :
>    Cannot open file
>
> also the attempts withogrInfo()haven't been successful.
>
> So I have two questions on this matter. How should I handle the issue withreadOGR()? Is there any other way (possibly easy) to import a custom projection in R?
>
> Note: I'm using R 2.15.2 and rgdal 0.8-10.
>
> Thanks in advance
>
> Ivan
>
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list