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($this->getId()."_".$num)
179: ->setMinScale(!isset($this->configuration["minScale"]) ? null : $this->configuration["minScale"])
180: ->setMaxScale(!isset($this->configuration["maxScale"]) ? null : $this->configuration["maxScale"])
181: ->setSelected(!isset($this->configuration["visible"]) ? false : $this->configuration["visible"])
182: ->setPriority($num)
183: ->setWmslayersource(new WmsLayerSource())
184: ->setWmsInstance($this);
185: $rootlayer->setToggle(false);
186: $rootlayer->setAllowtoggle(true);
187: $this->addLayer($rootlayer);
188: foreach($this->configuration["layers"] as $layerDef)
189: {
190: $num++;
191: $layer = new WmsInstanceLayer();
192: $layersource = new WmsLayerSource();
193: $layersource->setName($layerDef["name"]);
194: if(isset($layerDef["legendurl"])){
195: $style = new Style();
196: $style->setName(null);
197: $style->setTitle(null);
198: $style->setAbstract(null);
199: $legendUrl = new LegendUrl();
200: $legendUrl->setWidth(null);
201: $legendUrl->setHeight(null);
202: $onlineResource = new OnlineResource();
203: $onlineResource->setFormat(null);
204: $onlineResource->setHref($layerDef["legendurl"]);
205: $legendUrl->setOnlineResource($onlineResource);
206: $style->setLegendUrl($legendUrl);
207: $layersource->addStyle($style);
208: }
209: $layer->setTitle($layerDef["title"])
210: ->setId($this->getId() . '-' . $num)
211: ->setMinScale(!isset($layerDef["minScale"]) ? null : $layerDef["minScale"])
212: ->setMaxScale(!isset($layerDef["maxScale"]) ? null : $layerDef["maxScale"])
213: ->setSelected(!isset($layerDef["visible"]) ? false : $layerDef["visible"])
214: ->setInfo(!isset($layerDef["queryable"]) ? false : $layerDef["queryable"])
215: ->setParent($rootlayer)
216: ->setWmslayersource($layersource)
217: ->setWmsInstance($this);
218: $layer->setAllowinfo($layer->getInfo() !== null && $layer->getInfo() ? true : false);
219: $rootlayer->addSublayer($layer);
220: $this->addLayer($layer);
221: }
222: $children = array($this->generateLayersConfiguration($rootlayer));
223: $wmsconf->setChildren($children);
224: } else
225: {
226: $wmsconf->setChildren($this->configuration["children"]);
227: }
228: $this->configuration = $wmsconf->toArray();
229: }
230:
231: 232: 233:
234: public function generateConfiguration()
235: {
236: $rootlayer = $this->getRootlayer();
237: $llbbox = $rootlayer->getWmslayersource()->getLatlonBounds();
238: $srses = array(
239: $llbbox->getSrs() => array(
240: floatval($llbbox->getMinx()),
241: floatval($llbbox->getMiny()),
242: floatval($llbbox->getMaxx()),
243: floatval($llbbox->getMaxy())
244: )
245: );
246: foreach($rootlayer->getWmslayersource()->getBoundingBoxes() as $bbox)
247: {
248: $srses = array_merge($srses,
249: array($bbox->getSrs() => array(
250: floatval($bbox->getMinx()),
251: floatval($bbox->getMiny()),
252: floatval($bbox->getMaxx()),
253: floatval($bbox->getMaxy()))));
254: }
255: $wmsconf = new WmsInstanceConfiguration();
256: $wmsconf->setType(strtolower($this->getType()));
257: $wmsconf->setTitle($this->title);
258: $wmsconf->setIsBaseSource(true);
259:
260: $options = new WmsInstanceConfigurationOptions();
261: $options->setUrl($this->source->getGetMap()->getHttpGet())
262: ->setProxy($this->getProxy())
263: ->setVisible($this->getVisible())
264: ->setFormat($this->getFormat())
265: ->setInfoformat($this->getInfoformat())
266: ->setTransparency($this->transparency)
267: ->setOpacity($this->opacity / 100)
268: ->setTiled($this->tiled)
269: ->setBbox($srses);
270: $wmsconf->setOptions($options);
271: $wmsconf->setChildren(array($this->generateLayersConfiguration($rootlayer)));
272: $this->configuration = $wmsconf->toArray();
273: }
274:
275: 276: 277: 278: 279: 280: 281:
282: public function generateLayersConfiguration(WmsInstanceLayer $layer,
283: $configuration = array())
284: {
285: if($layer->getActive() === true)
286: {
287: $children = array();
288: foreach($layer->getSublayer() as $sublayer)
289: {
290: $configurationTemp = $this->generateLayersConfiguration($sublayer);
291: if(count($configurationTemp) > 0){
292: $children[] = $configurationTemp;
293: }
294: }
295: $layerConf = $layer->getConfiguration();
296: $configuration = array(
297: "options" => $layerConf,
298: "state" => array(
299: "visibility" => null,
300: "info" => null,
301: "outOfScale" => null,
302: "outOfBounds" => null),);
303: if(count($children) > 0)
304: {
305: $configuration["children"] = $children;
306: }
307: }
308: return $configuration;
309: }
310:
311: 312: 313: 314: 315: 316:
317: public function setLayers($layers)
318: {
319: $this->layers = $layers;
320:
321: return $this;
322: }
323:
324: 325: 326: 327: 328:
329: public function getLayers()
330: {
331: return $this->layers;
332: }
333:
334: 335: 336: 337: 338:
339: public function getRootlayer()
340: {
341: foreach($this->layers as $layer)
342: {
343: if($layer->getParent() === null)
344: {
345: return $layer;
346: }
347: }
348: return null;
349: }
350:
351: 352: 353: 354: 355: 356:
357: public function setTitle($title)
358: {
359: $this->title = $title;
360:
361: return $this;
362: }
363:
364: 365: 366: 367: 368:
369: public function getTitle()
370: {
371: return $this->title;
372: }
373:
374: 375: 376: 377: 378: 379:
380: public function setSrs($srs)
381: {
382: $this->srs = $srs;
383:
384: return $this;
385: }
386:
387: 388: 389: 390: 391:
392: public function getSrs()
393: {
394: return $this->srs;
395: }
396:
397: 398: 399: 400: 401: 402:
403: public function setFormat($format)
404: {
405: $this->format = $format;
406:
407: return $this;
408: }
409:
410: 411: 412: 413: 414:
415: public function getFormat()
416: {
417: return $this->format !== null ? $this->format : 'image/png';
418: }
419:
420: 421: 422: 423: 424: 425:
426: public function setInfoformat($infoformat)
427: {
428: $this->infoformat = $infoformat;
429:
430: return $this;
431: }
432:
433: 434: 435: 436: 437:
438: public function getInfoformat()
439: {
440: return $this->infoformat;
441: }
442:
443: 444: 445: 446: 447: 448:
449: public function setExceptionformat($exceptionformat)
450: {
451: $this->exceptionformat = $exceptionformat;
452:
453: return $this;
454: }
455:
456: 457: 458: 459: 460:
461: public function getExceptionformat()
462: {
463: return $this->exceptionformat;
464: }
465:
466: 467: 468: 469: 470: 471:
472: public function setTransparency($transparency)
473: {
474: $this->transparency = $transparency;
475:
476: return $this;
477: }
478:
479: 480: 481: 482: 483:
484: public function getTransparency()
485: {
486: return $this->transparency;
487: }
488:
489: 490: 491: 492: 493: 494:
495: public function setVisible($visible)
496: {
497: $this->visible = $visible;
498:
499: return $this;
500: }
501:
502: 503: 504: 505: 506:
507: public function getVisible()
508: {
509: return $this->visible;
510: }
511:
512: 513: 514: 515: 516: 517:
518: public function setOpacity($opacity)
519: {
520: $this->opacity = $opacity;
521:
522: return $this;
523: }
524:
525: 526: 527: 528: 529:
530: public function getOpacity()
531: {
532: return $this->opacity;
533: }
534:
535: 536: 537: 538: 539: 540:
541: public function setProxy($proxy)
542: {
543: $this->proxy = $proxy;
544:
545: return $this;
546: }
547:
548: 549: 550: 551: 552:
553: public function getProxy()
554: {
555: return $this->proxy;
556: }
557:
558: 559: 560: 561: 562: 563:
564: public function setTiled($tiled)
565: {
566: $this->tiled = $tiled;
567:
568: return $this;
569: }
570:
571: 572: 573: 574: 575:
576: public function getTiled()
577: {
578: return $this->tiled;
579: }
580:
581: 582: 583: 584: 585: 586:
587: public function setSource(WmsSource $wmssource = null)
588: {
589: $this->source = $wmssource;
590:
591: return $this;
592: }
593:
594: 595: 596: 597: 598:
599: public function getSource()
600: {
601: return $this->source;
602: }
603:
604: 605: 606: 607: 608: 609:
610: public function addLayer(WmsInstanceLayer $layer)
611: {
612: $this->layers->add($layer);
613:
614: return $this;
615: }
616:
617: 618: 619: 620: 621:
622: public function removeLayer(WmsInstanceLayer $layers)
623: {
624: $this->layers->removeElement($layers);
625: }
626:
627: 628: 629:
630: public function getType()
631: {
632: return "wms";
633: }
634:
635: 636: 637:
638: public function getManagerType()
639: {
640: return "wms";
641: }
642:
643: 644: 645:
646: public function getAssets()
647: {
648: return array(
649: 'js' => array(
650: '@MapbenderWmsBundle/Resources/public/mapbender.source.wms.js'),
651: 'css' => array());
652: }
653:
654: 655: 656:
657: public function getLayerset()
658: {
659: parent::getLayerset();
660: }
661:
662: 663: 664:
665: public function remove(EntityManager $em)
666: {
667: $this->removeLayerRecursive($em, $this->getRootlayer());
668: $em->remove($this);
669: }
670:
671: 672: 673: 674: 675:
676: private function removeLayerRecursive(EntityManager $em,
677: WmsInstanceLayer $instLayer)
678: {
679: foreach($instLayer->getSublayer() as $sublayer)
680: {
681: $this->removeLayerRecursive($em, $sublayer);
682: }
683: $em->remove($instLayer);
684: $em->flush();
685: }
686:
687: }