|
@@ -22,7 +22,7 @@
|
|
#% keyword: import
|
|
#% keyword: import
|
|
#% keyword: projection
|
|
#% keyword: projection
|
|
#%end
|
|
#%end
|
|
-#%option
|
|
|
|
|
|
+#%option
|
|
#% key: input
|
|
#% key: input
|
|
#% type: string
|
|
#% type: string
|
|
#% required: yes
|
|
#% required: yes
|
|
@@ -72,27 +72,27 @@
|
|
|
|
|
|
import sys
|
|
import sys
|
|
import os
|
|
import os
|
|
-import shutil
|
|
|
|
import atexit
|
|
import atexit
|
|
-import math
|
|
|
|
|
|
|
|
import grass.script as grass
|
|
import grass.script as grass
|
|
from grass.exceptions import CalledModuleError
|
|
from grass.exceptions import CalledModuleError
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+# initialize global vars
|
|
|
|
+TMPLOC = None
|
|
|
|
+SRCGISRC = None
|
|
|
|
+GISDBASE = None
|
|
|
|
+
|
|
|
|
+
|
|
def cleanup():
|
|
def cleanup():
|
|
# remove temp location
|
|
# 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
|
|
|
|
|
|
|
|
- # initialize global vars
|
|
|
|
- tmploc = None
|
|
|
|
- srcgisrc = None
|
|
|
|
- gisdbase = None
|
|
|
|
|
|
+def main():
|
|
|
|
+ global TMPLOC, SRCGISRC, GISDBASE
|
|
|
|
|
|
# list formats and exit
|
|
# list formats and exit
|
|
if flags['f']:
|
|
if flags['f']:
|
|
@@ -103,35 +103,35 @@ def main():
|
|
if flags['l']:
|
|
if flags['l']:
|
|
grass.run_command('v.in.ogr', flags='l', input=options['input'])
|
|
grass.run_command('v.in.ogr', flags='l', input=options['input'])
|
|
return 0
|
|
return 0
|
|
-
|
|
|
|
|
|
+
|
|
OGRdatasource = options['input']
|
|
OGRdatasource = options['input']
|
|
output = options['output']
|
|
output = options['output']
|
|
layers = options['layer']
|
|
layers = options['layer']
|
|
-
|
|
|
|
|
|
+
|
|
vflags = None
|
|
vflags = None
|
|
if options['extent'] == 'region':
|
|
if options['extent'] == 'region':
|
|
vflags = 'r'
|
|
vflags = 'r'
|
|
vopts = {}
|
|
vopts = {}
|
|
if options['encoding']:
|
|
if options['encoding']:
|
|
vopts['encoding'] = options['encoding']
|
|
vopts['encoding'] = options['encoding']
|
|
-
|
|
|
|
|
|
+
|
|
grassenv = grass.gisenv()
|
|
grassenv = grass.gisenv()
|
|
tgtloc = grassenv['LOCATION_NAME']
|
|
tgtloc = grassenv['LOCATION_NAME']
|
|
tgtmapset = grassenv['MAPSET']
|
|
tgtmapset = grassenv['MAPSET']
|
|
- gisdbase = grassenv['GISDBASE']
|
|
|
|
|
|
+ GISDBASE = grassenv['GISDBASE']
|
|
tgtgisrc = os.environ['GISRC']
|
|
tgtgisrc = os.environ['GISRC']
|
|
- srcgisrc = grass.tempfile()
|
|
|
|
-
|
|
|
|
- tmploc = 'temp_import_location_' + str(os.getpid())
|
|
|
|
|
|
+ SRCGISRC = grass.tempfile()
|
|
|
|
+
|
|
|
|
+ TMPLOC = 'temp_import_location_' + str(os.getpid())
|
|
|
|
|
|
- f = open(srcgisrc, 'w')
|
|
|
|
|
|
+ f = open(SRCGISRC, 'w')
|
|
f.write('MAPSET: PERMANENT\n')
|
|
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.write('GUI: text\n')
|
|
f.close()
|
|
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
|
|
# create temp location from input without import
|
|
grass.verbose(_("Creating temporary location for <%s>...") % OGRdatasource)
|
|
grass.verbose(_("Creating temporary location for <%s>...") % OGRdatasource)
|
|
@@ -140,13 +140,13 @@ def main():
|
|
if output:
|
|
if output:
|
|
vopts['output'] = output
|
|
vopts['output'] = output
|
|
try:
|
|
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:
|
|
except CalledModuleError:
|
|
grass.fatal(_("Unable to create location from OGR datasource <%s>") % OGRdatasource)
|
|
grass.fatal(_("Unable to create location from OGR datasource <%s>") % OGRdatasource)
|
|
|
|
|
|
# switch to temp location
|
|
# switch to temp location
|
|
- os.environ['GISRC'] = str(srcgisrc)
|
|
|
|
|
|
+ os.environ['GISRC'] = str(SRCGISRC)
|
|
|
|
|
|
# switch to target location
|
|
# switch to target location
|
|
os.environ['GISRC'] = str(tgtgisrc)
|
|
os.environ['GISRC'] = str(tgtgisrc)
|
|
@@ -155,58 +155,57 @@ def main():
|
|
if grass.run_command('v.in.ogr', input=OGRdatasource, flags='j',
|
|
if grass.run_command('v.in.ogr', input=OGRdatasource, flags='j',
|
|
errors='status', quiet=True) == 0:
|
|
errors='status', quiet=True) == 0:
|
|
try:
|
|
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
|
|
return 0
|
|
- except CalledModuleError as e:
|
|
|
|
|
|
+ except CalledModuleError:
|
|
grass.fatal(_("Unable to import <%s>") % OGRdatasource)
|
|
grass.fatal(_("Unable to import <%s>") % OGRdatasource)
|
|
-
|
|
|
|
|
|
+
|
|
# make sure target is not xy
|
|
# 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)
|
|
grass.fatal(_("Coordinate reference system not available for current location <%s>") % tgtloc)
|
|
-
|
|
|
|
|
|
+
|
|
# switch to temp location
|
|
# switch to temp location
|
|
- os.environ['GISRC'] = str(srcgisrc)
|
|
|
|
|
|
+ os.environ['GISRC'] = str(SRCGISRC)
|
|
|
|
|
|
# make sure input is not xy
|
|
# 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':
|
|
if options['extent'] == 'region':
|
|
# switch to target location
|
|
# switch to target location
|
|
os.environ['GISRC'] = str(tgtgisrc)
|
|
os.environ['GISRC'] = str(tgtgisrc)
|
|
|
|
|
|
# v.in.region in tgt
|
|
# v.in.region in tgt
|
|
vreg = 'vreg_' + str(os.getpid())
|
|
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
|
|
# reproject to src
|
|
# switch to temp location
|
|
# switch to temp location
|
|
- os.environ['GISRC'] = str(srcgisrc)
|
|
|
|
|
|
+ os.environ['GISRC'] = str(SRCGISRC)
|
|
try:
|
|
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:
|
|
except CalledModuleError:
|
|
grass.fatal(_("Unable to reproject to source location"))
|
|
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)
|
|
|
|
|
|
|
|
|
|
+ # set region from region vector
|
|
|
|
+ grass.run_command('g.region', res='1')
|
|
|
|
+ grass.run_command('g.region', vector=vreg)
|
|
|
|
|
|
# import into temp location
|
|
# import into temp location
|
|
grass.message(_("Importing <%s> ...") % OGRdatasource)
|
|
grass.message(_("Importing <%s> ...") % OGRdatasource)
|
|
try:
|
|
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:
|
|
except CalledModuleError:
|
|
grass.fatal(_("Unable to import OGR datasource <%s>") % OGRdatasource)
|
|
grass.fatal(_("Unable to import OGR datasource <%s>") % OGRdatasource)
|
|
|
|
|
|
# if output is not define check source mapset
|
|
# if output is not define check source mapset
|
|
if not output:
|
|
if not output:
|
|
output = grass.list_grouped('vector')['PERMANENT'][0]
|
|
output = grass.list_grouped('vector')['PERMANENT'][0]
|
|
-
|
|
|
|
|
|
+
|
|
# switch to target location
|
|
# switch to target location
|
|
os.environ['GISRC'] = str(tgtgisrc)
|
|
os.environ['GISRC'] = str(tgtgisrc)
|
|
|
|
|
|
@@ -214,20 +213,20 @@ def main():
|
|
if not grass.overwrite() and \
|
|
if not grass.overwrite() and \
|
|
grass.find_file(output, element='vector', mapset='.')['mapset']:
|
|
grass.find_file(output, element='vector', mapset='.')['mapset']:
|
|
grass.fatal(_("option <%s>: <%s> exists.") % ('output', output))
|
|
grass.fatal(_("option <%s>: <%s> exists.") % ('output', output))
|
|
-
|
|
|
|
|
|
+
|
|
if options['extent'] == 'region':
|
|
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
|
|
# v.proj
|
|
grass.message(_("Reprojecting <%s>...") % output)
|
|
grass.message(_("Reprojecting <%s>...") % output)
|
|
try:
|
|
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:
|
|
except CalledModuleError:
|
|
grass.fatal(_("Unable to to reproject vector <%s>") % output)
|
|
grass.fatal(_("Unable to to reproject vector <%s>") % output)
|
|
-
|
|
|
|
|
|
+
|
|
return 0
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if __name__ == "__main__":
|