1 | #!/usr/bin/env python |
---|
2 | #****************************************************************************** |
---|
3 | # $Id: gcps2wld.py 18194 2009-12-06 20:07:45Z rouault $ |
---|
4 | # |
---|
5 | # Name: gcps2wld |
---|
6 | # Project: GDAL Python Interface |
---|
7 | # Purpose: Translate the set of GCPs on a file into first order approximation |
---|
8 | # in world file format. |
---|
9 | # Author: Frank Warmerdam, warmerdam@pobox.com |
---|
10 | # |
---|
11 | #****************************************************************************** |
---|
12 | # Copyright (c) 2002, Frank Warmerdam |
---|
13 | # |
---|
14 | # Permission is hereby granted, free of charge, to any person obtaining a |
---|
15 | # copy of this software and associated documentation files (the "Software"), |
---|
16 | # to deal in the Software without restriction, including without limitation |
---|
17 | # the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
18 | # and/or sell copies of the Software, and to permit persons to whom the |
---|
19 | # Software is furnished to do so, subject to the following conditions: |
---|
20 | # |
---|
21 | # The above copyright notice and this permission notice shall be included |
---|
22 | # in all copies or substantial portions of the Software. |
---|
23 | # |
---|
24 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
---|
25 | # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
26 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
---|
27 | # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
28 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
29 | # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
30 | # DEALINGS IN THE SOFTWARE. |
---|
31 | #****************************************************************************** |
---|
32 | |
---|
33 | try: |
---|
34 | from osgeo import gdal |
---|
35 | except ImportError: |
---|
36 | import gdal |
---|
37 | |
---|
38 | import sys |
---|
39 | import os.path |
---|
40 | |
---|
41 | if len(sys.argv) < 2: |
---|
42 | print("Usage: gcps2wld.py source_file") |
---|
43 | sys.exit(1) |
---|
44 | |
---|
45 | |
---|
46 | |
---|
47 | filename = sys.argv[1] |
---|
48 | dataset = gdal.Open( filename ) |
---|
49 | if dataset is None: |
---|
50 | print('Unable to open ', filename) |
---|
51 | sys.exit(1) |
---|
52 | |
---|
53 | gcps = dataset.GetGCPs() |
---|
54 | |
---|
55 | if gcps is None or len(gcps) == 0: |
---|
56 | print('No GCPs found on file ' + filename) |
---|
57 | sys.exit(1) |
---|
58 | |
---|
59 | geotransform = gdal.GCPsToGeoTransform( gcps ) |
---|
60 | |
---|
61 | if geotransform is None: |
---|
62 | print('Unable to extract a geotransform.') |
---|
63 | sys.exit( 1 ) |
---|
64 | |
---|
65 | print(geotransform[1]) |
---|
66 | print(geotransform[4]) |
---|
67 | print(geotransform[2]) |
---|
68 | print(geotransform[5]) |
---|
69 | print(geotransform[0] + 0.5 * geotransform[1] + 0.5 * geotransform[2]) |
---|
70 | print(geotransform[3] + 0.5 * geotransform[4] + 0.5 * geotransform[5]) |
---|
71 | |
---|
72 | |
---|