108 lines
3.2 KiB
Dart
108 lines
3.2 KiB
Dart
|
|
import 'dart:convert';
|
|
|
|
import 'package:be_happy/domain/entities/point.dart';
|
|
|
|
class Zone {
|
|
final int id;
|
|
final String title;
|
|
final String description;
|
|
final String type;
|
|
final bool isActive;
|
|
final String shapeType;
|
|
final List<List<List<Point>>> geometry;
|
|
final String speedLimit;
|
|
|
|
Zone({
|
|
required this.id,
|
|
required this.title,
|
|
required this.description,
|
|
required this.type,
|
|
required this.isActive,
|
|
required this.shapeType,
|
|
required this.geometry,
|
|
required this.speedLimit,
|
|
});
|
|
|
|
List<Point> get points {
|
|
if (geometry.isNotEmpty && geometry[0].isNotEmpty) {
|
|
return geometry[0][0];
|
|
}
|
|
return [];
|
|
}
|
|
|
|
factory Zone.fromJson(Map<String, dynamic> json) {
|
|
final zoneCoordinates = json['coordinates'] as Map<String, dynamic>? ?? {};
|
|
final String coordsString = zoneCoordinates['coordinates'] ?? '[]';
|
|
final String shapeType = zoneCoordinates['type'] ?? 'Polygon';
|
|
|
|
List<List<List<Point>>> parsedGeometry = [];
|
|
|
|
try {
|
|
final dynamic decoded = jsonDecode(coordsString);
|
|
|
|
if (decoded is List && decoded.isNotEmpty) {
|
|
if (shapeType == 'Polygon') {
|
|
final polygonRings = _parseRings(decoded);
|
|
if (polygonRings.isNotEmpty) parsedGeometry.add(polygonRings);
|
|
} else if (shapeType == 'MultiPolygon') {
|
|
for (var poly in decoded) {
|
|
if (poly is List) {
|
|
final polygonRings = _parseRings(poly);
|
|
if (polygonRings.isNotEmpty) parsedGeometry.add(polygonRings);
|
|
}
|
|
}
|
|
} else {
|
|
final singleRing = _parseCoordinateList(decoded);
|
|
if (singleRing.isNotEmpty) parsedGeometry.add([singleRing]);
|
|
}
|
|
}
|
|
} catch (e, stackTrace) {
|
|
// Replace with your project logger (e.g., debugPrint or Logger)
|
|
assert(() {
|
|
print("PARSE ERROR for Zone ID ${json['id']}: $e\n$stackTrace");
|
|
return true;
|
|
}());
|
|
}
|
|
|
|
return Zone(
|
|
id: json['id'] as int? ?? 0,
|
|
title: json['title'] as String? ?? 'Unknown',
|
|
description: json['description'] as String? ?? '',
|
|
type: json['type'] as String? ?? '',
|
|
isActive: json['isActive'] as bool? ?? false,
|
|
speedLimit: json['speedLimit'] as String? ?? '',
|
|
shapeType: shapeType,
|
|
geometry: parsedGeometry,
|
|
);
|
|
}
|
|
|
|
static List<List<Point>> _parseRings(List rawRings) {
|
|
List<List<Point>> polygonRings = [];
|
|
for (var ring in rawRings) {
|
|
if (ring is List) {
|
|
final ringPoints = _parseCoordinateList(ring);
|
|
if (ringPoints.isNotEmpty) polygonRings.add(ringPoints);
|
|
}
|
|
}
|
|
return polygonRings;
|
|
}
|
|
|
|
static List<Point> _parseCoordinateList(List rawCoords) {
|
|
List<Point> ringPoints = [];
|
|
for (var coord in rawCoords) {
|
|
if (coord is List && coord.length >= 2) {
|
|
ringPoints.add(Point(
|
|
(coord[1] as num).toDouble(),
|
|
(coord[0] as num).toDouble(),
|
|
));
|
|
}
|
|
}
|
|
return ringPoints;
|
|
}
|
|
|
|
@override
|
|
String toString() {
|
|
return 'Zone{id: $id, title: $title, type: $type, polygonsCount: ${geometry.length}}';
|
|
}
|
|
} |