geofield.devel_generate.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * @file
  4. * Create random data to populate geofields.
  5. */
  6. /**
  7. * Implements hook_devel_generate().
  8. */
  9. function geofield_devel_generate($object, $field, $instance, $bundle) {
  10. if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
  11. return devel_generate_multiple('_geofield_devel_generate', $object, $field, $instance, $bundle);
  12. }
  13. else {
  14. return _geofield_devel_generate($object, $field, $instance, $bundle);
  15. }
  16. }
  17. function _geofield_devel_generate($object, $field, $instance, $bundle) {
  18. $object_field = array();
  19. $type = str_replace('geofield_', '', $instance['widget']['type']);
  20. $val = array(
  21. 'wkt' => NULL,
  22. 'lat' => NULL,
  23. 'lon' => NULL,
  24. 'top' => NULL,
  25. 'bottom' => NULL,
  26. 'right' => NULL,
  27. 'left' => NULL,
  28. );
  29. if ($type == 'latlon' || $type == 'bounds') {
  30. list($val['lon'], $val['lat']) = _random_point();
  31. // don't actually need lat/lon but provide a center for our bounds
  32. if ($type == 'bounds') {
  33. $lat_diff = _dd_generate(2, 10) / 100;
  34. $lon_diff = _dd_generate(2, 10) / 100;
  35. $val['left'] = $val['lon'] - $lon_diff;
  36. $val['right'] = $val['lon'] + $lon_diff;
  37. $val['top'] = $val['lat'] - $lat_diff;
  38. $val['bottom'] = $val['lat'] + $lat_diff;
  39. }
  40. }
  41. else {
  42. $type = 'wkt';
  43. $val['wkt'] = _wkt_generate();
  44. }
  45. $values = geofield_compute_values($val, $type);
  46. return $values;
  47. }
  48. /**
  49. * Helper to generate DD coordinates
  50. */
  51. function _dd_generate($min, $max, $int = FALSE) {
  52. $func = 'rand';
  53. if (function_exists('mt_rand')) {
  54. $func = 'mt_rand';
  55. }
  56. $number = $func($min, $max);
  57. if ($int || abs($number) === 180) {
  58. return $number;
  59. }
  60. $decimals = $func(1, pow(10, 5)) / pow(10, 5);
  61. return round($number + $decimals, 5);
  62. }
  63. /**
  64. * Helper to generate a random WKT string
  65. *
  66. * Try to keeps values sane, no shape is more than 100km across
  67. */
  68. function _wkt_generate() {
  69. $types = array(
  70. 'point',
  71. 'linestring',
  72. 'polygon',
  73. 'multipoint',
  74. 'multilinestring',
  75. 'multipolygon',
  76. );
  77. // don't always generate the same type
  78. shuffle($types);
  79. $type = $types[0];
  80. $func = '_wkt_generate_' . $type;
  81. if (function_exists($func)) {
  82. $wkt = $func();
  83. return drupal_strtoupper($type) . ' (' . $wkt . ')';
  84. }
  85. return 'POINT (0 0)';
  86. }
  87. function _random_point() {
  88. $lon = _dd_generate(-180, 180);
  89. $lat = _dd_generate(-84, 84);
  90. return array($lon, $lat);
  91. }
  92. function _wkt_generate_point($point = FALSE) {
  93. $point = $point ? $point : _random_point();
  94. return implode(' ', $point);
  95. }
  96. function _wkt_generate_multipoint() {
  97. $num = _dd_generate(1, 5, TRUE);
  98. $start = _random_point();
  99. $points[] = _wkt_generate_point($start);
  100. for ($i = 0; $i < $num; $i += 1) {
  101. $diff = _random_point();
  102. $start[0] += $diff[0] / 100;
  103. $start[1] += $diff[1] / 100;
  104. $points[] = _wkt_generate_point($start);
  105. }
  106. return implode(', ', $points);
  107. }
  108. // make a line that looks like a line
  109. function _wkt_generate_linestring($start = FALSE, $segments = FALSE) {
  110. $start = $start ? $start : _random_point();
  111. $segments = $segments ? $segments : _dd_generate(1, 5, TRUE);
  112. $points[] = $start[0] . ' ' . $start[1];
  113. // Points are at most 1km away from each other
  114. for ($i = 0; $i < $segments; $i += 1) {
  115. $diff = _random_point();
  116. $start[0] += $diff[0] / 100;
  117. $start[1] += $diff[1] / 100;
  118. $points[] = $start[0] . ' ' . $start[1];
  119. }
  120. return implode(", ", $points);
  121. }
  122. // make a line that looks like a line
  123. function _wkt_generate_multilinestring() {
  124. $start = _random_point();
  125. $num = _dd_generate(1, 3, TRUE);
  126. $lines[] = _wkt_generate_linestring($start);
  127. for ($i = 0; $i < $num; $i += 1) {
  128. $diff = _random_point();
  129. $start[0] += $diff[0] / 100;
  130. $start[1] += $diff[1] / 100;
  131. $lines[] = _wkt_generate_linestring($start);
  132. }
  133. return '(' . implode('), (', $lines) . ')';
  134. }
  135. function _wkt_generate_polygon($start = FALSE, $segments = FALSE) {
  136. $start = $start ? $start : _random_point();
  137. $segments = $segments ? $segments : _dd_generate(2, 4, TRUE);
  138. $poly = _wkt_generate_linestring($start, $segments);
  139. // close the polygon
  140. return '(' . $poly . ' , ' . $start[0] . ' ' . $start[1] . ')';
  141. }
  142. function _wkt_generate_multipolygon() {
  143. $start = _random_point();
  144. $num = _dd_generate(1, 5, TRUE);
  145. $segments = _dd_generate(2, 3, TRUE);
  146. $poly[] = _wkt_generate_polygon($start, $segments);
  147. for ($i = 0; $i < $num; $i += 1) {
  148. $diff = _random_point();
  149. $start[0] += $diff[0] / 100;
  150. $start[1] += $diff[1] / 100;
  151. $poly[] = _wkt_generate_polygon($start, $segments);
  152. }
  153. return '(' . implode(', ', $poly) . ')';
  154. }