3

I am trying to convert a raster object to a polygon object using R but I am running into some problems using my Windows 7 machine.

I originally tried to use rasterToPolygons (package: raster) but it kept crashing my computer so I opted to use the R port of gdal_polygonize with directions found here

However, I cannot get this to work because I receive the error message:

ImportError: No module named site
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv) : 
  Cannot open file
In addition: Warning message:
running command '"python" "C:/OSGeo4W64" "C:\Users\RDCERWAW\AppData\Local\Temp\1\RtmpGgkaqN\file1ddc84b56b5.asc" -f "ESRI Shapefile" "C:\Users\RDCERWAW\AppData\Local\Temp\1\RtmpGgkaqN\file1ddcbde726a.shp"' had status 1 

The problem seems to be with the system2() call. When I try to use it to call Python, I get the error

ImportError: No module named site
Warning message:
running command '"python" print hello' had status 1

(The above is probably incorrect, but I can't find out how to test the call to python using R)

Gdal is installed at C:/OSGeo4W64 and the Path to both gdal_polygonize and Python are in my PATH. I can run gdal_polygonize from the console by typing

gdal_polygonize C:\GIS\temp.tif if 'ESRI Shapefile' C:\GIS\temp.shp

I ran the gdal_polygonizeR() function line by line and found out there were two problems. First, the raster that I was using as input had been created by cropping another raster and so the data source was a temporary file that no longer existed.

Secondly, the call to the directory for python (C:\OSGeo4W64\bin) was not able to import gdal, so I had to install gdal separately from the OSGeo4W installation (not sure why) and then I was able to import gdal from within OSGeo4W.

8
  • What has this got to do with R? I would focus on that, you give no details on the crash, your computer, or the code.
    – mdsumner
    Dec 6, 2014 at 19:13
  • The crash comes when I use rasterToPolygons() from the raster package. There are no messages within R, it is simply that R stops working. It appears to be a memory issue and others have also encountered this problem.
    – user44796
    Dec 7, 2014 at 13:12
  • When I parts of the function, the issue appears to be in the call to Python. The following codeystem2('python', args=(sprintf('"%1$s" "%2$s" -f "%3$s" "%4$s.shp"', pypath, rastpath, gdalformat, outshape)))
    – user44796
    Dec 12, 2014 at 18:19
  • Please provide a reproducible example of this problem, then we can understand what's going on (i.e. provide us with the .tif file you're trying to convert) Dec 14, 2014 at 9:54
  • @user44796 You can edit your question with additional information you provided in the comments as well as code that folks can debug and examine
    – dassouki
    Dec 15, 2014 at 15:11

0

Browse other questions tagged or ask your own question.