[GRASS-SVN] r66454 - grass/trunk/scripts/v.import
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri Oct 9 11:15:14 PDT 2015
Author: annakrat
Date: 2015-10-09 11:15:14 -0700 (Fri, 09 Oct 2015)
New Revision: 66454
Modified:
grass/trunk/scripts/v.import/v.import.py
Log:
v.import: pep8 cleanup, fix copy and paste bug in error message
Modified: grass/trunk/scripts/v.import/v.import.py
===================================================================
--- grass/trunk/scripts/v.import/v.import.py 2015-10-09 18:04:07 UTC (rev 66453)
+++ grass/trunk/scripts/v.import/v.import.py 2015-10-09 18:15:14 UTC (rev 66454)
@@ -22,7 +22,7 @@
#% keyword: import
#% keyword: projection
#%end
-#%option
+#%option
#% key: input
#% type: string
#% required: yes
@@ -72,28 +72,28 @@
import sys
import os
-import shutil
import atexit
-import math
import grass.script as grass
from grass.exceptions import CalledModuleError
-
+
+# initialize global vars
+TMPLOC = None
+SRCGISRC = None
+GISDBASE = None
+
+
def cleanup():
# remove temp location
- if tmploc:
- grass.try_rmdir(os.path.join(gisdbase, tmploc))
- if srcgisrc:
- grass.try_remove(srcgisrc)
+ if TMPLOC:
+ grass.try_rmdir(os.path.join(GISDBASE, TMPLOC))
+ if SRCGISRC:
+ grass.try_remove(SRCGISRC)
+
def main():
- global tmploc, srcgisrc, gisdbase
+ global TMPLOC, SRCGISRC, GISDBASE
- # initialize global vars
- tmploc = None
- srcgisrc = None
- gisdbase = None
-
# list formats and exit
if flags['f']:
grass.run_command('v.in.ogr', flags='f')
@@ -103,35 +103,35 @@
if flags['l']:
grass.run_command('v.in.ogr', flags='l', input=options['input'])
return 0
-
+
OGRdatasource = options['input']
output = options['output']
layers = options['layer']
-
+
vflags = None
if options['extent'] == 'region':
vflags = 'r'
vopts = {}
if options['encoding']:
vopts['encoding'] = options['encoding']
-
+
grassenv = grass.gisenv()
tgtloc = grassenv['LOCATION_NAME']
tgtmapset = grassenv['MAPSET']
- gisdbase = grassenv['GISDBASE']
+ GISDBASE = grassenv['GISDBASE']
tgtgisrc = os.environ['GISRC']
- srcgisrc = grass.tempfile()
-
- tmploc = 'temp_import_location_' + str(os.getpid())
+ SRCGISRC = grass.tempfile()
- f = open(srcgisrc, 'w')
+ TMPLOC = 'temp_import_location_' + str(os.getpid())
+
+ f = open(SRCGISRC, 'w')
f.write('MAPSET: PERMANENT\n')
- f.write('GISDBASE: %s\n' % gisdbase)
- f.write('LOCATION_NAME: %s\n' % tmploc);
+ f.write('GISDBASE: %s\n' % GISDBASE)
+ f.write('LOCATION_NAME: %s\n' % TMPLOC)
f.write('GUI: text\n')
f.close()
- tgtsrs = grass.read_command('g.proj', flags = 'j', quiet = True)
+ tgtsrs = grass.read_command('g.proj', flags='j', quiet=True)
# create temp location from input without import
grass.verbose(_("Creating temporary location for <%s>...") % OGRdatasource)
@@ -140,13 +140,13 @@
if output:
vopts['output'] = output
try:
- grass.run_command('v.in.ogr', input = OGRdatasource,
- location = tmploc, flags = 'i', quiet = True, **vopts)
+ grass.run_command('v.in.ogr', input=OGRdatasource,
+ location=TMPLOC, flags='i', quiet=True, **vopts)
except CalledModuleError:
grass.fatal(_("Unable to create location from OGR datasource <%s>") % OGRdatasource)
# switch to temp location
- os.environ['GISRC'] = str(srcgisrc)
+ os.environ['GISRC'] = str(SRCGISRC)
# switch to target location
os.environ['GISRC'] = str(tgtgisrc)
@@ -155,58 +155,57 @@
if grass.run_command('v.in.ogr', input=OGRdatasource, flags='j',
errors='status', quiet=True) == 0:
try:
- grass.run_command('v.in.ogr', input = OGRdatasource,
- flags = vflags, **vopts)
- grass.message(_("Input <%s> successfully imported without reprojection") % OGRdatasource)
+ grass.run_command('v.in.ogr', input=OGRdatasource,
+ flags=vflags, **vopts)
+ grass.message(_("Input <%s> successfully imported without reprojection") % OGRdatasource)
return 0
- except CalledModuleError as e:
+ except CalledModuleError:
grass.fatal(_("Unable to import <%s>") % OGRdatasource)
-
+
# make sure target is not xy
- if grass.parse_command('g.proj', flags = 'g')['name'] == 'xy_location_unprojected':
+ if grass.parse_command('g.proj', flags='g')['name'] == 'xy_location_unprojected':
grass.fatal(_("Coordinate reference system not available for current location <%s>") % tgtloc)
-
+
# switch to temp location
- os.environ['GISRC'] = str(srcgisrc)
+ os.environ['GISRC'] = str(SRCGISRC)
# make sure input is not xy
- if grass.parse_command('g.proj', flags = 'g')['name'] == 'xy_location_unprojected':
- grass.fatal(_("Coordinate reference system not available for input <%s>") % GDALdatasource)
-
+ if grass.parse_command('g.proj', flags='g')['name'] == 'xy_location_unprojected':
+ grass.fatal(_("Coordinate reference system not available for input <%s>") % OGRdatasource)
+
if options['extent'] == 'region':
# switch to target location
os.environ['GISRC'] = str(tgtgisrc)
# v.in.region in tgt
vreg = 'vreg_' + str(os.getpid())
- grass.run_command('v.in.region', output = vreg, quiet = True)
+ grass.run_command('v.in.region', output=vreg, quiet=True)
# reproject to src
# switch to temp location
- os.environ['GISRC'] = str(srcgisrc)
+ os.environ['GISRC'] = str(SRCGISRC)
try:
- grass.run_command('v.proj', input = vreg, output = vreg,
- location = tgtloc, mapset = tgtmapset, quiet = True)
+ grass.run_command('v.proj', input=vreg, output=vreg,
+ location=tgtloc, mapset=tgtmapset, quiet=True)
except CalledModuleError:
grass.fatal(_("Unable to reproject to source location"))
-
+
# set region from region vector
- grass.run_command('g.region', res = '1')
- grass.run_command('g.region', vector = vreg)
+ grass.run_command('g.region', res='1')
+ grass.run_command('g.region', vector=vreg)
-
# import into temp location
grass.message(_("Importing <%s> ...") % OGRdatasource)
try:
- grass.run_command('v.in.ogr', input = OGRdatasource,
- flags = vflags, **vopts)
+ grass.run_command('v.in.ogr', input=OGRdatasource,
+ flags=vflags, **vopts)
except CalledModuleError:
grass.fatal(_("Unable to import OGR datasource <%s>") % OGRdatasource)
# if output is not define check source mapset
if not output:
output = grass.list_grouped('vector')['PERMANENT'][0]
-
+
# switch to target location
os.environ['GISRC'] = str(tgtgisrc)
@@ -214,20 +213,20 @@
if not grass.overwrite() and \
grass.find_file(output, element='vector', mapset='.')['mapset']:
grass.fatal(_("option <%s>: <%s> exists.") % ('output', output))
-
+
if options['extent'] == 'region':
- grass.run_command('g.remove', type = 'vector', name = vreg,
- flags = 'f', quiet = True)
+ grass.run_command('g.remove', type='vector', name=vreg,
+ flags='f', quiet=True)
# v.proj
grass.message(_("Reprojecting <%s>...") % output)
try:
- grass.run_command('v.proj', location = tmploc,
- mapset = 'PERMANENT', input = output,
- quiet = True)
+ grass.run_command('v.proj', location=TMPLOC,
+ mapset='PERMANENT', input=output,
+ quiet=True)
except CalledModuleError:
grass.fatal(_("Unable to to reproject vector <%s>") % output)
-
+
return 0
if __name__ == "__main__":
More information about the grass-commit
mailing list