GeoDonkey for QGIS 1.12
This commit is contained in:
commit
20ab32da30
63 files changed
+11157
No files matched your search
@@ -0,0 +1,124 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from typing import List, Optional, Sequence, Tuple
|
||||
|
||||
Sample = Tuple[float, Optional[float]]
|
||||
|
||||
|
||||
def nice_step(rough: float, snap: bool = False) -> float:
|
||||
if not rough or rough <= 0 or not math.isfinite(rough):
|
||||
return 1.0
|
||||
power = math.floor(math.log10(rough))
|
||||
base = 10.0 ** power
|
||||
share = rough / base
|
||||
if snap:
|
||||
for limit, factor in ((1.5, 1.0), (3.0, 2.0), (7.0, 5.0)):
|
||||
if share < limit:
|
||||
return factor * base
|
||||
return 10.0 * base
|
||||
for factor in (1.0, 2.0, 5.0, 10.0):
|
||||
if share <= factor * 1.0000001:
|
||||
return factor * base
|
||||
return 10.0 * base
|
||||
|
||||
|
||||
def ticks(low: float, high: float, target: int = 5) -> List[float]:
|
||||
if not math.isfinite(low) or not math.isfinite(high):
|
||||
return []
|
||||
if high < low:
|
||||
low, high = high, low
|
||||
if high == low:
|
||||
return [low]
|
||||
step = nice_step((high - low) / max(1, target), snap=True)
|
||||
first = math.ceil(low / step) * step
|
||||
out: List[float] = []
|
||||
value = first
|
||||
while value <= high + step * 1e-9 and len(out) < 1000:
|
||||
out.append(value + 0.0 if value else 0.0)
|
||||
value += step
|
||||
return out
|
||||
|
||||
|
||||
def padded(low: float, high: float, fraction: float = 0.08,
|
||||
minimum: float = 1.0) -> Tuple[float, float]:
|
||||
if not math.isfinite(low) or not math.isfinite(high):
|
||||
return (0.0, max(minimum, 1.0))
|
||||
if high < low:
|
||||
low, high = high, low
|
||||
span = high - low
|
||||
if span < minimum:
|
||||
middle = (high + low) / 2.0
|
||||
low, high = middle - minimum / 2.0, middle + minimum / 2.0
|
||||
span = high - low
|
||||
room = span * fraction
|
||||
return (low - room, high + room)
|
||||
|
||||
|
||||
class Scale(object):
|
||||
def __init__(self, low: float, high: float,
|
||||
start: float, end: float) -> None:
|
||||
self.low = float(low)
|
||||
self.high = float(high)
|
||||
self.start = float(start)
|
||||
self.end = float(end)
|
||||
span = self.high - self.low
|
||||
self._span = span if span else 1.0
|
||||
|
||||
def to_pixel(self, value: float) -> float:
|
||||
share = (float(value) - self.low) / self._span
|
||||
return self.start + share * (self.end - self.start)
|
||||
|
||||
def to_value(self, pixel: float) -> float:
|
||||
width = self.end - self.start
|
||||
if not width:
|
||||
return self.low
|
||||
share = (float(pixel) - self.start) / width
|
||||
return self.low + share * self._span
|
||||
|
||||
|
||||
def bounds(samples: Sequence[Sample]) -> Tuple[float, float, float, float]:
|
||||
xs = [float(x) for x, _ in samples if math.isfinite(x)]
|
||||
ys = [float(y) for _, y in samples
|
||||
if y is not None and math.isfinite(float(y))]
|
||||
if not xs:
|
||||
return (0.0, 1.0, 0.0, 1.0)
|
||||
if not ys:
|
||||
return (min(xs), max(xs), 0.0, 1.0)
|
||||
return (min(xs), max(xs), min(ys), max(ys))
|
||||
|
||||
|
||||
def segments(samples: Sequence[Sample]) -> List[List[Sample]]:
|
||||
out: List[List[Sample]] = []
|
||||
current: List[Sample] = []
|
||||
for x, y in samples:
|
||||
if y is None:
|
||||
if len(current) > 0:
|
||||
out.append(current)
|
||||
current = []
|
||||
continue
|
||||
current.append((float(x), float(y)))
|
||||
if current:
|
||||
out.append(current)
|
||||
return [piece for piece in out if piece]
|
||||
|
||||
|
||||
def nearest(samples: Sequence[Sample], x: float) -> int:
|
||||
best, distance = -1, None
|
||||
for index, (value, _) in enumerate(samples):
|
||||
gap = abs(float(value) - float(x))
|
||||
if distance is None or gap < distance:
|
||||
best, distance = index, gap
|
||||
return best
|
||||
|
||||
|
||||
def gain_and_loss(samples: Sequence[Sample]) -> Tuple[float, float]:
|
||||
up = down = 0.0
|
||||
for piece in segments(samples):
|
||||
for (_, a), (_, b) in zip(piece, piece[1:]):
|
||||
change = float(b) - float(a)
|
||||
if change > 0:
|
||||
up += change
|
||||
else:
|
||||
down -= change
|
||||
return (up, down)
|
||||
Reference in new issue
Block a user