Skip to content
This repository
Browse code

Cleaned up remainder of geoutils.create_geotiff()

  • Loading branch information...
commit fe65a2e31cdc21d05f1085c525789e179ff7d719 1 parent 16ca6ec
authored March 11, 2012 Michal Migurski committed March 12, 2012

Showing 1 changed file with 78 additions and 84 deletions. Show diff stats Hide diff stats

  1. 162  decoder/geoutils.py
162  decoder/geoutils.py
... ...
@@ -1,9 +1,10 @@
  1
+from sys import stderr
1 2
 from tempfile import mkstemp
2 3
 from os import close, unlink
3 4
 from math import hypot
4  
-from subprocess import Popen
  5
+from subprocess import Popen, PIPE
5 6
 
6  
-from osgeo import gdal, osr
  7
+from osgeo import osr
7 8
 
8 9
 try:
9 10
     from PIL import Image
@@ -19,11 +20,13 @@
19 20
 from matrixmath import Point
20 21
 from dimensions import ptpin
21 22
 
  23
+epsg900913 = '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over'
  24
+
22 25
 def calculate_gcps(p2s, paper_width_pt, paper_height_pt, north, west, south, east):
23 26
     """
24 27
     """
25 28
     merc = osr.SpatialReference()
26  
-    merc.ImportFromEPSG(900913)
  29
+    merc.ImportFromProj4(epsg900913)
27 30
     
28 31
     latlon = osr.SpatialReference()
29 32
     latlon.ImportFromEPSG(4326)
@@ -51,13 +54,6 @@ def calculate_gcps(p2s, paper_width_pt, paper_height_pt, north, west, south, eas
51 54
     ll_gcp = (ll_x, ll_y, ll_px.x, ll_px.y)
52 55
     
53 56
     return ul_gcp, ur_gcp, lr_gcp, ll_gcp
54  
-    
55  
-    ul_gcp = gdal.GCP(ul_x, ul_y, ul_z, ul_px.x, ul_px.y)
56  
-    ur_gcp = gdal.GCP(ur_x, ur_y, ur_z, ur_px.x, ur_px.y)
57  
-    lr_gcp = gdal.GCP(lr_x, lr_y, lr_z, lr_px.x, lr_px.y)
58  
-    ll_gcp = gdal.GCP(ll_x, ll_y, ll_z, ll_px.x, ll_px.y)
59  
-    
60  
-    return ul_gcp, ur_gcp, lr_gcp, ll_gcp
61 57
 
62 58
 def calculate_geotransform(gcps, full_width, fuller_width, buffer):
63 59
     """ Return a geotransform tuple that puts the GCPs into a chosen image size.
@@ -103,77 +99,81 @@ def create_geotiff(image, p2s, paper_width_pt, paper_height_pt, north, west, sou
103 99
     #
104 100
     gcps = calculate_gcps(p2s, paper_width_pt, paper_height_pt, north, west, south, east)
105 101
     merc = osr.SpatialReference()
106  
-    merc.ImportFromEPSG(900913)
107  
-    
108  
-    handle, png_filename = mkstemp(dir='.', prefix='geotiff-', suffix='.png')
109  
-    handle, vrt_filename = mkstemp(dir='.', prefix='geotiff-', suffix='.vrt')
110  
-    handle, tif1_filename = mkstemp(dir='.', prefix='geotiff-', suffix='.tif')
111  
-    handle, tif2_filename = mkstemp(dir='.', prefix='geotiff-', suffix='.tif')
112  
-    handle, jpg_filename = mkstemp(dir='.', prefix='geotiff-', suffix='.jpg')
113  
-    
114  
-    image.save(png_filename)
115  
-    
116  
-    translate = 'gdal_translate -of VRT'.split()
117  
-    
118  
-    for (e, n, x, y) in gcps:
119  
-        translate += ('-gcp %(x).1f %(y).1f %(e).1f %(n).1f' % locals()).split()
120  
-    
121  
-    translate += ['-a_srs', 'EPSG:900913']
122  
-    translate += [png_filename, vrt_filename]
123  
-    
124  
-    print ' '.join(translate)
125  
-    
126  
-    translate = Popen(translate)
127  
-    translate.wait()
128  
-    
129  
-    if translate.returncode:
130  
-        raise Exception(translate.returncode)
131  
-    
132  
-    warp = 'gdalwarp -of GTiff -tps -co COMPRESS=JPEG -co JPEG_QUALITY=80'.split()
133  
-    warp += ['-dstnodata', '153 153 153']
134  
-    warp += [vrt_filename, tif1_filename]
135  
-    
136  
-    print ' '.join(warp)
137  
-    
138  
-    warp = Popen(warp)
139  
-    warp.wait()
140  
-    
141  
-    if warp.returncode:
142  
-        raise Exception(warp.returncode)
143  
-    
144  
-    #
145  
-    # Read the raw bytes of the GeoTIFF for return.
146  
-    #
147  
-    geotiff_bytes = open(tif1_filename).read()
148  
-    
149  
-    #
150  
-    # Do another one, smaller this time for the projected JPEG.
151  
-    #
152  
-    xform, img_bounds, img_size = calculate_geotransform(gcps, 760, 960, 100)
153  
-    
154  
-    warp = 'gdalwarp -of GTiff -tps'.split()
155  
-    warp += ('-te %.1f %.1f %.1f %.1f' % img_bounds).split()
156  
-    warp += ('-ts %d %d' % img_size).split()
157  
-    warp += ['-dstnodata', '153 153 153']
158  
-    warp += [vrt_filename, tif2_filename]
159  
-    
160  
-    print ' '.join(warp)
161  
-    
162  
-    warp = Popen(warp)
163  
-    warp.wait()
164  
-    
165  
-    if warp.returncode:
166  
-        raise Exception(warp.returncode)
  102
+    merc.ImportFromProj4(epsg900913)
167 103
     
168  
-    unlink(png_filename)
169  
-    unlink(vrt_filename)
170  
-
171  
-    geojpeg_img = Image.open(tif2_filename)
172  
-    geojpeg_img.save(jpg_filename)
  104
+    handle, png_filename = mkstemp(prefix='geotiff-', suffix='.png')
  105
+    handle, vrt_filename = mkstemp(prefix='geotiff-', suffix='.vrt')
  106
+    handle, tif1_filename = mkstemp(prefix='geotiff-', suffix='.tif')
  107
+    handle, tif2_filename = mkstemp(prefix='geotiff-', suffix='.tif')
  108
+    handle, jpg_filename = mkstemp(prefix='geotiff-', suffix='.jpg')
173 109
     
174  
-    print geojpeg_img
  110
+    try:
  111
+        image.save(png_filename)
  112
+        
  113
+        translate = 'gdal_translate -of VRT'.split()
  114
+        
  115
+        for (e, n, x, y) in gcps:
  116
+            translate += ('-gcp %(x).1f %(y).1f %(e).1f %(n).1f' % locals()).split()
  117
+        
  118
+        translate += ['-a_srs', epsg900913]
  119
+        translate += [png_filename, vrt_filename]
  120
+        
  121
+        print >> stderr, '%', ' '.join(translate)
  122
+        
  123
+        translate = Popen(translate, stdout=PIPE)
  124
+        translate.wait()
  125
+        
  126
+        if translate.returncode:
  127
+            raise Exception(translate.returncode)
  128
+        
  129
+        warp1 = 'gdalwarp -of GTiff -tps -co COMPRESS=JPEG -co JPEG_QUALITY=80'.split()
  130
+        warp1 += ['-dstnodata', '153 153 153']
  131
+        warp1 += [vrt_filename, tif1_filename]
  132
+        
  133
+        print >> stderr, '%', ' '.join(warp1)
  134
+        
  135
+        warp1 = Popen(warp1, stdout=PIPE)
  136
+        warp1.wait()
  137
+        
  138
+        if warp1.returncode:
  139
+            raise Exception(warp1.returncode)
  140
+        
  141
+        #
  142
+        # Read the raw bytes of the GeoTIFF for return.
  143
+        #
  144
+        geotiff_bytes = open(tif1_filename).read()
  145
+        
  146
+        #
  147
+        # Do another one, smaller this time for the projected JPEG.
  148
+        #
  149
+        xform, img_bounds, img_size = calculate_geotransform(gcps, 760, 960, 100)
  150
+        
  151
+        warp2 = 'gdalwarp -of GTiff -tps'.split()
  152
+        warp2 += ('-te %.1f %.1f %.1f %.1f' % img_bounds).split()
  153
+        warp2 += ('-ts %d %d' % img_size).split()
  154
+        warp2 += ['-dstnodata', '153 153 153']
  155
+        warp2 += [vrt_filename, tif2_filename]
  156
+        
  157
+        print >> stderr, '%', ' '.join(warp2)
  158
+        
  159
+        warp2 = Popen(warp2, stdout=PIPE)
  160
+        warp2.wait()
  161
+        
  162
+        if warp2.returncode:
  163
+            raise Exception(warp2.returncode)
  164
+        
  165
+        geojpeg_img = Image.open(tif2_filename)
  166
+        geojpeg_img.save(jpg_filename)
  167
+        
  168
+    except Exception, e:
  169
+        raise
175 170
     
176  
-    exit(1)
  171
+    finally:
  172
+        unlink(png_filename)
  173
+        unlink(vrt_filename)
  174
+        unlink(tif1_filename)
  175
+        unlink(tif2_filename)
  176
+        unlink(jpg_filename)
177 177
     
178 178
     #
179 179
     # Project image bounds to geographic coordinates
@@ -188,12 +188,6 @@ def create_geotiff(image, p2s, paper_width_pt, paper_height_pt, north, west, sou
188 188
     lon2, lat2, z2 = proj.TransformPoint(x2, y2)
189 189
     img_bounds = min(lat1, lat2), min(lon1, lon2), max(lat1, lat2), max(lon1, lon2)
190 190
     
191  
-    #
192  
-    # Close out and return.
193  
-    #
194  
-    unlink(geotiff_filename)
195  
-    unlink(geojpeg_filename)
196  
-
197 191
     return geotiff_bytes, geojpeg_img, img_bounds
198 192
 
199 193
 def list_tiles_for_bounds(image, s2p, paper_width_pt, paper_height_pt, north, west, south, east):

0 notes on commit fe65a2e

Please sign in to comment.
Something went wrong with that request. Please try again.