Spaces:
Sleeping
Sleeping
Download track.py from Assefore/forest-suitability: direct link, hf CLI and curl.
- Browser
- Download file 5.17 kB
-
https://huggingface.co/spaces/Assefore/forest-suitability/resolve/main/track.py
- Command line
-
hf download hf://spaces/Assefore/forest-suitability/track.py
-
curl -L -o track.py https://huggingface.co/spaces/Assefore/forest-suitability/resolve/main/track.py
5.17 kB
| # -*- coding: utf-8 -*- | |
| """ | |
| track.py | |
| ──────── | |
| 산지 '용도(트랙)' 의사결정 엔진 — Assefore의 핵심 차별점. | |
| 다드림은 '임산물 적지'만 보여주지만, Assefore는 한 산지를 세 갈래로 평가해 | |
| "이 산은 어느 트랙으로 가야 하는가"를 통합 판정한다. | |
| [트리플 트랙] | |
| ① 임산물(cultivation) : 재배 적합도·소득률·리스크 종합 (scoring/risk 에서 산출) | |
| ② 조경수(landscape) : 조경수로 쓸 나무가 있을 가능성 + 반출(이식) 용이성 | |
| ③ 임업(forestry) : 고령·고품질(대경목) 임목 비율 → 목재 생산 가능성 | |
| 각 트랙 0~100 지수 → 최고 점수 트랙을 1차 추천. | |
| ※ 창업대회용 1차 설계 — 계수는 재배지식/현장 검증 후 정교화 예정(추정). | |
| """ | |
| # ── 조경수로 흔히 쓰이는 수종 (부분일치 매칭) ───────────────── | |
| LANDSCAPE_SPECIES = [ | |
| "소나무", "잣나무", "주목", "편백", "측백", "향나무", "구상나무", | |
| "단풍", "느티", "벚", "이팝", "산딸", "마가목", "층층", "복자기", | |
| "대왕", "메타세쿼이아", "은행", "팥배", "노각", | |
| ] | |
| # ── 목재(임업) 가치가 높은 수종 ────────────────────────────── | |
| TIMBER_SPECIES = [ | |
| "잣나무", "낙엽송", "일본잎갈", "소나무", "리기다", "편백", "삼나무", | |
| "참나무", "굴참", "신갈", "졸참", "물푸레", "자작", | |
| ] | |
| def _clamp(x, lo=0.0, hi=100.0): | |
| return max(lo, min(hi, x)) | |
| def _match(name, keywords): | |
| name = name or "" | |
| return any(k in name for k in keywords) | |
| def landscape_index(data): | |
| """ | |
| 조경수 가능성(0~100). | |
| - 수종이 조경수종인가 (주동인) | |
| - 적당한 크기·수형 (너무 어리거나 과대하면 이식 부적합) | |
| - 반출 용이성 (저지대·완경사일수록 굴취·운반 쉬움) | |
| """ | |
| species = data.get("species", "") or data.get("forest_type", "") | |
| age = float(data.get("age_years", 30) or 30) | |
| height = float(data.get("tree_height", 0) or 0) | |
| slope = float(data.get("slope", 20) or 20) | |
| elev = float(data.get("elevation", 400) or 400) | |
| sp_score = 60 if _match(species, LANDSCAPE_SPECIES) else 15 # 수종 적합(최대 60) | |
| # 이식 적기: 수령 15~40년·수고 6~15m 부근이 조경수 상품성 좋음 | |
| form = 0 | |
| if 10 <= age <= 45: | |
| form += 20 | |
| if 5 <= height <= 16: | |
| form += 10 | |
| # 반출 용이성: 가파르거나 깊은 산일수록 감점 | |
| access = 10 | |
| access -= max(0, (slope - 25)) * 0.4 | |
| access -= max(0, (elev - 600)) * 0.01 | |
| access = max(0, access) | |
| return round(_clamp(sp_score + form + access), 0) | |
| def forestry_index(data): | |
| """ | |
| 임업(목재) 가능성(0~100). | |
| - 고령 임목(영급 높을수록) — 주동인 | |
| - 대경목(경급)·임분 높이 → 재적 많음 | |
| - 목재 가치 수종 | |
| """ | |
| age = float(data.get("age_years", 30) or 30) | |
| height = float(data.get("tree_height", 0) or 0) | |
| species = data.get("species", "") or data.get("forest_type", "") | |
| dmcls = data.get("dia_class", "") or "" # 경급명(소경목/중경목/대경목) — 있으면 사용 | |
| # 고령도: 31년(4영급)부터 가점, 51년+ 만점권 | |
| age_score = _clamp((age - 20) * 1.6, 0, 45) | |
| # 경급/수고: 대경목 또는 수고 높을수록 | |
| size_score = 0 | |
| if "대경목" in dmcls: | |
| size_score = 30 | |
| elif "중경목" in dmcls: | |
| size_score = 18 | |
| elif "소경목" in dmcls: | |
| size_score = 6 | |
| else: # 경급 정보 없으면 수고로 대체 | |
| size_score = _clamp(height * 1.3, 0, 30) | |
| sp_score = 15 if _match(species, TIMBER_SPECIES) else 5 | |
| return round(_clamp(age_score + size_score + sp_score), 0) | |
| def recommend_track(data, cultivation_score): | |
| """ | |
| 세 트랙 점수를 비교해 추천 트랙을 반환. | |
| cultivation_score: 임산물 트랙 점수(이 산지 최고 작물의 종합점수 등 0~100). | |
| 반환: | |
| { | |
| "scores": {"임산물":.., "조경수":.., "임업":..}, | |
| "best": 트랙명, "best_score": .., | |
| "ranking": [(트랙, 점수)...내림차순], | |
| "verdict": 사람이 읽는 한 줄 판정, | |
| } | |
| """ | |
| ls = landscape_index(data) | |
| fr = forestry_index(data) | |
| cu = round(float(cultivation_score or 0), 0) | |
| scores = {"임산물": cu, "조경수": ls, "임업": fr} | |
| ranking = sorted(scores.items(), key=lambda x: x[1], reverse=True) | |
| best, best_score = ranking[0] | |
| # 판정 문구: 1·2위 격차가 작으면 '복합' 제안 | |
| gap = ranking[0][1] - ranking[1][1] | |
| if gap < 10: | |
| verdict = (f"{ranking[0][0]}·{ranking[1][0]} 복합 트랙 권장 " | |
| f"(점수 {ranking[0][1]:.0f}/{ranking[1][1]:.0f} — 구역 분할 검토)") | |
| else: | |
| verdict = f"{best} 트랙 우선 권장 (점수 {best_score:.0f})" | |
| return { | |
| "scores": scores, "best": best, "best_score": best_score, | |
| "ranking": ranking, "verdict": verdict, | |
| } | |