122 lines
3 KiB
Markdown
122 lines
3 KiB
Markdown
# GIS Classification
|
|
|
|
Python project for land cover classification using GIS data (GeoTIFF + Shapefile).
|
|
|
|
## Project Structure
|
|
|
|
```
|
|
gis-classification/
|
|
├── main.py # Main script with parameters
|
|
├── requirements.txt # Dependencies
|
|
├── data/ # Input data folder
|
|
├── output/ # Classification results
|
|
├── static/ # Web frontend
|
|
└── src/
|
|
├── classifier.py # Main classification pipeline
|
|
├── api.py # FastAPI web server
|
|
├── data/
|
|
│ └── loader.py # Data loading (GeoTIFF, Shapefile)
|
|
├── strategies/
|
|
│ ├── base.py # Strategy interface
|
|
│ └── classifiers.py # Built-in strategies (RF, SVM, LR)
|
|
└── utils/
|
|
```
|
|
|
|
## Installation
|
|
|
|
```bash
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
Or with uv:
|
|
```bash
|
|
uv pip install -r requirements.txt
|
|
```
|
|
|
|
## Usage
|
|
|
|
### CLI Mode
|
|
|
|
1. Place your input files in `data/`:
|
|
- `landsat.tif` - GeoTIFF from Landsat
|
|
- `polygons.shp` - Shapefile with class labels
|
|
|
|
2. Configure parameters in `main.py`:
|
|
- Input/output paths
|
|
- Classification strategy (RandomForest, SVM, LogisticRegression)
|
|
- Training parameters
|
|
|
|
3. Run:
|
|
```bash
|
|
python main.py
|
|
```
|
|
|
|
### Web Interface
|
|
|
|
Start the web server:
|
|
|
|
```bash
|
|
uvicorn src.api:app --host 0.0.0.0 --port 8000 --reload
|
|
```
|
|
|
|
Then open http://localhost:8000 in your browser.
|
|
|
|
**Features:**
|
|
- Upload GeoTIFF raster and Shapefile training data
|
|
- Select classification strategy (Random Forest, SVM, Logistic Regression, MLE)
|
|
- View training metrics (Accuracy, Cohen's Kappa)
|
|
- Interactive map visualization with Leaflet
|
|
- Download classified GeoTIFF results
|
|
|
|
**API Endpoints:**
|
|
- `GET /` - Web interface
|
|
- `POST /train` - Train classifier with uploaded files
|
|
- `POST /predict` - Run classification
|
|
- `GET /result/{session_id}` - Get result metadata
|
|
- `GET /result/{session_id}/download` - Download classified GeoTIFF
|
|
- `GET /docs` - Interactive API documentation (Swagger UI)
|
|
|
|
## Adding Custom Classification Strategy
|
|
|
|
Create a new class implementing `ClassificationStrategy`:
|
|
|
|
```python
|
|
from src.strategies import ClassificationStrategy
|
|
import numpy as np
|
|
|
|
class MyCustomStrategy(ClassificationStrategy):
|
|
def train(self, X: np.ndarray, y: np.ndarray) -> None:
|
|
# Your training logic
|
|
pass
|
|
|
|
def predict(self, X: np.ndarray) -> np.ndarray:
|
|
# Your prediction logic
|
|
pass
|
|
|
|
def predict_proba(self, X: np.ndarray) -> np.ndarray:
|
|
pass
|
|
|
|
def get_params(self) -> dict:
|
|
pass
|
|
|
|
@property
|
|
def name(self) -> str:
|
|
return "MyCustom"
|
|
```
|
|
|
|
Then use in `main.py`:
|
|
```python
|
|
STRATEGY = MyCustomStrategy()
|
|
```
|
|
|
|
## Output
|
|
|
|
- `output/classified.tif` - Classified raster (GeoTIFF)
|
|
- Console output with accuracy metrics (Accuracy, Cohen's Kappa)
|
|
- Web interface visualization with interactive map
|
|
|
|
## Metrics
|
|
|
|
The classifier reports:
|
|
- **Accuracy**: Overall classification accuracy
|
|
- **Cohen's Kappa**: Agreement statistic accounting for chance (values > 0.8 indicate excellent agreement)
|