| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- <?php
- /**
- * http://drupal.stackexchange.com/questions/121225/display-driving-distance-between-coordinates
- * http://stackoverflow.com/questions/10053358/measuring-the-distance-between-two-coordinates-in-php
- */
- require_once dirname(__FILE__) . '/includes/geo.api.inc';
- require_once dirname(__FILE__) . '/includes/field.formatters.inc';
- require_once dirname(__FILE__) . '/includes/blocks.inc';
- function custom_maps_leaflet_map_info() {
- $array = array(
- 'mymodule' => array(
- 'label' => 'custom_maps',
- 'description' => t('This is the custom_maps map style.'),
- 'settings' => array(
- 'zoom' => 5,
- 'minZoom' => 1,
- 'maxZoom' => 18,
- 'dragging' => TRUE,
- 'touchZoom' => TRUE,
- 'scrollWheelZoom' => TRUE,
- 'doubleClickZoom' => TRUE,
- 'zoomControl' => TRUE,
- 'attributionControl' => TRUE,
- 'trackResize' => TRUE,
- 'fadeAnimation' => TRUE,
- 'zoomAnimation' => TRUE,
- 'closePopupOnClick' => TRUE,
- ),
- 'attribution' => array(
- 'prefix' => '',
- ),
- 'layers' => array(
- 'earth' => array(
- 'type' => 'mymodule',
- 'urlTemplate' => 'http://{s}.tile.stamen.com/toner/{z}/{x}/{y}.png',
- 'options' => array(
- 'attribution' => NULL,
- 'noWrap' => TRUE,
- ),
- ),
- ),
- ),
- );
- return array();
- }
- function custom_maps_preprocess_node(&$variables) {
- if ($variables['type'] == 'activity') {
-
- // dpm('UES');
- // drupal_add_js(drupal_get_path('module', 'custom_maps') . '/js/map-build.js', array(
- // 'type' => 'file',
- // // 'every_page' => TRUE,
- // 'scope' => 'footer',
- // 'preprocess' => TRUE,
- // 'weight' => 150,
- // ));
- // $map = leaflet_map_get_info('osm-cycle');
- $map = leaflet_map_get_info('google-roadmap');
- $map['settings']['scrollWheelZoom'] = FALSE;
- // $features = array(
- // array(
- // 'type' => 'linestring',
- // 'points' => array(
- // 0 => array('lat' => 13.24, 'lon' => 12.32),
- // 1 => array('lat' => 13.24, 'lon' => 13.32),
- // 2 => array('lat' => 13.24, 'lon' => 14.32),
- // 3 => array('lat' => 13.24, 'lon' => 15.32),
- // 4 => array('lat' => 13.24, 'lon' => 16.32),
- // ),
- // // 'popup' => l($variables['title'], 'node/' . $variables['nid']),
- // 'leaflet_id' => 'mapbox'
- // )
- // );
- $features[] = custom_maps_get_linestring_from_node($variables['node']);
- $variables['content']['map_box'][0]['#markup'] = leaflet_render_map($map, $features, '300px');
- }
- }
- /**
- * @deprecated
- */
- function custom_maps_get_linestring_from_node_json($node) {
- if ( class_exists('GPX') ) {
- $gpx = file_get_contents($node->field_map_file['und'][0]['uri']);
- $gpx_reader = new GPX();
- $geometry = $gpx_reader->read($gpx,TRUE);
- $json = (array) json_decode($geometry->out('json'));
-
- } else {
- $json = array();
- }
- $json['leaflet_id'] = $node->nid;
- $json['type'] = 'linestring';
- $json['points'] = ( isset($json['coordinates']) ) ? $json['coordinates'] : array();
- unset($json['coordinates']);
- foreach ($json['points'] as $index => $point) {
- $aux = array('lat' => strval($point[1]), 'lon' => strval($point[0]));
- $json['points'][$index] = $aux;
- }
- return $json;
- }
- function custom_maps_get_linestring_from_node($node) {
- $linestring = array();
- $linestring['type'] = 'linestring';
- $linestring['points'] = array();
- $linestring['leaflet_id'] = $node->nid;
- if ( isset($node->field_track_points['und']) ) {
- foreach ($node->field_track_points['und'] as $index => $point) {
- $linestring['points'][] = $point;
- }
- }
- return $linestring;
- }
- function custom_maps_node_presave($node) {
- if ( $node->type == 'activity') {
- custom_maps_activity_add_trackinfo($node);
- custom_maps_calculate_totals($node);
- }
- }
- function __get_map_file_uri($node) {
- $field_map_file = NULL;
- if ( isset($node->field_map_file['und'][0]['uri']) ) {
- $field_map_file = $node->field_map_file['und'][0]['uri'];
- } else {
- if ( isset($node->field_map_file['und'][0]['fid']) ) {
- $file_obj = file_load($node->field_map_file['und'][0]['fid']);
- if ( $file_obj ) {
- $field_map_file = $file_obj->uri;
- }
- }
- if ( isset($node->original->field_map_file['und'][0]['uri']) && $field_map_file == NULL) {
- $field_map_file = $node->original->field_map_file['und'][0]['uri'];
- }
- }
- return $field_map_file;
- }
- function custom_maps_activity_add_trackinfo($node) {
- $field_map_file = __get_map_file_uri($node);
- if ( isset($field_map_file) ) {
- $xml_data = new SimpleXMLElement(file_get_contents($field_map_file));
- unset($node->field_track_points[LANGUAGE_NONE]);
- unset($node->field_track_elevations[LANGUAGE_NONE]);
- unset($node->field_track_times[LANGUAGE_NONE]);
- switch ( strtolower(pathinfo($field_map_file, PATHINFO_EXTENSION)) ) {
- case 'gpx':
- custom_maps_process_gpx_file($node, $xml_data);
- break;
- case 'tcx':
- custom_maps_process_tcx_file($node, $xml_data);
- break;
-
- default:
- # code...
- break;
- }
- }
- }
- function custom_maps_process_gpx_file($node, $gpx_data) {
- if ($gpx_data->metadata->time) {
- $node->field_date[LANGUAGE_NONE][0]['value'] = strtotime((string) $gpx_data->metadata->time);
- }
- $index = 0;
- foreach ( $gpx_data->trk->trkseg->trkpt as $trkpt ) {
- // custom_maps_add_trackpoint($node, $trkpt, $index);
- $geodata = custom_maps_add_geodata($trkpt);
- $node->field_track_points[LANGUAGE_NONE][$index] = $geodata;
- $node->field_track_elevations[LANGUAGE_NONE][$index]['value'] = (float) $trkpt->ele;
- $node->field_track_times[LANGUAGE_NONE][$index]['value'] = strtotime((string) $trkpt->time);
- $index++;
- }
- }
- function custom_maps_process_tcx_file($node, $tcx_data) {
- $index = 0;
- foreach ( $tcx_data->Activities->Activity->Lap->Track->Trackpoint as $trkpt) {
- $trackpoint = array('lat' => $trkpt->Position->LatitudeDegrees, 'lon' => $trkpt->Position->LongitudeDegrees);
- $geodata = custom_maps_add_geodata($trackpoint);
- $node->field_track_points[LANGUAGE_NONE][$index] = $geodata;
- $node->field_track_times[LANGUAGE_NONE][$index]['value'] = strtotime((string) $trkpt->Time);
- $node->field_track_elevations[LANGUAGE_NONE][$index]['value'] = isset($trkpt->AltitudeMeters) ? (float) $trkpt->AltitudeMeters : 0;
- if ( (float) $trkpt->AltitudeMeters == 0 && $index > 0 )
- $node->field_track_elevations[LANGUAGE_NONE][$index]['value'] = $node->field_track_elevations[LANGUAGE_NONE][$index -1]['value'];
- $index++;
- }
- if ( $node->field_track_elevations[LANGUAGE_NONE][0]['value'] == 0 && isset($node->field_track_elevations[LANGUAGE_NONE][1]['value']) )
- $node->field_track_elevations[LANGUAGE_NONE][0]['value'] = $node->field_track_elevations[LANGUAGE_NONE][1]['value'];
- if ( $index > 0) {
- $node->field_date[LANGUAGE_NONE][0]['value'] = $node->field_track_times[LANGUAGE_NONE][0]['value'];
-
- }
- }
- function custom_maps_calculate_totals($node) {
- $count = count($node->field_track_points[LANGUAGE_NONE]);
- $total_distance = 0;
- $total_time = $node->field_track_times[LANGUAGE_NONE][$count -1]['value'] -
- $node->field_track_times[LANGUAGE_NONE][0]['value'];
- if ( $total_time > 0) {
- $max_speed = 0;
- $max_altitude = 0;
- $min_altitude = 20000;
- $ascent = 0;
- $descent = 0;
- foreach ( $node->field_track_points[LANGUAGE_NONE] as $index => $point ) {
- $last_point = ( isset($last_point) ) ? $last_point : $point;
- $distance = custom_maps_calculate_distance($last_point, $point);
- $seconds = ( $index > 0) ?
- $node->field_track_times[LANGUAGE_NONE][$index]['value'] - $node->field_track_times[LANGUAGE_NONE][$index-1]['value'] : 0;
- $int_speed = ( $seconds) ? ($distance / $seconds) * 3.6 : 0;
- if ( $int_speed > $max_speed ) {
- $max_speed = $int_speed;
- }
- $elevation = isset($node->field_track_elevations[LANGUAGE_NONE][$index]['value']) ? $node->field_track_elevations[LANGUAGE_NONE][$index]['value'] : 0;
- if ( $elevation > 0 ) {
- if ( $elevation > $max_altitude ) {
- $max_altitude = $elevation;
- }
- if ( $elevation < $min_altitude ) {
- $min_altitude = $elevation;
- }
- if ( $index > 0 ) {
- $past_elevation = $node->field_track_elevations[LANGUAGE_NONE][$index-1]['value'];
- if ( $elevation >= $past_elevation ) {
- $ascent += $elevation - $past_elevation;
- } else {
- $descent += $past_elevation - $elevation;
- }
- }
- }
-
- $total_distance += $distance;
- $last_point = $point;
- }
- $node->field_total_time[LANGUAGE_NONE][0]['value'] = $total_time;
- $node->field_distance[LANGUAGE_NONE][0]['value'] = $total_distance;
- $node->field_max_speed[LANGUAGE_NONE][0]['value'] = $max_speed;
- $node->field_average_speed[LANGUAGE_NONE][0]['value'] = ($total_distance / $total_time) * 3.6 ;
- $node->field_average_pace[LANGUAGE_NONE][0]['value'] = ( $total_time / ($total_distance / 1000) ) ;
- $node->field_max_altitude[LANGUAGE_NONE][0]['value'] = $max_altitude;
- $node->field_min_altitude[LANGUAGE_NONE][0]['value'] = $min_altitude;
- $node->field_ascent[LANGUAGE_NONE][0]['value'] = $ascent;
- $node->field_descent[LANGUAGE_NONE][0]['value'] = $descent;
- }
-
- }
|