forked from hugowetterberg/simple_geo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple_geo.module
990 lines (883 loc) · 28.9 KB
/
simple_geo.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
<?php
// $Id$
/**
* @file
* Module for simple assigning and viewing of geo data
*/
define('SIMPLE_GEO_PAIR_SEPARATOR', ',');
define('SIMPLE_GEO_COORDINATE_SEPARATOR', ' ');
require('includes/simple_geo_themeable_functions.php');
function simple_geo_init() {
if (variable_get('simple_geo_manually_load', 1)) {
simple_geo_load();
}
}
function simple_geo_load() {
static $loaded;
$settings = array();
if(empty($loaded)) {
drupal_add_css(drupal_get_path('module', 'simple_geo') .'/css/simple_geo.css');
$def_pos = variable_get('simple_geo_default_position', 0);
if ($def_pos) {
$settings['simple_geo_default_position'] = $def_pos;
}
$settings['simple_geo_search_address'] = variable_get('simple_geo_search_address', 1);
$settings['simple_geo_search_address_icon'] = variable_get('simple_geo_search_address_icon', 1);
$settings['simple_geo_min_zoom'] = variable_get('simple_geo_min_zoom', 1);
$settings['simple_geo_max_zoom'] = variable_get('simple_geo_max_zoom', 14);
$settings['simple_geo_overview_map_show'] = variable_get('simple_geo_overview_map_show', 0);
$settings['simple_geo_overview_map_size'] = variable_get('simple_geo_overview_map_size', 150);
$settings['simple_geo_micromap_parent'] = variable_get('simple_geo_micromap_parent', '#main-inner');
$settings['simple_geo_micromap_add_mode'] = variable_get('simple_geo_micromap_add_mode', 'prepend');
$gc_suffix = variable_get('simple_geo_geocoding_suffix', '');
if (!empty($gc_suffix)) {
$settings['simple_geo_geocoding_suffix'] = $gc_suffix;
}
$settings['simple_geo_micromap_parent'] = variable_get('simple_geo_micromap_parent', '#main-inner');
$settings['simple_geo_micromap_add_mode'] = variable_get('simple_geo_micromap_add_mode', 'prepend');
if (!empty($settings)) {
drupal_add_js($settings, 'setting');
}
$api_key = variable_get('gmaps_api_key', False);
if ($api_key) {
if (variable_get('simple_geo_use_microformat_map', 1)) {
drupal_add_js(drupal_get_path('module', 'simple_geo') .'/js/LookupControl.js', 'module', 'header');
drupal_add_js(drupal_get_path('module', 'simple_geo') .'/js/micromap.js', 'module', 'header');
}
if (variable_get('simple_geo_add_google_jsapi', 1)) {
drupal_set_html_head('<script type="text/javascript" src="http://www.google.com/jsapi?key='. $api_key .'"></script>');
}
}
$loaded = TRUE;
}
}
/**
* Implementation of hook_menu().
*
* @return void
*/
function simple_geo_menu() {
$items = array();
$items['node/%node/map'] = array(
'title' => 'Map',
'page callback' => 'simple_geo_view_position',
'page arguments' => array(1),
'access callback' => 'node_access',
'access arguments' => array('view', 1),
'type' => MENU_CALLBACK,
'file' => 'includes/simple_geo_fallback_functions.php',
'weight' => 0,
);
$items['admin/settings/simple-geo'] = array(
'title' => 'Simple geo configuration',
'page callback' => 'drupal_get_form',
'page arguments' => array('simple_geo_settings'),
'access arguments' => array('administer simple geo'),
'file' => 'simple_geo_admin.inc.php',
'type' => MENU_NORMAL_ITEM,
'weight' => 0,
);
return $items;
}
/**
* Implementation of hook_perm().
*
* @return array
*/
function simple_geo_perm() {
return array(
'edit own nodes area', 'edit own nodes position', 'edit all areas', 'edit all positions', 'administer simple geo'
);
}
/**
* Unimplemented.
*
* This method will provide mechanisms for guessing positions
*
*/
function simple_geo_guess_position($node) {
return '';
}
function simple_geo_user($op, &$edit, &$account, $category = NULL) {
if (($category!==NULL && $category!='account') || !variable_get('simple_geo_position_users', 1)) {
return;
}
switch ($op) {
case 'form':
$set = array(
'#type' => 'item',
'#prefix' => '<div id="simple_geo_form">',
'#suffix' => '</div>',
'#title' => t('Position'),
'#weight' => -20,
);
$position = simple_geo_position($account->uid, 'user');
$set['simple_geo_position'] = array(
'#type' => 'textfield',
'#title' => t('Position'),
'#attributes' => array('class' => 'simple-geo-position'),
'#default_value' => $position,
'#weight' => 1,
);
$set['map_placeholder'] = array(
'#type' => 'markup',
'#value' => '<div class="map-placeholder"></div>',
'#weight' => -1,
'#after_build' => array('simple_geo_add_form_js'),
);
return array('simple_geo' => $set);
break;
case 'load':
$account->simple_geo_position = simple_geo_position($account->uid, 'user');
break;
case 'update':
if (isset($edit['simple_geo_position'])) {
simple_geo_delete_position($account->uid, 'user');
if (!empty($edit['simple_geo_position'])) {
simple_geo_set_position($account->uid, 'user', $edit['simple_geo_position']);
}
}
//Make sure that we've cleared out all our stuff
//to prevent user data pollution
foreach ($edit as $key => $value) {
if (strpos($key, 'simple_geo_') === 0) {
unset($edit[$key]);
}
}
break;
}
}
/**
* Implementation of hook_form_alter().
*
* @return void
*/
function simple_geo_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'node_type_form') {
$type = $form['#node_type']->type;
$set = array(
'#type' => 'fieldset',
'#title' => t('Geographic fields'),
'#collapsible' => TRUE);
$set['simple_geo_allow_position'] = array(
'#type' => 'checkbox',
'#default_value' => variable_get('simple_geo_allow_position_'. $type, FALSE),
'#title' => t('Use position'),
);
$set['simple_geo_allow_area'] = array(
'#type' => 'checkbox',
'#default_value' => variable_get('simple_geo_allow_area_'. $type, FALSE),
'#title' => t('Use area'),
);
$set['simple_geo_sync_translations'] = array(
'#type' => 'checkbox',
'#default_value' => variable_get('simple_geo_sync_translations_'. $type, TRUE),
'#title' => t('Synchronize translations'),
);
$set['simple_geo_show_in_form'] = array(
'#type' => 'checkbox',
'#default_value' => variable_get('simple_geo_show_in_form_'. $type, TRUE),
'#title' => t('Show simple geo in node form'),
);
$form['simple_geo'] = $set;
}
else if ($form['#id'] == 'node-form') {
$node = $form['#node'];
global $user;
if ($form_id == $node->type .'_node_form') {
$allow_editing = variable_get('simple_geo_show_in_form_'. $node->type, TRUE);
if ($allow_editing) {
$allow_position = variable_get('simple_geo_allow_position_'. $node->type, FALSE);
$allow_area = variable_get('simple_geo_allow_area_'. $node->type, FALSE);
$set = array(
'#type' => 'item',
'#prefix' => '<div id="simple_geo_form">',
'#suffix' => '</div>',
'#title' => t('Position'),
);
$editing = FALSE;
if (module_exists('content')) {
$set['#weight'] = content_extra_field_weight($form['type']['#value'], 'simple_geo');
}
if ($allow_position && (user_access('edit all positions') ||
($node->uid == $user->uid && user_access('edit own nodes position')))) {
if (!isset($node->simple_geo_position)) {
$position = simple_geo_guess_position($node);
}
else {
$position = $node->simple_geo_position;
}
$set['simple_geo_position'] = array(
'#type' => 'textfield',
'#title' => t('Position'),
'#attributes' => array('class' => 'simple-geo-position'),
'#default_value' => $position,
'#weight' => 1,
);
$editing = TRUE;
}
if ($allow_area && (user_access('edit all areas') ||
($node->uid == $user->uid && user_access('edit own nodes area')))) {
$area = '';
if (isset($node->simple_geo_area)) {
$area = $node->simple_geo_area;
}
$set['simple_geo_area'] = array(
'#type' => 'textarea',
'#title' => t('Area'),
'#attributes' => array('class' => 'simple-geo-area'),
'#default_value' => $area,
'#weight' => 2,
);
$editing = TRUE;
}
if ($editing) {
$set['map_placeholder'] = array(
'#type' => 'markup',
'#value' => '<div class="map-placeholder"></div>',
'#weight' => -1,
'#after_build' => array('simple_geo_add_form_js'),
);
$form['simple_geo'] = $set;
}
}
}
}
}
function simple_geo_add_form_js($form) {
simple_geo_load();
drupal_add_js(drupal_get_path('module', 'simple_geo') .'/js/LookupControl.js', 'module', 'header');
drupal_add_js(drupal_get_path('module', 'simple_geo') .'/js/edit.js', 'module', 'header');
drupal_add_css(drupal_get_path('module', 'simple_geo') .'/css/simple_geo.css');
return $form;
}
/**
* Implementation of hook_content_extra_fields().
*/
function simple_geo_content_extra_fields($type_name) {
$extra = array();
if (variable_get('simple_geo_allow_position_'. $type_name, FALSE) || variable_get('simple_geo_allow_area_'. $type_name, FALSE)) {
$extra['simple_geo'] = array(
'label' => t('Position'),
'description' => t('Simple Geo module form.'),
'weight' => 0,
);
}
return $extra;
}
/**
* Implementation of hook_apachesolr_update_index().
*/
function simple_geo_apachesolr_update_index(&$doc, $node) {
if (isset($node->simple_geo_position)) {
$doc->setField('ss_simple_geo_position', $node->simple_geo_position);
$areas = simple_geo_contained_by($node->nid, 'node', 'node');
if (!empty($areas)) {
foreach ($areas as $area) {
$doc->addField('im_simple_geo_area', $area->id);
}
}
}
}
/**
* Implementation of hook_nodeapi().
*
* @return mixed
*/
function simple_geo_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
$sync_translations = variable_get('simple_geo_sync_translations_'. $node->type, TRUE);
$allow_position = variable_get('simple_geo_allow_position_'. $node->type, FALSE);
$allow_area = variable_get('simple_geo_allow_area_'. $node->type, FALSE);
switch ($op) {
case 'delete':
simple_geo_delete_position($node->nid, 'node');
simple_geo_delete_area($node->nid, 'node');
simple_geo_delete_from_cache($node->nid, 'node');
break;
case 'prepare translation':
$src = $node->translation_source;
$node->simple_geo_position = $src->simple_geo_position;
$node->simple_geo_area = $src->simple_geo_area;
break;
case 'update':
case 'insert':
global $user;
// Check access, data and configuration to determine if we will
// set position or area.
$setting_position = $allow_position && !empty($node->simple_geo_position);
$setting_area = $allow_area && !empty($node->simple_geo_area);
if ($allow_position || $allow_area) {
// Build a list of affected nodes, this includes translations of the node
// if sync_translations is enabled for the content type
$affected_nodes = array( $node->nid => TRUE );
if ($sync_translations && $node->tnid) {
$tres = db_query("SELECT nid FROM {node} WHERE tnid=%d AND tnid<>0 AND nid<>%d", array(
':tnid' => $node->tnid,
':nid' => $node->nid,
));
while ($t = db_result($tres)) {
$affected_nodes[$t] = TRUE;
}
}
if ($setting_position) { // Update position
foreach ($affected_nodes as $nid => $affected) {
simple_geo_set_position($nid, 'node', $node->simple_geo_position);
}
}
else if ($allow_position) { // Delete position
foreach ($affected_nodes as $nid => $affected) {
simple_geo_delete_position($nid, 'node');
}
}
if ($setting_area) { // Update area
foreach ($affected_nodes as $nid => $affected) {
simple_geo_set_area($nid, 'node', $node->simple_geo_area);
}
}
else if ($allow_area) { // Delete area
foreach ($affected_nodes as $nid => $affected) {
simple_geo_delete_area($nid, 'node');
}
}
// Update the cache for the affected nodes
foreach ($affected_nodes as $nid => $affected) {
simple_geo_update_cache($nid, 'node');
}
}
break;
case 'load':
if ($allow_position) {
$node->simple_geo_position = simple_geo_position($node->nid, 'node');
}
if ($allow_area) {
$node->simple_geo_area = simple_geo_area($node->nid, 'node');
}
break;
case 'view':
$map_view = '';
if ($allow_position && !empty($node->simple_geo_position)) {
$coords = explode(' ', $node->simple_geo_position);
$map_link = variable_get('simple_geo_show_map_link', 'always');
switch ($map_link) {
case 'always':
$show_map_link = True;
break;
case 'teaser':
$show_map_link = $a3;
break;
case 'full':
$show_map_link = !$a3;
break;
case 'never':
$show_map_link = False;
break;
}
if ($show_map_link) {
$map_view .= '<div class="node-map"><a rel="map" title="'.
t('Map for @name', array('@name' => $node->title)) .'" href="'. url('node/'. $node->nid .'/map/') .'">'.
t('Show on map') .'</a></div>';
}
if (variable_get('simple_geo_add_microformat_tag', 1)) {
$map_view .= '<div class="geo"><span class="latitude">'. $coords[0] .'</span><span class="longitude">'. $coords[1] .'</span></div>';
}
}
if (!empty($node->simple_geo_area) && variable_get('simple_geo_add_microformat_tag', 1)) {
$map_view .= '<script type="text/javascript">var node_'. $node->nid .'_position="'. $node->simple_geo_position .'";'.
'var node_'. $node->nid .'_area="'. $node->simple_geo_area .'";</script>';
}
$node->content['simple_geo'] = array(
'#weight' => 50,
'#value' => $map_view,
);
break;
}
}
/**
* Envelop a coordinate in a circle.
*
* @param string $lat
* Latitude.
* @param string $lon
* Longitude.
* @param float|int $radius
* Radius in km.
* @param int $steps
* Optional. The number of vertexes in the circle. Defaults to 8.
* @param bool $close
* Optional. Whether to close the circle. Defaults to FALSE.
* @return array
*/
function simple_geo_circle($lat, $lon, $radius, $steps=8, $close=FALSE) {
$R = 6371; // The radius of earth (in kilometers)
$delta_lon = rad2deg($radius/$R/cos(deg2rad($lat)));
$delta_lat = rad2deg($radius/$R);
$pts = array();
$step = pi() / ($steps / 2);
if ($close) {
$steps++;
}
for ($i = 0; $i < $steps; $i++) {
$rad = $step * $i;
$pts[] = array(
$lat + $delta_lat * cos($rad),
$lon + $delta_lon * sin($rad),
);
}
return $pts;
}
/**
* Gets the position of a object.
*
* @param int $nid The id of the node
* @param string $type The type of the object
*
* @return string The point as a coordinate pair in the same format as WKT
*/
function simple_geo_position($id, $type) {
$res = db_query("SELECT AsText(position) FROM {simple_geo_position} WHERE nid = %d AND type='%s'", $id, $type);
if ($wkt = db_result($res)) {
return simple_geo_clean_wkt('point', $wkt);
}
}
/**
* Gets the area of a object.
*
* @param int $nid The id of the object
* @param string $type The type of the object
*
* @return string The vertexes of the polygon as coordinate pairs in the same format as WKT
*/
function simple_geo_area($id, $type) {
$res = db_query("SELECT AsText(area) FROM {simple_geo_area} WHERE nid = %d", $id, $type);
if ($wkt = db_result($res)) {
return simple_geo_clean_wkt('polygon', $wkt);
}
}
/**
* Gets the areas that contains the object.
*
* @param int $nid The id of the object.
* @param string $type The type of the object.
* @param string $area_type Optional. The type of the areas.
* @return array
* An array of areas (only including type if $area_type is NULL)
*/
function simple_geo_contained_by($id, $type, $area_type=NULL) {
$result = array();
if (!empty($area_type)) {
$res = db_query("SELECT area_nid AS id FROM {simple_geo_in_area}
WHERE point_nid = %d
AND point_type='%s'
AND area_type='%s'",
$id, $type, $area_type);
}
else {
$res = db_query("SELECT area_nid AS id, area_type AS type FROM {simple_geo_in_area}
WHERE point_nid = %d
AND point_type='%s'",
$id, $type);
}
while ($area = db_fetch_object($res)) {
$result[] = $area;
}
return $result;
}
/**
* Gets the points contained by the area.
*
* @param int $nid The id of the area.
* @param string $type The type of the area.
* @param string $object_type Optional. The type of the objects.
* @return array
* An array of objects (only including type if $object_type is NULL)
*/
function simple_geo_contained($id, $type, $object_type=NULL) {
$result = array();
if (!empty($point_type)) {
$res = db_query("SELECT point_nid AS id FROM {simple_geo_in_area}
WHERE area_nid = %d
AND area_type='%s'
AND point_type='%s'",
$id, $type, $point_type);
}
else {
$res = db_query("SELECT area_nid AS id, area_type AS type FROM {simple_geo_in_area}
WHERE area_nid = %d
AND area_type='%s'",
$id, $type);
}
while ($area = db_fetch_object($res)) {
$result[] = $area;
}
return $result;
}
function simple_geo_delete_from_cache($id, $type) {
db_query("DELETE FROM {simple_geo_in_area}
WHERE (point_nid = %d AND point_type='%s')
OR (area_nid = %d AND area_type='%s')",
$id, $type, $id, $type);
}
/**
* Updates the cache for the spatial relationship between points and polygons
*
* @param int $nid The node to update the cache for
* @param string $op The cache type to update. 'area', 'point' or 'all', defaults to 'all'
*
* @return void
*/
function simple_geo_update_cache($id, $type, $op='all') {
$updates = array();
$op_all = $op == 'all';
$op_point = $op_all || $op == 'point';
$op_area = $op_all || $op == 'area';
if ($op_all) {
//Delete all cache entries
simple_geo_delete_from_cache($id, $type);
}
else if ($op_point) {
//Delete all position cache entries
db_query("DELETE FROM {simple_geo_in_area}
WHERE point_nid = %d AND point_type='%s'", $id, $type);
}
else if ($op_area) {
//Delete all area cache entries
db_query("DELETE FROM {simple_geo_in_area} WHERE area_nid = %d AND area_type='%s'", $id, $type);
}
//Refresh cache for position
if ($op_point) {
$position = simple_geo_position($id, $type);
if ($position) {
$p_array = simple_geo_coordinates_to_array($position);
$res = db_query("SELECT nid, type, AsText(area) as area_wkt
FROM {simple_geo_area}
WHERE Contains(area, GeomFromText('%s'))", simple_geo_to_wkt('point', $position));
while ($bb_match = db_fetch_object($res)) {
$a_coords = simple_geo_clean_wkt('polygon', $bb_match->area_wkt);
if (simple_geo_point_in_polygon($p_array, $a_coords)) {
$p = array($id, $type, $bb_match->nid, $bb_match->type);
$updates[join('-', $p)] = $p;
}
}
}
}
//Refresh cache for area
if ($op_area) {
$area = simple_geo_area($id, $type);
if ($area) {
$a_array = simple_geo_coordinates_to_array($area);
$res = db_query("SELECT nid, type, AsText(position) as position_wkt
FROM {simple_geo_position}
WHERE Contains(GeomFromText('%s'), position)", simple_geo_to_wkt('polygon', $area));
$points = array();
while ($bb_match = db_fetch_object($res)) {
$p_coords = simple_geo_clean_wkt('point', $bb_match->position_wkt);
if (simple_geo_point_in_polygon($p_coords, $a_array)) {
$p = array($bb_match->nid, $bb_match->type, $id, $type);
$updates[join('-', $p)] = $p;
}
}
}
}
if (count($updates)) {
//Add all new cache items
$params = array();
$query = "INSERT INTO {simple_geo_in_area}(point_nid, point_type, area_nid, area_type) VALUES";
foreach ($updates as $pk => $update) {
$query .= "(%d, '%s', %d, '%s'),";
$params = array_merge($params, $update);
}
array_unshift($params, trim($query, ','));
call_user_func_array('db_query', $params);
//Notify all modules of new spatial relationships
$handlers = module_implements('simple_geo_inside');
foreach ($updates as $pk => $update) {
foreach ($handlers as $module) {
call_user_func_array($module .'_simple_geo_inside', $update);
}
}
}
}
/**
* Checks if a nodes point is inside a nodes area
*
* @param int $area_nid The nid of the area node
* @param int $position_nid The nid of the position node
*
* @return void
*/
function simple_geo_contains($area_nid, $area_type, $position_nid, $position_type) {
$res = db_query("SELECT COUNT(*) FROM {simple_geo_in_area} WHERE point_nid=%d AND point_type='%s' AND area_nid=%d AND area_type='%s'",
$position_nid, $position_type, $area_nid, $area_type);
return db_result($res)==1;
}
/**
* Function that returns the necessary information for integrating with the
* position data table
*
* @return void
*/
function simple_geo_position_db_info() {
return array(
'table' => 'simple_geo_position',
'id' => 'nid',
'type' => 'type',
'geometry' => 'position',
);
}
/**
* Function that returns the necessary information for integrating with the
* area data table
*
* @return void
*/
function simple_geo_area_db_info() {
return array(
'table' => 'simple_geo_area',
'id' => 'nid',
'type' => 'type',
'geometry' => 'area',
);
}
/**
* Function that returns the necessary information for integrating with the
* spatial relationship cache
*
* @return void
*/
function simple_geo_cache_db_info() {
return array(
'table' => 'simple_geo_in_area',
'position' => 'point_nid',
'position_type' => 'point_type',
'area' => 'area_nid',
'area_type' => 'area_type',
);
}
/**
* Checks if a point is inside a polygon
*
* @param array $point An array containing lat & long or a WKT formatted string with a coordinate pair
* @param array $point An array containing arrays of lat & long or a WKT formatted string with a coordinate pairs
*
* @return bool
*/
function simple_geo_point_in_polygon($point, $polygon) {
// Transform string coordinates into arrays with lat and long values
if (!is_array($point)) {
$point = simple_geo_coordinates_to_array($point);
}
$vertices = $polygon;
if (!is_array($vertices)) {
$vertices = simple_geo_coordinates_to_array($vertices);
}
// Check if the point sits exactly on a vertex
if (simple_geo_point_on_vertex($point, $vertices)) {
return TRUE;
}
// Check if the point is inside the polygon or on the boundary
$intersections = 0;
$vertices_count = count($vertices);
for ($i=1; $i < $vertices_count; $i++) {
$vertex1 = $vertices[$i-1];
$vertex2 = $vertices[$i];
if ($vertex1[1] == $vertex2[1]
and $vertex1[1] == $point[1]
and $point[0] > min($vertex1[0], $vertex2[0])
and $point[0] < max($vertex1[0], $vertex2[0])) { // Check if point is on an horizontal polygon boundary
return TRUE;
}
if ($point[1] > min($vertex1[1], $vertex2[1])
and $point[1] <= max($vertex1[1], $vertex2[1])
and $point[0] <= max($vertex1[0], $vertex2[0])
and $vertex1[1] != $vertex2[1]) {
$xinters = ($point[1] - $vertex1[1]) * ($vertex2[0] - $vertex1[0]) / ($vertex2[1] - $vertex1[1]) + $vertex1[0];
if ($xinters == $point[0]) { // Check if point is on the polygon boundary (other than horizontal)
return TRUE;
}
if ($vertex1[0] == $vertex2[0] || $point[0] <= $xinters) {
$intersections++;
}
}
}
// If the number of edges we passed through is even, then it's in the polygon.
if ($intersections % 2 != 0) {
return TRUE;
}
return FALSE;
}
/**
* Helper function for simple_geo_point_in_polygon that checks
* if a point is on a vertex
*
* @return bool
*/
function simple_geo_point_on_vertex($point, $vertices) {
foreach ($vertices as $vertex) {
if ($point == $vertex) {
return TRUE;
}
}
}
/**
* Deletes the position of a node
*
* Does NOT update the cache
* see simple_geo_update_cache for updating the cache
*
* @return void
*/
function simple_geo_delete_position($id, $type) {
db_query("DELETE FROM {simple_geo_position} WHERE nid = %d AND type = '%s'", $id, $type);
}
/**
* Deletes the area of a node
*
* Does NOT update the cache
* see simple_geo_update_cache for updating the cache
*
* @return void
*/
function simple_geo_delete_area($id, $type) {
db_query("DELETE FROM {simple_geo_area} WHERE nid = %d AND type = '%s'", $id, $type);
}
/**
* Sets the position of a node
*
* Does NOT update the cache
* see simple_geo_update_cache for updating the cache
*
* @return void
*/
function simple_geo_set_position($id, $type, $position) {
simple_geo_delete_position($id, $type);
db_query("INSERT INTO {simple_geo_position}(nid, type, position)
VALUES(%d, '%s', GeomFromText('%s'))", $id, $type, simple_geo_to_wkt('point', $position));
}
/**
* Sets the area of a node
*
* Does NOT update the cache
* see simple_geo_update_cache for updating the cache
*
* @return void
*/
function simple_geo_set_area($id, $type, $area) {
simple_geo_delete_area($id, $type);
db_query("INSERT INTO {simple_geo_area}(nid, type, area)
VALUES(%d, '%s', GeomFromText('%s'))", $id, $type, simple_geo_to_wkt('polygon', $area));
}
/**
* Constructs a WKT-string for points and polygons
*
* @param string $type The WKT-type to construct, 'point', 'polygon' or 'linestring'
* @param string $coord The coodinate string with coordinates and pairs separated according to the WKT standard
*
* @return string The WKT-string
*/
function simple_geo_to_wkt($type, $coord) {
switch ($type) {
case 'point':
return 'POINT('. $coord .')';
case 'polygon':
return 'POLYGON(('. $coord .'))';
case 'linestring':
return 'LINESTRING('. $coord .')';
}
}
/**
* Takes a of with coordinates in the WKT-format and
* and returns them as an array.
*
* If the string only contains a single coordinate pair
* an array with lat,long will be returned. Otherwise
* an array containing arrays for each coordinate pair
* will be returned.
*
* @return void
*/
function simple_geo_coordinates_to_array($coords) {
$pairs = explode(',', $coords);
$pair_count = count($pairs);
for ($i=0; $i<$pair_count; $i++) {
$pairs[$i] = explode(' ', $pairs[$i]);
}
if ($pair_count==1) {
return $pairs[0];
}
else {
return $pairs;
}
}
/**
* Helper function that takes a string with WKT
* formatted coordinate pairs and changes it to
* use other separators.
*
* @return string The reformatted coordinate pairs
*/
function simple_geo_custom_separators($coords, $pair=SIMPLE_GEO_PAIR_SEPARATOR, $coordinate=SIMPLE_GEO_COORDINATE_SEPARATOR) {
//No change required
if ($pair==SIMPLE_GEO_PAIR_SEPARATOR && $coordinate==SIMPLE_GEO_COORDINATE_SEPARATOR) {
return $coords;
}
//Map separators to temporary tokens to avoid mixup of separator types
$coords = str_replace(SIMPLE_GEO_PAIR_SEPARATOR, '#P', $coords);
$coords = str_replace(SIMPLE_GEO_COORDINATE_SEPARATOR, '#C', $coords);
//Return the string with the new separators
return str_replace('#P', $pair, str_replace('#C', $coordinate, $coords));
}
/**
* Removes WKT geometry type information to return a
* clean string containing the coordinate pairs
*
* @param $type The type of the WKT string. 'point' or 'polygon'
* @param $type The WKT-representation of a geometry
*
* @return string The coordinate pairs
*/
function simple_geo_clean_wkt($type, $wkt) {
switch ($type) {
case 'point':
return preg_replace('/^POINT\((.+)\)$/', '$1', $wkt);
case 'polygon':
return preg_replace('/^POLYGON\(\((.+)\)\)$/', '$1', $wkt);
}
}
/**
* Implementation of hook_views_api().
*/
function simple_geo_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'simple_geo') . '/includes',
);
}
/**
* Implementation of hook_node_type().
*/
function simple_geo_node_type($op, $info) {
$vars = array(
'simple_geo_allow_area_',
'simple_geo_allow_position_',
'simple_geo_sync_translations_',
'simple_geo_show_in_form',
);
switch ($op) {
case 'delete':
foreach ($vars as $var) {
variable_del($var . $info->type);
}
break;
case 'update':
foreach ($vars as $var) {
$option = variable_get($var . $info->type, NULL);
if ($option !== NULL) {
variable_set($var . $info->type, $option);
if (isset($info->old_type)) {
variable_del($var . $info->old_type);
}
}
}
break;
}
}