Пример #1
0
    def write_bluenotes(cls, feature_list, path, list_of_list=True):
        if not os.path.exists(os.path.dirname(path)):
            raise RuntimeError("the passed path does not exist: %s" % path)

        path = Helper.truncate_too_long(path)

        if not isinstance(feature_list, list):
            raise RuntimeError(
                "the passed parameter as feature_list is not a list: %s" %
                type(feature_list))

        if os.path.splitext(path)[-1] == '.shp':
            path = path[:-4]

        GdalAux()
        # create the data source
        try:
            ds = GdalAux.create_ogr_data_source(
                ogr_format=GdalAux.ogr_formats['ESRI Shapefile'],
                output_path=path)
            lyr = cls._create_ogr_point_lyr_and_fields(ds)

        except RuntimeError as e:
            logger.error("%s" % e)
            return

        if list_of_list:
            if len(feature_list[0]) != len(feature_list[1]):
                raise RuntimeError("invalid input for list of list")
            tmp_list = feature_list
            feature_list = list()
            for i, x in enumerate(tmp_list[0]):
                if len(tmp_list) >= 4:
                    feature_list.append(
                        [x, tmp_list[1][i], tmp_list[2][i], tmp_list[3][i]])
                else:
                    feature_list.append([x, tmp_list[1][i], tmp_list[2][i]])

        for feature in feature_list:
            ft = ogr.Feature(lyr.GetLayerDefn())
            ft.SetField('note', feature[2])
            if len(feature) >= 4:
                ft.SetField('info', feature[3])

            pt = ogr.Geometry(ogr.wkbPoint25D)
            pt.SetPoint(0, feature[0], feature[1])

            try:
                ft.SetGeometry(pt)

            except Exception as e:
                RuntimeError("%s > pt: %s, %s" % (e, feature[0], feature[1]))

            if lyr.CreateFeature(ft) != 0:
                raise RuntimeError("Unable to create feature")
            ft.Destroy()

        return True
Пример #2
0
    def test_create_ogr_data_source(self):
        for ogr_format in GdalAux.ogr_formats.keys():
            output_file = self.tp.output_data_folder().joinpath(
                "ex_gdal_aux%s" % GdalAux.ogr_exts[ogr_format])
            if output_file.exists():
                output_file.unlink()

            output_ds = GdalAux.create_ogr_data_source(
                ogr_format=GdalAux.ogr_formats[ogr_format],
                output_path=str(output_file))
            lyr = output_ds.CreateLayer("test", None, ogr.wkbPoint)
            self.assertIsNotNone(lyr)
            output_ds = None
Пример #3
0
    def write_soundings(cls, feature_list, path):
        """Feature list as list of long, lat, depth"""
        if not os.path.exists(os.path.dirname(path)):
            raise RuntimeError("the passed path does not exist: %s" % path)

        path = Helper.truncate_too_long(path)

        if not isinstance(feature_list, list):
            raise RuntimeError(
                "the passed parameter as feature_list is not a list: %s" %
                type(feature_list))

        if os.path.splitext(path)[-1] == '.shp':
            path = path[:-4]

        GdalAux()
        # create the data source
        try:
            ds = GdalAux.create_ogr_data_source(
                ogr_format=GdalAux.ogr_formats['ESRI Shapefile'],
                output_path=path)
            lyr = cls._create_ogr_point_lyr_and_fields(ds)

        except RuntimeError as e:
            logger.error("%s" % e)
            return

        for feature in feature_list:
            ft = ogr.Feature(lyr.GetLayerDefn())
            ft.SetField('info', "%.1f" % feature[2])

            pt = ogr.Geometry(ogr.wkbPoint25D)
            pt.SetPoint(0, feature[0], feature[1], feature[2])

            try:
                ft.SetGeometry(pt)

            except Exception as e:
                RuntimeError("%s > pt: %s, %s, %s" %
                             (e, feature[0], feature[1], feature[2]))

            if lyr.CreateFeature(ft) != 0:
                raise RuntimeError("Unable to create feature")
            ft.Destroy()

        return True
Пример #4
0
    def generate_output(self, output_folder, output_name):

        logger.debug("do EXIF: %s" % self.do_exif)

        # create ascii file
        self.output_ascii = os.path.join(output_folder, "%s.ascii" % output_name)
        ascii_fod = open(self.output_ascii, 'w')
        ascii_fod.write('Latitude;Longitude;Observed time;Colour;Nature of surface - qualifying terms;'
                        'Nature of surface;Remarks;Source date;Source indication;Images;'
                        'CMECS Substrate Name;CMECS Substrate Code;'
                        'CMECS Co-occurring Element 1 Name;CMECS Co-occurring Element 1 Code;'
                        'CMECS Co-occurring Element 2 Name;CMECS Co-occurring Element 2 Code\n')

        # create output folder
        self.cmecs_output_folder = os.path.join(output_folder, output_name)
        if not os.path.exists(self.cmecs_output_folder):
            os.mkdir(self.cmecs_output_folder)

        # create 'Images' output folder
        self.images_output_folder = os.path.join(self.cmecs_output_folder, "Images")
        if not os.path.exists(self.images_output_folder):
            os.mkdir(self.images_output_folder)

        # create shapefile
        self.output_shp = os.path.join(self.cmecs_output_folder, output_name)
        GdalAux()
        try:
            ds = GdalAux.create_ogr_data_source(ogr_format=GdalAux.ogr_formats['ESRI Shapefile'],
                                                output_path=self.output_shp)
            lyr = self._create_ogr_point_lyr_and_fields(ds)

        except RuntimeError as e:
            logger.error("%s" % e)
            return False

        # populate
        for idx, feature in enumerate(self.sbdare_features):

            # create OGR feature
            ft = ogr.Feature(lyr.GetLayerDefn())

            # retrieve position for ASCII format
            lat = Geodesy.dd2dms(feature.centroid.y)
            lon = Geodesy.dd2dms(feature.centroid.x)
            lat_str = "%02.0f-%02.0f-%05.2f%s" % (abs(lat[0]), lat[1], lat[2], ("N" if (lat[0] > 0) else "S"))
            lon_str = "%03.0f-%02.0f-%05.2f%s" % (abs(lon[0]), lon[1], lon[2], ("E" if (lon[0] > 0) else "W"))
            # print(lat_str, lon_str)

            # retrieve position for shapefile format
            pt = ogr.Geometry(ogr.wkbPoint)
            pt.SetPoint(0, feature.centroid.x, feature.centroid.y)
            try:
                ft.SetGeometry(pt)
            except Exception as e:
                RuntimeError("%s > #%d pt: %s, %s" % (e, idx, feature.centroid.x, feature.centroid.y))

            info = self._retrieve_info(feature=feature, feature_idx=idx)
            info = self._calc_cmecs(info=info, feature_idx=idx)

            # for each SBDARE, write a row in the ASCII file
            ascii_fod.write("%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s\n"
                            % (lat_str, lon_str,
                               info.observed_time, info.colour, info.natqua, info.natsur,
                               info.remrks, info.sordat, info.sorind,
                               info.images, info.c_subn, info.c_subc,
                               info.c_cen1, info.c_cec1, info.c_cen2, info.c_cec2))

            # actually write the feature in the shapefile
            self._write_shape_attributes(ft, info)
            if lyr.CreateFeature(ft) != 0:
                raise RuntimeError("Unable to create feature")
            ft.Destroy()

        # finalize ASCII file
        ascii_fod.close()

        return self._finalize_generate_output(root_folder=output_folder, base_folder=output_name, remove_folder=False)
Пример #5
0
import logging
from pathlib import Path
from osgeo import ogr

from hyo2.abc.lib.testing_paths import TestingPaths
from hyo2.abc.lib.gdal_aux import GdalAux
from hyo2.abc.lib.logging import set_logging

logger = logging.getLogger(__name__)
set_logging(ns_list=["hyo2.abc"])

tp = TestingPaths(root_folder=Path(__file__).parent.parent.parent.resolve())

gdal_version = GdalAux.current_gdal_version()
logger.debug("GDAL version: %s" % gdal_version)

for ogr_format in GdalAux.ogr_formats.keys():
    output_file = tp.output_data_folder().joinpath(
        "ex_gdal_aux%s" % GdalAux.ogr_exts[ogr_format])
    if output_file.exists():
        output_file.unlink()
    logger.debug("file: %s" % output_file)

    output_ds = GdalAux.create_ogr_data_source(
        ogr_format=GdalAux.ogr_formats[ogr_format],
        output_path=str(output_file))
    lyr = output_ds.CreateLayer("test", None, ogr.wkbPoint)
    output_ds = None
Пример #6
0
    def write_tin(cls, feature_list_a, feature_list_b, path, list_of_list=True):
        if not os.path.exists(os.path.dirname(path)):
            raise RuntimeError("the passed path does not exist: %s" % path)

        path = Helper.truncate_too_long(path)

        if not isinstance(feature_list_a, list):
            raise RuntimeError("the passed parameter as feature_list_a is not a list: %s" % type(feature_list_a))

        if not isinstance(feature_list_b, list):
            raise RuntimeError("the passed parameter as feature_list_b is not a list: %s" % type(feature_list_b))

        if os.path.splitext(path)[-1] == '.kml':
            path = path[:-4]

        GdalAux()
        # create the data source
        try:
            ds = GdalAux.create_ogr_data_source(ogr_format=GdalAux.ogr_formats['KML'],
                                                output_path=path)
            lyr = cls._create_ogr_line_lyr_and_fields(ds)

        except RuntimeError as e:
            logger.error("%s" % e)
            return

        if list_of_list:
            if len(feature_list_a[0]) != len(feature_list_a[1]):
                raise RuntimeError("invalid input for list of list")
            if len(feature_list_b[0]) != len(feature_list_b[1]):
                raise RuntimeError("invalid input for list of list")
            if len(feature_list_a) != len(feature_list_b):
                raise RuntimeError("invalid input for list of list")

            tmp_list_a = feature_list_a
            feature_list_a = list()
            for i, x in enumerate(tmp_list_a[0]):
                feature_list_a.append([x, tmp_list_a[1][i]])

            tmp_list_b = feature_list_b
            feature_list_b = list()
            for i, x in enumerate(tmp_list_b[0]):
                feature_list_b.append([x, tmp_list_b[1][i]])

        for i, point in enumerate(feature_list_a):
            ft = ogr.Feature(lyr.GetLayerDefn())
            ft.SetField('note', "tin edge")

            ln = ogr.Geometry(ogr.wkbLineString)
            ln.AddPoint(point[0], point[1])
            ln.AddPoint(feature_list_b[i][0], feature_list_b[i][1])

            try:
                ft.SetGeometry(ln)

            except Exception as e:
                RuntimeError("%s > ln: %s, %s / %s, %s"
                             % (e, point[0], point[1], feature_list_b[i][0], feature_list_b[i][1]))

            if lyr.CreateFeature(ft) != 0:
                raise RuntimeError("Unable to create feature")
            ft.Destroy()

        return True
Пример #7
0
    def export_profiles_metadata(self, project_name, output_folder, ogr_format=GdalAux.ogr_formats['ESRI Shapefile'],
                                 filter_fields=None):
        self.filter_fields = filter_fields
        if self.filter_fields is None:
            self.filter_fields = ExportDbFields()

        GdalAux()
        output = os.path.join(self.export_folder(output_folder=output_folder), project_name)

        # create the data source
        try:
            ds = GdalAux.create_ogr_data_source(ogr_format=ogr_format, output_path=output)
            lyr = self._create_ogr_lyr_and_fields(ds)

        except RuntimeError as e:
            logger.error("%s" % e)
            return

        rows = self.db.list_profiles()
        if rows is None:
            raise RuntimeError("Unable to retrieve profiles. Empty database?")
        if len(rows) == 0:
            raise RuntimeError("Unable to retrieve profiles. Empty database?")

        for row in rows:

            ft = ogr.Feature(lyr.GetLayerDefn())

            if self.filter_fields.fields['pk']:
                ft.SetField('pk', int(row[0]))

            if self.filter_fields.fields['datetime']:
                ft.SetField('datetime', row[1].isoformat())

            if self.filter_fields.fields['sensor']:
                ft.SetField('sensor', Dicts.first_match(Dicts.sensor_types, row[3]))

            if self.filter_fields.fields['probe']:
                ft.SetField('probe', Dicts.first_match(Dicts.probe_types, row[4]))

            if self.filter_fields.fields['path']:
                ft.SetField('path', row[5])

            if self.filter_fields.fields['agency']:
                if row[6]:
                    ft.SetField('agency', row[6])

            if self.filter_fields.fields['survey']:
                if row[7]:
                    ft.SetField('survey', row[7])

            if self.filter_fields.fields['vessel']:
                if row[8]:
                    ft.SetField('vessel', row[8])

            if self.filter_fields.fields['sn']:
                if row[9]:
                    ft.SetField('sn', row[9])

            if self.filter_fields.fields['proc_time']:
                ft.SetField('proc_time', row[10].isoformat())

            if self.filter_fields.fields['proc_info']:
                ft.SetField('proc_info', row[11])

            if self.filter_fields.fields['surveyline']:
                if row[12]:
                    ft.SetField('surveyline', row[12])

            if self.filter_fields.fields['comments']:
                if row[13]:
                    ft.SetField('comments', row[13])

            if self.filter_fields.fields['press_uom']:
                if row[14]:
                    ft.SetField('press_uom', row[14])

            if self.filter_fields.fields['depth_uom']:
                if row[15]:
                    ft.SetField('depth_uom', row[15])

            if self.filter_fields.fields['ss_uom']:
                if row[16]:
                    ft.SetField('ss_uom', row[16])

            if self.filter_fields.fields['temp_uom']:
                if row[17]:
                    ft.SetField('temp_uom', row[17])

            if self.filter_fields.fields['cond_uom']:
                if row[18]:
                    ft.SetField('cond_uom', row[18])

            if self.filter_fields.fields['sal_uom']:
                if row[19]:
                    ft.SetField('sal_uom', row[19])

            if self.filter_fields.fields['ss_at_mind']:
                if row[20]:
                    ft.SetField('ss_at_mind', row[20])

            if self.filter_fields.fields['min_depth']:
                if row[21]:
                    ft.SetField('min_depth', row[21])

            if self.filter_fields.fields['max_depth']:
                if row[22]:
                    ft.SetField('max_depth', row[22])

            if self.filter_fields.fields['max_raw_d']:
                if row[23]:
                    ft.SetField('max_raw_d', row[23])

            pt = ogr.Geometry(ogr.wkbPoint)
            lat = row[2].y
            lon = row[2].x
            if lon > 180.0:  # Go back to negative longitude
                lon -= 360.0
            pt.SetPoint_2D(0, lon, lat)

            if self.filter_fields.fields['POINT_X']:
                ft.SetField('POINT_X', lon)

            if self.filter_fields.fields['POINT_Y']:
                ft.SetField('POINT_Y', lat)

            try:
                ft.SetGeometry(pt)

            except Exception as e:
                RuntimeError("%s > pt: %s, %s" % (e, lon, lat))

            if lyr.CreateFeature(ft) != 0:
                raise RuntimeError("Unable to create feature")
            ft.Destroy()

        ds = None
        return True