gis-classification/README.md

163 lines
4 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
├── Dockerfile # Docker image configuration
├── docker-compose.yml # Docker Compose configuration
├── .dockerignore # Docker ignore rules
├── 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
```
### Docker Deployment
**Build and run with Docker Compose:**
```bash
docker-compose up --build
```
**Or run with Docker directly:**
```bash
docker build -t gis-classification .
docker run -p 8000:8000 -v $(pwd)/data:/app/data -v $(pwd)/output:/app/output gis-classification
```
Then open http://localhost:8000 in your browser.
**Production with Nginx (optional):**
1. Uncomment the nginx section in `docker-compose.yml`
2. Create `nginx.conf` (see example below)
3. Run `docker-compose up -d`
Example `nginx.conf`:
```nginx
events { worker_connections 1024; }
http {
server {
listen 80;
location / {
proxy_pass http://gis-classification:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
```
## 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)