[GRASS-user] problems importing vector and raster data via python script

I'm running a python script under windows/eclipse, where I try to import a shape and a raster file with v.in.ogr and r.in.gdal.

grass.run_command("r.in.gdal",'-o', input='E:\pythoninput\mnyzeron5m.asc', output='dem5m', '-o')

grass.run_command("v.in.ogr", '-o', dsn='E:\pythoninput\Mercier_ditch.shp', output='ditch2', '-o')

The problem is that I can't use the '-o' flag to override the projection check. If I put the '-o' at the end, I get the following error message:

grass.run_command("r.in.gdal", input='E:\pythoninput\mnyzeron5m.asc', output='dem5m', '-o')
SyntaxError: non-keyword arg after keyword arg

If I put the '-o' before the input it is ignored, and I get the error that the projection does not match the current location.

grass.run_command("r.in.gdal", '-o', input='E:\pythoninput\mnyzeron5m.asc', output='dem5m')

ERREUR :Projection of dataset does not appear to match current location.

        Location PROJ_INFO is:
        name: Lambert Conformal Conic
        proj: lcc
        ellps: clark80IGN
        lat_1: 46.8
        lat_0: 46.8
        lon_0: 0
        k_0: 0.99987742
        x_0: 600000
        y_0: 2200000
        towgs84: -168,-60,320,0,0,0,0
        pm: paris
        no_defs: defined

        Import dataset PROJ_INFO is:
        cellhd.proj = 0 (unreferenced/unknown)

        You can use the -o flag to r.in.gdal to override this check and use
        the location definition for the dataset.
        Consider generating a new location from the input dataset using the
        'location' parameter.

Does anybody know how I can actually override the projection check?

Sonja

Sonja Jankowfsky wrote:

I'm running a python script under windows/eclipse, where I try to import
a shape and a raster file with v.in.ogr and r.in.gdal.

grass.run_command("r.in.gdal",'-o',
input='E:\pythoninput\mnyzeron5m.asc', output='dem5m', '-o')

Python uses backslash as an escape character; if you need to use a
backslash in a string literal, either use two backslashes, i.e.:

  input='E:\\pythoninput\\mnyzeron5m.asc'

or use a raw literal, i.e.:

  input=r'E:\pythoninput\mnyzeron5m.asc'

Alternatively, a forward slash will work. But scripts should rarely
need to have literal pathnames embedded within them.

grass.run_command("v.in.ogr", '-o',
dsn='E:\pythoninput\Mercier_ditch.shp', output='ditch2', '-o')

run_command() etc use the "flags" argument to specify flags (without
the leading '-'), e.g.:

grass.run_command("v.in.ogr", flags='o', dsn=r'E:\pythoninput\Mercier_ditch.shp', output='ditch2')

--
Glynn Clements <glynn@gclements.plus.com>

Many thanks.

So, the forward slash does not work in windows. (and I know I should not use literal pathnames, but it's just for testing). So either two forward or backward slashs, or one backward.

One other question concerning the flags: How can I put two flags at the same time, as for example -o --o for overwriting the projection check and an already existing file?

I tried everything: flags='o', flags='-o'

                flags='o -o' or flags='-o o'

                flags='o' '-o'

but the interpreter isn't happy with this solutions.

Is there any way to put several flags?

Sonja

Glynn Clements a écrit :

Sonja Jankowfsky wrote:

I'm running a python script under windows/eclipse, where I try to import a shape and a raster file with v.in.ogr and r.in.gdal.

grass.run_command("r.in.gdal",'-o', input='E:\pythoninput\mnyzeron5m.asc', output='dem5m', '-o')
    
Python uses backslash as an escape character; if you need to use a
backslash in a string literal, either use two backslashes, i.e.:

  input='E:\\pythoninput\\mnyzeron5m.asc'

or use a raw literal, i.e.:

  input=r'E:\pythoninput\mnyzeron5m.asc'

Alternatively, a forward slash will work. But scripts should rarely
need to have literal pathnames embedded within them.

grass.run_command("v.in.ogr", '-o', dsn='E:\pythoninput\Mercier_ditch.shp', output='ditch2', '-o')
    
run_command() etc use the "flags" argument to specify flags (without
the leading '-'), e.g.:

grass.run_command("v.in.ogr", flags='o', dsn=r'E:\pythoninput\Mercier_ditch.shp', output='ditch2')

--
Sonja Jankowfsky
UR Hydrologie-Hydraulique
Cemagref de Lyon
3 bis quai Chauveau CP 220
69336 Lyon Cedex 09
FRANCE
Tel : (+33)4 72 20 86 11 Fax : (+33)4 78 47 78 75

Many thanks also for the link, it works!!

Sonja

Christian Kaiser a écrit :

Sonja,

I think you can use something like this:
grass.run_command("v.in.ogr", flags='o', dsn=r'E:\pythoninput\Mercier_ditch.shp', output='ditch2', overwrite=True)

See the make_command function for the options:
<http://download.osgeo.org/grass/grass6_progman/namespacepython_1_1core.html#a6745e7335b5bfcc30d89443fc912c63b&gt;

For the flags not starting with -- but only with - (short flags), I think you can just concatenate them (flags='oc' for example). For the long flags (overwrite, verbose, quiet), they are a separate argument in the run_command function.

Hope this helps,
Christian

On 28 janv. 2010, at 10:38, Sonja Jankowfsky wrote:

Many thanks.

So, the forward slash does not work in windows. (and I know I should not use literal pathnames, but it's just for testing). So either two forward or backward slashs, or one backward.

One other question concerning the flags: How can I put two flags at the same time, as for example -o --o for overwriting the projection check and an already existing file?

I tried everything: flags='o', flags='-o'

              flags='o -o' or flags='-o o'

              flags='o' '-o'

but the interpreter isn't happy with this solutions.

Is there any way to put several flags?

Sonja

Glynn Clements a écrit :

Sonja Jankowfsky wrote:

I'm running a python script under windows/eclipse, where I try to import a shape and a raster file with v.in.ogr and r.in.gdal.

grass.run_command("r.in.gdal",'-o', input='E:\pythoninput\mnyzeron5m.asc', output='dem5m', '-o')
   

Python uses backslash as an escape character; if you need to use a
backslash in a string literal, either use two backslashes, i.e.:

  input='E:\\pythoninput\\mnyzeron5m.asc'

or use a raw literal, i.e.:

  input=r'E:\pythoninput\mnyzeron5m.asc'

Alternatively, a forward slash will work. But scripts should rarely
need to have literal pathnames embedded within them.

grass.run_command("v.in.ogr", '-o', dsn='E:\pythoninput\Mercier_ditch.shp', output='ditch2', '-o')
   

run_command() etc use the "flags" argument to specify flags (without
the leading '-'), e.g.:

grass.run_command("v.in.ogr", flags='o', dsn=r'E:\pythoninput\Mercier_ditch.shp', output='ditch2')

--
Sonja Jankowfsky
UR Hydrologie-Hydraulique
Cemagref de Lyon
3 bis quai Chauveau CP 220
69336 Lyon Cedex 09
FRANCE
Tel : (+33)4 72 20 86 11 Fax : (+33)4 78 47 78 75 _______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user
    
--
Sonja Jankowfsky
UR Hydrologie-Hydraulique
Cemagref de Lyon
3 bis quai Chauveau CP 220
69336 Lyon Cedex 09
FRANCE
Tel : (+33)4 72 20 86 11 Fax : (+33)4 78 47 78 75

Sonja Jankowfsky wrote:

So, the forward slash does not work in windows. (and I know I should not
use literal pathnames, but it's just for testing). So either two forward
or backward slashs, or one backward.

No: two backward (\\) or one forward (/) slash. Backslashes are used
to represent unprintable characters such as newline (\n), tab (\t)
etc.

One other question concerning the flags: How can I put two flags at the
same time, as for example -o --o for overwriting the projection check
and an already existing file?

The quiet (--q), verbose (--v) and overwrite (--o) flags have their
own options, so:

  grass.run_command(..., flags='o', overwrite=True)

for -o and --o.

The GRASS parser allows normal flags to be concatenated, so "-abc" is
equivalent to "-a -b -c", but the flags beginning with two dashes have
to be given separately.

--
Glynn Clements <glynn@gclements.plus.com>