File size: 3,239 Bytes
e383754
 
4b38b88
 
 
e383754
4b38b88
 
e383754
4b38b88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e383754
 
 
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
import logging
import numpy as np
import rasterio
from rasterio.warp import transform_bounds

def extract_features_from_geotiff(image_path, output_folder, feature_type="trees"):
    """Simple feature extraction for HF Spaces."""
    try:
        logging.info(f"Extracting {feature_type} from {image_path}")
        
        with rasterio.open(image_path) as src:
            # Simple NDVI calculation
            if src.count >= 3:
                red = src.read(1).astype(float)
                green = src.read(2).astype(float)
                nir = src.read(4).astype(float) if src.count >= 4 else green
                
                ndvi = np.divide(nir - red, nir + red + 1e-10)
                mask = ndvi > 0.2
            else:
                band = src.read(1)
                mask = band > np.percentile(band, 60)
            
            # Get bounds
            bounds = src.bounds
            if src.crs:
                west, south, east, north = transform_bounds(
                    src.crs, 'EPSG:4326',
                    bounds.left, bounds.bottom, bounds.right, bounds.top
                )
            else:
                west, south, east, north = -74.1, 40.6, -73.9, 40.8
        
        # Create simple features
        features = []
        height, width = mask.shape
        grid_size = max(10, min(height, width) // 50)
        
        feature_id = 0
        for y in range(0, height, grid_size):
            for x in range(0, width, grid_size):
                cell = mask[y:y+grid_size, x:x+grid_size]
                if np.sum(cell) > grid_size * grid_size * 0.3:
                    
                    x_ratio = x / width
                    y_ratio = y / height
                    
                    lon1 = west + x_ratio * (east - west)
                    lat1 = north - y_ratio * (north - south)
                    
                    x2_ratio = min((x + grid_size) / width, 1.0)
                    y2_ratio = min((y + grid_size) / height, 1.0)
                    
                    lon2 = west + x2_ratio * (east - west)
                    lat2 = north - y2_ratio * (north - south)
                    
                    polygon_coords = [
                        [lon1, lat1], [lon2, lat1], [lon2, lat2], [lon1, lat2], [lon1, lat1]
                    ]
                    
                    feature = {
                        "type": "Feature",
                        "id": feature_id,
                        "properties": {
                            "feature_type": feature_type,
                            "confidence": 0.8
                        },
                        "geometry": {
                            "type": "Polygon",
                            "coordinates": [polygon_coords]
                        }
                    }
                    
                    features.append(feature)
                    feature_id += 1
        
        return {
            "type": "FeatureCollection",
            "features": features,
            "feature_type": feature_type
        }
        
    except Exception as e:
        logging.error(f"Error extracting features: {str(e)}")
        return {"type": "FeatureCollection", "features": []}