PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/gdal-1.9.1-fedora/swig/python/samples/gdal_edit.py

#
Python | 154 lines | 123 code | 2 blank | 29 comment | 1 complexity | 9084f0b775e927f47327a2be05598ee1 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0
  1. #!/usr/bin/env python
  2. ###############################################################################
  3. # $Id: gdal_edit.py 23103 2011-09-22 16:58:49Z rouault $
  4. #
  5. # Project: GDAL samples
  6. # Purpose: Edit in place various information of an existing GDAL dataset
  7. # Author: Even Rouault <even dot rouault at mines dash paris dot org>
  8. #
  9. ###############################################################################
  10. # Copyright (c) 2011, Even Rouault <even dot rouault at mines dash paris dot org>
  11. #
  12. # Permission is hereby granted, free of charge, to any person obtaining a
  13. # copy of this software and associated documentation files (the "Software"),
  14. # to deal in the Software without restriction, including without limitation
  15. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  16. # and/or sell copies of the Software, and to permit persons to whom the
  17. # Software is furnished to do so, subject to the following conditions:
  18. #
  19. # The above copyright notice and this permission notice shall be included
  20. # in all copies or substantial portions of the Software.
  21. #
  22. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  23. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  25. # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  27. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  28. # DEALINGS IN THE SOFTWARE.
  29. ###############################################################################
  30. import sys
  31. from osgeo import gdal
  32. def Usage():
  33. print('Usage: gdal_edit [--help-general] [-a_srs srs_def] [-a_ullr ulx uly lrx lry]')
  34. print(' [-tr xres yres] [-unsetgt] [-a_nodata value] ')
  35. print(' [-mo "META-TAG=VALUE"]* datasetname')
  36. print('')
  37. print('Edit in place various information of an existing GDAL dataset.')
  38. return -1
  39. def gdal_edit(argv):
  40. argv = gdal.GeneralCmdLineProcessor( argv )
  41. if argv is None:
  42. return -1
  43. datasetname = None
  44. srs = None
  45. ulx = None
  46. uly = None
  47. lrx = None
  48. lry = None
  49. nodata = None
  50. xres = None
  51. yres = None
  52. unsetgt = False
  53. molist = []
  54. i = 1
  55. argc = len(argv)
  56. while i < argc:
  57. if argv[i] == '-a_srs' and i < len(argv)-1:
  58. srs = argv[i+1]
  59. i = i + 1
  60. elif argv[i] == '-a_ullr' and i < len(argv)-4:
  61. ulx = float(argv[i+1])
  62. i = i + 1
  63. uly = float(argv[i+1])
  64. i = i + 1
  65. lrx = float(argv[i+1])
  66. i = i + 1
  67. lry = float(argv[i+1])
  68. i = i + 1
  69. elif argv[i] == '-tr' and i < len(argv)-2:
  70. xres = float(argv[i+1])
  71. i = i + 1
  72. yres = float(argv[i+1])
  73. i = i + 1
  74. elif argv[i] == '-a_nodata' and i < len(argv)-1:
  75. nodata = float(argv[i+1])
  76. i = i + 1
  77. elif argv[i] == '-mo' and i < len(argv)-1:
  78. molist.append(argv[i+1])
  79. i = i + 1
  80. elif argv[i] == '-unsetgt' :
  81. unsetgt = True
  82. elif argv[i][0] == '-':
  83. sys.stderr.write('Unrecognized option : %s\n' % argv[i])
  84. return Usage()
  85. elif datasetname is None:
  86. datasetname = argv[i]
  87. else:
  88. sys.stderr.write('Unexpected option : %s\n' % argv[i])
  89. return Usage()
  90. i = i + 1
  91. if datasetname is None:
  92. return Usage()
  93. if srs is None and lry is None and yres is None and not unsetgt and nodata is None and len(molist) == 0:
  94. print('No option specified')
  95. print('')
  96. return Usage()
  97. exclusive_option = 0
  98. if lry is not None:
  99. exclusive_option = exclusive_option + 1
  100. if yres is not None:
  101. exclusive_option = exclusive_option + 1
  102. if unsetgt:
  103. exclusive_option = exclusive_option + 1
  104. if exclusive_option > 1:
  105. print('-a_ullr, -tr and -unsetgt options are exclusive.')
  106. print('')
  107. return Usage()
  108. ds = gdal.Open(datasetname, gdal.GA_Update)
  109. if ds is None:
  110. return -1
  111. if srs is not None:
  112. ds.SetProjection(srs)
  113. if lry is not None:
  114. gt = [ ulx, (lrx - ulx) / ds.RasterXSize, 0,
  115. uly, 0, (lry - uly) / ds.RasterYSize ]
  116. ds.SetGeoTransform(gt)
  117. if yres is not None:
  118. gt = ds.GetGeoTransform()
  119. # Doh ! why is gt a tuple and not an array...
  120. gt = [ gt[i] for i in range(6) ]
  121. gt[1] = xres
  122. gt[5] = yres
  123. ds.SetGeoTransform(gt)
  124. if unsetgt:
  125. ds.SetGeoTransform([0,1,0,0,0,1])
  126. if nodata is not None:
  127. for i in range(ds.RasterCount):
  128. ds.GetRasterBand(1).SetNoDataValue(nodata)
  129. if len(molist) != 0:
  130. ds.SetMetadata(molist)
  131. ds = None
  132. return 0
  133. if __name__ == '__main__':
  134. sys.exit(gdal_edit(sys.argv))