Skip to content

Commit

Permalink
adopted absolute import for local packages
Browse files Browse the repository at this point in the history
  • Loading branch information
giumas committed May 4, 2024
1 parent 5d6aa1a commit 610d449
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 123 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/bag_on_linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ jobs:
- name: Install dependencies
run: |
conda config --add channels conda-forge
conda install appdirs cartopy gdal matplotlib-base numpy psutil pyproj qt-material hyo2.abc2
conda install appdirs cartopy gdal matplotlib-base numpy psutil pyproj qt-material
pip install PySide6
sudo apt-get install -y libegl1
pip install hyo2.abc2
pip install --no-deps .
- name: Lint with flake8
run: |
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/bag_on_windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ jobs:
- name: Install dependencies
run: |
conda config --add channels conda-forge
conda install appdirs cartopy gdal matplotlib-base numpy psutil pyproj qt-material hyo2.abc2
conda install appdirs cartopy gdal matplotlib-base numpy psutil pyproj qt-material
conda install h5py dateutil lxml
pip install PySide6
pip install hyo2.abc2
pip install --no-deps .
- name: Lint with flake8
run: |
Expand Down
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ HydrOffice BAG Tools
:target: https://www.hydroffice.org/manuals/bag/index.html
:alt: Latest Documentation

.. image:: https://api.codacy.com/project/badge/Grade/c7551d8f90ba4b0086c7b8dc81376260
:target: https://www.codacy.com/app/hydroffice/hyo2_bag/dashboard
.. image:: https://app.codacy.com/project/badge/Grade/b39ce4141d3e412fa789c7d5ee4971f3
:target: https://app.codacy.com/gh/hydroffice/hyo2_bag/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade
:alt: codacy

.. image:: https://coveralls.io/repos/github/hydroffice/hyo2_bag/badge.svg?branch=master
Expand Down
2 changes: 1 addition & 1 deletion hyo2/bag/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

name = 'BAG'
__version__ = '1.2.2'
__version__ = '1.2.3'
__author__ = 'gmasetti@ccom.unh.edu'
__license__ = 'LGPLv3 license'
__copyright__ = 'Copyright (c) 2024, University of New Hampshire, Center for Coastal and Ocean Mapping'
10 changes: 5 additions & 5 deletions hyo2/bag/bag.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __init__(self, name, mode='r', driver=None,
self._str = None

@classmethod
def is_bag(cls, bag_path:str, advanced: bool = False) -> bool:
def is_bag(cls, bag_path: str, advanced: bool = False) -> bool:
if not advanced:
return os.path.splitext(bag_path)[-1].lower() == ".bag"

Expand Down Expand Up @@ -154,7 +154,7 @@ def elevation_min_max(self) -> Tuple[float, float]:
# logger.debug('shape: %s, %s' % (rows, cols))

mem_row = cols * 32 / 1024 / 1024
mem = mem_row * rows
# mem = mem_row * rows
# logger.debug('estimated memory: %.1f MB' % mem)
chunk_size = 8096
chunk_rows = int(chunk_size / mem_row) + 1
Expand Down Expand Up @@ -190,7 +190,7 @@ def vr_refinements_shape(self):
return self[BAGFile._bag_varres_refinements].shape

def vr_elevation_min_max(self) -> Tuple[float, float]:
rows, cols = self.vr_refinements_shape()
# rows, cols = self.vr_refinements_shape()
# logger.debug('refinements shape: %s, %s' % (rows, cols))

vr_el = self[BAGFile._bag_varres_refinements][0]['depth']
Expand Down Expand Up @@ -253,7 +253,7 @@ def uncertainty_min_max(self) -> Tuple[float, float]:
# logger.debug('shape: %s, %s' % (rows, cols))

mem_row = cols * 32 / 1024 / 1024
mem = mem_row * rows
# mem = mem_row * rows
# logger.debug('estimated memory: %.1f MB' % mem)
chunk_size = 8096
chunk_rows = int(chunk_size / mem_row) + 1
Expand Down Expand Up @@ -282,7 +282,7 @@ def uncertainty_min_max(self) -> Tuple[float, float]:
return unc_min, unc_max

def vr_uncertainty_min_max(self) -> Tuple[float, float]:
rows, cols = self.vr_refinements_shape()
# rows, cols = self.vr_refinements_shape()
# logger.debug('shape: %s, %s' % (rows, cols))

vr_unc = self[BAGFile._bag_varres_refinements][0]['depth_uncrt']
Expand Down
28 changes: 7 additions & 21 deletions hyo2/bag/base.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
import os
import sys
import logging

log = logging.getLogger(__name__)

import h5py
from h5py._hl.base import with_phil

from .helper import BAGError


PY3 = sys.version_info[0] == 3
if PY3:
def u(s):
return s
else:
def u(s):
return unicode(s.replace(r'\\', r'\\\\'), "unicode_escape")
logger = logging.getLogger(__name__)


def is_bag(file_name):
Expand Down Expand Up @@ -69,33 +57,31 @@ def __init__(self, name, mode: str = "r", driver=None,

def close(self):
""" Close the file. All open objects become invalid """
log.debug("closing")
logger.debug("closing")
super(File, self).close()

def flush(self):
""" Tell the BAG library to flush its buffers. """
log.debug("flushing")
logger.debug("flushing")
super(File, self).flush()

@with_phil
def __repr__(self):
if not self.id:
log.info("closed file")
r = u('<BAG file>\n')
logger.info("closed file")
r = '<BAG file>\n'
r += " <status: closed>"
else:
# Filename has to be forced to Unicode if it comes back bytes
# Mode is always a "native" string
filename = self.filename
if isinstance(filename, bytes): # Can't decode fname
filename = filename.decode('utf8', 'replace')
r = u('<BAG file "%s" (mode %s)>') % (os.path.basename(filename), self.mode)
r = '<BAG file "%s" (mode %s)>' % (os.path.basename(filename), self.mode)
r += " <status: open>\n"
r += " <id: %s>\n" % self.id
r += " <name: %s>\n" % self.name
r += " <driver: %s>\n" % self.driver
r += " <user block size: %s>\n" % self.userblock_size

if PY3:
return r
return r.encode('utf8')
return r
17 changes: 8 additions & 9 deletions hyo2/bag/bbox.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import os
import logging

log = logging.getLogger(__name__)

from osgeo import ogr, osr
from .meta import Meta
from .helper import BAGError, Helper
from . import __version__
from hyo2.bag.meta import Meta
from hyo2.bag.helper import BAGError, Helper
from hyo2.bag import __version__


logger = logging.getLogger(__name__)
ogr.UseExceptions()


Expand All @@ -20,16 +20,15 @@ class Bbox2Gdal(object):
'shp': ["ESRI Shapefile", "bag.shp"],
}

def __init__(self, bag_meta, fmt="kml", title=None, out_file=None):
assert isinstance(bag_meta, Meta)
def __init__(self, bag_meta: Meta, fmt="kml", title=None, out_file=None):
self.bag_meta = bag_meta
if not self.bag_meta.valid_bbox():
raise BAGError("invalid bbox read in BAG metadata")

self.title = title
if self.title is None:
self.title = "Metadata"
log.debug("title: %s" % self.title)
logger.debug("title: %s" % self.title)

# get the ogr driver
self.drv = ogr.GetDriverByName(self.formats[fmt][0])
Expand All @@ -40,7 +39,7 @@ def __init__(self, bag_meta, fmt="kml", title=None, out_file=None):
self.out_file = out_file
if self.out_file is None:
self.out_file = os.path.abspath(self.formats[fmt][1])
log.debug("output: %s" % self.out_file)
logger.debug("output: %s" % self.out_file)

if os.path.exists(self.out_file):
os.remove(self.out_file)
Expand Down
28 changes: 12 additions & 16 deletions hyo2/bag/density.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import os
import logging

log = logging.getLogger(__name__)

import numpy as np

from osgeo import gdal, osr
from .meta import Meta
from .helper import BAGError, Helper
from . import __version__
from .helper import BAGError
from .bag import BAGFile


logger = logging.getLogger(__name__)
gdal.UseExceptions()


Expand All @@ -22,29 +20,27 @@ class Density2Gdal(object):
'xyz': [b"XYZ", "bag.leidos.density.xyz"],
}

def __init__(self, bag_density, bag_meta, fmt="geotiff", out_file=None, epsg=None):
def __init__(self, bag_density: np.ndarray, bag_meta: Meta, fmt="geotiff", out_file=None, epsg=None):
"""Export the elevation layer in one of the listed formats"""
assert isinstance(bag_density, np.ndarray)
assert isinstance(bag_meta, Meta)
self.bag_den = bag_density
self.bag_meta = bag_meta

# get the IN-MEMORY ogr driver
self.mem = gdal.GetDriverByName(b"MEM")
if self.mem is None:
raise BAGError("%s driver not available.\n" % self.formats[fmt][0])
log.debug("format: %s" % fmt)
logger.debug("format: %s" % fmt)

# set the output file
self.out_file = out_file
if self.out_file is None:
self.out_file = os.path.abspath(self.formats[fmt][1])
log.debug("output: %s" % self.out_file)
logger.debug("output: %s" % self.out_file)

if os.path.exists(self.out_file):
os.remove(self.out_file)

log.debug("dtype: %s" % self.bag_den.dtype)
logger.debug("dtype: %s" % self.bag_den.dtype)
self.rst = self.mem.Create(utf8_path=self.out_file, xsize=self.bag_meta.cols, ysize=self.bag_meta.rows,
bands=1, eType=gdal.GDT_Float32)
# GDAL geo-transform refers to the top left corner of the top left pixel of the raster.
Expand All @@ -58,7 +54,7 @@ def __init__(self, bag_density, bag_meta, fmt="geotiff", out_file=None, epsg=Non
if self.bag_meta.wkt_srs is not None:
self.srs.ImportFromWkt(self.bag_meta.wkt_srs)
else:
log.warning("unable to recover valid spatial reference info")
logger.warning("unable to recover valid spatial reference info")
self.rst.SetProjection(self.srs.ExportToWkt())
self.bnd.FlushCache()

Expand All @@ -68,8 +64,8 @@ def __init__(self, bag_density, bag_meta, fmt="geotiff", out_file=None, epsg=Non
# check if re-projection is required
if not epsg:
# if not, we just create a copy in the selected format
dst_ds = self.drv.CreateCopy(self.out_file, self.rst)
dst_ds = None
_ = self.drv.CreateCopy(self.out_file, self.rst)
_ = None
self.rst = None
return

Expand All @@ -87,6 +83,6 @@ def __init__(self, bag_density, bag_meta, fmt="geotiff", out_file=None, epsg=Non
0.125 # error threshold --> use same value as in gdalwarp
)
# Create the final warped raster
dst_ds = self.drv.CreateCopy(self.out_file, tmp_ds)
dst_ds = None
_ = self.drv.CreateCopy(self.out_file, tmp_ds)
_ = None
self.rst = None
33 changes: 15 additions & 18 deletions hyo2/bag/elevation.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import os
import logging

log = logging.getLogger(__name__)

import numpy as np

from osgeo import gdal, osr
from .meta import Meta
from .helper import BAGError
from .bag import BAGFile

from hyo2.bag.meta import Meta
from hyo2.bag.helper import BAGError
from hyo2.bag.bag import BAGFile


logger = logging.getLogger(__name__)
gdal.UseExceptions()


Expand All @@ -21,29 +21,27 @@ class Elevation2Gdal:
'xyz': ["XYZ", "bag.elevation.xyz"],
}

def __init__(self, bag_elevation, bag_meta, fmt="geotiff", out_file=None, epsg=None):
def __init__(self, bag_elevation: np.ndarray, bag_meta: Meta, fmt="geotiff", out_file=None, epsg=None):
"""Export the elevation layer in one of the listed formats"""
assert isinstance(bag_elevation, np.ndarray)
assert isinstance(bag_meta, Meta)
self.bag_elv = bag_elevation
self.bag_meta = bag_meta

# get the IN-MEMORY ogr driver
self.mem = gdal.GetDriverByName("MEM")
if self.mem is None:
raise BAGError("%s driver not available.\n" % self.formats[fmt][0])
log.debug("format: %s" % fmt)
logger.debug("format: %s" % fmt)

# set the output file
self.out_file = out_file
if self.out_file is None:
self.out_file = os.path.abspath(self.formats[fmt][1])
log.debug("output: %s" % self.out_file)
logger.debug("output: %s" % self.out_file)

if os.path.exists(self.out_file):
os.remove(self.out_file)

log.debug("dtype: %s" % self.bag_elv.dtype)
logger.debug("dtype: %s" % self.bag_elv.dtype)
self.rst = self.mem.Create(utf8_path=self.out_file, xsize=self.bag_meta.cols, ysize=self.bag_meta.rows,
bands=1, eType=gdal.GDT_Float32)
# GDAL geo-transform refers to the top left corner of the top left pixel of the raster.
Expand All @@ -57,7 +55,7 @@ def __init__(self, bag_elevation, bag_meta, fmt="geotiff", out_file=None, epsg=N
if self.bag_meta.wkt_srs is not None:
self.srs.ImportFromWkt(self.bag_meta.wkt_srs)
else:
log.warning("unable to recover valid spatial reference info")
logger.warning("unable to recover valid spatial reference info")
self.rst.SetProjection(self.srs.ExportToWkt())
self.bnd.FlushCache()

Expand All @@ -67,8 +65,8 @@ def __init__(self, bag_elevation, bag_meta, fmt="geotiff", out_file=None, epsg=N
# check if re-projection is required
if not epsg:
# if not, we just create a copy in the selected format
dst_ds = self.drv.CreateCopy(self.out_file, self.rst)
dst_ds = None
_ = self.drv.CreateCopy(self.out_file, self.rst)
_ = None
self.rst = None
return

Expand All @@ -86,7 +84,6 @@ def __init__(self, bag_elevation, bag_meta, fmt="geotiff", out_file=None, epsg=N
0.125 # error threshold --> use same value as in gdalwarp
)
# Create the final warped raster
dst_ds = self.drv.CreateCopy(self.out_file, tmp_ds)
dst_ds = None
_ = self.drv.CreateCopy(self.out_file, tmp_ds)
_ = None
self.rst = None

Loading

0 comments on commit 610d449

Please sign in to comment.