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

Roger Bivand Roger.Bivand at nhh.no
Mon Apr 13 22:01:06 CEST 2009


On Mon, 13 Apr 2009, Zev Ross wrote:

> 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:

Is it possible that shQuote() might help here - maybe the multiple layers 
are a problem, but quite often this function is good at guessing?

Roger

>
> 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
>>> 
>> 
>
>

-- 
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