Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import os
  4. import re
  5. import sys
  6. import getopt
  7. from shutil import copyfile
  8.  
  9. def main(argv):
  10. inputFile = ''
  11. inputRes = ''
  12. inputEPSG = ''
  13. try:
  14. opts, args = getopt.getopt(argv,"h:i:r:s",["help","infile=","res=","srs="])
  15. except getopt.GetoptError:
  16. print('xyz2tif.py -i <input.xyz> -r <resulution> -s <EPSG:xxxx>')
  17. sys.exit(2)
  18. for opt, arg in opts:
  19. if opt in ("-h", "--help"):
  20. print("xyz2tif converts a text file with values for x y z in a regular gird to a TIF-file")
  21. print('xyz2tif.py -i <input.xyz> -r <resulution> -s <EPSG:xxxx>')
  22. print("xyz2tif.py --infile <input.xyz> --res <resulution> -srs <EPSG:xxxx>")
  23. sys.exit()
  24. elif opt in ("-i", "--infile"):
  25. inputFile = arg #filename.xyz
  26. if inputFile == "":
  27. print("Missing input.xyz!")
  28. sys.exit()
  29. elif opt in ("-r", "--res"):
  30. inputRes = arg # 1
  31. if inputRes == '':
  32. print("Missing information about spatial resulution!")
  33. sys.exit()
  34. elif opt in ("-s", "--srs"):
  35. inputEPSG = arg # EPSG:25833
  36. if inputEPSG == '':
  37. print("Missing information about the SRS!")
  38. sys.exit()
  39.  
  40. if (inputFile != '') & (inputRes != '') & (inputEPSG != ''):
  41.  
  42. fileName = re.findall('(.+?)\.[^\.]+$', inputFile)[0]
  43.  
  44. # Create VRT-file for reading the xyz-format
  45. f = open('xyz_dgm_reader.vrt', 'a')
  46. f.write('<OGRVRTDataSource><OGRVRTLayer name="dgm"><SrcDataSource>dgm.csv</SrcDataSource><GeometryType>wkbPoint</GeometryType><LayerSRS>' + inputEPSG + '</LayerSRS><GeometryField encoding="PointFromColumns" x="field_1" y="field_2" z="field_3"/></OGRVRTLayer></OGRVRTDataSource>')
  47. f.close()
  48.  
  49. print('Converting ' + inputFile + ' into TIF-file...')
  50.  
  51. copyfile(inputFile, 'dgm.csv') # Make copy of file and rename for reading in
  52. os.system('gdal_rasterize -a field_3 -tr ' + inputRes + ' ' + inputRes + ' xyz_dgm_reader.vrt ' + fileName + '.tif') # Convert with help of VRT to raster
  53. os.remove('xyz_dgm_reader.vrt') # Remove VRT
  54. os.remove('dgm.csv') # Remove copy
  55.  
  56. else:
  57. print("")
  58. print("Sorry, missing some information!")
  59. sys.exit()
  60.  
  61. if __name__ == "__main__":
  62. main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement