2

I have been using ArcMap to access GIS data on a spatial data server. I want to figure out how to do the same within R.

I know how to read shapefiles into R. I have successfully used maptools and rgdal to open and map locally stored shapefiles (e.g. http://www.nceas.ucsb.edu/scicomp/usecases/ReadWriteESRIShapeFiles)

My problem is when the data is not stored locally, but rather it is on an Application Server. I believe it's an Oracle database. I've been given information about the 1. Server 2. Instance (a number) 3. Database 4. User and 5. Password. Normally, I would include an example, but it's doubtful that an external user could access the servers.

For example here's how to read and plot local files in R

    library(rgdal)
    ogrInfo(".", "nw-rivers")
    centroids.rg <- readOGR(".", "nw-centroids") 
    plot(centroids.rg)

The "." points to the local directory. How would I change this to access data on a server? The actual syntax of code would be helpful.

climatron
  • 23
  • 3

1 Answers1

2

You can read data from Oracle Spatial DBs using GDAL/OGR:

http://www.gdal.org/ogr/drv_oci.html

if you have the driver in your GDAL/OGR installation. If:

require(rgdal)
ogrDrivers()

shows the Oracle driver then you can use readOGR with all the parameters in the right place.

At a guess, and by analogy with the PostGIS example, I'd say try:

dsn="OCI:userid/password@database_instance:")
ogrListLayers(dsn)
s = readOGR(dsn, layername)

but I don't have an Oracle server to test it on (if I did I'd ditch it tomorrow for PostGIS, and spend the license saving on a yacht) and you don't sound certain its an Oracle server anyway. The general principle for connecting to any spatial database is the same - check you have an OGR driver, figure out what the dsn parameter looks like, try it.

Another way is to go via ODBC, or another non-spatial R database connection. However you'll likely get back the spatial data in WKB or WKT form and have to convert to SpatialWhatevers (point, lines, polygons?).

PostGIS example is here:

https://gis.stackexchange.com/questions/64950/which-is-the-best-way-of-working-with-postgis-data-in-r

Community
  • 1
  • 1
Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • Thanks for the quick reply... if I had information like Server – xxx-yyy Instance – 1234 Database – abcde User – mylogin Pword - mypassword what would be the syntax of dsn? – climatron Apr 24 '14 at 02:21
  • You can convert WKB to Spatial objects using the `readWKB` function in the package [wkb](http://cran.r-project.org/web/packages/wkb/index.html) and you can convert WKT to Spatial objects with the `readWKT` function in the package [rgeos](http://cran.r-project.org/web/packages/rgeos/index.html). – ianmcook Mar 02 '15 at 15:47