Skip to content

Commit

Permalink
Merge pull request #112 from OpenDataAnalytics/raster-bbox
Browse files Browse the repository at this point in the history
Raster bbox
  • Loading branch information
dorukozturk committed Jan 12, 2018
2 parents ccd2e58 + 1712ee6 commit cf4afb9
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -40,6 +40,7 @@ htmlcov/
.cache
nosetests.xml
coverage.xml
tests/data/output/

# Translations
*.mo
Expand Down
3 changes: 2 additions & 1 deletion conda_env.yml
Expand Up @@ -18,4 +18,5 @@ dependencies:
- pillow
- gdal
- psycopg2
- geoalchemy2
- geoalchemy2
- rasterio
36 changes: 36 additions & 0 deletions gaia/geo/gdal_functions.py
Expand Up @@ -24,6 +24,9 @@
import gdalconst
import numpy
import gdal
import shapely
import rasterio
import rasterio.features
from gaia import GaiaException
try:
import gdalnumeric
Expand Down Expand Up @@ -445,6 +448,39 @@ def gdal_zonalstats(zones, raster):
return list(gen_zonalstats(zones, raster))


def rasterio_bbox(raster_input):
"""
This function will return bounding box information
for a raster layer.
:param raster_input: raster input filepath
:return: list of coordinates in (xmin, ymin, xmax, ymax) format
"""

footprint = rasterio_footprint(raster_input)
shape = shapely.geometry.Polygon(footprint)
return list(shape.bounds)


def rasterio_footprint(raster_input):
"""
This function will return the footprint of a raster
layer
:param raster_input: raster input filepath
:return: list of coordinates
"""
with rasterio.open(raster_input) as dataset:
# Read the dataset's valid data mask as a ndarray.
mask = dataset.dataset_mask()

# Extract feature shapes and values from the array.
feature = rasterio.features.shapes(mask, transform=dataset.affine)
footprint = next(feature)[0]
shape = shapely.geometry.shape(json.loads(json.dumps(footprint)))
return list(shape.minimum_rotated_rectangle.exterior.coords)


def gen_zonalstats(zones_json, raster):
"""
Generator function that yields the statistics of a raster dataset
Expand Down
9 changes: 9 additions & 0 deletions gaia/geo/geo_inputs.py
Expand Up @@ -37,6 +37,7 @@
from gaia.inputs import GaiaIO, FileIO, UnsupportedFormatException
from gaia.filters import filter_pandas, filter_postgis
from gaia.geo.gdal_functions import gdal_reproject
from gaia.geo.gdal_functions import rasterio_bbox, rasterio_footprint


class VectorMixin(object):
Expand Down Expand Up @@ -254,6 +255,14 @@ def read(self, as_numpy_array=False, as_single_band=True,
else:
return out_data

def get_bbox(self):
"""Return bounding box of the dataset"""
return rasterio_bbox(self.uri)

def get_footprint(self):
"""Return footpring of the dataset"""
return rasterio_footprint(self.uri)


class ProcessIO(GaiaIO):
"""IO for nested GaiaProcess objects"""
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -22,3 +22,4 @@ pillow>=3.1.1
gdal>=2.1.0
psycopg2>=2.6.1
geoalchemy2>=0.2.6
rasterio>=0.36.0
23 changes: 23 additions & 0 deletions tests/cases/test_processors.py
Expand Up @@ -261,6 +261,29 @@ def test_distance(self):
if process:
process.purge()

def test_raster_bbox(self):
raster_io = RasterFileIO(
uri=os.path.join(testfile_path,
'satellite_test_data',
'LC81070352015218LGN00_B5.TIF'))

bbox = raster_io.get_bbox()
self.assertEquals(bbox, [307366.7298195886, 3869968.931078481,
539974.1151507461, 4107482.3190209507])

def test_raster_footprint(self):
raster_io = RasterFileIO(
uri=os.path.join(testfile_path,
'satellite_test_data',
'LC81070352015218LGN00_B5.TIF'))

footprint = raster_io.get_footprint()
self.assertEquals(footprint, [(354404.6484867488, 4107482.3190209507),
(307366.7298195886, 3915415.660631991),
(492936.19648358587, 3869968.931078481),
(539974.1151507461, 4062035.5894674407),
(354404.6484867488, 4107482.3190209507)])

def test_subset_raster(self):
"""
Test SubsetProcess for vector & raster inputs
Expand Down
Binary file modified tests/data/satellite_test_data/LC81070352015218LGN00_B4.TIF
Binary file not shown.
Binary file modified tests/data/satellite_test_data/LC81070352015218LGN00_B5.TIF
Binary file not shown.
Binary file modified tests/data/satellite_test_data/LC81070352015218LGN00_BQA.TIF
Binary file not shown.

0 comments on commit cf4afb9

Please sign in to comment.