@@ -267,54 +267,73 @@ func (v2Service WMSService) GetTopLayer() (*WMSLayer, error) {
267267 return nil , errors .New ("unable to detect the toplayer of this WMS service" )
268268}
269269
270- func (v2Service WMSService ) GetChildLayers (parent WMSLayer ) ([]WMSLayer , error ) {
271- children := make ([]WMSLayer , 0 )
272-
270+ // MapLayersToV3
271+ func (v2Service WMSService ) MapLayersToV3 () pdoknlv3.Layer {
272+ // Creates map of Groups: layers in that group
273+ // and a list of all layers without a group
274+ groupedLayers := map [string ][]pdoknlv3.Layer {}
275+ var notGroupedLayers []pdoknlv3.Layer
273276 for _ , layer := range v2Service .Layers {
274- if layer .Group != nil && * layer .Group == parent .Name {
275- children = append (children , layer )
277+ if layer .Group == nil {
278+ notGroupedLayers = append (notGroupedLayers , layer .MapToV3 (v2Service ))
279+ } else {
280+ groupedLayers [* layer .Group ] = append (groupedLayers [* layer .Group ], layer .MapToV3 (v2Service ))
276281 }
277282 }
278283
279- if len (children ) == 0 {
280- return children , errors .New ("no child layers found" )
284+ // if a topLayer is defined in the v2 it be the only layer without a group
285+ // and there are other layers that have the topLayer as their group
286+ // so if there is exactly 1 layer without a group
287+ // and the name of that layer exist as a key in the map of Groups: layer in that group
288+ // then that layer must be the topLayer
289+ var topLayer * pdoknlv3.Layer
290+ if len (notGroupedLayers ) == 1 {
291+ _ , ok := groupedLayers [* notGroupedLayers [0 ].Name ]
292+ if ok {
293+ topLayer = & notGroupedLayers [0 ]
294+ }
281295 }
282296
283- return children , nil
284- }
285-
286- // MapLayersToV3
287- func (v2Service WMSService ) MapLayersToV3 () pdoknlv3.Layer {
288- topLayer , err := v2Service .GetTopLayer ()
289- if err != nil {
290- panic (err )
291- }
297+ var middleLayers []pdoknlv3.Layer
292298
293- var layer pdoknlv3.Layer
299+ // if the topLayer is not defined in the v2 layers
300+ // it needs to be created with defaults from the service
301+ // and in this case the middleLayers are all layers without a group
294302 if topLayer == nil {
295- layer = pdoknlv3.Layer {
296- Name : "wms" ,
303+ topLayer = & pdoknlv3.Layer {
297304 Title : & v2Service .Title ,
298305 Abstract : & v2Service .Abstract ,
299306 Keywords : v2Service .Keywords ,
300307 Layers : & []pdoknlv3.Layer {},
301308 }
302309
303- var childLayersV3 []pdoknlv3.Layer
304- for _ , childLayer := range v2Service .Layers {
305- childLayersV3 = append (childLayersV3 , childLayer .MapToV3 (v2Service ))
310+ // adding the bottom layers to the middle layers they are grouped by
311+ for _ , layer := range notGroupedLayers {
312+ bottomLayers := groupedLayers [* layer .Name ]
313+ layer .Layers = & bottomLayers
314+ middleLayers = append (middleLayers , layer )
306315 }
307- layer .Layers = & childLayersV3
308- } else {
309- layer = topLayer .MapToV3 (v2Service )
310316 }
311317
312- return layer
318+ // if the topLayer is defined in the v2 layers
319+ // meaning the topLayer has a name at this point
320+ // then the middleLayers are all layers that had the topLayer name as their group
321+ // and the bottomLayers are all layers that had a middleLayer as a group
322+ if topLayer .Name != nil {
323+ for _ , layer := range groupedLayers [* topLayer .Name ] {
324+ bottomLayers := groupedLayers [* layer .Name ]
325+ layer .Layers = & bottomLayers
326+ middleLayers = append (middleLayers , layer )
327+ }
328+ }
329+ topLayer .Layers = & middleLayers
330+
331+ return * topLayer
313332}
314333
315334func (v2Layer WMSLayer ) MapToV3 (v2Service WMSService ) pdoknlv3.Layer {
316335 layer := pdoknlv3.Layer {
317- Name : v2Layer .Name ,
336+ Name : & v2Layer .Name ,
318337 Title : v2Layer .Title ,
319338 Abstract : v2Layer .Abstract ,
320339 Keywords : v2Layer .Keywords ,
@@ -382,17 +401,6 @@ func (v2Layer WMSLayer) MapToV3(v2Service WMSService) pdoknlv3.Layer {
382401
383402 if v2Layer .Data != nil {
384403 layer .Data = Pointer (ConvertV2DataToV3 (* v2Layer .Data ))
385- } else {
386- childLayersV2 , err := v2Service .GetChildLayers (v2Layer )
387- if err != nil {
388- panic (err )
389- }
390-
391- var childLayersV3 []pdoknlv3.Layer
392- for _ , childLayer := range childLayersV2 {
393- childLayersV3 = append (childLayersV3 , childLayer .MapToV3 (v2Service ))
394- }
395- layer .Layers = & childLayersV3
396404 }
397405
398406 return layer
@@ -401,7 +409,7 @@ func (v2Layer WMSLayer) MapToV3(v2Service WMSService) pdoknlv3.Layer {
401409func mapV3LayerToV2Layers (v3Layer pdoknlv3.Layer , parent * pdoknlv3.Layer , serviceEPSG string ) []WMSLayer {
402410 var layers []WMSLayer
403411
404- if parent == nil && v3Layer .Name == "wms" {
412+ if parent == nil && * v3Layer .Name == "wms" {
405413 // Default top layer, do not include in v2 layers
406414 if v3Layer .Layers != nil {
407415 for _ , childLayer := range * v3Layer .Layers {
@@ -410,7 +418,7 @@ func mapV3LayerToV2Layers(v3Layer pdoknlv3.Layer, parent *pdoknlv3.Layer, servic
410418 }
411419 } else {
412420 v2Layer := WMSLayer {
413- Name : v3Layer .Name ,
421+ Name : * v3Layer .Name ,
414422 Title : v3Layer .Title ,
415423 Abstract : v3Layer .Abstract ,
416424 Keywords : v3Layer .Keywords ,
@@ -421,7 +429,7 @@ func mapV3LayerToV2Layers(v3Layer pdoknlv3.Layer, parent *pdoknlv3.Layer, servic
421429 v2Layer .Visible = PointerVal (v3Layer .Visible , true )
422430
423431 if parent != nil {
424- v2Layer .Group = & parent .Name
432+ v2Layer .Group = parent .Name
425433 }
426434
427435 if v3Layer .DatasetMetadataURL != nil && v3Layer .DatasetMetadataURL .CSW != nil {
0 commit comments