[R-sig-Geo] RPyGeo Query -- probably easy

Alexander Brenning brenning at uwaterloo.ca
Fri Dec 18 19:45:38 CET 2009


Hi,

yes, the quotes are indeed tricky... The challenge is to provide the 
correct mix of escaped and un-escaped single and double quotes to Python 
/ ArcGIS as in this Python example:

gp.select_analysis("nfroads.shp", "paved.shp",
     ' "ROAD_CLASS" = \'PAVED\' ')

In order to achieve this, the backslashes in \' have to be escaped 
themselves, otherwise R will interpret them as escape symbols. After 
all, \' ends up being coded as \\\' in R. Also, double double-quotes 
have to be avoided; the quote.args argument of rpygeo.geoprocessor can 
be used to suppress the outermost double quotes, single quotes can be 
used explicitly instead: " ' sqlquery ' "

So here are two solutions:


env = rpygeo.build.env( workspace = "C:/TEMP",
               overwriteoutput = TRUE )

rpygeo.geoprocessor("select_analysis",
     list( "nfroads.shp", "paved.shp",
           " ' \"ROAD_CLASS\" = \\\'PAVED\\\' ' "),
     clean.up = FALSE, quote.args = c(T,T,F),
     env = env)


# alternative:

rpygeo.geoprocessor("select_analysis('nfroads.shp','paved.shp','\"ROAD_CLASS\"=\\\'PAVED\\\'')",
      env = env, clean.up = FALSE)


I hope this helps...

Cheers
  Alex



Maarten van Strien wrote:
> Andrew, thanks for your reply!
> I tried exactly your command, but still the resulting rpygeo.py file
> contains the following line:
> 
> gp.select_analysis( "nfroads.shp", "paved.shp", "\"ROAD_CLASS\" = 'PAVED' "
> )
> 
> So the \"ROAD_CLASS\" = 'PAVED' is still surrounded by double quotes ("). I
> have tried many different Python commands, but the only one working for me
> is:
> 
> gp.select_analysis( "nfroads.shp", "paved.shp", ' "ROAD_CLASS" = \'PAVED\' '
> )
> 
> Somehow I need to instruct RPyGeo to leave away the double quotes and
> replace them by single quotes.
> 
> Kind regards, Maarten
> 
> 
> 
> Maarten
>  
> I'm not sure about the single quotes but possibly you could add an extra
> special character break (the \ ) to indicate to python that the double
> quotes surrounding your field name are not the end of a string.  You could
> try the following code:
>  
> rpygeo.geoprocessor("select_analysis", c("nfroads.shp", "paved.shp", '
> \\"ROAD_CLASS\\" = \'PAVED\' '), py.file= "rpygeo.py", working.directory =
>  wrkspc, env = myenv, clean.up = FALSE)
> 
>  
> Best Regards
>  
> Andrew
>  
> Dr Andrew Crowe
>  
> Lancaster Environment Centre
> Lancaster University
> Lancaster    LA1 4YQ
> UK

-- 
Alexander Brenning
brenning at uwaterloo.ca - T +1-519-888-4567 ext 35783
Department of Geography and Environmental Management
University of Waterloo
200 University Ave. W - Waterloo, ON - Canada N2L 3G1
http://www.fes.uwaterloo.ca/geography/faculty/brenning/



More information about the R-sig-Geo mailing list