"""
# Shapefiles, GeoPackage, GeoTIFF, JPEG and GeoJSON

GIS software exchanges points, lines and polygons as shapefiles, GeoPackages or GeoJSON, and rasters as GeoTIFF or
georeferenced JPEG.
`write_shapefile`, `write_geopackage` and `write_geotiff` write a `PointSet`, `Polylines` or 2D `BlockModel` with
its CRS, and `write_geojson` writes a `PointSet` or `Polylines`. The readers return the same container.
"""

# %% [hidden]
import sys
from pathlib import Path

HERE = Path(__file__).parent if "__file__" in globals() else Path.cwd()
sys.path.insert(0, str(HERE.parents[1]))

# %%
import json
import tempfile

import boitata as bt
import matplotlib.pyplot as plt
import numpy as np
from common import ACCENT, HIGHLIGHT, INK, map_axes, save

data = bt.datasets.soil_geochemistry_survey()
samples, boundary, covariates = data["samples"], data["boundary"], data["covariates"]
print(samples)
print(boundary)
print(covariates)

# %% [markdown]
# ## Shapefiles
#
# `write_shapefile` stores the coordinates, the attributes (names of at most 10 characters) and, when the container
# has one, the CRS in a `.prj`. Points and polylines go to separate files. The format wants outer rings clockwise, so
# the counter-clockwise boundary comes back with its vertices in reverse order.

# %%
folder = Path(tempfile.mkdtemp())
bt.write_shapefile(folder / "samples.shp", samples)
bt.write_shapefile(folder / "boundary.shp", boundary)
print(sorted(p.name for p in folder.glob("samples.*")))
points, outline = bt.read_shapefile(folder / "samples.shp"), bt.read_shapefile(folder / "boundary.shp")
print("same points:", np.array_equal(points.coords, samples.coords))
print(
    "same boundary, reversed:",
    np.array_equal(outline.coords, boundary.coords[::-1]),
    "| closed:",
    outline.closed,
)

# %% [markdown]
# A `Polylines` feature is made of parts. A closed part is a ring whose last vertex joins the first, and a ring inside
# another ring of the same feature is a hole. Open parts are lines: the samples lie on east-west survey lines about
# 200 m apart, and each line, joined sample to sample, goes to a line shapefile.

# %%
north = np.round(samples.y, -2)
rows = [samples.coords[north == y][np.argsort(samples.x[north == y]), :2] for y in np.unique(north)]
survey = bt.Polylines(rows, attributes={"NORTHING": np.unique(north)})
bt.write_shapefile(folder / "lines.shp", survey)
lines = bt.read_shapefile(folder / "lines.shp")
print(lines)

# %% [markdown]
# ## GeoPackage
#
# A GeoPackage is a single SQLite file holding many layers, with attribute names of any length and the CRS as an EPSG
# code or WKT. `write_geopackage` adds a layer, or replaces the one of the same name. `read_geopackage` needs `layer=`
# when the file holds more than one. Python's own `sqlite3` reads and writes the tables.

# %%
package = folder / "survey.gpkg"
bt.write_geopackage(package, samples, layer="samples")
bt.write_geopackage(package, boundary, layer="boundary")
bt.write_geopackage(package, survey, layer="lines")
points = bt.read_geopackage(package, layer="samples")
print(points)
print("same points:", np.array_equal(points.coords, samples.coords), "| CRS:", points.crs)
print(
    "same boundary area:", np.allclose(bt.read_geopackage(package, layer="boundary").area(), boundary.area())
)

# %% [markdown]
# ## GeoTIFF
#
# `write_geotiff` writes each column of a 2D grid as a band, nulls as the `nodata` value and the CRS in the
# geokeys, so GIS software opens it as a georeferenced raster. It handles rotated grids, and writes a masked model
# with nodata in its absent cells: here only the cells inside the survey area remain. Bands hold numbers, so the
# lithology names go in as the codes of a `Categories` scheme.

# %%
scheme = bt.Categories.from_values(covariates["LITHOLOGY"])
numeric = bt.BlockModel(
    covariates.origin,
    covariates.size,
    covariates.count,
    attributes={
        "ELEVATION_M": covariates["ELEVATION_M"],
        "MAG_NT": covariates["MAG_NT"],
        "LITHOLOGY": scheme.encode(covariates["LITHOLOGY"]),
    },
)
inside = numeric.filter(covariates["INSIDE"] == 1)
bt.write_geotiff(folder / "covariates.tif", inside)
raster = bt.read_geotiff(folder / "covariates.tif")
print(raster)
print(f"{(folder / 'covariates.tif').stat().st_size / 1e6:.2f} MB")
magnetics = raster["MAG_NT"]
present = np.isfinite(magnetics)
print(
    f"same origin: {raster.origin == covariates.origin} | cells with data: {present.sum()} of {len(magnetics)}"
)
for name, n in zip(scheme.names, np.bincount(raster["LITHOLOGY"][present].astype(int))):
    print(f"{name:>16}: {n} cells")

# %% [markdown]
# The raster comes back as a regular grid: the 2527 cells outside the survey area are null in every band.

# %%
(x0, y0, _), (x1, y1, _) = raster.bounds
fig, ax = plt.subplots(figsize=(7, 5), layout="constrained")
image = ax.imshow(raster.grid("MAG_NT")[0], origin="lower", extent=(x0, x1, y0, y1), cmap="cividis")
for part in outline.parts:
    ring = np.vstack([part, part[:1]])
    ax.plot(ring[:, 0], ring[:, 1], color=INK, lw=1.2)
for part in lines.parts:
    ax.plot(part[:, 0], part[:, 1], color="white", lw=0.6)
fig.colorbar(image, ax=ax, shrink=0.8, label="MAG_NT")
map_axes(ax, "GeoTIFF magnetics, shapefile boundary and lines")
save(fig, "gis")

# %% [markdown]
# ## JPEG
#
# A georeferenced JPEG is an 8-bit image with a `.jgw` world file (pixel size, rotation and the center of the
# top-left pixel) and an `.aux.xml` sidecar holding the CRS. `write_jpeg` takes one column (gray) or three (red,
# green, blue) of whole numbers from 0 to 255 with no nulls, so the magnetics are scaled to that range first. JPEG is
# lossy: even at `quality=100` some levels change. `read_jpeg` names the bands `band_1`, `band_2`, ...

# %%
mag = covariates["MAG_NT"]
gray = bt.BlockModel(
    covariates.origin,
    covariates.size,
    covariates.count,
    attributes={"MAG": np.round(255 * (mag - mag.min()) / (mag.max() - mag.min()))},
    crs="local survey grid",
)
bt.write_jpeg(folder / "magnetics.jpg", gray, quality=100)
photo = bt.read_jpeg(folder / "magnetics.jpg")
print(photo)
print(sorted(p.name for p in folder.glob("magnetics.*")))
error = np.abs(photo["band_1"] - gray["MAG"])
print(
    f"same origin: {photo.origin == gray.origin} | CRS: {photo.crs} | largest change: {error.max():.0f} levels"
)

# %%
fig, (left, right) = plt.subplots(1, 2, figsize=(10, 4.2), layout="constrained")
image = left.imshow(photo.grid("band_1")[0], origin="lower", extent=(x0, x1, y0, y1), cmap="gray")
fig.colorbar(image, ax=left, shrink=0.8, label="Level")
map_axes(left, "JPEG magnetics")
change = np.abs(photo.grid("band_1")[0] - gray.grid("MAG")[0])
image = right.imshow(change, origin="lower", extent=(x0, x1, y0, y1), cmap="cividis")
fig.colorbar(image, ax=right, shrink=0.8, label="Levels")
map_axes(right, "Change after the JPEG round trip")
save(fig, "jpeg")

# %% [markdown]
# ## GeoJSON
#
# `write_geojson` writes a `PointSet` or `Polylines` as a GeoJSON feature collection, the format web maps read.
# RFC 7946 wants longitude and latitude on WGS 84, so coordinates with a CRS are converted with `to_crs` on the way
# out. `read_geojson` reads points, lines and polygons back with their properties as columns, z included.
#
# Twelve collars and a pit outline, in UTM, stand in for a lease a web map would show.

# %%
rng = np.random.default_rng(7)
collars = bt.PointSet(
    np.column_stack(
        [rng.uniform(612e3, 614e3, 12), rng.uniform(7.781e6, 7.783e6, 12), rng.uniform(880, 940, 12)]
    ),
    {"HOLE": [f"DH{i:03d}" for i in range(12)], "DEPTH": np.where(np.arange(12) == 3, np.nan, 150.0)},
    crs="EPSG:31983",
    length_unit="m",
)
pit = np.array([[612.2e3, 7.7812e6], [613.8e3, 7.7812e6], [613.8e3, 7.7828e6], [612.2e3, 7.7828e6]])
pond = np.array([[612.9e3, 7.7818e6], [613.2e3, 7.7818e6], [613.2e3, 7.7821e6], [612.9e3, 7.7821e6]])
road = np.array([[611.8e3, 7.7805e6, 870.0], [612.6e3, 7.7812e6, 885.0], [613.0e3, 7.7820e6, 900.0]])
outlines = bt.Polylines(
    [pit, pond, road],
    features=[0, 0, 1],
    closed=[True, True, False],
    attributes={"NAME": ["pit", "haul road"]},
    crs="EPSG:31983",
)

# %% [markdown]
# Each point and each feature becomes a GeoJSON feature. The pit with its pond is a polygon with a hole, the road a
# line string. Coordinates come out in degrees, heights in meters as they were; the null depth is JSON `null`.

# %%
bt.write_geojson(folder / "collars.geojson", collars)
bt.write_geojson(folder / "outlines.geojson", outlines)
first = json.loads((folder / "collars.geojson").read_text())["features"][3]
print(json.dumps(first))
print([f["geometry"]["type"] for f in json.loads((folder / "outlines.geojson").read_text())["features"]])

# %% [markdown]
# A file without a `crs` member is in EPSG:4326. `to_crs` takes it back to UTM, within a fraction of a millimeter.

# %%
points = bt.read_geojson(folder / "collars.geojson")
print(points.crs, points.column_names)
back = points.to_crs("EPSG:31983")
print(f"largest difference {np.abs(back.coords - collars.coords).max() * 1e3:.4f} mm")
lines = bt.read_geojson(folder / "outlines.geojson")
print(lines.crs, lines.column_names, lines.closed)

# %%
fig, ax = plt.subplots(figsize=(6, 5), layout="constrained")
for part, closed in zip(lines.parts, lines.closed):
    ring = np.vstack([part, part[:1]]) if closed else part
    ax.plot(ring[:, 0], ring[:, 1], color=INK if closed else HIGHLIGHT, lw=1.2)
ax.scatter(points.x, points.y, s=14, color=ACCENT)
ax.set_aspect(1 / np.cos(np.radians(points.y.mean())))
ax.locator_params(axis="x", nbins=4)
ax.set_xlabel("Longitude (°)")
ax.set_ylabel("Latitude (°)")
ax.set_title("Collars, pit and haul road read back from GeoJSON")
save(fig, "geojson")
plt.show()
