flutterized
This commit is contained in:
parent
2a093e3126
commit
ac62865cd6
9 changed files with 408 additions and 384 deletions
|
|
@ -4,50 +4,126 @@ import 'package:http/http.dart' as http;
|
|||
import 'package:dart_geohash/dart_geohash.dart';
|
||||
import 'package:latlong2/latlong.dart';
|
||||
import 'package:turf/turf.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:flutter/foundation.dart' show kIsWeb;
|
||||
|
||||
class GeoMetadataLookup {
|
||||
final String apiUrl;
|
||||
final Directory cacheDir;
|
||||
final bool enableCacheExpiration;
|
||||
final Duration cacheExpirationDuration;
|
||||
final Distance distance = const Distance();
|
||||
final http.Client httpClient;
|
||||
Directory? cacheDir;
|
||||
|
||||
GeoMetadataLookup({
|
||||
required this.apiUrl,
|
||||
required this.cacheDir,
|
||||
}) {
|
||||
if (!cacheDir.existsSync()) {
|
||||
cacheDir.createSync(recursive: true);
|
||||
this.enableCacheExpiration = false,
|
||||
this.cacheExpirationDuration = const Duration(days: 14),
|
||||
http.Client? client,
|
||||
}) : httpClient = client ?? http.Client() {
|
||||
_initCacheDir();
|
||||
}
|
||||
|
||||
Future<void> _initCacheDir() async {
|
||||
if (!kIsWeb) {
|
||||
cacheDir = await getApplicationDocumentsDirectory();
|
||||
if (enableCacheExpiration) {
|
||||
await _cleanExpiredCache();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<Map<String, dynamic>>> lookup(double latitude, double longitude, {double distance = 10.0}) async {
|
||||
final String geohash = GeoHasher().encode(longitude, latitude);
|
||||
final File cacheFile = File('${cacheDir.path}/$geohash.geojson.gz');
|
||||
Future<List<Map<String, dynamic>>> lookup(double latitude, double longitude, {double distanceThreshold = 10.0}) async {
|
||||
final String geohash = GeoHasher().encode(latitude, longitude, precision: 4);
|
||||
|
||||
if (kIsWeb) {
|
||||
return _lookupWeb(geohash);
|
||||
} else {
|
||||
return _lookupFile(geohash, latitude, longitude, distanceThreshold);
|
||||
}
|
||||
}
|
||||
|
||||
if (!cacheFile.existsSync()) {
|
||||
Future<List<Map<String, dynamic>>> _lookupFile(String geohash, double latitude, double longitude, double distanceThreshold) async {
|
||||
final File cacheFile = File('${cacheDir?.path}/$geohash.geojson.gz');
|
||||
|
||||
await _updateLastAccessTime(geohash);
|
||||
final bool isExpired = await _isCacheExpired(geohash);
|
||||
|
||||
if (!cacheFile.existsSync() || isExpired) {
|
||||
await _downloadGeoJson(geohash, cacheFile);
|
||||
}
|
||||
|
||||
final String geoJsonStr = utf8.decode(GZipCodec().decode(cacheFile.readAsBytesSync()));
|
||||
final Map<String, dynamic> geoJson = jsonDecode(geoJsonStr);
|
||||
return _findMetadata(latitude, longitude, distance, geoJson);
|
||||
return _findMetadata(latitude, longitude, geoJson, distanceThreshold);
|
||||
}
|
||||
|
||||
Future<List<Map<String, dynamic>>> _lookupWeb(String geohash) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final String? cachedGeoJson = prefs.getString('geojson_$geohash');
|
||||
|
||||
if (cachedGeoJson == null || await _isCacheExpired(geohash)) {
|
||||
final response = await http.get(Uri.parse('$apiUrl?geohash=$geohash&format=geojson'));
|
||||
if (response.statusCode == 200) {
|
||||
await prefs.setString('geojson_$geohash', utf8.decode(response.bodyBytes));
|
||||
await _updateLastAccessTime(geohash);
|
||||
return _findMetadata(0, 0, jsonDecode(utf8.decode(response.bodyBytes)), 10.0);
|
||||
} else {
|
||||
throw Exception('Failed to download GeoJSON for geohash: $geohash');
|
||||
}
|
||||
}
|
||||
return _findMetadata(0, 0, jsonDecode(cachedGeoJson), 10.0);
|
||||
}
|
||||
|
||||
Future<void> _downloadGeoJson(String geohash, File cacheFile) async {
|
||||
final response = await http.get(Uri.parse('$apiUrl$geohash'));
|
||||
if (response.statusCode == 200) {
|
||||
cacheFile.writeAsBytesSync(GZipCodec().encode(response.bodyBytes));
|
||||
} else {
|
||||
throw Exception('Failed to download GeoJSON for geohash: $geohash');
|
||||
final response = await httpClient.get(Uri.parse('$apiUrl?geohash=$geohash&format=geojson'));
|
||||
if (response.statusCode == 200) {
|
||||
cacheFile.writeAsBytesSync(GZipCodec().encode(response.bodyBytes));
|
||||
await _updateLastAccessTime(geohash);
|
||||
} else {
|
||||
throw Exception('Failed to download GeoJSON for geohash: $geohash');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _updateLastAccessTime(String geohash) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setInt('cache_last_access_$geohash', DateTime.now().millisecondsSinceEpoch);
|
||||
}
|
||||
|
||||
Future<bool> _isCacheExpired(String geohash) async {
|
||||
if (!enableCacheExpiration) return false;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final int? lastAccess = prefs.getInt('cache_last_access_$geohash');
|
||||
if (lastAccess == null) return true;
|
||||
return DateTime.now().difference(DateTime.fromMillisecondsSinceEpoch(lastAccess)) > cacheExpirationDuration;
|
||||
}
|
||||
|
||||
Future<void> _cleanExpiredCache() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final keys = prefs.getKeys().where((key) => key.startsWith('cache_last_access_')).toList();
|
||||
for (var key in keys) {
|
||||
final geohash = key.replaceFirst('cache_last_access_', '');
|
||||
if (await _isCacheExpired(geohash)) {
|
||||
final File cacheFile = File('${cacheDir?.path}/$geohash.geojson.gz');
|
||||
if (cacheFile.existsSync()) {
|
||||
cacheFile.deleteSync();
|
||||
}
|
||||
await prefs.remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<Map<String, dynamic>> _findMetadata(double latitude, double longitude, double distanceThreshold, Map<String, dynamic> geoJson) {
|
||||
List<Map<String, dynamic>> _findMetadata(double latitude, double longitude, Map<String, dynamic> geoJson, double distanceThreshold) {
|
||||
final Position point = Position(longitude, latitude);
|
||||
List<Map<String, dynamic>> results = [];
|
||||
|
||||
for (var feature in geoJson['features']) {
|
||||
final geometry = feature['geometry'];
|
||||
final properties = feature['properties'] ?? {};
|
||||
final metadata = properties['tags']?.cast<String, dynamic>() ?? {};
|
||||
|
||||
metadata.removeWhere((key, value) => value == null);
|
||||
|
||||
if (geometry['type'] == 'Polygon') {
|
||||
final polygon = Polygon(
|
||||
|
|
@ -58,7 +134,7 @@ class GeoMetadataLookup {
|
|||
.toList(),
|
||||
);
|
||||
if (booleanPointInPolygon(point, polygon)) {
|
||||
results.add({'type': 'polygon', ...properties});
|
||||
results.add({"type": "polygon", ...metadata});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -69,21 +145,7 @@ class GeoMetadataLookup {
|
|||
.toList(),
|
||||
);
|
||||
if (_isPointNearLine(point, line, distanceThreshold)) {
|
||||
results.add({'type': 'line', ...properties});
|
||||
}
|
||||
}
|
||||
|
||||
else if (geometry['type'] == 'MultiLineString') {
|
||||
for (var lineCoords in geometry['coordinates']) {
|
||||
final line = LineString(
|
||||
coordinates: (lineCoords as List)
|
||||
.map((p) => Position(p[0] as double, p[1] as double))
|
||||
.toList(),
|
||||
);
|
||||
if (_isPointNearLine(point, line, distanceThreshold)) {
|
||||
results.add({'type': 'multi_line', ...properties});
|
||||
break;
|
||||
}
|
||||
results.add({"type": "line", ...metadata});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -91,8 +153,7 @@ class GeoMetadataLookup {
|
|||
}
|
||||
|
||||
bool _isPointNearLine(Position point, LineString line, double distanceThreshold) {
|
||||
for (var i = 0; i < line.coordinates.length - 1; i++) {
|
||||
final p1 = line.coordinates[i];
|
||||
for (var p1 in line.coordinates) {
|
||||
final double dist = distance.distance(
|
||||
LatLng(point.lat.toDouble(), point.lng.toDouble()),
|
||||
LatLng(p1.lat.toDouble(), p1.lng.toDouble()),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue