Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ Example Usage: Other functions
NDV, xsize, ysize, GeoT, Projection, DataType = gr.get_geo_info(raster)

# Load raster
data = load_tiff(raster)
data = gr.load_tiff(raster)

# Find location of point (x,y) on raster, e.g. to extract info at that location
col, row = gr.map_pixel(x,y,GeoT[1],GeoT[-1], GeoT[0],GeoT[3])
Expand All @@ -150,15 +150,15 @@ Example Usage: Other functions
gr.aggregate(data,NDV,(10,10))

# Align two rasters
data2 = load_tiff(raster2)
data2 = gr.load_tiff(raster2)
(alignedraster_o, alignedraster_a, GeoT_a) = gr.align_rasters(raster, raster2, how=np.mean)

# Create GeoRaster
A=gr.GeoRaster(data, GeoT, nodata_value=NDV)

# Load another raster
NDV, xsize, ysize, GeoT, Projection, DataType = gr.get_geo_info(raster2)
data = load_tiff(raster2)
data = gr.load_tiff(raster2)
B=gr.GeoRaster(data2, GeoT, nodata_value=NDV)

# Plot Raster
Expand Down
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ Example Usage: Other functions
NDV, xsize, ysize, GeoT, Projection, DataType = gr.get_geo_info(raster)

# Load raster
data = load_tiff(raster)
data = gr.load_tiff(raster)

# Find location of point (x,y) on raster, e.g. to extract info at that location
col, row = gr.map_pixel(x,y,GeoT[1],GeoT[-1], GeoT[0],GeoT[3])
Expand All @@ -183,15 +183,15 @@ Example Usage: Other functions
gr.aggregate(data,NDV,(10,10))

# Align two rasters
data2 = load_tiff(raster2)
data2 = gr.load_tiff(raster2)
(alignedraster_o, alignedraster_a, GeoT_a) = gr.align_rasters(raster, raster2, how=np.mean)

# Create GeoRaster
A=gr.GeoRaster(data, GeoT, nodata_value=NDV)

# Load another raster
NDV, xsize, ysize, GeoT, Projection, DataType = gr.get_geo_info(raster2)
data = load_tiff(raster2)
data = gr.load_tiff(raster2)
B=gr.GeoRaster(data2, GeoT, nodata_value=NDV)

# Plot Raster
Expand Down
23 changes: 15 additions & 8 deletions georasters/georasters.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,25 @@ def aggregate(raster, ndv, block_size):
return raster2

# Function to write a new file.
def create_geotiff(name, Array, driver, ndv, xsize, ysize, geot, projection, datatype, band=1):
def create_geotiff(name, Array, driver, ndv, xsize, ysize, geot, projection, datatype, band=1, **kwargs):
'''
Creates new geotiff from array
Creates new geotiff from array.
'''
if isinstance(datatype, np.int) == False:
if datatype.startswith('gdal.GDT_') == False:
datatype = eval('gdal.GDT_'+datatype)
newfilename = name+'.tif'
# Set nans to the original No Data Value
Array[np.isnan(Array)] = ndv
if len(Array[np.isnan(Array)]) > 0:
Array[np.isnan(Array)] = ndv
# Set up the dataset
DataSet = driver.Create(newfilename, xsize, ysize, 1, datatype)
DataSet = driver.Create(newfilename, xsize, ysize, 1, datatype, **kwargs)
# the '1' is for band 1.
DataSet.SetGeoTransform(geot)
DataSet.SetProjection(projection.ExportToWkt())
# Write the array
DataSet.GetRasterBand(band).WriteArray(Array)
DataSet.GetRasterBand(band).SetNoDataValue(ndv)
DataSet.GetRasterBand(band).SetNoDataValue(np.float64(ndv))

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't forcing a np.float64(ndv) force any dataset to become float, even the ones that are integers and so increasing file size?

return newfilename

# Function to aggregate and align rasters
Expand Down Expand Up @@ -455,7 +456,7 @@ def copy(self):
return GeoRaster(self.raster.copy(), self.geot, nodata_value=self.nodata_value,
projection=self.projection, datatype=self.datatype)

def to_tiff(self, filename):
def to_tiff(self, filename, **kwargs):
'''
geo.to_tiff(filename)

Expand All @@ -464,6 +465,10 @@ def to_tiff(self, filename):
If GeoRaster does not have datatype, then it tries to assign a type.
You can assign the type yourself by setting
geo.datatype = 'gdal.GDT_'+type

kwargs are passed to GeoTiff driver, and may include GDAL options like compression:

ds.to_tiff(filename, options=['COMPRESS=LZMA'])
'''
if self.datatype is None:
self.datatype = gdal_array.NumericTypeCodeToGDALTypeCode(self.raster.data.dtype)
Expand All @@ -474,9 +479,11 @@ def to_tiff(self, filename):
else:
self.raster = self.raster.astype(np.float64)
self.datatype = gdal_array.NumericTypeCodeToGDALTypeCode(self.raster.data.dtype)
self.raster.data[self.raster.mask] = self.nodata_value

if len(self.raster.data[self.raster.mask]) > 0:
self.raster.data[self.raster.mask] = self.nodata_value
create_geotiff(filename, self.raster, gdal.GetDriverByName('GTiff'), self.nodata_value,
self.shape[1], self.shape[0], self.geot, self.projection, self.datatype)
self.shape[1], self.shape[0], self.geot, self.projection, self.datatype, **kwargs)

def to_pandas(self):
"""
Expand Down