ソースを参照

copy v.import from addons to trunk (will be integrated to wxGUI, work in progress)

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@65738 15284696-431f-4ddb-bdfa-cd5b030d7da7
Martin Landa 9 年 前
コミット
b5a1b8496b
3 ファイル変更247 行追加0 行削除
  1. 7 0
      scripts/v.import/Makefile
  2. 41 0
      scripts/v.import/v.import.html
  3. 199 0
      scripts/v.import/v.import.py

+ 7 - 0
scripts/v.import/Makefile

@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = v.import
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

+ 41 - 0
scripts/v.import/v.import.html

@@ -0,0 +1,41 @@
+<h2>DESCRIPTION</h2>
+
+<em>v.import</em> imports selected layers from a OGR vector datasource
+into the current location and mapset. If the projection of the input
+does not match the projection of the location, the input is reprojected
+into the current location. If the projection of the input does match
+the projection of the location, the input is imported directly with
+<a href="v.in.ogr.html">v.in.ogr</a>.
+
+<h4>Topology cleaning</h4>
+
+When importing polygons, non-topological polygons are converted to
+topological areas. If the original polygons contain errors (unexpected
+overlapping areas or small gaps between polygons), the import might
+need to be repeated using the <em>snap</em> option for
+<a href="v.in.ogr.html">v.in.ogr</a>. In this case, automated import and
+reprojection does not work, and the data need to be manually imported
+first with appropriate settings and optionally cleaned afterwards with
+<a href="v.clean.html">v.clean</a>. The cleaned import can then be
+manually reprojected to the target location.
+
+<h2>EXAMPLE</h2>
+
+<div class="code"><pre>
+# import SHAPE file at full extent and reproject to current location projection
+v.import input=research_area.shp output=research_area extents=input
+</pre></div>
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="v.in.ogr.html">v.in.ogr</a>,
+<a href="v.proj.html">v.proj</a>
+</em>
+
+<h2>AUTHOR</h2>
+
+Markus Metz
+
+<p>
+<i>Last changed: $Date: 2015-01-20 20:52:27 +0100 (Tue, 20 Jan 2015) $</i>

+ 199 - 0
scripts/v.import/v.import.py

@@ -0,0 +1,199 @@
+#!/usr/bin/env python
+
+############################################################################
+#
+# MODULE:       v.import
+#
+# AUTHOR(S):    Markus Metz
+#
+# PURPOSE:      Import and reproject vector data on the fly
+#
+# COPYRIGHT:    (C) 2015 by GRASS development team
+#
+#               This program is free software under the GNU General
+#               Public License (>=v2). Read the file COPYING that
+#               comes with GRASS for details.
+#
+#############################################################################
+
+#%module
+#% description: Imports vector data into a GRASS vector map using OGR library and reproject on the fly.
+#% keyword: vector
+#% keyword: import
+#% keyword: projection
+#%end
+#%option 
+#% key: input
+#% type: string
+#% required: yes
+#% description: Name of OGR datasource to be imported
+#% gisprompt: old,datasource,datasource
+#%end
+#%option
+#% key: layer
+#% type: string
+#% multiple: yes
+#% description: OGR layer name. If not given, all available layers are imported
+#% guisection: Input
+#% gisprompt: old,datasource_layer,datasource_layer
+#%end
+#%option G_OPT_V_OUTPUT
+#% description: Name for output vector map (default: input)
+#% guisection: Output
+#%end
+#%option
+#% key: extents
+#% type: string
+#% required: yes
+#% options: input,region
+#% description: Ouput vector map extents
+#% descriptions: region;extents of current region;input;extents of input map
+#% guisection: Output
+#%end
+
+
+import sys
+import os
+import shutil
+import atexit
+import math
+
+import grass.script as grass
+
+    
+def cleanup():
+    # remove temp location
+    if tmploc:
+        grass.try_rmdir(os.path.join(gisdbase, tmploc))
+    if srcgisrc:
+        grass.try_remove(srcgisrc)
+
+def main():
+    global tmploc, srcgisrc, gisdbase
+
+    OGRdatasource = options['input']
+    output = options['output']
+    layers = options['layer']
+    
+    # initialize global vars
+    tmploc = None
+    srcgisrc = None
+    gisdbase = None
+
+    vflags = None
+    if options['extents'] == 'region':
+        vflags = 'r'
+
+    grassenv = grass.gisenv()
+    tgtloc = grassenv['LOCATION_NAME']
+    tgtmapset = grassenv['MAPSET']
+    gisdbase = grassenv['GISDBASE']
+    tgtgisrc = os.environ['GISRC']
+    srcgisrc = grass.tempfile()
+    
+    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('GUI: text\n')
+    f.close()
+
+    tgtsrs = grass.read_command('g.proj', flags = 'j', quiet = True)
+
+    # create temp location from input without import
+    grass.message(_("Creating temporary location for <%s>...") % OGRdatasource) 
+    returncode = grass.run_command('v.in.ogr', input = OGRdatasource,
+                                   layer = layers, output = output,
+                                   location = tmploc, flags = 'i', quiet = True)
+    # if it fails, return
+    if returncode != 0:
+        grass.fatal(_("Unable to create location from OGR datasource <%s>") % OGRdatasource)
+
+    # switch to temp location
+    os.environ['GISRC'] = str(srcgisrc)
+
+    # compare source and target srs
+    insrs = grass.read_command('g.proj', flags = 'j', quiet = True)
+
+    # switch to target location
+    os.environ['GISRC'] = str(tgtgisrc)
+
+    if insrs == tgtsrs:
+        # try v.in.ogr directly
+        grass.message(_("Importing <%s>...") % OGRdatasource) 
+        returncode = grass.run_command('v.in.ogr', input = OGRdatasource,
+                                       layer = layers, output = output,
+                                       flags = vflags)
+        # if it succeeds, return
+        if returncode == 0:
+            grass.message(_("Input <%s> successfully imported without reprojection") % OGRdatasource) 
+            return 0
+        else:
+            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':
+        grass.fatal(_("Coordinate reference system not available for current location <%s>") % tgtloc)
+    
+    # switch to temp location
+    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 options['extents'] == '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)
+
+        # reproject to src
+        # switch to temp location
+        os.environ['GISRC'] = str(srcgisrc)
+        returncode = grass.run_command('v.proj', input = vreg, output = vreg, 
+                                       location = tgtloc, mapset = tgtmapset, quiet = True)
+        
+        if returncode != 0:
+            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)
+
+
+    # import into temp location
+    grass.message(_("Importing <%s> ...") % OGRdatasource)
+    returncode = grass.run_command('v.in.ogr', input = OGRdatasource,
+                                   layer = layers, output = output,
+                                   flags = vflags, verbose = True)
+    
+    # if it fails, return
+    if returncode != 0:
+        grass.fatal(_("Unable to import OGR datasource <%s>") % OGRdatasource)
+    
+    # switch to target location
+    os.environ['GISRC'] = str(tgtgisrc)
+
+    if options['extents'] == 'region':
+        grass.run_command('g.remove', type = 'vector', name = vreg,
+                          flags = 'f', quiet = True)
+
+    # v.proj
+    grass.message(_("Reprojecting <%s>...") % output)
+    returncode = grass.run_command('v.proj', location = tmploc,
+                                   mapset = 'PERMANENT', input = output,
+                                   quiet = True)
+    if returncode != 0:
+        grass.fatal(_("Unable to to reproject vector <%s>") % output)
+    
+    return 0
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    atexit.register(cleanup)
+    sys.exit(main())