File size: 1,931 Bytes
b20834a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
import re
import os

# Assuming __file__ contains the absolute path to the current file
current_dir = os.path.dirname(__file__)

shapefile_path = os.path.join(current_dir, "soil/30169_L93/30169_L93.shp")
stu_path = os.path.join(current_dir,'soil/stu.csv')
smu_path = os.path.join(current_dir,'soil/smu.csv')
stuorg_path = os.path.join(current_dir,'soil/stuorg.csv')
smu_stu_path = os.path.join(current_dir,'soil/attricod.txt')

def read_soil_types(file_path):
    soil_types = {}
    with open(file_path, 'r') as file:
        for line in file:
            # Using regular expression to match short name and full name
            match = re.match(r'^\s*([A-Za-z]+)\s+(.*)\s*$', line)
            if match:
                short_name, full_name = match.groups()
                soil_types[short_name] = full_name.strip()
    return soil_types

def get_full_soil_names(short_names, soil_types):
    full_names = []
    for short_name in short_names:
        full_name = soil_types.get(short_name)
        if full_name:
            full_names.append(full_name)
    return full_names

def get_soil_types(lat, lon):
    'SOIL Full 1974 (modified CEC 1985) FAO-Unesco legend soil name.'
    gdf = gpd.read_file(shapefile_path)
    gdf = gdf.to_crs(4326)
    stu = pd.read_csv(stu_path, delimiter=";")
    smu = pd.read_csv(smu_path, delimiter=";")
    stuorg = pd.read_csv(stuorg_path, delimiter=";")
    point = Point(lon, lat)
    for idx, row in gdf.iterrows():
        if row.geometry.contains(point):
            break
    smu = row['SMU']
    stu_list = stuorg[stuorg["smu"] == smu]['stu'].tolist()
    all_stu = stu[stu["stu"].isin(stu_list)]
    soiltypes_shortnames = all_stu['soil'].tolist()
    soiltypes = read_soil_types(smu_stu_path)
    soiltypes_fullnames = get_full_soil_names(soiltypes_shortnames, soiltypes)
    return soiltypes_fullnames