-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_map.py
More file actions
219 lines (208 loc) · 7.49 KB
/
Copy pathwrite_map.py
File metadata and controls
219 lines (208 loc) · 7.49 KB
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
content = """import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:latlong2/latlong.dart';
import 'package:cropsense/core/constants.dart';
import 'package:cropsense/core/theme.dart';
import 'package:cropsense/core/utils.dart';
import 'package:cropsense/data/models/risk_map.dart';
import 'package:cropsense/providers/map_provider.dart';
import 'package:cropsense/screens/map/widgets/map_legend.dart';
import 'package:cropsense/screens/map/widgets/district_popup.dart';
class MapScreen extends ConsumerStatefulWidget {
const MapScreen({super.key});
@override
ConsumerState<MapScreen> createState() => _MapScreenState();
}
class _MapScreenState extends ConsumerState<MapScreen> {
List<dynamic> _features = [];
RiskMapEntry? _selectedEntry;
String _selectedCrop = 'wheat';
final _mapController = MapController();
@override
void initState() {
super.initState();
_loadGeoJson();
}
Future<void> _loadGeoJson() async {
try {
final raw = await rootBundle.loadString('assets/geojson/pakistan_districts.geojson');
final decoded = jsonDecode(raw) as Map<String, dynamic>;
setState(() { _features = decoded['features'] as List<dynamic>; });
} catch (e) {
debugPrint('GeoJSON load error: \$e');
}
}
RiskMapEntry? _getEntry(String id, List<RiskMapEntry> entries) {
try { return entries.firstWhere((e) => e.district == id); }
catch (_) { return null; }
}
List<LatLng> _toLatLng(List<dynamic> coords) {
return coords.map((c) => LatLng(
(c[1] as num).toDouble(),
(c[0] as num).toDouble(),
)).toList();
}
@override
Widget build(BuildContext context) {
final riskMapAsync = ref.watch(riskMapProvider);
final screenWidth = MediaQuery.of(context).size.width;
final compact = screenWidth < 800;
return Scaffold(
backgroundColor: AppColors.offWhite,
body: Column(
children: [
_buildToolbar(compact),
Expanded(
child: Row(
children: [
Expanded(
child: riskMapAsync.when(
loading: () => const Center(child: CircularProgressIndicator(color: AppColors.deepGreen)),
error: (e, _) => Center(child: Text('Error: \$e')),
data: (rm) => _buildMap(rm, compact),
),
),
_selectedEntry != null && !compact
? Padding(
padding: const EdgeInsets.all(16),
child: DistrictPopup(
district: _selectedEntry!,
onClose: () => setState(() => _selectedEntry = null),
),
)
: const SizedBox.shrink(),
],
),
),
],
),
);
}
Widget _buildToolbar(bool compact) {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 8),
decoration: BoxDecoration(
color: AppColors.cardSurface,
border: Border(bottom: BorderSide(color: AppColors.grey200)),
),
child: Row(
children: [
Text('Pakistan Risk Map', style: AppTextStyles.headingMedium),
const SizedBox(width: 24),
compact ? const SizedBox.shrink() : Expanded(
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: AppCrops.all.map((crop) {
final sel = _selectedCrop == crop['id'];
return Padding(
padding: const EdgeInsets.only(right: 8),
child: FilterChip(
label: Text(crop['label']!),
selected: sel,
onSelected: (_) => setState(() => _selectedCrop = crop['id']!),
selectedColor: AppColors.deepGreen,
checkmarkColor: Colors.white,
labelStyle: TextStyle(color: sel ? Colors.white : AppColors.darkText, fontSize: 13),
backgroundColor: AppColors.grey100,
side: BorderSide.none,
),
);
}).toList(),
),
),
),
const Spacer(),
IconButton(
onPressed: () => ref.refresh(riskMapProvider),
icon: const Icon(Icons.refresh_rounded),
color: AppColors.deepGreen,
),
],
),
);
}
Widget _buildMap(RiskMapResponse riskMap, bool compact) {
return Stack(
children: [
FlutterMap(
mapController: _mapController,
options: MapOptions(
initialCenter: LatLng(MapConstants.pakistanLat, MapConstants.pakistanLng),
initialZoom: MapConstants.defaultZoom,
minZoom: MapConstants.minZoom,
maxZoom: MapConstants.maxZoom,
onTap: (_, __) => setState(() => _selectedEntry = null),
),
children: [
TileLayer(
urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
userAgentPackageName: 'com.cropsense',
),
PolygonLayer(
polygons: _buildPolygons(riskMap.districts),
),
],
),
Positioned(
left: 16, bottom: 16,
child: const MapLegend(),
),
_selectedEntry != null && compact
? Positioned(
left: 16, right: 16, bottom: 16,
child: DistrictPopup(
district: _selectedEntry!,
onClose: () => setState(() => _selectedEntry = null),
),
)
: const SizedBox.shrink(),
],
);
}
List<Polygon> _buildPolygons(List<RiskMapEntry> entries) {
final polygons = <Polygon>[];
for (final feature in _features) {
final props = feature['properties'] as Map<String, dynamic>;
final districtId = props['district'] as String;
final entry = _getEntry(districtId, entries);
final isSelected = _selectedEntry?.district == districtId;
final fillColor = entry != null
? riskColor(entry.riskLevel.name).withValues(alpha: 0.65)
: AppColors.grey400.withValues(alpha: 0.4);
final borderColor = isSelected
? Colors.white
: (entry != null ? riskColor(entry.riskLevel.name) : AppColors.grey400);
final geometry = feature['geometry'] as Map<String, dynamic>;
final geoType = geometry['type'] as String;
final coordinates = geometry['coordinates'] as List<dynamic>;
if (geoType == 'Polygon') {
final pts = _toLatLng(coordinates[0] as List<dynamic>);
polygons.add(Polygon(
points: pts,
color: fillColor,
borderColor: borderColor,
borderStrokeWidth: isSelected ? 3.0 : 1.5,
));
} else if (geoType == 'MultiPolygon') {
for (final poly in coordinates) {
final pts = _toLatLng((poly as List<dynamic>)[0] as List<dynamic>);
polygons.add(Polygon(
points: pts,
color: fillColor,
borderColor: borderColor,
borderStrokeWidth: isSelected ? 3.0 : 1.5,
));
}
}
}
return polygons;
}
}
"""
with open('lib/screens/map/map_screen.dart', 'w', encoding='utf-8') as f:
f.write(content)
print('map_screen.dart written successfully!')