ProfileXYZ.py 21.64 KiB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
# ProfileXYZ.py -- Pamhyr
# Copyright (C) 2023-2024  INRAE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

# -*- coding: utf-8 -*-

import logging
import numpy as np
from typing import List
from functools import reduce

from tools import timer
from shapely import geometry

from Model.Tools.PamhyrDB import SQLSubModel
from Model.Except import ClipboardFormatError
from Model.Geometry.Profile import Profile
from Model.Geometry.PointXYZ import PointXYZ
from Model.Geometry.Vector_1d import Vector1d

logger = logging.getLogger()


class ProfileXYZ(Profile, SQLSubModel):
    _sub_classes = [
        PointXYZ,
    ]

    def __init__(self,
                 id: int = -1,
                 name: str = "",
                 rk: float = 0.,
                 reach=None,
                 num=0,
                 nb_point: int = 0,
                 code1: int = 0, code2: int = 0,
                 status=None):
        """ProfileXYZ constructor

        Args:
            num: The number of this profile
            code1: The interpolation code 1
            code2: The interpolation code 2
            rk: Kilometer point
            name: The name of profile

        Returns:
            Nothing.
        """
        super(ProfileXYZ, self).__init__(
            id=id,
            num=num,
            name=name,
            rk=rk,
            code1=code1, code2=code2,
            _type="XYZ",
            reach=reach,
            status=status,
        )

    @classmethod
    def _db_create(cls, execute):
        execute("""
          CREATE TABLE geometry_profileXYZ(
            id INTEGER NOT NULL PRIMARY KEY,
            ind INTEGER NOT NULL,
            name TEXT,
            reach INTEGER NOT NULL,
            rk REAL NOT NULL,
            num INTEGER NOT NULL,
            code1 INTEGER NOT NULL,
            code2 INTEGER NOT NULL,
            sl INTEGER,
            FOREIGN KEY(reach) REFERENCES river_reach(id),
            FOREIGN KEY(sl) REFERENCES sedimentary_layer(id)
          )
        """)

        return cls._create_submodel(execute)

    @classmethod
    def _db_update(cls, execute, version):
        major, minor, release = version.strip().split(".")
        if major == minor == "0":
            rl = int(release)
            if rl < 2:
                execute(
                    """
                    ALTER TABLE geometry_profileXYZ
                    ADD COLUMN sl INTEGER
                    REFERENCES sedimentary_layer(id)
                    """
                )

            if rl < 11:
                execute(
                    """
                    ALTER TABLE geometry_profileXYZ
                    RENAME COLUMN kp TO rk
                    """
                )

        return cls._update_submodel(execute, version)

    @classmethod
    def _db_load(cls, execute, data=None):
        profiles = []
        status = data["status"]
        reach = data["reach"]

        table = execute(
            "SELECT id, ind, name, rk, num, code1, code2, sl " +
            "FROM geometry_profileXYZ " +
            f"WHERE reach = {reach.id}"
        )

        for row in table:
            id = row[0]
            ind = row[1]
            name = row[2]
            rk = row[3]
            num = row[5]
            code1 = row[5]
            code2 = row[6]
            sl = row[7]

            new = cls(
                id=id, num=num,
                name=name, rk=rk,
                code1=code1, code2=code2,
                reach=reach,
                status=status
            )

            if sl == -1 or sl is None:
                new._sl = None
            else:
                new._sl = next(
                    filter(
                        lambda s: s.id == sl,
                        data["sediment_layers_list"].sediment_layers
                    )
                )

            data["profile"] = new
            new._points = PointXYZ._db_load(execute, data.copy())

            yield ind, new

    def _db_save(self, execute, data=None):
        ok = True
        ind = data["ind"]

        sl = self._sl.id if self._sl is not None else -1

        sql = (
            "INSERT OR REPLACE INTO " +
            "geometry_profileXYZ(id, ind, name, reach, " +
            "rk, num, code1, code2, sl) " +
            "VALUES (" +
            f"{self.id}, {ind}, '{self._db_format(self._name)}', " +
            f"{self.reach.id}, {self.rk}, {self.num}, " +
            f"{self.code1}, {self.code1}, {sl}" +
            ")"
        )
        execute(sql)

        points = self.points

        data["profile"] = self
        execute(f"DELETE FROM geometry_pointXYZ WHERE profile = {self.id}")

        ind = 0
        for point in points:
            data["ind"] = ind
            ok &= point._db_save(execute, data)
            ind += 1

        return ok

    @classmethod
    def from_data(cls, header, data):
        profile = None
        try:
            if len(header) == 0:
                name = data[0]
                rk = data[1]
                reach = data[2]
                status = data[3]

                profile = cls(
                    id=-1,
                    name=name,
                    rk=rk,
                    reach=reach,
                    status=status
                )
            else:
                valid_header = {'name', 'reach', 'rk', 'status'}
                d = {}
                for i, v in enumerate(data):
                    h = header[i].strip().lower().split(' ')[0]
                    if h in valid_header:
                        d[h] = v

                profile = cls(**d)
        except Exception as e:
            logger.error(e)
            raise ClipboardFormatError(header, data)

        return profile

    def point_from_data(self, header, data):
        def float_format(s: str):
            return float(
                s.replace(",", ".")
            )

        point = None
        try:
            if len(header) == 0:
                x = float_format(data[0])
                y = float_format(data[1])
                z = float_format(data[2])
                name = data[3] if len(data) == 4 else ""

                point = PointXYZ(
                    x, y, z, name, profile=self, status=self._status
                )
            else:
                valid_header = {'name', 'x', 'y', 'z'}
                d = {"status": self._status, "profile": self}
                for i, v in enumerate(data):
                    h = header[i].strip().lower().split(' ')[0]
                    if h in valid_header:
                        d[h] = v

                point = PointXYZ(**d)
        except Exception as e:
            raise ClipboardFormatError(header=header, data=data)

        return point

    def display_name(self):
        name = ""
        if self.name != "":
            name += f"{self.name} "

        name += f"({self.rk})"

        return name

    def x(self):
        return [point.x for point in self.points]

    def y(self):
        return [point.y for point in self.points]

    def z(self):
        return [point.z for point in self.points]

    def names(self):
        return [point.name for point in self.points]

    def x_max(self):
        return max(self.filter_isnan(self.x()))

    def x_min(self):
        return min(self.filter_isnan(self.x()))

    def y_max(self):
        return max(self.filter_isnan(self.y()))

    def y_min(self):
        return min(self.filter_isnan(self.y()))

    def z_max(self):
        return max(self.filter_isnan(self.z()))

    def z_min(self):
        return min(self.filter_isnan(self.z()))

    def import_points(self, list_points: list):
        """Import a list of points to profile

        Args:
            list_points: Liste of PointXYZ

        Returns:
            Nothing.
        """
        for point in list_points:
            pt = PointXYZ(*point, profile=self, status=self._status)
            self.points.append(pt)
        self._status.modified()

    def get_point_i(self, index: int) -> PointXYZ:
        """Get point at index.

        Args:
            index: Index of point.

        Returns:
            The point.
        """
        try:
            return self.points[index]
        except IndexError:
            raise IndexError(f"Invalid point index: {index}")

    def get_point_by_name(self, name: str) -> PointXYZ:
        """Get point by name.

        Args:
            name: Point name.

        Returns:
            The point.
        """
        try:
            n_name = name.lower().strip()
            return next(
                filter(
                    lambda p: p.name.lower().strip() == n_name,
                    self.points
                )
            )
        except Exception as e:
            logger.debug(f"{e}")
            raise IndexError(
                f"Invalid point name: {name} " +
                f"for profile ({self.id}) rk = {self.rk}"
            )

    def has_standard_named_points(self):
        l, r = reduce(
            lambda acc, n: (
                (acc[0] | (n == "rg")),
                (acc[1] | (n == "rd"))
            ),
            map(lambda p: p.name.lower().strip(),
                self.points),
            (False, False)
        )

        return l & r

    def add(self):
        """Add a new PointXYZ to profile.

        Returns:
            Nothing.
        """
        point_xyz = PointXYZ(0., 0., 0., profile=self, status=self._status)
        self.points.append(point_xyz)
        self._status.modified()

    def insert(self, index: int):
        """Insert a new point at index.

        Args:
            index: The index of new profile.

        Returns:
            The new point.
        """
        point = PointXYZ(0., 0., 0., profile=self, status=self._status)
        self.points.insert(index, point)
        self._status.modified()
        return point

    def filter_isnan(self, lst):
        """Returns the input list without 'nan' element

        Args:
            lst: The list to filter

        Returns:
            The list without 'nan'
        """
        return [x for x in lst if not np.isnan(x)]

    def speed(self, q, z):
        area = self.wet_area(z)

        if area == 0:
            return 0

        return q / area

    def width_approximation(self):
        if self.has_standard_named_points():
            rg = self.get_point_by_name("rg")
            rd = self.get_point_by_name("rd")
        else:
            rg = self.points[0]
            rd = self.points[-1]

        return abs(rg.dist(rd))

    def wet_width(self, z):
        start, end = self.get_all_water_limits_ac(z)

        if len(start) == 0:
            return 0

        length = 0.0
        for s, e in zip(start, end):
            length += abs(s - e)
        return length

    def wet_perimeter(self, z):
        lines = self.wet_lines(z)

        if lines is None:
            return 0

        length = 0.0
        for line in lines:
            length += line.length
        return length

    def wet_area(self, z):
        lines = self.wet_lines(z)

        if lines is None:
            return 0

        area = 0.0
        for line in lines:
            if len(line.coords) > 2:
                poly = geometry.Polygon(line)
                area += poly.area
        return area

    def wet_radius(self, z):
        p = self.wet_perimeter(z)
        a = self.wet_area(z)

        if p == 0:
            return 0

        return a/p

    def wet_line(self, z):
        points = self.wet_points(z)
        if len(points) < 3:
            return None

        zz = map(lambda p: p.z, points)
        station = self._get_station(points)

        line = geometry.LineString(list(zip(station, zz)))
        return line

    def wet_lines(self, z):
        points = self._points
        if len(points) < 3:
            return None

        lines = []

        zz = list(map(lambda p: p.z, points))
        station = self._get_station(points)

        line = []
        for i in range(self.number_points-1):

            if zz[i] >= z and zz[i+1] < z:
                y = np.interp(
                    z,
                    [zz[i], zz[i+1]],
                    [station[i], station[i+1]]
                )
                line.append([y, z])

            if zz[i] < z:
                line.append([station[i], zz[i]])

            if zz[i] <= z and zz[i+1] >= z:
                y = np.interp(
                    z,
                    [zz[i], zz[i+1]],
                    [station[i], station[i+1]]
                )
                line.append([y, z])
                if len(line) > 2:
                    lines.append(geometry.LineString(line))
                line = []

        if zz[self.number_points-1] < z:
            line.append([station[self.number_points-1], z])
            if len(line) > 2:
                lines.append(geometry.LineString(line))
            line = []

        return lines

    def max_water_depth(self, z):
        return z - self.z_min()

    def mean_water_depth(self, z):
        a = self.wet_area(z)
        w = self.wet_width(z)

        if w == 0:
            return 0

        return a/w

    def wet_polygon(self, z):
        points = self.wet_points(z)
        if len(points) < 3:
            return None

        zz = map(lambda p: p.z, points)
        station = self._get_station(points)

        poly = geometry.Polygon(list(zip(station, zz)))
        return poly

    def wet_points(self, z):
        left, right = self.get_water_limits(z)

        points = list(filter(lambda p: p.z < z, self._points))
        points = [left] + points + [right]
        points = sorted(points, key=lambda p: p.x)

        return points

    def get_nb_wet_areas(self, z):

        n_zones = 0
        points = self._points
        if points[0].z <= z:
            n_zones += 1

        for i in range(self.number_points-1):
            if points[i].z > z and points[i+1].z <= z:
                n_zones += 1

        return n_zones

    def get_all_water_limits_ac(self, z):
        """
        Determine all water limits for z elevation.
        """

        points = self._points
        if len(points) < 3:
            return None

        zz = list(map(lambda p: p.z, points))
        station = self._get_station(points)

        start = []
        if points[0].z <= z:
            start.append(station[0])

        for i in range(self.number_points-1):
            if zz[i] > z and zz[i+1] <= z:
                y = np.interp(
                    z,
                    [zz[i], zz[i+1]],
                    [station[i], station[i+1]]
                )
                start.append(y)

        end = []
        if points[-1].z <= z:
            end.append(station[-1])

        for i in reversed(range(self.number_points-1)):
            if zz[i] <= z and zz[i+1] > z:
                y = np.interp(
                    z,
                    [zz[i], zz[i+1]],
                    [station[i], station[i+1]]
                )
                end.append(y)

        if len(start) != len(end):
            logger.error(f"ERROR in get_all_water_limits_ac")
            return [], []

        return start, list(reversed(end))

    def get_water_limits(self, z):
        """
        Determine left and right limits of water elevation.
        """

        # Get the index of first point with elevation lesser than water
        # elevation (for the right and left river side)
        i_left = -1
        i_right = -1

        for i in range(self.number_points):
            if self.point(i).z <= z:
                i_left = i
                break

        for i in reversed(range(self.number_points)):
            if self.point(i).z <= z:
                i_right = i
                break

        # Interpolate points at river left side
        if (i_left > 0):
            x = np.interp(
                z,
                [self.point(i_left).z, self.point(i_left - 1).z],
                [self.point(i_left).x, self.point(i_left - 1).x]
            )
            y = np.interp(
                z,
                [self.point(i_left).z, self.point(i_left - 1).z],
                [self.point(i_left).y, self.point(i_left - 1).y]
            )
            pt_left = PointXYZ(x, y, z, name="wl_left")
        else:
            pt_left = self.point(0)

        # Interpolate points at river right side
        if (i_right < self.number_points - 1):
            x = np.interp(
                z,
                [self.point(i_right).z, self.point(i_right + 1).z],
                [self.point(i_right).x, self.point(i_right + 1).x]
            )
            y = np.interp(
                z,
                [self.point(i_right).z, self.point(i_right + 1).z],
                [self.point(i_right).y, self.point(i_right + 1).y]
            )
            pt_right = PointXYZ(x, y, z, name="wl_right")
        else:
            pt_right = self.point(self.number_points - 1)

        return pt_left, pt_right

    def get_station(self):
        """Projection of the points of the profile on a plane.

        Args:
            self: The profile

        Returns:
            Projection of the points of the profile on a plane.
        """
        if self.nb_points < 2:
            return [0.0]
        else:
            return self._get_station(self.points)

    @timer
    def _get_station(self, points):
        first_named_point = None
        index_first_named_point = None
        last_named_point = None

        first_point_not_nan = self._first_point_not_nan(points)
        last_point_not_nan = self._last_point_not_nan(points)

        for index, point in enumerate(points):
            if point.point_is_named():
                index_first_named_point = index
                first_named_point = point
                break

        for point in reversed(points):
            if point.point_is_named():
                last_named_point = point
                break

        station = []
        constant = 0.0

        if (first_named_point is not None
                and last_named_point is not None):
            if (first_named_point != last_named_point
                    and first_named_point.x != last_named_point.x):
                vector = Vector1d(first_named_point, last_named_point)
                norm_dir_vec = vector.normalized_direction_vector()
            else:
                vector = Vector1d(first_point_not_nan, last_point_not_nan)
                norm_dir_vec = vector.normalized_direction_vector()

            for point in points:
                xi = point.x - first_named_point.x
                yi = point.y - first_named_point.y
                station_i = (norm_dir_vec[0] * xi +
                             norm_dir_vec[1] * yi)
                station.append(station_i)

            constant = station[index_first_named_point]
        elif first_named_point is None:
            vector = Vector1d(first_point_not_nan, last_point_not_nan)
            norm_dir_vec = vector.normalized_direction_vector()

            for point in points:
                xi = point.x - first_point_not_nan.x
                yi = point.y - first_point_not_nan.y
                station_i = (norm_dir_vec[0] * xi +
                             norm_dir_vec[1] * yi)
                station.append(station_i)

            z_min = self.z_min()
            index_profile_z_min = next(
                filter(
                    lambda z: z[1] == z_min,
                    enumerate(self.z())
                )
            )
            constant = station[index_profile_z_min[0]]

        return list(map(lambda s: s - constant, station))

    def _first_point_not_nan(self, points):
        first_point = None

        for point in points:
            if not point.is_nan():
                first_point = point
                break

        return first_point

    def _last_point_not_nan(self, points):
        last_point = None

        for point in reversed(points):
            if not point.is_nan():
                last_point = point
                break

        return last_point

    def purge(self, np_purge):
        """
        Remove points to keep at most np_purge points.
        """
        if (self.nb_points <= np_purge):
            return

        nb_named = 2   # we consider the first and last point as named
        area = [0.0]

        for i in range(1, self.nb_points-1):
            if self.point(i).point_is_named():
                area.append(9999999.999)
                nb_named += 1
            else:
                area.append(
                    PointXYZ.areatriangle3d(
                        self.point(i-1),
                        self.point(i),
                        self.point(i+1))
                )

        area.append(0.0)

        while self.nb_points > max(np_purge, nb_named):
            to_rm = np.argmin(area[1:self.nb_points - 1]) + 1

            self.delete_i([to_rm])
            area.pop(to_rm)

            for i in [to_rm-1, to_rm]:
                if (i == 0):
                    continue

                if (i == self.nb_points - 1):
                    continue

                if self.point(i).point_is_named():
                    area[i] = 9999999.999
                else:
                    area[i] = PointXYZ.areatriangle3d(
                        self.point(i-1),
                        self.point(i),
                        self.point(i+1)
                    )

    def shift(self, x, y, z):
        for p in self.points:
            p.x = p.x + x
            p.y = p.y + y
            p.z = p.z + z