Skip to content

Commit fa9f2fd

Browse files
committed
download WMS TMS definitions from the server
1 parent 87ef34a commit fa9f2fd

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

lib/com/hydrologis/smash/import_export_plugins/import/gss/gss_import.dart

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class _GssImportWidgetState extends State<GssImportWidget>
8282
final List<dynamic> _baseMapsList = [];
8383
final List<dynamic> _projectsList = [];
8484
final List<dynamic> _tagsList = [];
85+
final Map<String, List<String>> _backGroundLayersMap = {"WMS": [], "TMS": []};
8586

8687
@override
8788
void afterFirstLayout(BuildContext context) {
@@ -143,6 +144,10 @@ class _GssImportWidgetState extends State<GssImportWidget>
143144
}
144145
}
145146

147+
var tmpBackGroundLayersMap = await ServerApi.getBackGroundLayers();
148+
_backGroundLayersMap["WMS"]!.addAll(tmpBackGroundLayersMap["WMS"]!);
149+
_backGroundLayersMap["TMS"]!.addAll(tmpBackGroundLayersMap["TMS"]!);
150+
146151
setState(() {
147152
_status = 1;
148153
});
@@ -419,6 +424,57 @@ class _GssImportWidgetState extends State<GssImportWidget>
419424
),
420425
),
421426
),
427+
Container(
428+
width: double.infinity,
429+
child: Card(
430+
margin: SmashUI.defaultMargin(),
431+
elevation: SmashUI.DEFAULT_ELEVATION,
432+
color: SmashColors.mainBackground,
433+
child: Column(
434+
children: <Widget>[
435+
Padding(
436+
padding: SmashUI.defaultPadding(),
437+
child: SmashUI.normalText("WMS/TMS Background Maps",
438+
bold: true),
439+
),
440+
Padding(
441+
padding: SmashUI.defaultPadding(),
442+
child: SmashUI.smallText(
443+
_backGroundLayersMap["WMS"]!.isEmpty &&
444+
_backGroundLayersMap["TMS"]!.isEmpty
445+
? "No WMS/TMS definitions available."
446+
: "WMS/TMS definitions are installed into the app and visible in the online layer view.",
447+
color: Colors.grey),
448+
),
449+
ListTile(
450+
leading: Icon(
451+
MdiIcons.earth,
452+
color: SmashColors.mainDecorations,
453+
),
454+
title: Text(
455+
"${_backGroundLayersMap["WMS"]!.length} WMS, ${_backGroundLayersMap["TMS"]!.length} TMS available."),
456+
trailing: Icon(
457+
Icons.file_download,
458+
color: SmashColors.mainDecorations,
459+
),
460+
onTap: () async {
461+
// add WMS and TMS to the preferences
462+
var wmsList = _backGroundLayersMap["WMS"];
463+
wmsList!.forEach((element) {
464+
GpPreferences().addNewWms(element);
465+
});
466+
var tmsList = _backGroundLayersMap["TMS"];
467+
tmsList!.forEach((element) {
468+
GpPreferences().addNewTms(element);
469+
});
470+
SmashDialogs.showToast(context,
471+
"WMS/TMS definitions added to online layers list.");
472+
},
473+
),
474+
],
475+
),
476+
),
477+
),
422478
],
423479
),
424480
);

lib/com/hydrologis/smash/import_export_plugins/utils/gss_server_api.dart

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:convert';
22
import 'package:dart_hydrologis_utils/dart_hydrologis_utils.dart';
33
import 'package:http/http.dart';
44
import 'package:smash_import_export_plugins/smash_import_export_plugins.dart';
5+
import 'package:smashlibs/com/hydrologis/flutterlibs/utils/logging.dart';
56
import 'dart:typed_data';
67

78
import 'package:smashlibs/smashlibs.dart';
@@ -232,6 +233,82 @@ class ServerApi {
232233
throw new StateError(response.body);
233234
}
234235
}
236+
237+
static const LAYERSKEY_URL = 'url';
238+
static const LAYERSKEY_TYPE = 'type';
239+
static const LAYERSKEY_FORMAT = 'format';
240+
static const LAYERSKEY_LABEL = 'label';
241+
static const LAYERSKEY_SRID = 'srid';
242+
static const LAYERSKEY_ISVISIBLE = 'isvisible';
243+
static const LAYERSKEY_OPACITY = 'opacity';
244+
static const LAYERSKEY_WMSVERSION = 'wmsversion';
245+
static const LAYERSKEY_ATTRIBUTION = 'attribution';
246+
static const LAYERSKEY_SUBDOMAINS = 'subdomains';
247+
static const LAYERSKEY_MINZOOM = 'minzoom';
248+
static const LAYERSKEY_MAXZOOM = 'maxzoom';
249+
250+
static Future<Map<String, List<String>>> getBackGroundLayers() async {
251+
var tokenHeader = getTokenHeader();
252+
Project? project = getCurrentGssProject();
253+
if (project == null) {
254+
throw StateError("No project was selected.");
255+
}
256+
Map<String, List<String>> layers = {"WMS": [], "TMS": []};
257+
258+
try {
259+
var uri = Uri.parse(
260+
getBaseUrl() + API_WMSSOURCES + "?$API_PROJECT_PARAM${project.id}");
261+
var response = await get(uri, headers: tokenHeader);
262+
if (response.statusCode == 200) {
263+
var list = jsonDecode(response.body);
264+
for (var item in list) {
265+
var json = '''
266+
{
267+
"$LAYERSKEY_LABEL": "${item['layername']}",
268+
"$LAYERSKEY_URL":"${item['getcapabilities']}",
269+
"$LAYERSKEY_ISVISIBLE": true,
270+
"$LAYERSKEY_OPACITY": ${item['opacity'] * 100},
271+
"$LAYERSKEY_FORMAT": "${item['imageformat']}",
272+
"$LAYERSKEY_ATTRIBUTION": "${item['attribution']}",
273+
"$LAYERSKEY_SRID": ${item['epsg']},
274+
"$LAYERSKEY_WMSVERSION": "${item['version']}",
275+
"$LAYERSKEY_TYPE": "wms"
276+
}
277+
''';
278+
layers['WMS']!.add(json);
279+
}
280+
}
281+
uri = Uri.parse(
282+
getBaseUrl() + API_TMSSOURCES + "?$API_PROJECT_PARAM${project.id}");
283+
response = await get(uri, headers: tokenHeader);
284+
if (response.statusCode == 200) {
285+
var list = jsonDecode(response.body);
286+
for (var item in list) {
287+
var subdomains = item['subdomains'];
288+
var json = '''
289+
{
290+
"$LAYERSKEY_LABEL": "${item['label']}",
291+
"$LAYERSKEY_URL": "${item['urltemplate']}",
292+
"$LAYERSKEY_MINZOOM": 1,
293+
"$LAYERSKEY_MAXZOOM": ${item['maxzoom']},
294+
"$LAYERSKEY_OPACITY": ${item['opacity'] * 100},
295+
"$LAYERSKEY_ATTRIBUTION": "${item['attribution']}",
296+
"$LAYERSKEY_TYPE": "tms",
297+
"$LAYERSKEY_ISVISIBLE": true ${subdomains != null && subdomains.isNotEmpty ? "," : ""}
298+
${subdomains != null ? "\"subdomains\": \"${subdomains.join(',')}\"" : ""}
299+
}
300+
''';
301+
layers['TMS']!.add(json);
302+
}
303+
}
304+
} catch (e) {
305+
if (e is Exception) {
306+
SMLogger().e("ERROR", e, null);
307+
}
308+
print(e);
309+
}
310+
return layers;
311+
}
235312
}
236313

237314
class Project {

0 commit comments

Comments
 (0)