Skip to content

Commit

Permalink
Fixes o2r-project#85 o2r-project#113 o2r-project#84 bbox - include test
Browse files Browse the repository at this point in the history
  • Loading branch information
SbastianGarzon committed Mar 5, 2021
1 parent 61259ad commit 5be5b54
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
16 changes: 11 additions & 5 deletions geoextent/lib/handleRaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def checkFileSupported(filepath):
return False


def getBoundingBox(filePath):
def getBoundingBox(filepath):
""" extracts bounding box from raster \n
input "filepath": type string, file path to raster file \n
returns bounding box of the file: type list, length = 4 , type = float, schema = [min(longs), min(lats), max(longs), max(lats)]
Expand All @@ -43,7 +43,7 @@ def getBoundingBox(filePath):
crs_output = hf.WGS84_EPSG_ID
gdal.UseExceptions()

geotiffContent = gdal.Open(filePath)
geotiffContent = gdal.Open(filepath)

# get the existing coordinate system
old_crs = osr.SpatialReference()
Expand All @@ -65,9 +65,15 @@ def getBoundingBox(filePath):
max_y = gt[3]

transform = osr.CoordinateTransformation(old_crs, new_crs)
# get the coordinates in lat long
lat_long_min = transform.TransformPoint(min_x, min_y)
lat_long_max = transform.TransformPoint(max_x, max_y)
try:
# get the coordinates in lat long
lat_long_min = transform.TransformPoint(min_x, min_y)
lat_long_max = transform.TransformPoint(max_x, max_y)
except:
# Assume that coordinates are in EPSG:4236
logger.debug("{}: There is no identifiable coordinate reference system. We will try to use EPSG: 4236 ".format(filepath))
lat_long_min = [min_y, min_x]
lat_long_max = [max_y, max_x]

bbox = [lat_long_min[0], lat_long_min[1], lat_long_max[0], lat_long_max[1]]

Expand Down
24 changes: 24 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os # used to get the location of the testdata
import sys
import tempfile
import urllib.request
import pytest
import geoextent.lib.extent as geoextent
from help_functions_test import create_zip, tolerance
Expand Down Expand Up @@ -169,6 +170,17 @@ def test_zipfile_nested_folders():
assert result["tbox"] == ['2017-04-08', '2020-02-06']


def test_png_file_extract_bbox():
with tempfile.TemporaryDirectory() as tmp:
url = 'https://zenodo.org/record/820562/files/20160100_Hpakan_20151123_PRE.png?download=1'
url2 = 'https://zenodo.org/record/820562/files/20160100_Hpakan_20151123_PRE.pngw?download=1'
urllib.request.urlretrieve(url, os.path.join(tmp, "20160100_Hpakan_20151123_PRE.png"))
urllib.request.urlretrieve(url2, os.path.join(tmp, "20160100_Hpakan_20151123_PRE.pngw"))
result = geoextent.fromFile(os.path.join(tmp, "20160100_Hpakan_20151123_PRE.png"), bbox=True)
assert result["bbox"] == pytest.approx([96.21146, 25.55834, 96.35490, 25.63293], abs=tolerance)
assert result["crs"] == "4326"


@pytest.mark.skip(reason="file format not implemented yet")
def test_netcdf_extract_time():
assert geoextent.fromFile("tests/testdata/nc/ECMWF_ERA-40_subset.nc", tbox=True) == ['2002-07-01', '2002-07-31']
Expand Down Expand Up @@ -199,6 +211,18 @@ def test_gml_extract_bbox_time():
assert result['tbox'] == ['2005-12-31', '2013-11-30']


def test_jpge_2000_extract_bbox():
result = geoextent.fromFile("tests/testdata/jpge2000/MSK_SNWPRB_60m.jp2", bbox=True)
assert result['bbox'] == pytest.approx([-74.09868, 4.434354, -73.10649, 5.425259], abs=tolerance)
assert result['crs'] == "4326"


def test_esri_ascii_extract_bbox():
result = geoextent.fromFile("tests/testdata/asc/Churfirsten_30m.asc", bbox=True)
assert result['bbox'] == pytest.approx([8.85620, 46.93996, 8.85699, 46.94050], abs=tolerance)
assert result['crs'] == "4326"


def test_not_found_file():
result = geoextent.fromFile('tests/testdata/empt.geojson', bbox=True)
assert result is None
Expand Down
5 changes: 4 additions & 1 deletion tests/testdata/data-sources.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
| [Geosoftware2] | wf_100m_klas | geotif | MIT |
| [geoextent] | onePoint | geojson | PDDL |
| [geoextent] | invalid_coordinate | geojson | PDDL |
| [geoextent] | onePoint | geojson | PDDL |
| [Carto BCN / martgnz] | Barcelona districts | geojson | CC-BY 4.0 |
| [Google] | abstractviews_timeprimitive_example | kml | CC-BY 4.0 |
| [Tkrajina] | gpx1.1_with_all_fields.gpx | gpx | Apache License 2.0 |
| [geoextent] | ifgi_to_denkpause.shp | shp | PDDL |
| [geoextent] | wandelroute_maastricht.gpkg | gpkg | PDDL |
| [Copernicus] | MSK_SNWPRB_60m | jp2 | [Copernicus Sentinel Data] |
| [Zenodo](https://zenodo.org/record/4323616) | Churfirsten_30m | asc | CC-BY 4.0 |

[sf]: https://github.com/r-spatial/sf
[geoextent]: https://github.com/o2r-project/geoextent
Expand All @@ -32,3 +33,5 @@
[Carto BCN / martgnz]: https://github.com/martgnz/bcn-geodata/tree/master/districtes
[Google]: https://developers.google.com/kml/documentation/time#gps
[Tkrajina]: https://github.com/tkrajina/gpxpy/blob/dev/test_files/gpx1.1_with_all_fields.gpx
[Copernicus]: https://scihub.copernicus.eu/dhus/odata/v1/Products('e6b0b515-cc16-4e70-94ba-12e80db73d19')/$value
[Copernicus Sentinel Data]: https://sentinel.esa.int/documents/247904/690755/Sentinel_Data_Legal_Notice

0 comments on commit 5be5b54

Please sign in to comment.