-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi_geo.php
130 lines (93 loc) · 2.51 KB
/
api_geo.php
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
<?php
// Wrapper around Cloudant geo API
require_once (dirname(__FILE__) . '/couchsimple.php');
require_once (dirname(__FILE__) . '/lib.php');
require_once (dirname(__FILE__) . '/api_utils.php');
require_once (dirname(__FILE__) . '/elastic.php');
//--------------------------------------------------------------------------------------------------
function default_display()
{
echo "hi";
}
//--------------------------------------------------------------------------------------------------
function display_wkt($wkt, $limit = 200, $callback = '')
{
global $config;
global $couch;
$url = $config['couchdb_options']['prefix']
. $config['couchdb_options']['host']
. '/' . $config['couchdb_options']['database']
. '/_design/geodd/_geo/points?g=' . urlencode($wkt)
. '&relation=intersects'
. '&include_docs=true'
. '&limit=' . $limit;
//echo $url;
$json = get($url);
$obj = json_decode($json);
$obj->status = 200;
// trim excess?
api_output($obj, $callback);
}
//--------------------------------------------------------------------------------------------------
function display_elastic_geo($geojson, $limit=50, $callback='')
{
global $config;
global $elastic;
$query_json = '{
"size" : <LIMIT>,
"query": {
"bool" : {
"must" : {
"match_all" : {}
},
"filter" : {
"geo_shape" : {
"search_data.geometry" : {
"shape": <SHAPE>
}
}
}
}
}
}';
$query_json = str_replace('<LIMIT>', $limit, $query_json);
$geojson_obj = json_decode($geojson);
$query_json = str_replace('<SHAPE>', json_encode($geojson_obj->geometry), $query_json);
//echo $query_json ;
$resp = $elastic->send('POST', '_search?pretty', $post_data = $query_json);
$obj = json_decode($resp);
// Add status
$obj->status = 200;
api_output($obj, $callback);
}
//--------------------------------------------------------------------------------------------------
function main()
{
$callback = '';
$handled = false;
// If no query parameters
if (count($_GET) == 0)
{
default_display();
exit(0);
}
if (isset($_GET['callback']))
{
$callback = $_GET['callback'];
}
if (!$handled)
{
if (isset($_GET['wkt']))
{
display_wkt($_GET['wkt'], 100, $callback);
$handled = true;
}
if (isset($_GET['geojson']))
{
display_elastic_geo($_GET['geojson'], 50, $callback);
$handled = true;
}
}
}
main();
?>