]> dev.renevier.net Git - syj.git/blob - application/models/Path.php
build process raise an exception when install file contains an invalide filename
[syj.git] / application / models / Path.php
1 <?php
2 /*  This file is part of Syj, Copyright (c) 2010-2011 Arnaud Renevier,
3     and is published under the AGPL license. */
4
5 class Syj_Model_Path extends Syj_Model_Generic
6 {
7     protected $_id;
8     protected $_geom;
9     protected $_creator;
10     protected $_title;
11     protected $_urlcomp;
12     protected $_creator_ip;
13
14     public function setId($id) {
15         $this->_id = (int) $id;
16         return $this;
17     }
18
19     public function getId() {
20         return $this->_id;
21     }
22
23     public function setGeom(gisconverter\Geometry $geom) {
24         $this->_geom = $geom;
25         return $this;
26     }
27
28     public function getGeom() {
29         return $this->_geom;
30     }
31
32     public function setCreator(Syj_Model_User $creator = null) {
33         $this->_creator = $creator;
34         return $this;
35     }
36
37     public function getCreator() {
38         return $this->_creator;
39     }
40
41     public function isCreator(Syj_Model_User $creator = null) {
42         if (!$creator or !$this->creator) {
43             return false;
44         }
45         return ($creator->id == $this->creator->id);
46     }
47
48     public function setTitle($title) {
49         $this->_title = (string) $title;
50         return $this;
51     }
52
53     public function getTitle() {
54         return $this->_title;
55     }
56
57     public function getDisplayTitle() {
58         if ($this->_title) {
59             return $this->_title;
60         } else if ($this->_id) {
61             $title = $this->getTranslator()->translate("route number %d");
62             return str_replace('%d', (string)$this->id, $title);
63         } else {
64             return "";
65         }
66     }
67
68     public function setUrlComp($_urlcomp) {
69         $this->_urlcomp = (string) $_urlcomp;
70         return $this;
71     }
72
73     public function getUrlComp() {
74         return $this->_urlcomp;
75     }
76
77     public function setCreatorIp($_creator_ip) {
78         $this->_creator_ip = (string) $_creator_ip;
79         return $this;
80     }
81
82     public function getCreatorIp() {
83         return $this->_creator_ip;
84     }
85
86     /*
87      * returns an array containing two arrays. First one is list of distance
88      * from start. Second on is altitude at that point.
89      * @error: ratio of void we accept
90      */
91     public function getAltiProfile($altiService, $error = 0) {
92         $points = $altiService->interpolate($this->_geom);
93         $points = array_map(function($point) {
94             if ($point instanceof \gisconverter\Geometry) {
95                 return array($point->lon, $point->lat);
96             }
97             return $point;
98         }, $points);
99         $altitudes = $altiService->altitude($points);
100
101         if (is_null($altitudes)) {
102             throw new Syj_Exception_NotImplemented("could not compute altitude profile");
103         }
104
105         $res = array(array(0, $altitudes[0]));
106         $prevpoint  = $points[0];
107         $prevdist  = 0;
108
109         $alticount = count($altitudes);
110         $altinull = 0;
111         foreach (range(1, count($altitudes) - 1) as $idx) {
112             $point = $points[$idx];
113             $delta = $altiService->vincentyDistance($prevpoint[0], $prevpoint[1], $point[0], $point[1]);
114             $dist = $prevdist + $delta;
115             if ($delta == 0) { // we have two similar points side to side
116                 continue;
117             }
118             if (is_null($altitudes[$idx])) {
119                 $altinull++;
120                 continue;
121             }
122             $prevpoint = $point;
123             $prevdist = $dist;
124             $res[] = array($dist, $altitudes[$idx]);
125         }
126
127         if ($altinull / $alticount > $error) {
128             throw new Syj_Exception_NotImplemented("too many void in altitude profile");
129         }
130         return $res;
131     }
132
133     public function getProfileCache($size) {
134         $cacheDir = Zend_Controller_Front::getInstance()->getParam('profileCache');
135         if (is_file($cacheDir) and !is_dir($cacheDir)) {
136             throw new Zend_Exception();
137         }
138         if (!is_dir($cacheDir)) {
139             if (@mkdir($cacheDir, 0755, true) === false) {
140                 throw new Zend_Exception();
141             }
142         }
143
144         return sprintf("%s/%s-%s.png", $cacheDir, (string)$this->_id, $size);
145     }
146
147     public function invalidateCache() {
148         @unlink($this->getProfileCache('small'));
149         @unlink($this->getProfileCache('big'));
150     }
151
152 }