1 | #!/usr/bin/env python |
---|
2 | #****************************************************************************** |
---|
3 | # $Id: gcps2vec.py 18194 2009-12-06 20:07:45Z rouault $ |
---|
4 | # |
---|
5 | # Project: GDAL |
---|
6 | # Purpose: Convert GCPs to a point layer. |
---|
7 | # Author: Frank Warmerdam, warmerdam@pobox.com |
---|
8 | # |
---|
9 | #****************************************************************************** |
---|
10 | # Copyright (c) 2005, Frank Warmerdam <warmerdam@pobox.com> |
---|
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 | # |
---|
31 | # $Log$ |
---|
32 | # Revision 1.2 2006/03/21 21:54:00 fwarmerdam |
---|
33 | # fixup headers |
---|
34 | # |
---|
35 | # Revision 1.1 2005/02/07 14:36:12 fwarmerdam |
---|
36 | # New |
---|
37 | # |
---|
38 | # |
---|
39 | |
---|
40 | try: |
---|
41 | from osgeo import gdal |
---|
42 | from osgeo import ogr |
---|
43 | except ImportError: |
---|
44 | import gdal |
---|
45 | import ogr |
---|
46 | |
---|
47 | import sys |
---|
48 | |
---|
49 | def Usage(): |
---|
50 | print('Usage: gcp2vec.py [-of <ogr_drivername>] <raster_file> <vector_file>') |
---|
51 | sys.exit(1) |
---|
52 | |
---|
53 | # ============================================================================= |
---|
54 | # Mainline |
---|
55 | # ============================================================================= |
---|
56 | |
---|
57 | format = 'GML' |
---|
58 | in_file = None |
---|
59 | out_file = None |
---|
60 | pixel_out = 0 |
---|
61 | |
---|
62 | gdal.AllRegister() |
---|
63 | argv = gdal.GeneralCmdLineProcessor( sys.argv ) |
---|
64 | if argv is None: |
---|
65 | sys.exit( 0 ) |
---|
66 | |
---|
67 | # Parse command line arguments. |
---|
68 | i = 1 |
---|
69 | while i < len(argv): |
---|
70 | arg = argv[i] |
---|
71 | |
---|
72 | if arg == '-of': |
---|
73 | format = argv[i] |
---|
74 | |
---|
75 | if arg == '-p': |
---|
76 | pixel_out = 1 |
---|
77 | |
---|
78 | elif in_file is None: |
---|
79 | in_file = argv[i] |
---|
80 | |
---|
81 | elif out_file is None: |
---|
82 | out_file = argv[i] |
---|
83 | |
---|
84 | else: |
---|
85 | Usage() |
---|
86 | |
---|
87 | i = i + 1 |
---|
88 | |
---|
89 | if out_file is None: |
---|
90 | Usage() |
---|
91 | |
---|
92 | # ---------------------------------------------------------------------------- |
---|
93 | # Open input file, and fetch GCPs. |
---|
94 | # ---------------------------------------------------------------------------- |
---|
95 | ds = gdal.Open( in_file ) |
---|
96 | if ds is None: |
---|
97 | print('Unable to open ', filename) |
---|
98 | sys.exit(1) |
---|
99 | |
---|
100 | gcp_srs = ds.GetGCPProjection() |
---|
101 | gcps = ds.GetGCPs() |
---|
102 | |
---|
103 | ds = None |
---|
104 | |
---|
105 | if gcps is None or len(gcps) == 0: |
---|
106 | print('No GCPs on file %s!' % in_file) |
---|
107 | sys.exit(1) |
---|
108 | |
---|
109 | # ---------------------------------------------------------------------------- |
---|
110 | # Create output file, and layer. |
---|
111 | # ---------------------------------------------------------------------------- |
---|
112 | |
---|
113 | drv = ogr.GetDriverByName( format ) |
---|
114 | if drv is None: |
---|
115 | print('No driver named %s available.' % format) |
---|
116 | sys.exit(1) |
---|
117 | |
---|
118 | ds = drv.CreateDataSource( out_file ) |
---|
119 | |
---|
120 | srs = None |
---|
121 | |
---|
122 | layer = ds.CreateLayer( 'gcps', srs, geom_type = ogr.wkbPoint25D ) |
---|
123 | |
---|
124 | if pixel_out == 0: |
---|
125 | fd = ogr.FieldDefn( 'Pixel', ogr.OFTReal ) |
---|
126 | layer.CreateField( fd ) |
---|
127 | |
---|
128 | fd = ogr.FieldDefn( 'Line', ogr.OFTReal ) |
---|
129 | layer.CreateField( fd ) |
---|
130 | else: |
---|
131 | fd = ogr.FieldDefn( 'X', ogr.OFTReal ) |
---|
132 | layer.CreateField( fd ) |
---|
133 | |
---|
134 | fd = ogr.FieldDefn( 'Y', ogr.OFTReal ) |
---|
135 | layer.CreateField( fd ) |
---|
136 | |
---|
137 | fd = ogr.FieldDefn( 'Id', ogr.OFTString ) |
---|
138 | layer.CreateField( fd ) |
---|
139 | |
---|
140 | fd = ogr.FieldDefn( 'Info', ogr.OFTString ) |
---|
141 | layer.CreateField( fd ) |
---|
142 | |
---|
143 | # ---------------------------------------------------------------------------- |
---|
144 | # Write GCPs. |
---|
145 | # ---------------------------------------------------------------------------- |
---|
146 | |
---|
147 | for gcp in gcps: |
---|
148 | |
---|
149 | feat = ogr.Feature( layer.GetLayerDefn() ) |
---|
150 | |
---|
151 | geom = ogr.Geometry( ogr.wkbPoint25D ) |
---|
152 | |
---|
153 | if pixel_out == 0: |
---|
154 | feat.SetField( 'Pixel', gcp.GCPPixel ) |
---|
155 | feat.SetField( 'Line', gcp.GCPLine ) |
---|
156 | geom.SetPoint( 0, gcp.GCPX, gcp.GCPY, gcp.GCPZ ) |
---|
157 | else: |
---|
158 | feat.SetField( 'X', gcp.GCPX ) |
---|
159 | feat.SetField( 'Y', gcp.GCPY ) |
---|
160 | geom.SetPoint( 0, gcp.GCPPixel, gcp.GCPLine ) |
---|
161 | |
---|
162 | feat.SetField( 'Id', gcp.Id ) |
---|
163 | feat.SetField( 'Info', gcp.Info ) |
---|
164 | |
---|
165 | feat.SetGeometryDirectly( geom ) |
---|
166 | layer.CreateFeature( feat ) |
---|
167 | |
---|
168 | feat = None |
---|
169 | ds.Destroy() |
---|
170 | |
---|
171 | |
---|