[R-sig-Geo] R/ArcGis and R-DCOM?
Roger Bivand
Roger.Bivand at nhh.no
Thu Nov 29 18:43:35 CET 2007
On Thu, 29 Nov 2007, paallen at attglobal.net wrote:
> Hi all,
>
> I am an ArcGis and R user. I normally just share data between the two
> with the old import/export game using csv, pdf, and jpg file. Lately I
> have just started to looked into the R-DCOM project and the r/arcgis
> repository
> (http://perso.univ-lr.fr/csainte/recherche/rarcgis/index.html).
This site was current for ArcGIS 8, but it seems that for ArcGIS 9, the
Python interface using win32com.client to access the geoprocessor is
easier to get running. One can call a short Python script from the R
system() command, or call R from a Python script run from ArcGIS
(typically using the R (D)COM StatConnector, or have a Python script use
both.
Two useful links are:
http://code.env.duke.edu/projects/mget
and
http://courses.washington.edu/geog465/
where only MGET uses R, but the UW course provides a lot of useful
guidance. Unfortunately, a University of Bergen course that we've just
finished is only on an intranet - the general conclusions were that
"things take time", and that the simplest scripts are the ones that work;
beyond that, much of the documentation needs patience because many things
do not work either at all, or as described - however, there are usually
workarounds. Stopping using the ModelBuilder was crucial in most cases -
it was far too difficult to debug - running code step-by-step from the
Python command prompt was the main mode of learning. Styrk Finne (a
participant) found that deleting the geoprocessor seemed to help on lab
machines that were seldom shut down - the GP seems to be started at boot
and accumulates zombie connections until it becomes unstable (in 9.1 and
for our installation - laptop users who shutdown frequently could do more
than users of the regular lab machines).
Most often the exchange is still by a subset of filetypes, but a good deal
is possible. This, combined with the use of drivers for raster and vector
data in the rgdal package, means that a lot can be streamlined.
> I am hoping to use it to streamline the sharing of data between the two
> easier. Specifically I am wanting to use r for some gridding and have
> it save the rasters into native arcgis rasters and automatically paste
> histograms/ecdf graphs onto layouts and plot princomp scores.
>
This imports an ASCII raster as written with writeAsciiGrid() in the
maptools package - there are ways to use the AAIGrid driver in rgdal too -
into Arc 9.1:
# import modules
import sys, os
from win32com.client import Dispatch
script = sys.argv[0]
# initialize ESRI ArcGIS geoprocessor
print "Initialising ArcGIS geoprocessor ..."
GP = Dispatch('esriGeoprocessing.GpDispatch.1')
GP.CheckOutExtension('Spatial')
GP.OverwriteOutput = 1 # setup for overwrite
out_ascii_grid = sys.argv[1]
out_ascii_type = sys.argv[2]
gridPath = sys.argv[3]
in_grid = sys.argv[4]
inRaster = gridPath + "/" + in_grid
try:
GP.ASCIIToRaster_conversion(out_ascii_grid, inRaster, out_ascii_type)
except:
msg = 'GP Error: %s' % GP.GetMessages()
GP.AddError(msg); print msg; raise
del(GP)
(very rough, but apart from error handling, you can see how you might pass
the arguments from system() in R).
To get R (D)COM Python support running, see the samples/scripts directory
of the R(D)COM install - essentially a one-off build in Pythonwin of the
COM hooks for the StatConnector server.
In rgdal, look at gdalDrivers() and ogrDrivers() to see what is available
- the standard Windows binary package has plenty of drivers for reading
things (but convert personal geodatabase files to say shapefiles or
coverages using the GP to read), and many fewer for writing to Arc -
again, use the GP to convert back.
Hope this helps,
Roger
PS. It would be very helpful if others with relevant experience could join
this thread, which can then be linked from the Rgeo website.
> I have tried the raster example on the r/arcgis repository page without luck.
>
> Has anyone else tried this or something similar?
>
> Regards,
>
> Phillip Allen
> Geochemist
>
> Sent via BlackBerry by AT&T
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, 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