forked from tisba/couchdocs
/
StoringGeoData.mm
191 lines (157 loc) · 4.87 KB
/
StoringGeoData.mm
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
#redirect Storing_GeoData
= Using CouchDB For Storing Google Geocoded JSON Data =
Google provides a free service that takes any string as an input and returns a bunch of JSON encoded data if that string matches a physical location. Check it out here: http://code.google.com/apis/maps/documentation/services.html#Geocoding
Unfortunately, Google's geocoding service is limited to 15k requests per day per IP. Sounds like a lot, but for certain applications this limit can be reached very quickly.
The following small library can help you get around these limitations by storing a local repository of data in CouchDB. This is a nice fit because CouchDB is JSON-native and the data returned from google is JSON-native.
The following is a rough and tumble library I put together called GeoCouch that you can use to easily handle geocoded data in CouchDB. This lib has a narrow scope right now - the requirements are:
* PHP 5+
* CouchDB
* A Google API Key (http://code.google.com/apis/maps/signup.html)
Download the file here: http://geocouch.googlecode.com/files/geocouch.php. You can also find the full source at the bottom of the page.
== Usage ==
{{{
<?php
require ('geocouch.php');
/*
* Don't forget to edit the $GeoCouch->conf parameters!
*/
$GeoCouch = new GeoCouch();
/*
* The all-in-one method.
* This geocodes the string and writes it to CouchDB
* The second parameter is any other fields other
* than the Google data that you want to save along
* with this document.
*
* NOTE: if this address already exists in CouchDB
* a new revision is created.
*
* Returns the CouchDB response, i.e.:
* {"ok" : true, "rev":"3825793742", "id" : "dallas-tx" }
*/
$GeoCouch->save('Dallas, TX', array('custom_field' => 'value'));
/*
* Simply geo coding.
* Does not write to CouchDB.
* Returns an Google Geocoded Object.
*/
$geoObj = $GeoCouch->geoCode('Dallas, TX');
/*
* Write some Geo JSON to CouchDB.
* First parameter is a unique name for the data
* Second parameter is the JSON - in
* this case the json_encoded $geoObj from above.
*/
$GeoCouch->put('Dallas, TX', json_encode($geoObj));
/*
* Get some existing geo data
*/
$geoObj = $GeoCouch->get('Dallas, TX');
?>
}}}
== GeoCouch Class ==
{{{
<?php
class GeoCouch
{
var $conf = array(
'host' => 'localhost',
'port' => '5984',
'db' => 'sf_geo',
'geocoder' => array(
'url' => 'http://maps.google.com/maps/geo?key=',
'key' => 'Your Google API Key',
),
);
var $address;
var $geoJSONResponse;
var $geoObj;
function GeoCouch() {
}
function geoCode($address = null)
{
$this->address = $address;
$url = $this->conf['geocoder']['url'].$this->conf['geocoder']['key'];
$url .= '&q='.urlencode($address);
$this->geoJSONResponse = $this->_geoCodeRequest($url);
$this->geoObj = json_decode($this->geoJSONResponse);
if(empty($this->geoObj->Status->code) || $this->geoObj->Status->code != 200) {
return false;
} else {
return $this->geoJSONResponse;
}
}
function _geoCodeRequest($url)
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);
ob_start();
curl_exec ($ch);
curl_close ($ch);
$string = ob_get_contents();
ob_end_clean();
return $string;
}
function locationName($str) {
return trim(preg_replace('/[^a-z0-9]+/i', '-', $str), '_');
}
function save($address, $extra = array())
{
$existing = $this->get($address);
if($this->geoCode($address))
{
if(!empty($existing->_rev)) {
$this->geoObj->_rev = $existing->_rev;
}
foreach($extra as $field => $value) {
$this->geoObj->$field = $value;
}
return $this->put($address, json_encode($this->geoObj));
}
else {
return false;
}
}
function get($name = null)
{
$s = $this->openSock();
$url = '/'.$this->conf['db'].'/'.$this->locationName($name);
$request = 'GET '.$url.' HTTP/1.0'. "\r\n";
$request .= 'Host: localhost'. "\r\n\r\n";
fwrite($s, $request);
return $this->parseCouchResponse($s);
}
function put($name = null, $json = null)
{
$s = $this->openSock();
$url = '/'.$this->conf['db'].'/'.$this->locationName($name);
$request = 'PUT '.$url.' HTTP/1.0'. "\r\n";
$request .= 'Host: localhost'. "\r\n";
$request .= 'Content-Length: '.strlen($json)."\r\n\r\n";
$request .= $json."\r\n";
fwrite($s, $request);
return $this->parseCouchResponse($s);
}
function openSock()
{
$s = fsockopen($this->conf['host'], $this->conf['port'], $errno, $errstr);
if(!$s) {
return $errno.':'.$errstr;
} else {
return $s;
}
}
function parseCouchResponse($s)
{
$response = '';
while(!feof($s)) {
$response .= fgets($s);
}
fclose($s);
list($headers, $body) = explode("\r\n\r\n", $response);
return json_decode($body);
}
}
?>
}}}