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

Zev Ross zev at zevross.com
Mon Apr 13 21:44:08 CEST 2009


Hi Alex,

Thank you so much for the suggestion and apologies for taking so long to 
test it. I think the quotes are still a problem. I do indeed write all 
my code in R-WinEdt but must be doing something wrong. In Python your 
suggested escaped quotes works fine but perhaps I missed something in 
RPyGeo:

This code works in Python:

gp.makefeaturelayer("abc.shp", "walk")
gp.SelectLayerByAttribute('walk','NEW_SELECTION','\"hrwk05\" <> 0')

This code works in R:

rpygeo.geoprocessor(
"makefeaturelayer('abc.shp', 'walk')",
"SelectLayerByAttribute('walk','NEW_SELECTION','')",
env=myenv)

This code gives me an error in R:

rpygeo.geoprocessor(
"makefeaturelayer('abc.shp','walk')",
"SelectLayerByAttribute('walk','NEW_SELECTION','\"hrwk05\" <> 0')",
 env=myenv)

File "x:\projects\rpygeo.py", line 10
gp.makefeaturelayer('abc.shp','walk')( 
"SelectLayerByAttribute('walk','NEW_SELECTION','"hrwk05" <> 0')" )
                                                                                                 
^
SyntaxError: invalid syntax
NULL

(note from Zev -- the carrot occurs under the "05")


Alexander Brenning wrote:
> Hi Zev,
>
> the problems are definitely your quotes. First, they are syntactically 
> incorrect - I count seven double quotes (I recommend using some editor 
> with syntax highlighting, like Tinn-R or R-WinEdt, this helps discover 
> mismatches). Second, escaping them (\") should allow you to put double 
> quotes inside a double-quoted character string.
>
> This might be closer to what you want to do (but I haven't tried to 
> run it - sorry, busy end of term...):
>
> rpygeo.geoprocessor("makefeaturelayer('X:\\abc.shp','walk')",
>     "SelectLayerByAttribute('walk','NEW_SELECTION','\"hrwk05\"<>0')",
>     env=myenv)
>
> Note that an additional argument clean.up=FALSE to the 
> rpygeo.geoprocessor allows you to inspect the Python file to check 
> whether the quotes are syntactically correct in Python.
>
> I hope this helps
>  Alex
>
>
>
> Zev Ross wrote:
>> Hi All,
>>
>> I'm wondering if anybody could give me a little guidance on 
>> formatting a query using RPyGeo. I'm excited to be able to run Python 
>> geoprocessing functions straight from R but am having trouble with 
>> the quote, double quote kinds of issues.
>>
>> Here's an example of what I'd like to run
>>
>> rpygeo.geoprocessor("makefeaturelayer('X:\\abc.shp',
>> 'walk')", "SelectLayerByAttribute('walk','NEW_SELECTION', ' "hrwk05" 
>> '<>0")",env=myenv)
>>
>> Where the sticking point is hrwk05 <> 0. In Python my query would be 
>> double quote, field name, close double quote, comparison, number e.g, 
>> "hrwk05" <> 0 but I'm not sure how to put this into RPyGeo
>>
>> If I run like so:
>>
>> rpygeo.geoprocessor("makefeaturelayer('X:\\abc.shp',
>> 'walk')", "SelectLayerByAttribute('walk','NEW_SELECTION', 
>> '')",env=myenv)
>>
>> it runs.
>>
>> Thank you in advance for assistance.
>>
>> Zev
>>
>

-- 
Zev Ross
ZevRoss Spatial Analysis
120 N Aurora, Suite 3A
Ithaca, NY 14850
607-277-0004 (phone)
866-877-3690 (fax, toll-free)
zev at zevross.com



More information about the R-sig-Geo mailing list