-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgeofield.apachesolr.inc
110 lines (101 loc) · 3.12 KB
/
geofield.apachesolr.inc
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
<?php
/**
* @file
* Provides hooks for integration with ApacheSolr (https://www.drupal.org/project/apachesolr)
*/
/**
* Implements hook_apachesolr_field_mappings().
*/
function geofield_apachesolr_field_mappings() {
$mappings['geofield'] = array(
'indexing_callback' => 'geofield_apachesolr_indexing_callback',
'map callback' => 'geofield_apachesolr_map_callback',
'index_type' => 'geohash',
'facets' => TRUE,
'query types' => array('term','geo'),
'query type' => 'term',
);
return $mappings;
}
/**
* Apachesolr indexing callback for geofield.
*
* It will index 2 fields on Solr:
*
* 1- Multi-valued field with all geometry(ies) points - as expected.
* 2- The centroid of the geometry(ies), or the unique point.
*
* The single-value field is meant to be used for stats or as a performance
* improvement in simple queries.
*
* The centroid can also be used in simple queries in favor of performance,
* instead of using the full geometry when it is not necessary.
*
* @param object $entity
* @param string $field_name
* @param string $index_key
* @param array $field_info
* @return array $fields
*/
function geofield_apachesolr_indexing_callback($entity, $field_name, $index_key, $field_info) {
$fields = array();
if (!empty($entity->{$field_name})) {
$values = $entity->{$field_name}[LANGUAGE_NONE];
// If there is no geometry, just return
if (empty($values[0]['geom'])) {
return $fields;
}
// Store multiple geometries in a multi-valued Solr field.
foreach ($values as $delta => $item) {
$fields[] = array(
'key' => $index_key,
'value' => $item['lat'] . ',' . $item['lon'],
);
}
// Store the centroid of multiple geometries or the unique point in a
// singular Solr field.
if ($field_info['multiple'] && !empty($values[0])) {
// If there are multiple values, then get the centroid.
if (count($values) > 1) {
// Combine the geometries.
$geoms = array();
foreach ($values as $delta => $item) {
$geoms[] = geoPHP::load($item['geom']);
}
$combined_geom = geoPHP::geometryReduce($geoms);
// Get the centroid.
$centroid = $combined_geom->getCentroid();
// Format the value to be indexed.
$formated_value = $centroid->y() . ',' . $centroid->x();
}
// There is no need for additional computation of a single item.
else {
$item = reset($values);
$formated_value = $item['lat'] . ',' . $item['lon'];
}
// Get the field info and make it singular.
$singular_field_info = $field_info;
$singular_field_info['multiple'] = FALSE;
$single_key = apachesolr_index_key($singular_field_info);
$fields[] = array(
'key' => $single_key,
'value' => $formated_value,
);
}
}
return $fields;
}
/**
* Map of the facet labels.
*
* @param array $values
* @param array $options
* @return type
*/
function geofield_apachesolr_map_callback(array $values, array $options) {
$map = array();
foreach ($values as $key) {
$map[$key] = substr($key, 1) . 'km';
}
return $map;
}