stds_export.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. """
  2. Export functions for space time datasets
  3. Usage:
  4. .. code-block:: python
  5. import grass.temporal as tgis
  6. input="temp_1950_2012@PERMANENT"
  7. output="/tmp/temp_1950_2012.tar.gz"
  8. compression="gzip"
  9. workdir="/tmp"
  10. where=None
  11. format_="GTiff"
  12. type_="strds"
  13. tgis.export_stds(input, output, compression, workdir, where, format_, type_)
  14. (C) 2012-2013 by the GRASS Development Team
  15. This program is free software under the GNU General Public
  16. License (>=v2). Read the file COPYING that comes with GRASS
  17. for details.
  18. :authors: Soeren Gebbert
  19. """
  20. import shutil
  21. import os
  22. import tarfile
  23. import tempfile
  24. from space_time_datasets import *
  25. from factory import *
  26. from open_stds import *
  27. import grass.script as gscript
  28. proj_file_name = "proj.txt"
  29. init_file_name = "init.txt"
  30. metadata_file_name = "metadata.txt"
  31. read_file_name = "readme.txt"
  32. list_file_name = "list.txt"
  33. tmp_tar_file_name = "archive"
  34. # This global variable is for unique vector map export,
  35. # since single vector maps may have several layer
  36. # and therefore several attribute tables
  37. exported_maps = {}
  38. ############################################################################
  39. def _export_raster_maps_as_gdal(rows, tar, list_file, new_cwd, fs, format_):
  40. for row in rows:
  41. name = row["name"]
  42. start = row["start_time"]
  43. end = row["end_time"]
  44. max_val = row["max"]
  45. min_val = row["min"]
  46. datatype = row["datatype"]
  47. if not end:
  48. end = start
  49. string = "%s%s%s%s%s\n" % (name, fs, start, fs, end)
  50. # Write the filename, the start_time and the end_time
  51. list_file.write(string)
  52. if format_ == "GTiff":
  53. # Export the raster map with r.out.gdal as tif
  54. out_name = name + ".tif"
  55. if datatype == "CELL":
  56. nodata = max_val + 1
  57. if nodata < 256 and min_val >= 0:
  58. gdal_type = "Byte"
  59. elif nodata < 65536 and min_val >= 0:
  60. gdal_type = "UInt16"
  61. elif min_val >= 0:
  62. gdal_type = "UInt32"
  63. else:
  64. gdal_type = "Int32"
  65. ret = gscript.run_command("r.out.gdal", flags="c", input=name,
  66. output=out_name, nodata=nodata,
  67. type=gdal_type, format="GTiff")
  68. else:
  69. ret = gscript.run_command("r.out.gdal", flags="c",
  70. input=name, output=out_name,
  71. format="GTiff")
  72. elif format_ == "AAIGrid":
  73. # Export the raster map with r.out.gdal as Arc/Info ASCII Grid
  74. out_name = name + ".asc"
  75. ret = gscript.run_command("r.out.gdal", flags="c", input=name,
  76. output=out_name, format="AAIGrid")
  77. if ret != 0:
  78. shutil.rmtree(new_cwd)
  79. tar.close()
  80. gscript.fatal(_("Unable to export raster map <%s>" % name))
  81. tar.add(out_name)
  82. # Export the color rules
  83. out_name = name + ".color"
  84. ret = gscript.run_command("r.colors.out", map=name, rules=out_name)
  85. if ret != 0:
  86. shutil.rmtree(new_cwd)
  87. tar.close()
  88. gscript.fatal(_("Unable to export color rules for raster "
  89. "map <%s> r.out.gdal" % name))
  90. tar.add(out_name)
  91. ############################################################################
  92. def _export_raster_maps(rows, tar, list_file, new_cwd, fs):
  93. for row in rows:
  94. name = row["name"]
  95. start = row["start_time"]
  96. end = row["end_time"]
  97. if not end:
  98. end = start
  99. string = "%s%s%s%s%s\n" % (name, fs, start, fs, end)
  100. # Write the filename, the start_time and the end_time
  101. list_file.write(string)
  102. # Export the raster map with r.pack
  103. ret = gscript.run_command("r.pack", input=name, flags="c")
  104. if ret != 0:
  105. shutil.rmtree(new_cwd)
  106. tar.close()
  107. gscript.fatal(_("Unable to export raster map <%s> with r.pack" %
  108. name))
  109. tar.add(name + ".pack")
  110. ############################################################################
  111. def _export_vector_maps_as_gml(rows, tar, list_file, new_cwd, fs):
  112. for row in rows:
  113. name = row["name"]
  114. start = row["start_time"]
  115. end = row["end_time"]
  116. layer = row["layer"]
  117. if not layer:
  118. layer = 1
  119. if not end:
  120. end = start
  121. string = "%s%s%s%s%s\n" % (name, fs, start, fs, end)
  122. # Write the filename, the start_time and the end_time
  123. list_file.write(string)
  124. # Export the vector map with v.out.ogr
  125. ret = gscript.run_command("v.out.ogr", input=name, dsn=(name + ".xml"),
  126. layer=layer, format="GML")
  127. if ret != 0:
  128. shutil.rmtree(new_cwd)
  129. tar.close()
  130. gscript.fatal(_("Unable to export vector map <%s> as "
  131. "GML with v.out.ogr" % name))
  132. tar.add(name + ".xml")
  133. tar.add(name + ".xsd")
  134. ############################################################################
  135. def _export_vector_maps(rows, tar, list_file, new_cwd, fs):
  136. for row in rows:
  137. name = row["name"]
  138. start = row["start_time"]
  139. end = row["end_time"]
  140. layer = row["layer"]
  141. # Export unique maps only
  142. if name in exported_maps:
  143. continue
  144. if not layer:
  145. layer = 1
  146. if not end:
  147. end = start
  148. string = "%s:%s%s%s%s%s\n" % (name, layer, fs, start, fs, end)
  149. # Write the filename, the start_time and the end_time
  150. list_file.write(string)
  151. # Export the vector map with v.pack
  152. ret = gscript.run_command("v.pack", input=name, flags="c")
  153. if ret != 0:
  154. shutil.rmtree(new_cwd)
  155. tar.close()
  156. gscript.fatal(_("Unable to export vector map <%s> with v.pack" %
  157. name))
  158. tar.add(name + ".pack")
  159. exported_maps[name] = name
  160. ############################################################################
  161. def _export_raster3d_maps(rows, tar, list_file, new_cwd, fs):
  162. for row in rows:
  163. name = row["name"]
  164. start = row["start_time"]
  165. end = row["end_time"]
  166. if not end:
  167. end = start
  168. string = "%s%s%s%s%s\n" % (name, fs, start, fs, end)
  169. # Write the filename, the start_time and the end_time
  170. list_file.write(string)
  171. # Export the raster 3d map with r3.pack
  172. ret = gscript.run_command("r3.pack", input=name, flags="c")
  173. if ret != 0:
  174. shutil.rmtree(new_cwd)
  175. tar.close()
  176. gscript.fatal(_("Unable to export raster map <%s> with r3.pack" %
  177. name))
  178. tar.add(name + ".pack")
  179. ############################################################################
  180. def export_stds(input, output, compression, workdir, where, format_="pack",
  181. type_="strds"):
  182. """Export space time datasets as tar archive with optional compression
  183. This method should be used to export space time datasets
  184. of type raster and vector as tar archive that can be reimported
  185. with the method import_stds().
  186. :param input: The name of the space time dataset to export
  187. :param output: The name of the archive file
  188. :param compression: The compression of the archive file:
  189. - "no" no compression
  190. - "gzip" GNU zip compression
  191. - "bzip2" Bzip compression
  192. :param workdir: The working directory used for extraction and packing
  193. :param where: The temporal WHERE SQL statement to select a subset
  194. of maps from the space time dataset
  195. :param format_: The export format:
  196. - "GTiff" Geotiff format, only for raster maps
  197. - "AAIGrid" Arc/Info ASCII Grid format, only for raster maps
  198. - "pack" The GRASS raster, 3D raster or vector Pack format,
  199. this is the default setting
  200. - "GML" GML file export format, only for vector maps,
  201. v.out.ogr export option
  202. :param type_: The space time dataset type
  203. - "strds" Space time raster dataset
  204. - "str3ds" Space time 3D raster dataset
  205. - "stvds" Space time vector dataset
  206. """
  207. # Save current working directory path
  208. old_cwd = os.getcwd()
  209. # Create the temporary directory and jump into it
  210. new_cwd = tempfile.mkdtemp(dir=workdir)
  211. os.chdir(new_cwd)
  212. if type_ == "strds":
  213. columns = "name,start_time,end_time,min,max,datatype"
  214. elif type_ == "stvds":
  215. columns = "name,start_time,end_time,layer"
  216. else:
  217. columns = "name,start_time,end_time"
  218. sp = open_old_stds(input, type_)
  219. rows = sp.get_registered_maps(columns, where, "start_time", None)
  220. if compression == "gzip":
  221. flag = "w:gz"
  222. elif compression == "bzip2":
  223. flag = "w:bz2"
  224. else:
  225. flag = "w:"
  226. # Open the tar archive to add the files
  227. tar = tarfile.open(tmp_tar_file_name, flag)
  228. list_file = open(list_file_name, "w")
  229. fs = "|"
  230. if rows:
  231. if type_ == "strds":
  232. if format_ == "GTiff" or format_ == "AAIGrid":
  233. _export_raster_maps_as_gdal(
  234. rows, tar, list_file, new_cwd, fs, format_)
  235. else:
  236. _export_raster_maps(rows, tar, list_file, new_cwd, fs)
  237. elif type_ == "stvds":
  238. if format_ == "GML":
  239. _export_vector_maps_as_gml(rows, tar, list_file, new_cwd, fs)
  240. else:
  241. _export_vector_maps(rows, tar, list_file, new_cwd, fs)
  242. elif type_ == "str3ds":
  243. _export_raster3d_maps(rows, tar, list_file, new_cwd, fs)
  244. list_file.close()
  245. # Write projection and metadata
  246. proj = gscript.read_command("g.proj", flags="j")
  247. proj_file = open(proj_file_name, "w")
  248. proj_file.write(proj)
  249. proj_file.close()
  250. init_file = open(init_file_name, "w")
  251. # Create the init string
  252. string = ""
  253. # This is optional, if not present strds will be assumed for backward
  254. # compatibility
  255. string += "%s=%s\n" % ("stds_type", sp.get_type())
  256. # This is optional, if not present gtiff will be assumed for
  257. # backward compatibility
  258. string += "%s=%s\n" % ("format", format_)
  259. string += "%s=%s\n" % ("temporal_type", sp.get_temporal_type())
  260. string += "%s=%s\n" % ("semantic_type", sp.get_semantic_type())
  261. if sp.is_time_relative():
  262. string += "%s=%s\n" % ("relative_time_unit",
  263. sp.get_relative_time_unit())
  264. string += "%s=%s\n" % ("number_of_maps", sp.metadata.get_number_of_maps())
  265. north, south, east, west, top, bottom = sp.get_spatial_extent_as_tuple()
  266. string += "%s=%s\n" % ("north", north)
  267. string += "%s=%s\n" % ("south", south)
  268. string += "%s=%s\n" % ("east", east)
  269. string += "%s=%s\n" % ("west", west)
  270. init_file.write(string)
  271. init_file.close()
  272. metadata = gscript.read_command("t.info", type=type_, input=sp.get_id())
  273. metadata_file = open(metadata_file_name, "w")
  274. metadata_file.write(metadata)
  275. metadata_file.close()
  276. read_file = open(read_file_name, "w")
  277. if type_ == "strds":
  278. read_file.write("This space time raster dataset was exported with "
  279. "t.rast.export of GRASS GIS 7\n")
  280. elif type_ == "stvds":
  281. read_file.write("This space time vector dataset was exported with "
  282. "t.vect.export of GRASS GIS 7\n")
  283. elif type_ == "str3ds":
  284. read_file.write("This space time 3D raster dataset was exported "
  285. "with t.rast3d.export of GRASS GIS 7\n")
  286. read_file.write("\n")
  287. read_file.write("Files:\n")
  288. if type_ == "strds":
  289. if format_ == "GTiff":
  290. # 123456789012345678901234567890
  291. read_file.write(" *.tif -- GeoTIFF raster files\n")
  292. read_file.write(" *.color -- GRASS GIS raster color rules\n")
  293. elif format_ == "pack":
  294. read_file.write(" *.pack -- GRASS raster files packed with r.pack\n")
  295. elif type_ == "stvds":
  296. # 123456789012345678901234567890
  297. if format_ == "GML":
  298. read_file.write(" *.xml -- Vector GML files\n")
  299. else:
  300. read_file.write(" *.pack -- GRASS vector files packed with v.pack\n")
  301. elif type_ == "str3ds":
  302. read_file.write(" *.pack -- GRASS 3D raster files packed with r3.pack\n")
  303. read_file.write("%13s -- Projection information in PROJ.4 format\n" %
  304. (proj_file_name))
  305. read_file.write("%13s -- GRASS GIS space time %s dataset information\n" %
  306. (init_file_name, sp.get_new_map_instance(None).get_type()))
  307. read_file.write("%13s -- Time series file, lists all maps by name "
  308. "with interval\n" % (list_file_name))
  309. read_file.write(" time stamps in ISO-Format. Field separator is |\n")
  310. read_file.write("%13s -- The output of t.info\n" %
  311. (metadata_file_name))
  312. read_file.write("%13s -- This file\n" % (read_file_name))
  313. read_file.close()
  314. # Append the file list
  315. tar.add(list_file_name)
  316. tar.add(proj_file_name)
  317. tar.add(init_file_name)
  318. tar.add(read_file_name)
  319. tar.add(metadata_file_name)
  320. tar.close()
  321. os.chdir(old_cwd)
  322. # Move the archive to its destination
  323. shutil.move(os.path.join(new_cwd, tmp_tar_file_name), output)
  324. # Remove the temporary created working directory
  325. shutil.rmtree(new_cwd)