Skip to content

Commit 62dcb59

Browse files
author
Matt Bertrand
committed
Use GDAL to reproject raster images, and remove rasterio dependency (only 1 or the other should be used in the same project; rasterio is nicer to use but still missing some required features like writing VRT files).
1 parent 29ceade commit 62dcb59

File tree

2 files changed

+29
-36
lines changed

2 files changed

+29
-36
lines changed

dataqs/helpers.py

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,10 @@
2929
import psycopg2
3030
import re
3131
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
3832
import unicodedata
33+
from StringIO import StringIO
34+
from osgeo import gdal
35+
from osgeo.osr import SpatialReference
3936
from geonode.geoserver.helpers import ogc_server_settings
4037
import ogr2ogr
4138

@@ -310,42 +307,38 @@ def gdal_band_subset(infile, bands, dst_filename, dst_format="GTiff"):
310307
out_ds = None
311308

312309

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):
314312
"""
315-
Use rasterio to warp an image from one projection to another
313+
Use GDAL to warp an image from one projection to another
316314
:param infile: Origina raster image
317315
:param outfile: Warped raster image
318316
:param dst_crs: Output projection
319317
:param dst_driver: Output filetype driver
318+
:param threshold: error threshold
319+
:param resampling: Resampling method
320320
:return: None
321321
"""
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
349342

350343

351344
def get_html(url=None):
@@ -366,6 +359,7 @@ def asciier(txt):
366359
norm_txt = re.sub('\s+', ' ', unicodedata.normalize('NFD', txt))
367360
ascii_txt = norm_txt.encode('ascii', 'ignore').decode('ascii')
368361
return ascii_txt
362+
<<<<<<< HEAD
369363

370364

371365
class MockResponse(object):

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
'shapely',
4242
'pymongo',
4343
'numpy',
44-
'rasterio==0.31.0',
4544
'gdal==1.10'
4645
]
4746
)

0 commit comments

Comments
 (0)