File size: 1,683 Bytes
843b944
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path

import geopandas as gpd
import pandas as pd
import rasterio


def _is_raster(path: str) -> bool:
    return Path(path).suffix.lower() in {".tif", ".tiff", ".nc", ".asc"}


def get_spatial_metadata(path: str) -> dict:
    if _is_raster(path):
        with rasterio.open(path) as src:
            return {
                "type": "raster",
                "crs": str(src.crs) if src.crs else "",
                "bounds": list(src.bounds),
                "width": src.width,
                "height": src.height,
                "count": src.count,
                "resolution": list(src.res),
                "driver": src.driver,
                "dtypes": list(src.dtypes),
            }

    vector = gpd.read_file(path)
    return {
        "type": "vector",
        "crs": str(vector.crs) if vector.crs else "",
        "feature_count": int(len(vector)),
        "columns": list(vector.columns),
        "geometry_types": sorted(vector.geometry.geom_type.dropna().unique().tolist()),
        "bounds": list(vector.total_bounds),
    }


def get_attribute_table(path: str, n: int = 25) -> pd.DataFrame:
    vector = gpd.read_file(path)
    return pd.DataFrame(vector.drop(columns="geometry").head(n))


def raster_quality_report(path: str) -> pd.DataFrame:
    with rasterio.open(path) as src:
        checks = [
            {"check": "has_crs", "value": bool(src.crs)},
            {"check": "has_bounds", "value": True},
            {"check": "band_count", "value": src.count},
            {"check": "resolution", "value": f"{src.res[0]} x {src.res[1]}"},
            {"check": "driver", "value": src.driver},
        ]
    return pd.DataFrame(checks)