1: <?php
2:
3: namespace Mapbender\WmsBundle\Entity;
4:
5: use Doctrine\Common\Collections\ArrayCollection;
6: use Doctrine\ORM\EntityManager;
7: use Doctrine\ORM\Mapping as ORM;
8: use Mapbender\WmsBundle\Component\WmsInstanceConfiguration;
9: use Mapbender\WmsBundle\Component\WmsInstanceConfigurationOptions;
10: use Mapbender\CoreBundle\Entity\SourceInstance;
11: use Mapbender\WmsBundle\Entity\WmsInstanceLayer;
12: use Mapbender\WmsBundle\Entity\WmsSource;
13: use Mapbender\WmsBundle\Component\Style;
14: use Mapbender\WmsBundle\Component\OnlineResource;
15: use Mapbender\WmsBundle\Component\LegendUrl;
16:
17: 18: 19: 20: 21: 22: 23: 24: 25:
26: class WmsInstance extends SourceInstance
27: {
28:
29: 30: 31: 32:
33: protected $configuration;
34:
35: 36: 37: 38:
39: protected $source;
40:
41: 42: 43: 44: 45:
46: protected $layers;
47:
48: 49: 50:
51: protected $srs;
52:
53: 54: 55:
56: protected $format;
57:
58: 59: 60:
61: protected $infoformat;
62:
63: 64: 65:
66: protected $exceptionformat = null;
67:
68: 69: 70:
71: protected $transparency = true;
72:
73: 74: 75:
76: protected $visible = true;
77:
78: 79: 80:
81: protected $opacity = 100;
82:
83: 84: 85:
86: protected $proxy = false;
87:
88: 89: 90:
91: protected $tiled = false;
92:
93: public function __construct()
94: {
95: $this->layers = new ArrayCollection();
96: }
97:
98: 99: 100: 101: 102:
103: public function setId($id)
104: {
105: $this->id = $id;
106:
107: return $this;
108: }
109:
110: 111: 112: 113: 114:
115: public function getId()
116: {
117: return $this->id;
118: }
119:
120: 121: 122: 123: 124:
125: public function setConfiguration($configuration)
126: {
127: $this->configuration = $configuration;
128: return $this;
129: }
130:
131: 132: 133: 134: 135:
136: public function getConfiguration()
137: {
138: if($this->getSource() === null)
139: {
140: $this->generateYmlConfiguration();
141: } else
142: {
143: if($this->configuration === null)
144: {
145: $this->generateConfiguration();
146: }
147: }
148: return $this->configuration;
149: }
150:
151: 152: 153:
154: public function generateYmlConfiguration()
155: {
156: $this->setSource(new WmsSource());
157: $wmsconf = new WmsInstanceConfiguration();
158: $wmsconf->setType(strtolower($this->getType()));
159: $wmsconf->setTitle($this->title);
160: $wmsconf->setIsBaseSource(true);
161:
162: $options = new WmsInstanceConfigurationOptions();
163: $options->setUrl($this->configuration["url"])
164: ->setProxy($this->proxy)
165: ->setVisible($this->visible)
166: ->setFormat($this->getFormat())
167: ->setInfoformat($this->infoformat)
168: ->setTransparency($this->transparency)
169: ->setOpacity($this->opacity / 100)
170: ->setTiled($this->tiled);
171: $wmsconf->setOptions($options);
172:
173: if(!key_exists("children", $this->configuration))
174: {
175: $num = 0;
176: $rootlayer = new WmsInstanceLayer();
177: $rootlayer->setTitle($this->title)
178: ->setId($num)
179: ->setPriority($num)
180: ->setWmslayersource(new WmsLayerSource())
181: ->setWmsInstance($this);
182: $rootlayer->setToggle(false);
183: $rootlayer->setAllowtoggle(true);
184: $this->addLayer($rootlayer);
185: foreach($this->configuration["layers"] as $layerDef)
186: {
187: $num++;
188: $layer = new WmsInstanceLayer();
189: $layersource = new WmsLayerSource();
190: $layersource->setName($layerDef["name"]);
191: if(isset($layerDef["legendurl"])){
192: $style = new Style();
193: $style->setName(null);
194: $style->setTitle(null);
195: $style->setAbstract(null);
196: $legendUrl = new LegendUrl();
197: $legendUrl->setWidth(null);
198: $legendUrl->setHeight(null);
199: $onlineResource = new OnlineResource();
200: $onlineResource->setFormat(null);
201: $onlineResource->setHref($layerDef["legendurl"]);
202: $legendUrl->setOnlineResource($onlineResource);
203: $style->setLegendUrl($legendUrl);
204: $layersource->addStyle($style);
205: }
206: $layer->setTitle($layerDef["title"])
207: ->setId($this->getId() . '-' . $num)
208: ->setSelected(!isset($layerDef["visible"]) ? false : $layerDef["visible"])
209: ->setInfo(!isset($layerDef["queryable"]) ? false : $layerDef["queryable"])
210: ->setParent($rootlayer)
211: ->setWmslayersource($layersource)
212: ->setWmsInstance($this);
213: $layer->setAllowinfo($layer->getInfo() !== null && $layer->getInfo() ? true : false);
214: $rootlayer->addSublayer($layer);
215: $this->addLayer($layer);
216: }
217: $children = array($this->generateLayersConfiguration($rootlayer));
218: $wmsconf->setChildren($children);
219: } else
220: {
221: $wmsconf->setChildren($this->configuration["children"]);
222: }
223: $this->configuration = $wmsconf->toArray();
224: }
225:
226: 227: 228:
229: public function generateConfiguration()
230: {
231: $rootlayer = $this->getRootlayer();
232: $llbbox = $rootlayer->getWmslayersource()->getLatlonBounds();
233: $srses = array(
234: $llbbox->getSrs() => array(
235: floatval($llbbox->getMinx()),
236: floatval($llbbox->getMiny()),
237: floatval($llbbox->getMaxx()),
238: floatval($llbbox->getMaxy())
239: )
240: );
241: foreach($rootlayer->getWmslayersource()->getBoundingBoxes() as $bbox)
242: {
243: $srses = array_merge($srses,
244: array($bbox->getSrs() => array(
245: floatval($bbox->getMinx()),
246: floatval($bbox->getMiny()),
247: floatval($bbox->getMaxx()),
248: floatval($bbox->getMaxy()))));
249: }
250: $wmsconf = new WmsInstanceConfiguration();
251: $wmsconf->setType(strtolower($this->getType()));
252: $wmsconf->setTitle($this->title);
253: $wmsconf->setIsBaseSource(true);
254:
255: $options = new WmsInstanceConfigurationOptions();
256: $options->setUrl($this->source->getGetMap()->getHttpGet())
257: ->setProxy($this->getProxy())
258: ->setVisible($this->getVisible())
259: ->setFormat($this->getFormat())
260: ->setInfoformat($this->getInfoformat())
261: ->setTransparency($this->transparency)
262: ->setOpacity($this->opacity / 100)
263: ->setTiled($this->tiled)
264: ->setBbox($srses);
265: $wmsconf->setOptions($options);
266: $wmsconf->setChildren(array($this->generateLayersConfiguration($rootlayer)));
267: $this->configuration = $wmsconf->toArray();
268: }
269:
270: 271: 272: 273: 274: 275: 276:
277: private function generateLayersConfiguration(WmsInstanceLayer $layer,
278: $configuration = array())
279: {
280: if($layer->getActive() === true)
281: {
282: $children = array();
283: foreach($layer->getSublayer() as $sublayer)
284: {
285: $configurationTemp = $this->generateLayersConfiguration($sublayer);
286: if(count($configurationTemp) > 0){
287: $children[] = $configurationTemp;
288: }
289: }
290: $layerConf = $layer->getConfiguration();
291: $configuration = array(
292: "options" => $layerConf,
293: "state" => array(
294: "visibility" => null,
295: "info" => null,
296: "outOfScale" => null,
297: "outOfBounds" => null),);
298: if(count($children) > 0)
299: {
300: $configuration["children"] = $children;
301: }
302: }
303: return $configuration;
304: }
305:
306: 307: 308: 309: 310: 311:
312: public function setLayers($layers)
313: {
314: $this->layers = $layers;
315:
316: return $this;
317: }
318:
319: 320: 321: 322: 323:
324: public function getLayers()
325: {
326: return $this->layers;
327: }
328:
329: 330: 331: 332: 333:
334: public function getRootlayer()
335: {
336: foreach($this->layers as $layer)
337: {
338: if($layer->getParent() === null)
339: {
340: return $layer;
341: }
342: }
343: return null;
344: }
345:
346: 347: 348: 349: 350: 351:
352: public function setTitle($title)
353: {
354: $this->title = $title;
355:
356: return $this;
357: }
358:
359: 360: 361: 362: 363:
364: public function getTitle()
365: {
366: return $this->title;
367: }
368:
369: 370: 371: 372: 373: 374:
375: public function setSrs($srs)
376: {
377: $this->srs = $srs;
378:
379: return $this;
380: }
381:
382: 383: 384: 385: 386:
387: public function getSrs()
388: {
389: return $this->srs;
390: }
391:
392: 393: 394: 395: 396: 397:
398: public function setFormat($format)
399: {
400: $this->format = $format;
401:
402: return $this;
403: }
404:
405: 406: 407: 408: 409:
410: public function getFormat()
411: {
412: return $this->format !== null ? $this->format : 'image/png';
413: }
414:
415: 416: 417: 418: 419: 420:
421: public function setInfoformat($infoformat)
422: {
423: $this->infoformat = $infoformat;
424:
425: return $this;
426: }
427:
428: 429: 430: 431: 432:
433: public function getInfoformat()
434: {
435: return $this->infoformat;
436: }
437:
438: 439: 440: 441: 442: 443:
444: public function setExceptionformat($exceptionformat)
445: {
446: $this->exceptionformat = $exceptionformat;
447:
448: return $this;
449: }
450:
451: 452: 453: 454: 455:
456: public function getExceptionformat()
457: {
458: return $this->exceptionformat;
459: }
460:
461: 462: 463: 464: 465: 466:
467: public function setTransparency($transparency)
468: {
469: $this->transparency = $transparency;
470:
471: return $this;
472: }
473:
474: 475: 476: 477: 478:
479: public function getTransparency()
480: {
481: return $this->transparency;
482: }
483:
484: 485: 486: 487: 488: 489:
490: public function setVisible($visible)
491: {
492: $this->visible = $visible;
493:
494: return $this;
495: }
496:
497: 498: 499: 500: 501:
502: public function getVisible()
503: {
504: return $this->visible;
505: }
506:
507: 508: 509: 510: 511: 512:
513: public function setOpacity($opacity)
514: {
515: $this->opacity = $opacity;
516:
517: return $this;
518: }
519:
520: 521: 522: 523: 524:
525: public function getOpacity()
526: {
527: return $this->opacity;
528: }
529:
530: 531: 532: 533: 534: 535:
536: public function setProxy($proxy)
537: {
538: $this->proxy = $proxy;
539:
540: return $this;
541: }
542:
543: 544: 545: 546: 547:
548: public function getProxy()
549: {
550: return $this->proxy;
551: }
552:
553: 554: 555: 556: 557: 558:
559: public function setTiled($tiled)
560: {
561: $this->tiled = $tiled;
562:
563: return $this;
564: }
565:
566: 567: 568: 569: 570:
571: public function getTiled()
572: {
573: return $this->tiled;
574: }
575:
576: 577: 578: 579: 580: 581:
582: public function setSource(WmsSource $wmssource = null)
583: {
584: $this->source = $wmssource;
585:
586: return $this;
587: }
588:
589: 590: 591: 592: 593:
594: public function getSource()
595: {
596: return $this->source;
597: }
598:
599: 600: 601: 602: 603: 604:
605: public function addLayer(WmsInstanceLayer $layer)
606: {
607: $this->layers->add($layer);
608:
609: return $this;
610: }
611:
612: 613: 614: 615: 616:
617: public function removeLayer(WmsInstanceLayer $layers)
618: {
619: $this->layers->removeElement($layers);
620: }
621:
622: 623: 624:
625: public function getType()
626: {
627: return "wms";
628: }
629:
630: 631: 632:
633: public function getManagerType()
634: {
635: return "wms";
636: }
637:
638: 639: 640:
641: public function getAssets()
642: {
643: return array(
644: 'js' => array(
645: '@MapbenderWmsBundle/Resources/public/mapbender.source.wms.js'),
646: 'css' => array());
647: }
648:
649: 650: 651:
652: public function getLayerset()
653: {
654: parent::getLayerset();
655: }
656:
657: 658: 659:
660: public function remove(EntityManager $em)
661: {
662: $this->removeLayerRecursive($em, $this->getRootlayer());
663: $em->remove($this);
664: }
665:
666: 667: 668: 669: 670:
671: private function removeLayerRecursive(EntityManager $em,
672: WmsInstanceLayer $instLayer)
673: {
674: foreach($instLayer->getSublayer() as $sublayer)
675: {
676: $this->removeLayerRecursive($em, $sublayer);
677: }
678: $em->remove($instLayer);
679: $em->flush();
680: }
681:
682: }