1: <?php
2: namespace Mapbender\KmlBundle\Element;
3:
4: use Mapbender\CoreBundle\Component\Element;
5: use Symfony\Component\DependencyInjection\ContainerInterface;
6: use Symfony\Component\HttpFoundation\Response;
7:
8: class KmlExport extends Element {
9:
10: public static function getClassTitle() {
11: return "KML export element";
12: }
13:
14: public static function getClassDescription() {
15: return "KML export element";
16: }
17:
18: public static function getClassTags() {
19: return array();
20: }
21:
22:
23: public function getWidgetName() {
24: return 'mapbender.mbKmlExport';
25: }
26:
27: public function getAssets() {
28: return array(
29: 'js' => array(
30: 'mapbender.element.kmlexport.js'
31: ),
32: 'css' => array()
33: );
34: }
35:
36: public function getConfiguration() {
37: $opts = array();
38: $opts['text'] = $this->getClassTitle();
39:
40:
41:
42:
43:
44:
45: return array(
46: 'options' => $opts,
47: 'init' => 'mbKmlExport',
48: );
49: }
50:
51: public function httpAction($action) {
52: switch($action) {
53: case 'mapexport':
54: return $this->map2Kml();
55: }
56: }
57:
58: private function map2Kml() {
59: $response = new Response();
60:
61: $layers = $this->container->get('request')->get('layers');
62: foreach($layers as $title => &$layer) {
63: parse_str($layer, $layer);
64:
65:
66:
67:
68: $layer['params']['WIDTH'] = 512;
69: $layer['params']['HEIGHT'] = 512;
70: $layer['params']['SRS'] = 'EPSG:4326';
71:
72: $delimiter = strpos($layer['options']['url'], '?') === False ?
73: '?' : '&';
74: $layer['getMap'] = $layer['options']['url'] . $delimiter
75: . http_build_query($layer['params']);
76: }
77:
78:
79: $extent = new \rectObj();
80: $extentIn = explode(',', $this->container->get('request')->get('extent'));
81: $extent->setExtent($extentIn[0], $extentIn[1], $extentIn[2], $extentIn[3]);
82:
83: $srs = $this->container->get('request')->get('srs');
84: $srsFrom = new \projectionObj($srs);
85: $srsTo = new \projectionObj('EPSG:4326');
86: $extent->project($srsFrom, $srsTo);
87:
88: $xml = $this->container->get('templating')
89: ->render('MapbenderKmlBundle:Element:kmlexport_map.kml.twig',
90: array('layers' => $layers, 'extent' => array(
91: 'minx' => $extent->minx,
92: 'miny' => $extent->miny,
93: 'maxx' => $extent->maxx,
94: 'maxy' => $extent->maxy,
95: )));
96:
97: $response->setContent($xml);
98: $response->headers->set('Content-Type',
99: 'application/vnd.google-earth.kml+xml');
100: $response->headers->set('Content-Disposition',
101: 'attachment; filename="bkg.kml"');
102: return $response;
103: }
104:
105: public function render() {
106: return $this->container->get('templating')
107: ->render('MapbenderKmlBundle:Element:kmlexport.html.twig', array(
108: 'id' => $this->getId(),
109: 'application' => $this->application->getSlug(),
110: 'configuration' => $this->getConfiguration(),
111: 'label' => $this->getClassTitle()));
112: }
113: }
114: