/usr/bin/gdal2xyz.py is in python-gdal 1.10.1+dfsg-5ubuntu1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | #! /usr/bin/python
###############################################################################
# $Id: gdal2xyz.py 24523 2012-05-31 20:57:05Z rouault $
#
# Project: GDAL
# Purpose: Script to translate GDAL supported raster into XYZ ASCII
# point stream.
# Author: Frank Warmerdam, warmerdam@pobox.com
#
###############################################################################
# Copyright (c) 2002, Frank Warmerdam <warmerdam@pobox.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
###############################################################################
try:
from osgeo import gdal
except ImportError:
import gdal
import sys
try:
import numpy as Numeric
except ImportError:
import Numeric
# =============================================================================
def Usage():
print('Usage: gdal2xyz.py [-skip factor] [-srcwin xoff yoff width height]')
print(' [-band b] [-csv] srcfile [dstfile]')
print('')
sys.exit( 1 )
# =============================================================================
#
# Program mainline.
#
if __name__ == '__main__':
srcwin = None
skip = 1
srcfile = None
dstfile = None
band_nums = []
delim = ' '
gdal.AllRegister()
argv = gdal.GeneralCmdLineProcessor( sys.argv )
if argv is None:
sys.exit( 0 )
# Parse command line arguments.
i = 1
while i < len(argv):
arg = argv[i]
if arg == '-srcwin':
srcwin = (int(argv[i+1]),int(argv[i+2]),
int(argv[i+3]),int(argv[i+4]))
i = i + 4
elif arg == '-skip':
skip = int(argv[i+1])
i = i + 1
elif arg == '-band':
band_nums.append( int(argv[i+1]) )
i = i + 1
elif arg == '-csv':
delim = ','
elif arg[0] == '-':
Usage()
elif srcfile is None:
srcfile = arg
elif dstfile is None:
dstfile = arg
else:
Usage()
i = i + 1
if srcfile is None:
Usage()
if band_nums == []: band_nums = [1]
# Open source file.
srcds = gdal.Open( srcfile )
if srcds is None:
print('Could not open %s.' % srcfile)
sys.exit( 1 )
bands = []
for band_num in band_nums:
band = srcds.GetRasterBand(band_num)
if band is None:
print('Could not get band %d' % band_num)
sys.exit( 1 )
bands.append(band)
gt = srcds.GetGeoTransform()
# Collect information on all the source files.
if srcwin is None:
srcwin = (0,0,srcds.RasterXSize,srcds.RasterYSize)
# Open the output file.
if dstfile is not None:
dst_fh = open(dstfile,'wt')
else:
dst_fh = sys.stdout
band_format = (("%g" + delim) * len(bands)).rstrip(delim) + '\n'
# Setup an appropriate print format.
if abs(gt[0]) < 180 and abs(gt[3]) < 180 \
and abs(srcds.RasterXSize * gt[1]) < 180 \
and abs(srcds.RasterYSize * gt[5]) < 180:
format = '%.10g' + delim + '%.10g' + delim + '%s'
else:
format = '%.3f' + delim + '%.3f' + delim + '%s'
# Loop emitting data.
for y in range(srcwin[1],srcwin[1]+srcwin[3],skip):
data = []
for band in bands:
band_data = band.ReadAsArray( srcwin[0], y, srcwin[2], 1 )
band_data = Numeric.reshape( band_data, (srcwin[2],) )
data.append(band_data)
for x_i in range(0,srcwin[2],skip):
x = x_i + srcwin[0]
geo_x = gt[0] + (x+0.5) * gt[1] + (y+0.5) * gt[2]
geo_y = gt[3] + (x+0.5) * gt[4] + (y+0.5) * gt[5]
x_i_data = []
for i in range(len(bands)):
x_i_data.append(data[i][x_i])
band_str = band_format % tuple(x_i_data)
line = format % (float(geo_x),float(geo_y), band_str)
dst_fh.write( line )
|