Error in reading shape file with readOGR()

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Error in reading shape file with readOGR()

Adam Fera
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
[hidden email]

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Error in reading shape file with readOGR()

Roger Bivand
Administrator
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: [hidden email]
http://orcid.org/0000-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Roger Bivand
Department of Economics
Norwegian School of Economics
Helleveien 30
N-5045 Bergen, Norway
Reply | Threaded
Open this post in threaded view
|

Re: Error in reading shape file with readOGR()

Loïc Dutrieux-2


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
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo