v.import.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: v.import
  5. #
  6. # AUTHOR(S): Markus Metz
  7. #
  8. # PURPOSE: Import and reproject vector data on the fly
  9. #
  10. # COPYRIGHT: (C) 2015 by GRASS development team
  11. #
  12. # This program is free software under the GNU General
  13. # Public License (>=v2). Read the file COPYING that
  14. # comes with GRASS for details.
  15. #
  16. #############################################################################
  17. #%module
  18. #% description: Imports vector data into a GRASS vector map using OGR library and reproject on the fly.
  19. #% keyword: vector
  20. #% keyword: import
  21. #% keyword: projection
  22. #%end
  23. #%option
  24. #% key: input
  25. #% type: string
  26. #% required: yes
  27. #% description: Name of OGR datasource to be imported
  28. #% gisprompt: old,datasource,datasource
  29. #%end
  30. #%option
  31. #% key: layer
  32. #% type: string
  33. #% multiple: yes
  34. #% description: OGR layer name. If not given, all available layers are imported
  35. #% guisection: Input
  36. #% gisprompt: old,datasource_layer,datasource_layer
  37. #%end
  38. #%option G_OPT_V_OUTPUT
  39. #% description: Name for output vector map (default: input)
  40. #% required: no
  41. #% guisection: Output
  42. #%end
  43. #%option
  44. #% key: extents
  45. #% type: string
  46. #% options: input,region
  47. #% answer: input
  48. #% description: Ouput vector map extents
  49. #% descriptions: input;extents of input map;region;extents of current region
  50. #% guisection: Output
  51. #%end
  52. #%flag
  53. #% key: f
  54. #% description: List supported OGR formats and exit
  55. #% suppress_required: yes
  56. #%end
  57. #%flag
  58. #% key: l
  59. #% description: List available OGR layers in data source and exit
  60. #%end
  61. import sys
  62. import os
  63. import shutil
  64. import atexit
  65. import math
  66. import grass.script as grass
  67. def cleanup():
  68. # remove temp location
  69. if tmploc:
  70. grass.try_rmdir(os.path.join(gisdbase, tmploc))
  71. if srcgisrc:
  72. grass.try_remove(srcgisrc)
  73. def main():
  74. global tmploc, srcgisrc, gisdbase
  75. # initialize global vars
  76. tmploc = None
  77. srcgisrc = None
  78. gisdbase = None
  79. # list formats and exit
  80. if flags['f']:
  81. grass.run_command('v.in.ogr', flags='f')
  82. return 0
  83. # list layers and exit
  84. if flags['l']:
  85. grass.run_command('v.in.ogr', flags='l', input=options['input'])
  86. return 0
  87. OGRdatasource = options['input']
  88. output = options['output']
  89. layers = options['layer']
  90. vflags = None
  91. if options['extents'] == 'region':
  92. vflags = 'r'
  93. grassenv = grass.gisenv()
  94. tgtloc = grassenv['LOCATION_NAME']
  95. tgtmapset = grassenv['MAPSET']
  96. gisdbase = grassenv['GISDBASE']
  97. tgtgisrc = os.environ['GISRC']
  98. srcgisrc = grass.tempfile()
  99. tmploc = 'temp_import_location_' + str(os.getpid())
  100. f = open(srcgisrc, 'w')
  101. f.write('MAPSET: PERMANENT\n')
  102. f.write('GISDBASE: %s\n' % gisdbase)
  103. f.write('LOCATION_NAME: %s\n' % tmploc);
  104. f.write('GUI: text\n')
  105. f.close()
  106. tgtsrs = grass.read_command('g.proj', flags = 'j', quiet = True)
  107. # create temp location from input without import
  108. grass.message(_("Creating temporary location for <%s>...") % OGRdatasource)
  109. v_in_kwargs = dict()
  110. if layers:
  111. v_in_kwargs['layer'] = layers
  112. if output:
  113. v_in_kwargs['output'] = output
  114. returncode = grass.run_command('v.in.ogr', input = OGRdatasource,
  115. location = tmploc, flags = 'i', quiet = True, **v_in_kwargs)
  116. # if it fails, return
  117. if returncode != 0:
  118. grass.fatal(_("Unable to create location from OGR datasource <%s>") % OGRdatasource)
  119. # switch to temp location
  120. os.environ['GISRC'] = str(srcgisrc)
  121. # compare source and target srs
  122. insrs = grass.read_command('g.proj', flags = 'j', quiet = True)
  123. # switch to target location
  124. os.environ['GISRC'] = str(tgtgisrc)
  125. if insrs == tgtsrs:
  126. # try v.in.ogr directly
  127. grass.message(_("Importing <%s>...") % OGRdatasource)
  128. returncode = grass.run_command('v.in.ogr', input = OGRdatasource,
  129. flags = vflags, **v_in_kwargs)
  130. # if it succeeds, return
  131. if returncode == 0:
  132. grass.message(_("Input <%s> successfully imported without reprojection") % OGRdatasource)
  133. return 0
  134. else:
  135. grass.fatal(_("Unable to import <%s>") % OGRdatasource)
  136. # make sure target is not xy
  137. if grass.parse_command('g.proj', flags = 'g')['name'] == 'xy_location_unprojected':
  138. grass.fatal(_("Coordinate reference system not available for current location <%s>") % tgtloc)
  139. # switch to temp location
  140. os.environ['GISRC'] = str(srcgisrc)
  141. # make sure input is not xy
  142. if grass.parse_command('g.proj', flags = 'g')['name'] == 'xy_location_unprojected':
  143. grass.fatal(_("Coordinate reference system not available for input <%s>") % GDALdatasource)
  144. if options['extents'] == 'region':
  145. # switch to target location
  146. os.environ['GISRC'] = str(tgtgisrc)
  147. # v.in.region in tgt
  148. vreg = 'vreg_' + str(os.getpid())
  149. grass.run_command('v.in.region', output = vreg, quiet = True)
  150. # reproject to src
  151. # switch to temp location
  152. os.environ['GISRC'] = str(srcgisrc)
  153. returncode = grass.run_command('v.proj', input = vreg, output = vreg,
  154. location = tgtloc, mapset = tgtmapset, quiet = True)
  155. if returncode != 0:
  156. grass.fatal(_("Unable to reproject to source location"))
  157. # set region from region vector
  158. grass.run_command('g.region', res = '1')
  159. grass.run_command('g.region', vector = vreg)
  160. # import into temp location
  161. grass.message(_("Importing <%s> ...") % OGRdatasource)
  162. returncode = grass.run_command('v.in.ogr', input = OGRdatasource,
  163. flags = vflags, **v_in_kwargs)
  164. # if it fails, return
  165. if returncode != 0:
  166. grass.fatal(_("Unable to import OGR datasource <%s>") % OGRdatasource)
  167. # if output is not define check source mapset
  168. if not output:
  169. output = grass.list_grouped('vector')['PERMANENT'][0]
  170. # switch to target location
  171. os.environ['GISRC'] = str(tgtgisrc)
  172. # check if map exists
  173. if not grass.overwrite() and \
  174. grass.find_file(output, element='vector', mapset='.')['mapset']:
  175. grass.fatal(_("option <%s>: <%s> exists.") % ('output', output))
  176. if options['extents'] == 'region':
  177. grass.run_command('g.remove', type = 'vector', name = vreg,
  178. flags = 'f', quiet = True)
  179. # v.proj
  180. grass.message(_("Reprojecting <%s>...") % output)
  181. returncode = grass.run_command('v.proj', location = tmploc,
  182. mapset = 'PERMANENT', input = output,
  183. quiet = True)
  184. if returncode != 0:
  185. grass.fatal(_("Unable to to reproject vector <%s>") % output)
  186. return 0
  187. if __name__ == "__main__":
  188. options, flags = grass.parser()
  189. atexit.register(cleanup)
  190. sys.exit(main())