-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
137 lines (75 loc) · 2.9 KB
/
functions.php
File metadata and controls
137 lines (75 loc) · 2.9 KB
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
<?php
function carto($query, $geojson) {
// run any cartodb query
include('api.php');
$api_key = $cartoapi;
$carto_root = 'http://fkh.cartodb.com/api/v2/sql?';
if ($geojson == TRUE) {
$format = "format=GeoJSON&";
}
$sql_query = urlencode($query);
$carto_url = $carto_root . $format . "q=" . $sql_query . "&api_key=" . $api_key ;
// doing the curl...
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $carto_url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$carto_result = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
return $carto_result;
}
// get the random point to seed the map, called by point.php
function getRandomPoint() {
//prevent query caching by fetching a random point
$cartodbid = rand(1,23821);
$get_random_point = "SELECT * FROM bkblocks WHERE cartodb_id = {$cartodbid} limit 1";
$result = carto($get_random_point, TRUE);
return $result;
}
// get a bounded point. $bounds is a bbox string.
function getRandomBoundedPoint($bounds) {
$timestamp = time();
$get_random_point = "SELECT *,'{$timestamp}' FROM bkblocks WHERE bkblocks.the_geom && ST_MakeEnvelope({$bounds}, 4326) order by random() limit 1";
$result = carto($get_random_point, TRUE);
return $result;
}
// construct a url to insert a point into the db
function addNeighborhoodName($name, $block) {
$timestamp = time();
$sql = "INSERT INTO NAMES (neighborhood, block, timestamp) VALUES ('{$name}', {$block}, {$timestamp})";
carto($sql, FALSE);
}
function countNames() {
$db = dbConnect();
$rs = $db->Execute('select count(*) as "count" from names;');
while ($row = $rs->FetchNextObject()) {
$count = $row->COUNT;
}
$totalCount = array(names => $count);
return $totalCount;
}
//returns a list of neighborhood names, where more than one marker has been named.
function getNeighborhoods() {
$sql = 'select count(*) as "Number", names.neighborhood from names group by names.neighborhood having count(*) > 1 order by names.neighborhood ASC';
$names = carto($sql, FALSE);
return $names;
//SELECT bkblocks.the_geom_webmercator, names.neighborhood FROM bkblocks, names where bkblocks.ID = names.block and names.neighborhood like '%Park Slope%'
}
function getMarkers($focus) {
//return all the markers for a particular place name;
$db = dbConnect();
$sql = "SELECT * FROM \"blocks\", \"names\" where \"blocks\".\"ID\" = \"names\".\"block\" and \"names\".\"neighborhood\" = '\\'" . $focus . "\\'';";
$rs = $db->Execute($sql);
$markers = array();
while ($row = $rs->FetchNextObject()) {
$location = array("x" => $row->X, "y" => $row->Y);
array_push($markers, $location);
}
return $markers;
}
function stats(){
//count total number of nabes and total blocks...
}
?>