-
-
Notifications
You must be signed in to change notification settings - Fork 891
Expand file tree
/
Copy pathcustom_crs.dart
More file actions
173 lines (155 loc) · 6.03 KB
/
custom_crs.dart
File metadata and controls
173 lines (155 loc) · 6.03 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
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map_example/widgets/drawer/menu_drawer.dart';
import 'package:latlong2/latlong.dart';
import 'package:proj4dart/proj4dart.dart' as proj4;
import 'package:url_launcher/url_launcher.dart';
class CustomCrsPage extends StatefulWidget {
static const String route = '/crs_custom';
const CustomCrsPage({super.key});
@override
CustomCrsPageState createState() => CustomCrsPageState();
}
class CustomCrsPageState extends State<CustomCrsPage> {
late final Proj4Crs epsg3413CRS;
double? maxZoom;
// Define start center
proj4.Point point = proj4.Point(x: 65.05166470332148, y: -19.171744826394896);
String initText = 'Map centered to';
late final proj4.Projection epsg4326;
late final proj4.Projection epsg3413;
@override
void initState() {
super.initState();
// EPSG:4326 is a predefined projection ships with proj4dart
epsg4326 = proj4.Projection.get('EPSG:4326')!;
// EPSG:3413 is a user-defined projection from a valid Proj4 definition string
// From: http://epsg.io/3413, proj definition: http://epsg.io/3413.proj4
// Find Projection by name or define it if not exists
epsg3413 = proj4.Projection.get('EPSG:3413') ??
proj4.Projection.add('EPSG:3413',
'+proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs');
// 9 example zoom level resolutions
final resolutions = <double>[
32768,
16384,
8192,
4096,
2048,
1024,
512,
256,
128,
];
final epsg3413Bounds = Bounds<double>(
const Point<double>(-4511619, -4511336),
const Point<double>(4510883, 4510996),
);
maxZoom = (resolutions.length - 1).toDouble();
// Define CRS
epsg3413CRS = Proj4Crs.fromFactory(
// CRS code
code: 'EPSG:3413',
// your proj4 delegate
proj4Projection: epsg3413,
// Resolution factors (projection units per pixel, for example meters/pixel)
// for zoom levels; specify either scales or resolutions, not both
resolutions: resolutions,
// Bounds of the CRS, in projected coordinates
// (if not specified, the layer's which uses this CRS will be infinite)
bounds: epsg3413Bounds,
// Tile origin, in projected coordinates, if set, this overrides the transformation option
// Some goeserver changes origin based on zoom level
// and some are not at all (use explicit/implicit null or use [Point(0, 0)])
// @see https://github.com/kartena/Proj4Leaflet/pull/171
origins: const [Point(0, 0)],
// Scale factors (pixels per projection unit, for example pixels/meter) for zoom levels;
// specify either scales or resolutions, not both
scales: null,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Custom CRS')),
drawer: const MenuDrawer(CustomCrsPage.route),
body: Padding(
padding: const EdgeInsets.all(8),
child: Column(
children: [
const Padding(
padding: EdgeInsets.only(top: 8, bottom: 2),
child: Text(
'This map is in EPSG:3413',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.blue,
fontSize: 16,
),
),
),
Padding(
padding: const EdgeInsets.only(top: 8, bottom: 2),
child: Text(
'$initText (${point.x.toStringAsFixed(5)}, ${point.y.toStringAsFixed(5)}) in EPSG:4326.',
),
),
Padding(
padding: const EdgeInsets.only(top: 2, bottom: 2),
child: Text(
'Which is (${epsg4326.transform(epsg3413, point).x.toStringAsFixed(2)}, ${epsg4326.transform(epsg3413, point).y.toStringAsFixed(2)}) in EPSG:3413.',
),
),
const Padding(
padding: EdgeInsets.only(top: 2, bottom: 8),
child: Text('Tap on map to get more coordinates!'),
),
Flexible(
child: FlutterMap(
options: MapOptions(
// Set the default CRS
crs: epsg3413CRS,
initialCenter: LatLng(point.x, point.y),
initialZoom: 3,
// Set maxZoom usually scales.length - 1 OR resolutions.length - 1
// but not greater
maxZoom: maxZoom,
onTap: (tapPosition, p) => setState(() {
initText = 'You clicked at';
point = proj4.Point(x: p.latitude, y: p.longitude);
}),
),
children: [
TileLayer(
wmsOptions: WMSTileLayerOptions(
crs: epsg3413CRS,
transparent: true,
format: 'image/jpeg',
baseUrl:
'https://www.gebco.net/data_and_products/gebco_web_services/north_polar_view_wms/mapserv?',
layers: const ['gebco_north_polar_view'],
),
),
RichAttributionWidget(
popupInitialDisplayDuration: const Duration(seconds: 5),
attributions: [
TextSourceAttribution(
'Imagery reproduced from the GEBCO_2022 Grid, GEBCO Compilation Group (2022) GEBCO 2022 Grid (doi:10.5285/e0f0bb80-ab44-2739-e053-6c86abc0289c)',
onTap: () => launchUrl(
Uri.parse(
'https://www.gebco.net/data_and_products/gebco_web_services/web_map_service/#polar',
),
),
),
],
),
],
),
),
],
),
),
);
}
}