Re: [R-sig-Geo] Error in reading shape file with readOGR()
On 14/02/2018 04:11, Roger Bivand wrote: > On Tue, 13 Feb 2018, Adam Fera wrote: > >> Hello, Amos, >> >> I came across this posting because I had a similar problem using R for >> mapping within GNU Emacs Org Mode on GNU/Linux. I work on a research >> team >> that uses a shared directory for data analysis. This makes it imperative >> to use relative pathnames so that everyone can use the exact same >> code, the >> exact same way, and achieve the exact same result. Unfortunately, the >> readOGR function in the rgdal package does not allow the use of relative >> pathnames in the dsn (data source name) argument. > > The underlying GDAL logic expects an expanded path. Until I can check > that using path.expand() inside the functions at least for reading, > perhaps writing, use it as dsn = > path.expand("~/relative/path/to/file/nypp_15c/"). I'd be grateful for > feedback on whether this works as well as setwd(). > sf uses enc2utf8(normalizePath(dsn)), so you could try normalizePath() > too - path.expand() handles ~ but not ../.. . readGDAL() handles > relative paths OK and uses normalizePath() internally. If you're working with shapefiles you may have a look at raster::shapefile(). It's a thin wrapper around read/writeOGR(), and I think it solves this relative path "issue" by using normalizePath(). But please don't take it as an advice to use shapefiles; it's not, there are much better formats out there ;) Cheers, Loïc > > Roger > >> >> To get around this, I use the setwd and getwd functions. I use setwd to >> set the working directory to the directory with the shapefile. Then, >> I use >> the getwd function as the dsn argument. Since setwd() can handle >> relative >> pathnames and getwd() prints the absolute pathname, this allows the >> user a >> way around the dsn= argument requiring an absolute pathname. This worked >> for my research team and allowed us all to use the same code for >> mapping in >> R. >> >> My R code looks something like this: >> >> ## SET WORKING DIRECTORY TO BE UNIFORM >> setwd("~/relative/path/to/file/nypp_15c/") >> >> ## IMPORT THE SHAPEFILE TO A SPATIAL DATA.FRAME >> library(rgdal) >> precincts.2015 <- readOGR( >> dsn=getwd(), >> layer="nypp") >> >> I would like to thank all the R developers, in particular Professor >> Bivand, >> for their extremely useful contributions in creating software for >> geospatial data analysis. >> >> I hope this helps, >> >> Adam G. Fera >> >> > ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Re: [R-sig-Geo] Error in reading shape file with readOGR()
On Tue, 13 Feb 2018, Adam Fera wrote: Hello, Amos, I came across this posting because I had a similar problem using R for mapping within GNU Emacs Org Mode on GNU/Linux. I work on a research team that uses a shared directory for data analysis. This makes it imperative to use relative pathnames so that everyone can use the exact same code, the exact same way, and achieve the exact same result. Unfortunately, the readOGR function in the rgdal package does not allow the use of relative pathnames in the dsn (data source name) argument. The underlying GDAL logic expects an expanded path. Until I can check that using path.expand() inside the functions at least for reading, perhaps writing, use it as dsn = path.expand("~/relative/path/to/file/nypp_15c/"). I'd be grateful for feedback on whether this works as well as setwd(). sf uses enc2utf8(normalizePath(dsn)), so you could try normalizePath() too - path.expand() handles ~ but not ../.. . readGDAL() handles relative paths OK and uses normalizePath() internally. Roger To get around this, I use the setwd and getwd functions. I use setwd to set the working directory to the directory with the shapefile. Then, I use the getwd function as the dsn argument. Since setwd() can handle relative pathnames and getwd() prints the absolute pathname, this allows the user a way around the dsn= argument requiring an absolute pathname. This worked for my research team and allowed us all to use the same code for mapping in R. My R code looks something like this: ## SET WORKING DIRECTORY TO BE UNIFORM setwd("~/relative/path/to/file/nypp_15c/") ## IMPORT THE SHAPEFILE TO A SPATIAL DATA.FRAME library(rgdal) precincts.2015 <- readOGR( dsn=getwd(), layer="nypp") I would like to thank all the R developers, in particular Professor Bivand, for their extremely useful contributions in creating software for geospatial data analysis. I hope this helps, Adam G. Fera -- Roger Bivand Department of Economics, Norwegian School of Economics, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; e-mail: roger.biv...@nhh.no http://orcid.org/-0003-2392-6140 https://scholar.google.no/citations?user=AWeghB0J&hl=en ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
[R-sig-Geo] Error in reading shape file with readOGR()
Hello, Amos, I came across this posting because I had a similar problem using R for mapping within GNU Emacs Org Mode on GNU/Linux. I work on a research team that uses a shared directory for data analysis. This makes it imperative to use relative pathnames so that everyone can use the exact same code, the exact same way, and achieve the exact same result. Unfortunately, the readOGR function in the rgdal package does not allow the use of relative pathnames in the dsn (data source name) argument. To get around this, I use the setwd and getwd functions. I use setwd to set the working directory to the directory with the shapefile. Then, I use the getwd function as the dsn argument. Since setwd() can handle relative pathnames and getwd() prints the absolute pathname, this allows the user a way around the dsn= argument requiring an absolute pathname. This worked for my research team and allowed us all to use the same code for mapping in R. My R code looks something like this: ## SET WORKING DIRECTORY TO BE UNIFORM setwd("~/relative/path/to/file/nypp_15c/") ## IMPORT THE SHAPEFILE TO A SPATIAL DATA.FRAME library(rgdal) precincts.2015 <- readOGR( dsn=getwd(), layer="nypp") I would like to thank all the R developers, in particular Professor Bivand, for their extremely useful contributions in creating software for geospatial data analysis. I hope this helps, Adam G. Fera -- Adam G. Fera, MPhil Doctoral Student Instructor, NYPD Executive MA Police Leadership Program Research Analyst, Misdemeanor Justice Project PhD Program in Criminal Justice John Jay College of Criminal Justice 524 West 59th Street New York NY 10019 adamfer...@gmail.com [[alternative HTML version deleted]] ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Re: [R-sig-Geo] Error in reading shape file with readOGR()
On Tue, 27 Jan 2015, Amos Thairu wrote: That works! It seems that readOGR() does not expand the ~ and thus requires the full expanded path. The reason for not expanding is that the dsn= argument takes very different string values depending on the driver, but as the driver is discovered in OGR itself, we can't say that we know that it is going to be a file or directory, for example for PostGIS or WFS. Hope this clarifies, Roger Thanks! On 27 January 2015 at 17:13, Frede Aakmann Tøgersen wrote: Hi Amos Some time ago since I have used readOGR() but I vaguely remember that readOGR() does not expand the ~ as e.g. list.files does. You need to give the full expanded path like something like this: "/home/my_username/datasets/mapping" which of course depends on the system setup (I'm guessing you are on a unix-alike system based on the path to gdal installation). Cannot remember if a relative path works. Yours sincerely / Med venlig hilsen Frede Aakmann Tøgersen Specialist, M.Sc., Ph.D. Plant Performance & Modeling Technology & Service Solutions T +45 9730 5135 M +45 2547 6050 fr...@vestas.com http://www.vestas.com Company reg. name: Vestas Wind Systems A/S This e-mail is subject to our e-mail disclaimer statement. Please refer to www.vestas.com/legal/notice If you have received this e-mail in error please contact the sender. > -Original Message- > From: R-sig-Geo [mailto:r-sig-geo-boun...@r-project.org] On Behalf Of > Amos Thairu > Sent: 27. januar 2015 14:50 > To: r-sig-geo@r-project.org > Subject: [R-sig-Geo] Error in reading shape file with readOGR() > > I am trying to import a shapefile using readOGR below: > > dataProjected <- readOGR(dsn="~/datasets/mapping", layer="locations") > > But I am facing the following error: > Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = > use_iconv, : > Cannot open file > > I have successfully loaded the same shapefile using readOGR in my windows > platform but cannot read that same file > using the same command in a linux platform. I guess the problem seems to > be > related to having uploaded the > .shp and its associated files from a Windows machine onto the linux > platform. > > The directory with the shape file is complete with the associated files as > shown below: > > list.files("~/datasets/mapping") > [1] "locations-c.dta" "locations-d.dta" "locations.dbf" > "locations.prj" "locations.sbn" > [6] "locations.sbx" "locations.shp" "locations.shp.xml" > "locations.shx" > > I get the following package startup message: > > library(rgdal) > Loading required package: sp > rgdal: version: 0.9-1, (SVN revision 518) > Geospatial Data Abstraction Library extensions to R successfully loaded > Loaded GDAL runtime: GDAL 1.7.3, released 2010/11/10 > Path to GDAL shared files: /usr/share/gdal > GDAL does not use iconv for recoding strings. > Loaded PROJ.4 runtime: Rel. 4.7.1, 23 September 2009, [PJ_VERSION: 470] > Path to PROJ.4 shared files: (autodetected) > > My session info is as follows: > > sessionInfo() > R version 3.1.2 (2014-10-31) > Platform: x86_64-redhat-linux-gnu (64-bit) > > locale: > [1] C > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > > other attached packages: > [1] rgdal_0.9-1 sp_1.0-17 > > loaded via a namespace (and not attached): > [1] grid_3.1.2 lattice_0.20-29 tools_3.1.2 > > Does anyone know why this is happening? Any assistance would be greatly > appreciated. > > -- > Regards, > Amos Mbugua. > > [[alternative HTML version deleted]] > > ___ > R-sig-Geo mailing list > R-sig-Geo@r-project.org > https://stat.ethz.ch/mailman/listinfo/r-sig-geo -- Regards, Amos Mbugua. [[alternative HTML version deleted]] ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo -- Roger Bivand Department of Economics, Norwegian School of Economics, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 91 00 e-mail: roger.biv...@nhh.no ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Re: [R-sig-Geo] Error in reading shape file with readOGR()
That works! It seems that readOGR() does not expand the ~ and thus requires the full expanded path. Thanks! On 27 January 2015 at 17:13, Frede Aakmann Tøgersen wrote: > Hi Amos > > Some time ago since I have used readOGR() but I vaguely remember that > readOGR() does not expand the ~ as e.g. list.files does. You need to give > the full expanded path like something like this: > > "/home/my_username/datasets/mapping" > > which of course depends on the system setup (I'm guessing you are on a > unix-alike system based on the path to gdal installation). Cannot remember > if a relative path works. > > > Yours sincerely / Med venlig hilsen > > > Frede Aakmann Tøgersen > Specialist, M.Sc., Ph.D. > Plant Performance & Modeling > > Technology & Service Solutions > T +45 9730 5135 > M +45 2547 6050 > fr...@vestas.com > http://www.vestas.com > > Company reg. name: Vestas Wind Systems A/S > This e-mail is subject to our e-mail disclaimer statement. > Please refer to www.vestas.com/legal/notice > If you have received this e-mail in error please contact the sender. > > > > -Original Message- > > From: R-sig-Geo [mailto:r-sig-geo-boun...@r-project.org] On Behalf Of > > Amos Thairu > > Sent: 27. januar 2015 14:50 > > To: r-sig-geo@r-project.org > > Subject: [R-sig-Geo] Error in reading shape file with readOGR() > > > > I am trying to import a shapefile using readOGR below: > > > dataProjected <- readOGR(dsn="~/datasets/mapping", layer="locations") > > > > But I am facing the following error: > > Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, > use_iconv = > > use_iconv, : > > Cannot open file > > > > I have successfully loaded the same shapefile using readOGR in my windows > > platform but cannot read that same file > > using the same command in a linux platform. I guess the problem seems to > > be > > related to having uploaded the > > .shp and its associated files from a Windows machine onto the linux > > platform. > > > > The directory with the shape file is complete with the associated files > as > > shown below: > > > list.files("~/datasets/mapping") > > [1] "locations-c.dta" "locations-d.dta" "locations.dbf" > > "locations.prj" "locations.sbn" > > [6] "locations.sbx" "locations.shp" "locations.shp.xml" > > "locations.shx" > > > > I get the following package startup message: > > > library(rgdal) > > Loading required package: sp > > rgdal: version: 0.9-1, (SVN revision 518) > > Geospatial Data Abstraction Library extensions to R successfully loaded > > Loaded GDAL runtime: GDAL 1.7.3, released 2010/11/10 > > Path to GDAL shared files: /usr/share/gdal > > GDAL does not use iconv for recoding strings. > > Loaded PROJ.4 runtime: Rel. 4.7.1, 23 September 2009, [PJ_VERSION: 470] > > Path to PROJ.4 shared files: (autodetected) > > > > My session info is as follows: > > > sessionInfo() > > R version 3.1.2 (2014-10-31) > > Platform: x86_64-redhat-linux-gnu (64-bit) > > > > locale: > > [1] C > > > > attached base packages: > > [1] stats graphics grDevices utils datasets methods base > > > > other attached packages: > > [1] rgdal_0.9-1 sp_1.0-17 > > > > loaded via a namespace (and not attached): > > [1] grid_3.1.2 lattice_0.20-29 tools_3.1.2 > > > > Does anyone know why this is happening? Any assistance would be greatly > > appreciated. > > > > -- > > Regards, > > Amos Mbugua. > > > > [[alternative HTML version deleted]] > > > > ___ > > R-sig-Geo mailing list > > R-sig-Geo@r-project.org > > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > -- Regards, Amos Mbugua. [[alternative HTML version deleted]] ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Re: [R-sig-Geo] Error in reading shape file with readOGR()
Hi Amos Some time ago since I have used readOGR() but I vaguely remember that readOGR() does not expand the ~ as e.g. list.files does. You need to give the full expanded path like something like this: "/home/my_username/datasets/mapping" which of course depends on the system setup (I'm guessing you are on a unix-alike system based on the path to gdal installation). Cannot remember if a relative path works. Yours sincerely / Med venlig hilsen Frede Aakmann Tøgersen Specialist, M.Sc., Ph.D. Plant Performance & Modeling Technology & Service Solutions T +45 9730 5135 M +45 2547 6050 fr...@vestas.com http://www.vestas.com Company reg. name: Vestas Wind Systems A/S This e-mail is subject to our e-mail disclaimer statement. Please refer to www.vestas.com/legal/notice If you have received this e-mail in error please contact the sender. > -Original Message- > From: R-sig-Geo [mailto:r-sig-geo-boun...@r-project.org] On Behalf Of > Amos Thairu > Sent: 27. januar 2015 14:50 > To: r-sig-geo@r-project.org > Subject: [R-sig-Geo] Error in reading shape file with readOGR() > > I am trying to import a shapefile using readOGR below: > > dataProjected <- readOGR(dsn="~/datasets/mapping", layer="locations") > > But I am facing the following error: > Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = > use_iconv, : > Cannot open file > > I have successfully loaded the same shapefile using readOGR in my windows > platform but cannot read that same file > using the same command in a linux platform. I guess the problem seems to > be > related to having uploaded the > .shp and its associated files from a Windows machine onto the linux > platform. > > The directory with the shape file is complete with the associated files as > shown below: > > list.files("~/datasets/mapping") > [1] "locations-c.dta" "locations-d.dta" "locations.dbf" > "locations.prj" "locations.sbn" > [6] "locations.sbx" "locations.shp" "locations.shp.xml" > "locations.shx" > > I get the following package startup message: > > library(rgdal) > Loading required package: sp > rgdal: version: 0.9-1, (SVN revision 518) > Geospatial Data Abstraction Library extensions to R successfully loaded > Loaded GDAL runtime: GDAL 1.7.3, released 2010/11/10 > Path to GDAL shared files: /usr/share/gdal > GDAL does not use iconv for recoding strings. > Loaded PROJ.4 runtime: Rel. 4.7.1, 23 September 2009, [PJ_VERSION: 470] > Path to PROJ.4 shared files: (autodetected) > > My session info is as follows: > > sessionInfo() > R version 3.1.2 (2014-10-31) > Platform: x86_64-redhat-linux-gnu (64-bit) > > locale: > [1] C > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > > other attached packages: > [1] rgdal_0.9-1 sp_1.0-17 > > loaded via a namespace (and not attached): > [1] grid_3.1.2 lattice_0.20-29 tools_3.1.2 > > Does anyone know why this is happening? Any assistance would be greatly > appreciated. > > -- > Regards, > Amos Mbugua. > > [[alternative HTML version deleted]] > > ___ > R-sig-Geo mailing list > R-sig-Geo@r-project.org > https://stat.ethz.ch/mailman/listinfo/r-sig-geo ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
[R-sig-Geo] Error in reading shape file with readOGR()
I am trying to import a shapefile using readOGR below: > dataProjected <- readOGR(dsn="~/datasets/mapping", layer="locations") But I am facing the following error: Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, : Cannot open file I have successfully loaded the same shapefile using readOGR in my windows platform but cannot read that same file using the same command in a linux platform. I guess the problem seems to be related to having uploaded the .shp and its associated files from a Windows machine onto the linux platform. The directory with the shape file is complete with the associated files as shown below: > list.files("~/datasets/mapping") [1] "locations-c.dta" "locations-d.dta" "locations.dbf" "locations.prj" "locations.sbn" [6] "locations.sbx" "locations.shp" "locations.shp.xml" "locations.shx" I get the following package startup message: > library(rgdal) Loading required package: sp rgdal: version: 0.9-1, (SVN revision 518) Geospatial Data Abstraction Library extensions to R successfully loaded Loaded GDAL runtime: GDAL 1.7.3, released 2010/11/10 Path to GDAL shared files: /usr/share/gdal GDAL does not use iconv for recoding strings. Loaded PROJ.4 runtime: Rel. 4.7.1, 23 September 2009, [PJ_VERSION: 470] Path to PROJ.4 shared files: (autodetected) My session info is as follows: > sessionInfo() R version 3.1.2 (2014-10-31) Platform: x86_64-redhat-linux-gnu (64-bit) locale: [1] C attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] rgdal_0.9-1 sp_1.0-17 loaded via a namespace (and not attached): [1] grid_3.1.2 lattice_0.20-29 tools_3.1.2 Does anyone know why this is happening? Any assistance would be greatly appreciated. -- Regards, Amos Mbugua. [[alternative HTML version deleted]] ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo