Add municipality and coordinate search, and a map target selector
Puts the controls that change what the map is looking at over the map itself, where they act, and gives them something to search. Search is one field, not two. A string either parses as coordinates or it does not, and the answer is obvious from the text, so making the user declare up front which kind of thing they are looking for would be asking them to do the program's job. Coordinates accept decimal and degrees-minutes-seconds, comma or space separated, with hemisphere letters — including the Italian O for ovest, because someone reading an Italian map will type it and silently reading it as east would put them the wrong side of Greenwich. The 1180 Piedmont municipalities are bundled rather than geocoded online. The app is region-scoped, so a list of one region's towns is small enough to ship (100 KB) and beats a geocoder on every axis that matters: instant, offline, no API key, no rate limit, and it cannot return a result somewhere the app has no radar for. Matching folds accents, so "aglie" finds "Agliè", and prefix matches outrank substring ones — typing "tor" should surface Torino, not the first alphabetical name that happens to contain those letters. tool/generate_places.py derives the list from Istat boundary shapefiles (CC BY 4.0). Two properties of that file cost time and are now written down: the geometry is UTM 32N rather than degrees, and the DBF is UTF-8 despite one bilingual Friulian record that makes strict cp1252 fail. A terminal renders utf-8 and latin-1 output identically, so the encoding cannot be settled by looking at printed text — it took dumping codepoints. A guard in the generator and a test against the shipped asset both check for mojibake now, and the guard caught a real mistake the moment it was written. The target selector switches between following the device, a saved place, and the whole region, and the selection doubles as what the app reopens on: "the place I marked" and "what I see when I open the app" are one idea to the person using it. Dragging the map while it is following stops the camera chasing them but leaves that preference alone, because looking somewhere else now is not the same as changing their mind about next launch. The position marker and the following are MapLibre's own, driven by onCameraTrackingDismissed, so there is no second location stream to keep in step with the map. A test asserts that all 1180 municipalities fall inside the region bounds, which is what actually validates the UTM-to-degrees conversion end to end. Verified on the emulator: the dropdown lists follow, region and both saved places; "aglie" finds Agliè; "44.3841 7.5426" offers the coordinate jump and lands on Cuneo at town-reading zoom; selecting follow moves the map to the device position with the blue dot on it and changes the recentre button to match. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -130,7 +130,7 @@ void main() {
|
||||
test('carries the mandatory attributions', () {
|
||||
final ids = config.attributions.map((a) => a.id).toSet();
|
||||
|
||||
expect(ids, containsAll(<String>['dpc', 'osm', 'openmaptiles']));
|
||||
expect(ids, containsAll(<String>['dpc', 'osm', 'openmaptiles', 'istat']));
|
||||
|
||||
final dpc = config.attributions.firstWhere((a) => a.id == 'dpc');
|
||||
expect(dpc.license, 'CC BY-SA');
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:nuvolari/core/region/region_config.dart';
|
||||
import 'package:nuvolari/features/places/comune.dart';
|
||||
import 'package:nuvolari/features/places/comuni_repository.dart';
|
||||
|
||||
ComuneIndex loadBundled() => ComuneIndex.parse(
|
||||
File('assets/regions/piemonte_comuni.json').readAsStringSync(),
|
||||
);
|
||||
|
||||
void main() {
|
||||
group('foldForSearch', () {
|
||||
test('lower-cases and strips the accents Italian names use', () {
|
||||
expect(foldForSearch('Agliè'), 'aglie');
|
||||
expect(foldForSearch('CIRIÈ'), 'cirie');
|
||||
expect(foldForSearch('Mondovì'), 'mondovi');
|
||||
expect(foldForSearch('Forlì'), 'forli');
|
||||
});
|
||||
|
||||
test('leaves unaccented text alone apart from case', () {
|
||||
expect(foldForSearch('Torino'), 'torino');
|
||||
expect(foldForSearch("Reggio nell'Emilia"), "reggio nell'emilia");
|
||||
});
|
||||
});
|
||||
|
||||
group('the bundled Piedmont list', () {
|
||||
late ComuneIndex index;
|
||||
late RegionConfig region;
|
||||
|
||||
setUpAll(() {
|
||||
index = loadBundled();
|
||||
region = RegionConfig.parse(
|
||||
File('assets/regions/piemonte.json').readAsStringSync(),
|
||||
);
|
||||
});
|
||||
|
||||
test('holds every Piedmont municipality', () {
|
||||
// Piedmont had 1180 municipalities at 1 January 2025. A number that
|
||||
// drifts means the generator picked up the wrong region or year.
|
||||
expect(index.comuni, hasLength(1180));
|
||||
});
|
||||
|
||||
test('names its source and licence', () {
|
||||
expect(index.source, contains('Istat'));
|
||||
expect(index.license, 'CC BY 4.0');
|
||||
});
|
||||
|
||||
// A name decoded with the wrong codepage is the failure mode this data has
|
||||
// already produced once, and it is silent unless something checks.
|
||||
test('no name is mis-decoded', () {
|
||||
final suspicious = index.comuni
|
||||
.where(
|
||||
(comune) => comune.name.contains('Ã') || comune.name.contains('�'),
|
||||
)
|
||||
.map((comune) => comune.name)
|
||||
.toList();
|
||||
|
||||
expect(suspicious, isEmpty);
|
||||
});
|
||||
|
||||
test('accented names survived the pipeline intact', () {
|
||||
final aglie = index.comuni.firstWhere((c) => c.istatCode == '001001');
|
||||
|
||||
expect(aglie.name, 'Agliè');
|
||||
expect(aglie.province, 'TO');
|
||||
});
|
||||
|
||||
test('every municipality falls inside the region bounds', () {
|
||||
final outside = index.comuni
|
||||
.where((comune) => !region.bounds.contains(comune.point))
|
||||
.map((comune) => comune.label)
|
||||
.toList();
|
||||
|
||||
expect(outside, isEmpty);
|
||||
});
|
||||
|
||||
test('istat codes are unique', () {
|
||||
final codes = index.comuni.map((c) => c.istatCode).toSet();
|
||||
|
||||
expect(codes, hasLength(index.comuni.length));
|
||||
});
|
||||
|
||||
test('provinces are the eight of Piedmont', () {
|
||||
final provinces = index.comuni.map((c) => c.province).toSet();
|
||||
|
||||
expect(provinces, {'TO', 'VC', 'NO', 'CN', 'AT', 'AL', 'BI', 'VB'});
|
||||
});
|
||||
|
||||
test('the provincial capitals are where they should be', () {
|
||||
final torino = index.comuni.firstWhere((c) => c.name == 'Torino');
|
||||
|
||||
// A centroid, so a few kilometres out is expected; tens would mean the
|
||||
// projection maths is wrong.
|
||||
expect(torino.point.latitude, closeTo(45.07, 0.1));
|
||||
expect(torino.point.longitude, closeTo(7.68, 0.1));
|
||||
});
|
||||
});
|
||||
|
||||
group('search', () {
|
||||
late ComuneIndex index;
|
||||
|
||||
setUpAll(() => index = loadBundled());
|
||||
|
||||
test('finds a town by its exact name', () {
|
||||
final results = index.search('Torino');
|
||||
|
||||
expect(results.first.name, 'Torino');
|
||||
});
|
||||
|
||||
// Someone typing "tor" wants Torino, not the first alphabetical name that
|
||||
// happens to contain those letters somewhere.
|
||||
test('ranks names that start with the query first', () {
|
||||
final results = index.search('tor');
|
||||
|
||||
expect(results.first.searchKey, startsWith('tor'));
|
||||
expect(results.map((c) => c.name), contains('Torino'));
|
||||
});
|
||||
|
||||
test('ignores accents in the query and in the data', () {
|
||||
expect(index.search('aglie').map((c) => c.name), contains('Agliè'));
|
||||
expect(index.search('Agliè').map((c) => c.name), contains('Agliè'));
|
||||
expect(index.search('mondovi').map((c) => c.name), contains('Mondovì'));
|
||||
});
|
||||
|
||||
test('is case-insensitive', () {
|
||||
expect(index.search('TORINO').first.name, 'Torino');
|
||||
expect(index.search('torino').first.name, 'Torino');
|
||||
});
|
||||
|
||||
test('matches in the middle of a name too', () {
|
||||
final results = index.search('mondov');
|
||||
|
||||
expect(results, isNotEmpty);
|
||||
});
|
||||
|
||||
test('an empty query returns nothing rather than everything', () {
|
||||
expect(index.search(''), isEmpty);
|
||||
expect(index.search(' '), isEmpty);
|
||||
});
|
||||
|
||||
test('caps the result list', () {
|
||||
// A single letter matches hundreds of names.
|
||||
expect(
|
||||
index.search('a').length,
|
||||
lessThanOrEqualTo(ComuneIndex.maxResults),
|
||||
);
|
||||
});
|
||||
|
||||
test('an unmatched query returns nothing', () {
|
||||
expect(index.search('zzzzzzzz'), isEmpty);
|
||||
});
|
||||
});
|
||||
|
||||
group('ComuneIndex.parse', () {
|
||||
test('rejects a document with no source', () {
|
||||
expect(
|
||||
() => ComuneIndex.parse(
|
||||
'{"places":[{"name":"X","province":"TO","istat":"1","lat":45,"lng":7}]}',
|
||||
),
|
||||
throwsFormatException,
|
||||
);
|
||||
});
|
||||
|
||||
test('rejects an empty list', () {
|
||||
expect(
|
||||
() => ComuneIndex.parse('{"source":"x","places":[]}'),
|
||||
throwsFormatException,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('ComuniRepository', () {
|
||||
test('derives the asset path from the region id', () {
|
||||
expect(
|
||||
ComuniRepository.assetPathFor('piemonte'),
|
||||
'assets/regions/piemonte_comuni.json',
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:nuvolari/features/places/coordinate_input.dart';
|
||||
|
||||
void main() {
|
||||
group('decimal degrees', () {
|
||||
test('parses what people paste from a map', () {
|
||||
final point = parseCoordinates('45.0703, 7.6869');
|
||||
|
||||
expect(point, isNotNull);
|
||||
expect(point!.latitude, closeTo(45.0703, 1e-9));
|
||||
expect(point.longitude, closeTo(7.6869, 1e-9));
|
||||
});
|
||||
|
||||
test('accepts a space or a semicolon as the separator', () {
|
||||
for (final text in <String>[
|
||||
'45.0703 7.6869',
|
||||
'45.0703; 7.6869',
|
||||
'45.0703,7.6869',
|
||||
]) {
|
||||
final point = parseCoordinates(text);
|
||||
expect(point, isNotNull, reason: text);
|
||||
expect(point!.latitude, closeTo(45.0703, 1e-9), reason: text);
|
||||
}
|
||||
});
|
||||
|
||||
test('latitude comes first, as on every consumer map', () {
|
||||
final point = parseCoordinates('7.6869, 45.0703')!;
|
||||
|
||||
expect(point.latitude, closeTo(7.6869, 1e-9));
|
||||
expect(point.longitude, closeTo(45.0703, 1e-9));
|
||||
});
|
||||
|
||||
test('handles hemisphere letters before or after the number', () {
|
||||
expect(parseCoordinates('45.07N, 7.68E')!.latitude, closeTo(45.07, 1e-9));
|
||||
expect(parseCoordinates('N45.07, E7.68')!.longitude, closeTo(7.68, 1e-9));
|
||||
expect(
|
||||
parseCoordinates('45.07S, 7.68W')!.latitude,
|
||||
closeTo(-45.07, 1e-9),
|
||||
);
|
||||
expect(
|
||||
parseCoordinates('45.07S, 7.68W')!.longitude,
|
||||
closeTo(-7.68, 1e-9),
|
||||
);
|
||||
});
|
||||
|
||||
// Someone reading coordinates off an Italian map types O for ovest.
|
||||
// Treating it as east would put them the wrong side of Greenwich.
|
||||
test('reads O as west', () {
|
||||
expect(
|
||||
parseCoordinates('45.07N, 7.68O')!.longitude,
|
||||
closeTo(-7.68, 1e-9),
|
||||
);
|
||||
});
|
||||
|
||||
test('accepts signed values', () {
|
||||
final point = parseCoordinates('-33.87, 151.21')!;
|
||||
|
||||
expect(point.latitude, closeTo(-33.87, 1e-9));
|
||||
expect(point.longitude, closeTo(151.21, 1e-9));
|
||||
});
|
||||
|
||||
test('accepts the decimal comma an Italian keyboard produces', () {
|
||||
final point = parseCoordinates('45,0703 7,6869')!;
|
||||
|
||||
expect(point.latitude, closeTo(45.0703, 1e-9));
|
||||
expect(point.longitude, closeTo(7.6869, 1e-9));
|
||||
});
|
||||
});
|
||||
|
||||
group('degrees, minutes and seconds', () {
|
||||
test('parses the full form', () {
|
||||
final point = parseCoordinates('45°04\'13"N 7°41\'13"E');
|
||||
|
||||
expect(point, isNotNull);
|
||||
expect(point!.latitude, closeTo(45 + 4 / 60 + 13 / 3600, 1e-6));
|
||||
expect(point.longitude, closeTo(7 + 41 / 60 + 13 / 3600, 1e-6));
|
||||
});
|
||||
|
||||
test('parses degrees and minutes without seconds', () {
|
||||
final point = parseCoordinates("45°04'N 7°41'E")!;
|
||||
|
||||
expect(point.latitude, closeTo(45 + 4 / 60, 1e-6));
|
||||
});
|
||||
|
||||
test('applies the southern and western hemispheres', () {
|
||||
final point = parseCoordinates('33°52\'04"S 151°12\'36"W')!;
|
||||
|
||||
expect(point.latitude, lessThan(0));
|
||||
expect(point.longitude, lessThan(0));
|
||||
});
|
||||
|
||||
test('rejects minutes or seconds at 60 or above', () {
|
||||
expect(parseCoordinates('45°60\'00"N 7°41\'13"E'), isNull);
|
||||
expect(parseCoordinates('45°04\'60"N 7°41\'13"E'), isNull);
|
||||
});
|
||||
});
|
||||
|
||||
group('rejection', () {
|
||||
// The same field takes a town name, so anything that is not coordinates has
|
||||
// to come back null rather than throw or guess.
|
||||
test('returns null for a town name', () {
|
||||
expect(parseCoordinates('Torino'), isNull);
|
||||
expect(parseCoordinates('Reggio nell Emilia'), isNull);
|
||||
expect(parseCoordinates(''), isNull);
|
||||
expect(parseCoordinates(' '), isNull);
|
||||
});
|
||||
|
||||
test('returns null for a single number', () {
|
||||
expect(parseCoordinates('45.07'), isNull);
|
||||
});
|
||||
|
||||
test('returns null for out-of-range values', () {
|
||||
expect(parseCoordinates('91.0, 7.0'), isNull);
|
||||
expect(parseCoordinates('45.0, 181.0'), isNull);
|
||||
expect(parseCoordinates('-91.0, 7.0'), isNull);
|
||||
});
|
||||
|
||||
test('returns null for trailing rubbish', () {
|
||||
expect(parseCoordinates('45.07, 7.68 and then some'), isNull);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -36,7 +36,16 @@ void main() {
|
||||
await tester.pumpWidget(wrap(const SourcesScreen(), region));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// The list is longer than the test viewport, so each entry is scrolled to
|
||||
// rather than expected to be on screen at once. Asserting only on what
|
||||
// happens to fit would let a credit fall off the bottom unnoticed.
|
||||
for (final attribution in region.attributions) {
|
||||
await tester.scrollUntilVisible(
|
||||
find.text(attribution.text),
|
||||
200,
|
||||
scrollable: find.byType(Scrollable).first,
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
find.text(attribution.text),
|
||||
findsOneWidget,
|
||||
|
||||
Reference in New Issue
Block a user