@@ -268,72 +268,67 @@ func (v2Service WMSService) GetTopLayer() (*WMSLayer, error) {
268268 return nil , errors .New ("unable to detect the toplayer of this WMS service" )
269269}
270270
271- func (v2Service WMSService ) GetChildLayers (parent WMSLayer ) ([]WMSLayer , error ) {
272- children := make ([]WMSLayer , 0 )
273-
271+ // MapLayersToV3
272+ func (v2Service WMSService ) MapLayersToV3 () pdoknlv3.Layer {
273+ // Creates map of Groups: layers in that group
274+ // and a list of all layers without a group
275+ groupedLayers := map [string ][]pdoknlv3.Layer {}
276+ var notGroupedLayers []pdoknlv3.Layer
274277 for _ , layer := range v2Service .Layers {
275- if layer .Group != nil && * layer .Group == parent .Name {
276- children = append (children , layer )
278+ if layer .Group == nil {
279+ notGroupedLayers = append (notGroupedLayers , layer .MapToV3 (v2Service ))
280+ } else {
281+ groupedLayers [* layer .Group ] = append (groupedLayers [* layer .Group ], layer .MapToV3 (v2Service ))
277282 }
278283 }
279284
280- if len (children ) == 0 {
281- return children , errors .New ("no child layers found" )
285+ // if a topLayer is defined in the v2 it be the only layer without a group
286+ // and there are other layers that have the topLayer as their group
287+ // so if there is exactly 1 layer without a group
288+ // and the name of that layer exist as a key in the map of Groups: layer in that group
289+ // then that layer must be the topLayer
290+ var topLayer * pdoknlv3.Layer
291+ if len (notGroupedLayers ) == 1 {
292+ _ , ok := groupedLayers [* notGroupedLayers [0 ].Name ]
293+ if ok {
294+ topLayer = & notGroupedLayers [0 ]
295+ }
282296 }
283297
284- return children , nil
285- }
298+ var middleLayers []pdoknlv3.Layer
286299
287- // MapLayersToV3
288- func (v2Service WMSService ) MapLayersToV3 () pdoknlv3.Layer {
289- topLayer , err := v2Service .GetTopLayer ()
290- if err != nil {
291- panic (err )
292- }
293-
294- var layer pdoknlv3.Layer
300+ // if the topLayer is not defined in the v2 layers
301+ // it needs to be created with defaults from the service
302+ // and in this case the middleLayers are all layers without a group
295303 if topLayer == nil {
296-
297- boundingBoxes := make ([]pdoknlv3.WMSBoundingBox , 0 )
298- if v2Service .Extent != nil {
299-
300- bboxStringList := strings .Split (* v2Service .Extent , " " )
301- bbox := pdoknlv3.WMSBoundingBox {
302- CRS : v2Service .DataEPSG ,
303- BBox : sharedModel.BBox {
304- MinX : bboxStringList [0 ],
305- MaxX : bboxStringList [2 ],
306- MinY : bboxStringList [1 ],
307- MaxY : bboxStringList [3 ],
308- },
309- }
310- boundingBoxes = append (boundingBoxes , bbox )
311- }
312-
313- layer = pdoknlv3.Layer {
314- Name : "wms" ,
315- Title : & v2Service .Title ,
316- Abstract : & v2Service .Abstract ,
317- Keywords : v2Service .Keywords ,
318- BoundingBoxes : boundingBoxes ,
319- Layers : & []pdoknlv3.Layer {},
304+ topLayer = & pdoknlv3.Layer {
305+ Title : & v2Service .Title ,
306+ Abstract : & v2Service .Abstract ,
307+ Keywords : v2Service .Keywords ,
308+ Layers : & []pdoknlv3.Layer {},
320309 }
310+ middleLayers = notGroupedLayers
311+ }
321312
322- var childLayersV3 []pdoknlv3.Layer
323- for _ , childLayer := range v2Service .Layers {
324- childLayersV3 = append (childLayersV3 , childLayer .MapToV3 (v2Service ))
313+ // if the topLayer is defined in the v2 layers
314+ // meaning the topLayer has a name at this point
315+ // then the middleLayers are all layers that had the topLayer name as their group
316+ // and the bottomLayers are all layers that had a middleLayer as a group
317+ if topLayer .Name != nil {
318+ for _ , layer := range groupedLayers [* topLayer .Name ] {
319+ bottomLayers := groupedLayers [* layer .Name ]
320+ layer .Layers = & bottomLayers
321+ middleLayers = append (middleLayers , layer )
325322 }
326- layer .Layers = & childLayersV3
327- } else {
328- layer = topLayer .MapToV3 (v2Service )
329323 }
324+ topLayer .Layers = & middleLayers
330325
331- return layer
326+ return * topLayer
332327}
333328
334329func (v2Layer WMSLayer ) MapToV3 (v2Service WMSService ) pdoknlv3.Layer {
335330 layer := pdoknlv3.Layer {
336- Name : v2Layer .Name ,
331+ Name : & v2Layer .Name ,
337332 Title : v2Layer .Title ,
338333 Abstract : v2Layer .Abstract ,
339334 Keywords : v2Layer .Keywords ,
@@ -401,17 +396,6 @@ func (v2Layer WMSLayer) MapToV3(v2Service WMSService) pdoknlv3.Layer {
401396
402397 if v2Layer .Data != nil {
403398 layer .Data = Pointer (ConvertV2DataToV3 (* v2Layer .Data ))
404- } else {
405- childLayersV2 , err := v2Service .GetChildLayers (v2Layer )
406- if err != nil {
407- panic (err )
408- }
409-
410- var childLayersV3 []pdoknlv3.Layer
411- for _ , childLayer := range childLayersV2 {
412- childLayersV3 = append (childLayersV3 , childLayer .MapToV3 (v2Service ))
413- }
414- layer .Layers = & childLayersV3
415399 }
416400
417401 return layer
@@ -420,7 +404,7 @@ func (v2Layer WMSLayer) MapToV3(v2Service WMSService) pdoknlv3.Layer {
420404func mapV3LayerToV2Layers (v3Layer pdoknlv3.Layer , parent * pdoknlv3.Layer , serviceEPSG string ) []WMSLayer {
421405 var layers []WMSLayer
422406
423- if parent == nil && v3Layer .Name == "wms" {
407+ if parent == nil && * v3Layer .Name == "wms" {
424408 // Default top layer, do not include in v2 layers
425409 if v3Layer .Layers != nil {
426410 for _ , childLayer := range * v3Layer .Layers {
@@ -429,7 +413,7 @@ func mapV3LayerToV2Layers(v3Layer pdoknlv3.Layer, parent *pdoknlv3.Layer, servic
429413 }
430414 } else {
431415 v2Layer := WMSLayer {
432- Name : v3Layer .Name ,
416+ Name : * v3Layer .Name ,
433417 Title : v3Layer .Title ,
434418 Abstract : v3Layer .Abstract ,
435419 Keywords : v3Layer .Keywords ,
@@ -440,7 +424,7 @@ func mapV3LayerToV2Layers(v3Layer pdoknlv3.Layer, parent *pdoknlv3.Layer, servic
440424 v2Layer .Visible = PointerVal (v3Layer .Visible , true )
441425
442426 if parent != nil {
443- v2Layer .Group = & parent .Name
427+ v2Layer .Group = parent .Name
444428 }
445429
446430 if v3Layer .DatasetMetadataURL != nil && v3Layer .DatasetMetadataURL .CSW != nil {
0 commit comments