110 lines
3.3 KiB
Dart
110 lines
3.3 KiB
Dart
// Fetch all addresses in Hoogerheide using Overpass API
|
|
// Run with: dart fetch_addresses_overpass.dart
|
|
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
const hoogerheideRelationId = 2716716;
|
|
const overpassUrl = 'https://overpass-api.de/api/interpreter';
|
|
|
|
// Overpass query to get all addresses (nodes with addr:housenumber) in Hoogerheide
|
|
const overpassQuery = '''
|
|
[out:json][timeout:60];
|
|
relation($hoogerheideRelationId);
|
|
map_to_area -> .searchArea;
|
|
(
|
|
node["addr:housenumber"]["addr:street"](area.searchArea);
|
|
way["addr:housenumber"]["addr:street"](area.searchArea);
|
|
);
|
|
out body;
|
|
>;
|
|
out skel qt;
|
|
''';
|
|
|
|
Future<void> main() async {
|
|
print('Fetching addresses from Hoogerheide (OSM relation $hoogerheideRelationId)...');
|
|
|
|
try {
|
|
final response = await http.post(
|
|
Uri.parse(overpassUrl),
|
|
body: {'data': overpassQuery},
|
|
headers: {'User-Agent': 'DeliveryApp/1.0'},
|
|
).timeout(const Duration(seconds: 60));
|
|
|
|
if (response.statusCode != 200) {
|
|
print('Error: HTTP ${response.statusCode}');
|
|
print(response.body);
|
|
exit(1);
|
|
}
|
|
|
|
final data = jsonDecode(response.body);
|
|
final elements = data['elements'] as List;
|
|
|
|
// Parse addresses
|
|
final addresses = <Map<String, dynamic>>[];
|
|
final streetCoords = <String, List<double>>{};
|
|
|
|
for (final el in elements) {
|
|
if (el['type'] == 'node') {
|
|
final tags = el['tags'] ?? {};
|
|
final street = tags['addr:street'] ?? tags['name'];
|
|
final housenumber = tags['addr:housenumber'];
|
|
final lat = el['lat'];
|
|
final lon = el['lon'];
|
|
|
|
if (street != null && housenumber != null && lat != null && lon != null) {
|
|
addresses.add({
|
|
'street': street,
|
|
'housenumber': housenumber,
|
|
'lat': lat,
|
|
'lon': lon,
|
|
});
|
|
|
|
// Store first coord for each street
|
|
if (!streetCoords.containsKey(street)) {
|
|
streetCoords[street] = [lat, lon];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
print('Found ${addresses.length} addresses on ${streetCoords.length} streets.');
|
|
|
|
// Save to JSON
|
|
final output = {
|
|
'addresses': addresses,
|
|
'streetCoords': streetCoords.map((k, v) => MapEntry(k, {'lat': v[0], 'lon': v[1]})),
|
|
'fetched_at': DateTime.now().toIso8601String(),
|
|
};
|
|
|
|
final file = File('assets/hoogerheide_addresses.json');
|
|
await file.create(recursive: true);
|
|
await file.writeAsString(jsonEncode(output));
|
|
print('Saved to ${file.path}');
|
|
|
|
// Also create a Dart file with streetCoords for easy import
|
|
final dartContent = StringBuffer();
|
|
dartContent.writeln('// Auto-generated from Overpass API');
|
|
dartContent.writeln('// Hoogerheide addresses fetched from OSM');
|
|
dartContent.writeln();
|
|
dartContent.writeln('class HoogerheideStreets {');
|
|
dartContent.writeln(' static const streetCoords = {');
|
|
|
|
streetCoords.forEach((street, coords) {
|
|
dartContent.writeln(" '$street': [${coords[0]}, ${coords[1]}],");
|
|
});
|
|
|
|
dartContent.writeln(' };');
|
|
dartContent.writeln('}');
|
|
|
|
final dartFile = File('lib/hoogerheide_streets.dart');
|
|
await dartFile.writeAsString(dartContent.toString());
|
|
print('Saved Dart file to ${dartFile.path}');
|
|
|
|
} catch (e) {
|
|
print('Error: $e');
|
|
exit(1);
|
|
}
|
|
}
|