custom_maps.module 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. /**
  3. * http://drupal.stackexchange.com/questions/121225/display-driving-distance-between-coordinates
  4. * http://stackoverflow.com/questions/10053358/measuring-the-distance-between-two-coordinates-in-php
  5. */
  6. require_once dirname(__FILE__) . '/includes/geo.api.inc';
  7. require_once dirname(__FILE__) . '/includes/field.formatters.inc';
  8. require_once dirname(__FILE__) . '/includes/blocks.inc';
  9. function custom_maps_leaflet_map_info() {
  10. $array = array(
  11. 'mymodule' => array(
  12. 'label' => 'custom_maps',
  13. 'description' => t('This is the custom_maps map style.'),
  14. 'settings' => array(
  15. 'zoom' => 5,
  16. 'minZoom' => 1,
  17. 'maxZoom' => 18,
  18. 'dragging' => TRUE,
  19. 'touchZoom' => TRUE,
  20. 'scrollWheelZoom' => TRUE,
  21. 'doubleClickZoom' => TRUE,
  22. 'zoomControl' => TRUE,
  23. 'attributionControl' => TRUE,
  24. 'trackResize' => TRUE,
  25. 'fadeAnimation' => TRUE,
  26. 'zoomAnimation' => TRUE,
  27. 'closePopupOnClick' => TRUE,
  28. ),
  29. 'attribution' => array(
  30. 'prefix' => '',
  31. ),
  32. 'layers' => array(
  33. 'earth' => array(
  34. 'type' => 'mymodule',
  35. 'urlTemplate' => 'http://{s}.tile.stamen.com/toner/{z}/{x}/{y}.png',
  36. 'options' => array(
  37. 'attribution' => NULL,
  38. 'noWrap' => TRUE,
  39. ),
  40. ),
  41. ),
  42. ),
  43. );
  44. return array();
  45. }
  46. function custom_maps_preprocess_node(&$variables) {
  47. if ($variables['type'] == 'activity') {
  48. // dpm('UES');
  49. // drupal_add_js(drupal_get_path('module', 'custom_maps') . '/js/map-build.js', array(
  50. // 'type' => 'file',
  51. // // 'every_page' => TRUE,
  52. // 'scope' => 'footer',
  53. // 'preprocess' => TRUE,
  54. // 'weight' => 150,
  55. // ));
  56. // $map = leaflet_map_get_info('osm-cycle');
  57. $map = leaflet_map_get_info('google-roadmap');
  58. $map['settings']['scrollWheelZoom'] = FALSE;
  59. // $features = array(
  60. // array(
  61. // 'type' => 'linestring',
  62. // 'points' => array(
  63. // 0 => array('lat' => 13.24, 'lon' => 12.32),
  64. // 1 => array('lat' => 13.24, 'lon' => 13.32),
  65. // 2 => array('lat' => 13.24, 'lon' => 14.32),
  66. // 3 => array('lat' => 13.24, 'lon' => 15.32),
  67. // 4 => array('lat' => 13.24, 'lon' => 16.32),
  68. // ),
  69. // // 'popup' => l($variables['title'], 'node/' . $variables['nid']),
  70. // 'leaflet_id' => 'mapbox'
  71. // )
  72. // );
  73. $features[] = custom_maps_get_linestring_from_node($variables['node']);
  74. $variables['content']['map_box'][0]['#markup'] = leaflet_render_map($map, $features, '300px');
  75. }
  76. }
  77. /**
  78. * @deprecated
  79. */
  80. function custom_maps_get_linestring_from_node_json($node) {
  81. if ( class_exists('GPX') ) {
  82. $gpx = file_get_contents($node->field_map_file['und'][0]['uri']);
  83. $gpx_reader = new GPX();
  84. $geometry = $gpx_reader->read($gpx,TRUE);
  85. $json = (array) json_decode($geometry->out('json'));
  86. } else {
  87. $json = array();
  88. }
  89. $json['leaflet_id'] = $node->nid;
  90. $json['type'] = 'linestring';
  91. $json['points'] = ( isset($json['coordinates']) ) ? $json['coordinates'] : array();
  92. unset($json['coordinates']);
  93. foreach ($json['points'] as $index => $point) {
  94. $aux = array('lat' => strval($point[1]), 'lon' => strval($point[0]));
  95. $json['points'][$index] = $aux;
  96. }
  97. return $json;
  98. }
  99. function custom_maps_get_linestring_from_node($node) {
  100. $linestring = array();
  101. $linestring['type'] = 'linestring';
  102. $linestring['points'] = array();
  103. $linestring['leaflet_id'] = $node->nid;
  104. if ( isset($node->field_track_points['und']) ) {
  105. foreach ($node->field_track_points['und'] as $index => $point) {
  106. $linestring['points'][] = $point;
  107. }
  108. }
  109. return $linestring;
  110. }
  111. function custom_maps_node_presave($node) {
  112. if ( $node->type == 'activity') {
  113. custom_maps_activity_add_trackinfo($node);
  114. custom_maps_calculate_totals($node);
  115. }
  116. }
  117. function __get_map_file_uri($node) {
  118. $field_map_file = NULL;
  119. if ( isset($node->field_map_file['und'][0]['uri']) ) {
  120. $field_map_file = $node->field_map_file['und'][0]['uri'];
  121. } else {
  122. if ( isset($node->field_map_file['und'][0]['fid']) ) {
  123. $file_obj = file_load($node->field_map_file['und'][0]['fid']);
  124. if ( $file_obj ) {
  125. $field_map_file = $file_obj->uri;
  126. }
  127. }
  128. if ( isset($node->original->field_map_file['und'][0]['uri']) && $field_map_file == NULL) {
  129. $field_map_file = $node->original->field_map_file['und'][0]['uri'];
  130. }
  131. }
  132. return $field_map_file;
  133. }
  134. function custom_maps_activity_add_trackinfo($node) {
  135. $field_map_file = __get_map_file_uri($node);
  136. if ( isset($field_map_file) ) {
  137. $xml_data = new SimpleXMLElement(file_get_contents($field_map_file));
  138. unset($node->field_track_points[LANGUAGE_NONE]);
  139. unset($node->field_track_elevations[LANGUAGE_NONE]);
  140. unset($node->field_track_times[LANGUAGE_NONE]);
  141. switch ( strtolower(pathinfo($field_map_file, PATHINFO_EXTENSION)) ) {
  142. case 'gpx':
  143. custom_maps_process_gpx_file($node, $xml_data);
  144. break;
  145. case 'tcx':
  146. custom_maps_process_tcx_file($node, $xml_data);
  147. break;
  148. default:
  149. # code...
  150. break;
  151. }
  152. }
  153. }
  154. function custom_maps_process_gpx_file($node, $gpx_data) {
  155. if ($gpx_data->metadata->time) {
  156. $node->field_date[LANGUAGE_NONE][0]['value'] = strtotime((string) $gpx_data->metadata->time);
  157. }
  158. $index = 0;
  159. foreach ( $gpx_data->trk->trkseg->trkpt as $trkpt ) {
  160. // custom_maps_add_trackpoint($node, $trkpt, $index);
  161. $geodata = custom_maps_add_geodata($trkpt);
  162. $node->field_track_points[LANGUAGE_NONE][$index] = $geodata;
  163. $node->field_track_elevations[LANGUAGE_NONE][$index]['value'] = (float) $trkpt->ele;
  164. $node->field_track_times[LANGUAGE_NONE][$index]['value'] = strtotime((string) $trkpt->time);
  165. $index++;
  166. }
  167. }
  168. function custom_maps_process_tcx_file($node, $tcx_data) {
  169. $index = 0;
  170. foreach ( $tcx_data->Activities->Activity->Lap->Track->Trackpoint as $trkpt) {
  171. $trackpoint = array('lat' => $trkpt->Position->LatitudeDegrees, 'lon' => $trkpt->Position->LongitudeDegrees);
  172. $geodata = custom_maps_add_geodata($trackpoint);
  173. $node->field_track_points[LANGUAGE_NONE][$index] = $geodata;
  174. $node->field_track_times[LANGUAGE_NONE][$index]['value'] = strtotime((string) $trkpt->Time);
  175. $node->field_track_elevations[LANGUAGE_NONE][$index]['value'] = isset($trkpt->AltitudeMeters) ? (float) $trkpt->AltitudeMeters : 0;
  176. if ( (float) $trkpt->AltitudeMeters == 0 && $index > 0 )
  177. $node->field_track_elevations[LANGUAGE_NONE][$index]['value'] = $node->field_track_elevations[LANGUAGE_NONE][$index -1]['value'];
  178. $index++;
  179. }
  180. if ( $node->field_track_elevations[LANGUAGE_NONE][0]['value'] == 0 && isset($node->field_track_elevations[LANGUAGE_NONE][1]['value']) )
  181. $node->field_track_elevations[LANGUAGE_NONE][0]['value'] = $node->field_track_elevations[LANGUAGE_NONE][1]['value'];
  182. if ( $index > 0) {
  183. $node->field_date[LANGUAGE_NONE][0]['value'] = $node->field_track_times[LANGUAGE_NONE][0]['value'];
  184. }
  185. }
  186. function custom_maps_calculate_totals($node) {
  187. $count = count($node->field_track_points[LANGUAGE_NONE]);
  188. $total_distance = 0;
  189. $total_time = $node->field_track_times[LANGUAGE_NONE][$count -1]['value'] -
  190. $node->field_track_times[LANGUAGE_NONE][0]['value'];
  191. if ( $total_time > 0) {
  192. $max_speed = 0;
  193. $max_altitude = 0;
  194. $min_altitude = 20000;
  195. $ascent = 0;
  196. $descent = 0;
  197. foreach ( $node->field_track_points[LANGUAGE_NONE] as $index => $point ) {
  198. $last_point = ( isset($last_point) ) ? $last_point : $point;
  199. $distance = custom_maps_calculate_distance($last_point, $point);
  200. $seconds = ( $index > 0) ?
  201. $node->field_track_times[LANGUAGE_NONE][$index]['value'] - $node->field_track_times[LANGUAGE_NONE][$index-1]['value'] : 0;
  202. $int_speed = ( $seconds) ? ($distance / $seconds) * 3.6 : 0;
  203. if ( $int_speed > $max_speed ) {
  204. $max_speed = $int_speed;
  205. }
  206. $elevation = isset($node->field_track_elevations[LANGUAGE_NONE][$index]['value']) ? $node->field_track_elevations[LANGUAGE_NONE][$index]['value'] : 0;
  207. if ( $elevation > 0 ) {
  208. if ( $elevation > $max_altitude ) {
  209. $max_altitude = $elevation;
  210. }
  211. if ( $elevation < $min_altitude ) {
  212. $min_altitude = $elevation;
  213. }
  214. if ( $index > 0 ) {
  215. $past_elevation = $node->field_track_elevations[LANGUAGE_NONE][$index-1]['value'];
  216. if ( $elevation >= $past_elevation ) {
  217. $ascent += $elevation - $past_elevation;
  218. } else {
  219. $descent += $past_elevation - $elevation;
  220. }
  221. }
  222. }
  223. $total_distance += $distance;
  224. $last_point = $point;
  225. }
  226. $node->field_total_time[LANGUAGE_NONE][0]['value'] = $total_time;
  227. $node->field_distance[LANGUAGE_NONE][0]['value'] = $total_distance;
  228. $node->field_max_speed[LANGUAGE_NONE][0]['value'] = $max_speed;
  229. $node->field_average_speed[LANGUAGE_NONE][0]['value'] = ($total_distance / $total_time) * 3.6 ;
  230. $node->field_average_pace[LANGUAGE_NONE][0]['value'] = ( $total_time / ($total_distance / 1000) ) ;
  231. $node->field_max_altitude[LANGUAGE_NONE][0]['value'] = $max_altitude;
  232. $node->field_min_altitude[LANGUAGE_NONE][0]['value'] = $min_altitude;
  233. $node->field_ascent[LANGUAGE_NONE][0]['value'] = $ascent;
  234. $node->field_descent[LANGUAGE_NONE][0]['value'] = $descent;
  235. }
  236. }