File size: 12,677 Bytes
b010f1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
"""
Site Processor - Digital Twin Initialization
Handles site boundary import, normalization, and buildable area calculation
"""
import geopandas as gpd
from shapely.geometry import Polygon, MultiPolygon, shape
from shapely.validation import make_valid
from shapely.ops import unary_union
import json
from pathlib import Path
from typing import Optional, List, Tuple, Union
import logging
import yaml

from src.models.domain import SiteBoundary, Constraint, ConstraintType

logger = logging.getLogger(__name__)


class SiteProcessor:
    """
    Site boundary processor for CAD/GIS file import and normalization
    
    Responsibilities:
    - Import Shapefile, GeoJSON, DXF files
    - Normalize and validate geometry
    - Calculate buildable area after constraints
    - Identify no-build zones
    """
    
    def __init__(self, regulations_path: str = "config/regulations.yaml"):
        """
        Initialize site processor
        
        Args:
            regulations_path: Path to regulations YAML
        """
        self.regulations_path = Path(regulations_path)
        self.regulations = self._load_regulations()
        self.logger = logging.getLogger(__name__)
    
    def _load_regulations(self) -> dict:
        """Load regulations from YAML"""
        if not self.regulations_path.exists():
            return {}
        with open(self.regulations_path, 'r', encoding='utf-8') as f:
            return yaml.safe_load(f)
    
    def import_from_shapefile(self, filepath: str) -> SiteBoundary:
        """
        Import site boundary from Shapefile (.shp)
        
        Args:
            filepath: Path to .shp file
            
        Returns:
            SiteBoundary object
        """
        self.logger.info(f"Importing Shapefile: {filepath}")
        
        gdf = gpd.read_file(filepath)
        
        if len(gdf) == 0:
            raise ValueError("Shapefile contains no geometry")
        
        # Get the first geometry (or union all)
        if len(gdf) == 1:
            geometry = gdf.geometry.iloc[0]
        else:
            geometry = unary_union(gdf.geometry)
        
        # Ensure it's a Polygon
        if isinstance(geometry, MultiPolygon):
            geometry = max(geometry.geoms, key=lambda g: g.area)
        
        # Validate and fix geometry
        geometry = self._normalize_geometry(geometry)
        
        # Create SiteBoundary
        site = SiteBoundary(
            geometry=geometry,
            area_sqm=geometry.area,
            metadata={
                'source': filepath,
                'crs': str(gdf.crs) if gdf.crs else 'unknown'
            }
        )
        
        # Calculate buildable area
        self._calculate_buildable_area(site)
        
        return site
    
    def import_from_geojson(self, filepath: str) -> SiteBoundary:
        """
        Import site boundary from GeoJSON file
        
        Args:
            filepath: Path to .geojson file
            
        Returns:
            SiteBoundary object
        """
        self.logger.info(f"Importing GeoJSON: {filepath}")
        
        with open(filepath, 'r', encoding='utf-8') as f:
            data = json.load(f)
        
        # Handle FeatureCollection or single Feature
        if data.get('type') == 'FeatureCollection':
            features = data.get('features', [])
            if not features:
                raise ValueError("GeoJSON contains no features")
            geometries = [shape(f['geometry']) for f in features]
            geometry = unary_union(geometries)
        elif data.get('type') == 'Feature':
            geometry = shape(data['geometry'])
        else:
            geometry = shape(data)
        
        # Ensure it's a Polygon
        if isinstance(geometry, MultiPolygon):
            geometry = max(geometry.geoms, key=lambda g: g.area)
        
        geometry = self._normalize_geometry(geometry)
        
        site = SiteBoundary(
            geometry=geometry,
            area_sqm=geometry.area,
            metadata={'source': filepath}
        )
        
        self._calculate_buildable_area(site)
        
        return site
    
    def import_from_dxf(self, filepath: str) -> SiteBoundary:
        """
        Import site boundary from DXF (AutoCAD) file
        
        Args:
            filepath: Path to .dxf file
            
        Returns:
            SiteBoundary object
        """
        import ezdxf
        
        self.logger.info(f"Importing DXF: {filepath}")
        
        doc = ezdxf.readfile(filepath)
        msp = doc.modelspace()
        
        # Find closed polylines or LWPolylines
        polygons = []
        
        for entity in msp:
            if entity.dxftype() == 'LWPOLYLINE':
                if entity.closed:
                    points = list(entity.get_points())
                    if len(points) >= 3:
                        polygons.append(Polygon([(p[0], p[1]) for p in points]))
            
            elif entity.dxftype() == 'POLYLINE':
                if entity.is_closed:
                    points = list(entity.points())
                    if len(points) >= 3:
                        polygons.append(Polygon([(p[0], p[1]) for p in points]))
            
            elif entity.dxftype() == 'LINE':
                # Lines would need to be assembled into polygons
                pass
        
        if not polygons:
            raise ValueError("No closed polygons found in DXF file")
        
        # Get the largest polygon as site boundary
        geometry = max(polygons, key=lambda p: p.area)
        geometry = self._normalize_geometry(geometry)
        
        site = SiteBoundary(
            geometry=geometry,
            area_sqm=geometry.area,
            metadata={
                'source': filepath,
                'dxf_version': doc.dxfversion
            }
        )
        
        self._calculate_buildable_area(site)
        
        return site
    
    def import_from_coordinates(self, coordinates: List[Tuple[float, float]]) -> SiteBoundary:
        """
        Create site boundary from list of coordinates
        
        Args:
            coordinates: List of (x, y) tuples forming polygon
            
        Returns:
            SiteBoundary object
        """
        if len(coordinates) < 3:
            raise ValueError("At least 3 coordinates required")
        
        geometry = Polygon(coordinates)
        geometry = self._normalize_geometry(geometry)
        
        site = SiteBoundary(
            geometry=geometry,
            area_sqm=geometry.area,
            metadata={'source': 'coordinates'}
        )
        
        self._calculate_buildable_area(site)
        
        return site
    
    def _normalize_geometry(self, geometry: Polygon) -> Polygon:
        """
        Normalize and validate geometry
        
        - Fix self-intersections
        - Ensure counter-clockwise orientation
        - Remove duplicate points
        - Simplify if too complex
        """
        if not geometry.is_valid:
            self.logger.warning("Invalid geometry detected, attempting fix")
            geometry = make_valid(geometry)
            
            # make_valid might return a collection
            if isinstance(geometry, MultiPolygon):
                geometry = max(geometry.geoms, key=lambda g: g.area)
        
        # Ensure counter-clockwise exterior
        if not geometry.exterior.is_ccw:
            geometry = Polygon(
                list(geometry.exterior.coords)[::-1],
                [list(ring.coords)[::-1] for ring in geometry.interiors]
            )
        
        # Simplify if too many vertices (> 1000)
        if len(geometry.exterior.coords) > 1000:
            # Tolerance of 0.1m
            geometry = geometry.simplify(0.1, preserve_topology=True)
        
        return geometry
    
    def _calculate_buildable_area(self, site: SiteBoundary):
        """
        Calculate buildable area after applying setbacks
        
        Args:
            site: SiteBoundary to update
        """
        setback = self.regulations.get('setbacks', {}).get('boundary_minimum', 50)
        
        # Create setback constraint
        setback_zone = site.geometry.buffer(-setback)
        
        if setback_zone.is_empty:
            self.logger.warning(f"Site too small for {setback}m setback")
            site.buildable_area_sqm = 0
        else:
            site.buildable_area_sqm = setback_zone.area
            
            # Add setback as constraint
            no_build_zone = site.geometry.difference(setback_zone)
            if not no_build_zone.is_empty:
                constraint = Constraint(
                    type=ConstraintType.SETBACK,
                    geometry=no_build_zone if isinstance(no_build_zone, Polygon) else no_build_zone.convex_hull,
                    buffer_distance_m=setback,
                    description=f"Boundary setback zone ({setback}m)",
                    is_hard=True
                )
                site.constraints.append(constraint)
        
        self.logger.info(
            f"Site area: {site.area_sqm:.0f}m², "
            f"Buildable: {site.buildable_area_sqm:.0f}m² "
            f"({site.buildable_area_sqm/site.area_sqm*100:.1f}%)"
        )
    
    def add_constraint(
        self,
        site: SiteBoundary,
        constraint_type: ConstraintType,
        geometry: Union[Polygon, List[Tuple[float, float]]],
        buffer_distance: float = 0,
        description: str = "",
        is_hard: bool = True
    ) -> Constraint:
        """
        Add a constraint to the site
        
        Args:
            site: SiteBoundary to update
            constraint_type: Type of constraint
            geometry: Constraint geometry or coordinates
            buffer_distance: Buffer distance in meters
            description: Human-readable description
            is_hard: Whether constraint is mandatory
            
        Returns:
            Created Constraint object
        """
        if isinstance(geometry, list):
            geom = Polygon(geometry)
        else:
            geom = geometry
        
        # Apply buffer if specified
        if buffer_distance > 0:
            geom = geom.buffer(buffer_distance)
        
        constraint = Constraint(
            type=constraint_type,
            geometry=geom,
            buffer_distance_m=buffer_distance,
            description=description or f"{constraint_type.value} constraint",
            is_hard=is_hard
        )
        
        site.constraints.append(constraint)
        
        # Recalculate buildable area
        site.calculate_buildable_area()
        
        return constraint
    
    def get_buildable_polygon(self, site: SiteBoundary) -> Polygon:
        """
        Get the actual buildable polygon after all constraints
        
        Args:
            site: SiteBoundary
            
        Returns:
            Polygon representing buildable area
        """
        buildable = site.geometry
        
        for constraint in site.constraints:
            if constraint.is_hard:
                buildable = buildable.difference(constraint.geometry)
        
        if isinstance(buildable, MultiPolygon):
            buildable = max(buildable.geoms, key=lambda g: g.area)
        
        return buildable
    
    def identify_no_build_zones(self, site: SiteBoundary) -> List[Constraint]:
        """
        Identify all no-build zones on the site
        
        Returns list of constraints representing no-build areas
        """
        return [c for c in site.constraints if c.is_hard and c.type == ConstraintType.NO_BUILD]


# Example usage
if __name__ == "__main__":
    from shapely.geometry import box
    
    # Create processor
    processor = SiteProcessor()
    
    # Example: Create from coordinates
    coords = [
        (0, 0), (500, 0), (500, 400), (300, 500), (0, 400), (0, 0)
    ]
    
    site = processor.import_from_coordinates(coords)
    
    print(f"Site ID: {site.id}")
    print(f"Total area: {site.area_sqm:.0f} m²")
    print(f"Buildable area: {site.buildable_area_sqm:.0f} m²")
    print(f"Number of constraints: {len(site.constraints)}")
    
    # Add a hazard zone
    hazard_coords = [(200, 200), (250, 200), (250, 250), (200, 250)]
    processor.add_constraint(
        site,
        ConstraintType.HAZARD_ZONE,
        hazard_coords,
        buffer_distance=100,
        description="Chemical storage buffer zone"
    )
    
    print(f"After hazard zone - Buildable: {site.buildable_area_sqm:.0f} m²")
    print(f"Total constraints: {len(site.constraints)}")