29
29
import psycopg2
30
30
import re
31
31
import sys
32
- from StringIO import StringIO
33
- import rasterio
34
- from osgeo import gdal , ogr
35
- from osr import SpatialReference
36
- from rasterio .warp import RESAMPLING
37
- from rasterio .warp import calculate_default_transform , reproject
38
32
import unicodedata
33
+ from StringIO import StringIO
34
+ from osgeo import gdal
35
+ from osgeo .osr import SpatialReference
39
36
from geonode .geoserver .helpers import ogc_server_settings
40
37
import ogr2ogr
41
38
@@ -310,42 +307,38 @@ def gdal_band_subset(infile, bands, dst_filename, dst_format="GTiff"):
310
307
out_ds = None
311
308
312
309
313
- def warp_image (infile , outfile , dst_crs = "EPSG:3857" , dst_driver = 'GTiff' ):
310
+ def warp_image (infile , outfile , dst_crs = 3857 , dst_driver = 'GTiff' ,
311
+ threshold = 0.125 , resampling = gdal .GRA_NearestNeighbour ):
314
312
"""
315
- Use rasterio to warp an image from one projection to another
313
+ Use GDAL to warp an image from one projection to another
316
314
:param infile: Origina raster image
317
315
:param outfile: Warped raster image
318
316
:param dst_crs: Output projection
319
317
:param dst_driver: Output filetype driver
318
+ :param threshold: error threshold
319
+ :param resampling: Resampling method
320
320
:return: None
321
321
"""
322
- with rasterio .drivers (CPL_DEBUG = False ):
323
- with rasterio .open (infile ) as src :
324
- res = None
325
- dst_transform , dst_width , dst_height = calculate_default_transform (
326
- src .crs , dst_crs , src .width , src .height ,
327
- * src .bounds , resolution = res )
328
- out_kwargs = src .meta .copy ()
329
- out_kwargs .update ({
330
- 'crs' : dst_crs ,
331
- 'transform' : dst_transform ,
332
- 'affine' : dst_transform ,
333
- 'width' : dst_width ,
334
- 'height' : dst_height ,
335
- 'driver' : dst_driver
336
- })
337
-
338
- with rasterio .open (outfile , 'w' , ** out_kwargs ) as dst :
339
- for i in range (1 , src .count + 1 ):
340
- reproject (
341
- source = rasterio .band (src , i ),
342
- destination = rasterio .band (dst , i ),
343
- src_transform = src .affine ,
344
- src_crs = src .crs ,
345
- dst_transform = out_kwargs ['transform' ],
346
- dst_crs = out_kwargs ['crs' ],
347
- resampling = RESAMPLING .nearest ,
348
- num_threads = 1 )
322
+ # Open source dataset
323
+ src_ds = gdal .Open (infile )
324
+
325
+ # Define target SRS
326
+ dst_srs = SpatialReference ()
327
+ dst_srs .ImportFromEPSG (dst_crs )
328
+ dst_wkt = dst_srs .ExportToWkt ()
329
+
330
+ tmp_ds = gdal .AutoCreateWarpedVRT (src_ds ,
331
+ None ,
332
+ dst_wkt ,
333
+ resampling ,
334
+ threshold )
335
+
336
+ # Create the final warped raster
337
+ try :
338
+ dst_ds = gdal .GetDriverByName (dst_driver ).CreateCopy (outfile , tmp_ds )
339
+ dst_ds .FlushCache ()
340
+ finally :
341
+ dst_ds = None
349
342
350
343
351
344
def get_html (url = None ):
@@ -366,6 +359,7 @@ def asciier(txt):
366
359
norm_txt = re .sub ('\s+' , ' ' , unicodedata .normalize ('NFD' , txt ))
367
360
ascii_txt = norm_txt .encode ('ascii' , 'ignore' ).decode ('ascii' )
368
361
return ascii_txt
362
+ < << << << HEAD
369
363
370
364
371
365
class MockResponse (object ):
0 commit comments