-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
229 lines (170 loc) · 7.14 KB
/
index.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
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
<?php
//TODO: Move Query to Function / Class so it's can useable
require_once __DIR__ . '/vendor/autoload.php';
include "lib/utility.php";
$klein = new \Klein\Klein();
header('Content-type:application/json');
header("Access-Control-Allow-Origin: *");
$klein->with('/region', function () use ($klein) {
$klein->respond('GET', '/?', function ($request, $response) {
$sql = "SELECT kabupaten_ from tb_kabupaten WHERE provinsi = 'Jawa Timur' ORDER BY kabupaten_";
$result = msq_fetch_all($sql);
$data['kota_kab'] = array();
$index = 0;
foreach($result as $item){
$reg = array();
$reg['id'] = $index++;
$reg['name'] = $item['kabupaten_'];
array_push($data['kota_kab'], $reg);
}
return json_encode($data);
});
});
$klein->with('/place', function () use ($klein) {
$klein->respond('GET', '/?', function ($request, $response) {
$sql = "SELECT jenis_fitur, id from tb_markers GROUP BY jenis_fitur";
$result['places'] = msq_fetch_all($sql);
return json_encode($result);
});
});
$klein->with('/polygon', function () use ($klein) {
$klein->respond('GET', '/?', function ($request, $response) {
$json = array();
$sql = "SELECT id, kabupaten_ AS kabupaten, kode, ibukota, provinsi, bupati_wal, wakil, batas_utar, batas_sela, batas_bara, batas_timu, lmb_geo FROM tb_kabupaten WHERE kabupaten_ = 'jember'";
$result = msq_fetch($sql);
$sql = "SELECT AsText(SHAPE) AS geometry FROM tb_kabupaten WHERE kabupaten_='jember'";
$raw = msq_fetch($sql);
$polygon = geoPHP::load($raw['geometry'], 'wkt');
$data = $polygon->asArray();
$json['type'] = "Feature";
$json['id'] = "1";
$json['properties'] = $result;
$json['geometry']['type'] = $polygon->geometryType();
$json['geometry']['coordinates'] = $data;
return json_encode($json);
});
$klein->respond('GET', '/[:name]', function ($request, $response) {
$json = array();
$region = ms_escape($request->name);
$sql = "SELECT kabupaten_ AS Kabupaten, kode Kode, ibukota 'Ibu Kota', provinsi Provinsi, bupati_wal 'Bupati/Wali Kota', wakil Wakil, batas_utar 'Batas Utara', batas_sela 'Batas Selatan', batas_bara 'Batas Barat', batas_timu 'Batas Timur', lmb_geo FROM tb_kabupaten WHERE kabupaten_ = '$region'";
$result = msq_fetch($sql);
if($result == null){
$json['code'] = 404;
$json['message'] = "Data Not Found!";
}else{
$sql = "SELECT AsText(SHAPE) AS geometry FROM tb_kabupaten WHERE kabupaten_='$region'";
$raw = msq_fetch($sql);
$polygon = geoPHP::load($raw['geometry'], 'wkt');
$data = $polygon->asArray();
$json['type'] = "Feature";
$json['id'] = "1";
$json['properties'] = $result;
$json['geometry']['type'] = $polygon->geometryType();
$json['geometry']['coordinates'] = $data;
}
return json_encode($json);
});
});
$klein->with('/markers', function () use ($klein) {
$klein->respond('GET', '/?', function ($request, $response) {
$data = array();
$sql = "SELECT lat_long FROM tb_markers";
$geometries = msq_fetch_all($sql);
$sql = "SELECT jenis_fitur, nama_fitur, kategori, kota_kab, deskripsi FROM tb_markers";
$properties = msq_fetch_all($sql);
if($geometries == null){
$data['code'] = 400;
$data['message'] = "Data Not Found!";
return json_encode($data);
}
$data['type'] = 'FeatureCollection';
$features = array();
$index = 0;
foreach($geometries as $geo){
$pointstr = explode(",", $geo['lat_long']);
$geometry['type'] = "Point";
$geometry['coordinates'] = array(floatval($pointstr[1]), floatval($pointstr[0]));
$propertie = $properties[$index];
$features[$index]['type'] = "Feature";
$features[$index]['geometry'] = $geometry;
$features[$index]['properties'] = $propertie;
$index++;
}
$data['features'] = $features;
return json_encode($data);
});
$klein->respond('GET', '/[:jenis]/?', function ($request, $response) {
$data = array();
$jenis = ms_escape($request->jenis);
$jenis = str_replace("_", " ", $jenis);
$sql = "SELECT lat_long FROM tb_markers WHERE jenis_fitur = '$jenis'";
$geometries = msq_fetch_all($sql);
$sql = "SELECT jenis_fitur 'Jenis Instansi', nama_fitur 'Nama Instansi', kategori 'Kategori', kota_kab 'Kab/Kota', deskripsi 'Deskripsi', lat_long 'Koordinat' FROM tb_markers WHERE jenis_fitur = '$jenis'";
$properties = msq_fetch_all($sql);
if($geometries == null){
$data['code'] = 400;
$data['message'] = "Data Not Found!";
return json_encode($data);
}
$data['type'] = 'FeatureCollection';
$features = array();
$index = 0;
foreach($geometries as $geo){
$pointstr = explode(",", $geo['lat_long']);
$geometry['type'] = "Point";
$geometry['coordinates'] = array(floatval($pointstr[1]), floatval($pointstr[0]));
$propertie = $properties[$index];
$features[$index]['type'] = "Feature";
$features[$index]['geometry'] = $geometry;
$features[$index]['properties'] = $propertie;
$index++;
}
$data['features'] = $features;
return json_encode($data);
});
$klein->respond('GET', '/[:jenis]/[:region]/?', function ($request, $response) {
$data = array();
$jenis = ms_escape($request->jenis);
$region = ms_escape($request->region);
$jenis = str_replace("_", " ", $jenis);
$sql = "SELECT lat_long FROM tb_markers WHERE jenis_fitur = '$jenis' AND kota_kab = '$region'";
$geometries = msq_fetch_all($sql);
$sql = "SELECT jenis_fitur, nama_fitur, kategori, kota_kab, deskripsi FROM tb_markers WHERE jenis_fitur = '$jenis' AND kota_kab = '$region'";
$properties = msq_fetch_all($sql);
if($geometries == null){
$data['code'] = 400;
$data['message'] = "Data Not Found!";
return json_encode($data);
}
$data['type'] = 'FeatureCollection';
$features = array();
$index = 0;
foreach($geometries as $geo){
$pointstr = explode(",", $geo['lat_long']);
$geometry['type'] = "Point";
$geometry['coordinates'] = array(floatval($pointstr[1]), floatval($pointstr[0]));
$propertie = $properties[$index];
$features[$index]['type'] = "Feature";
$features[$index]['geometry'] = $geometry;
$features[$index]['properties'] = $propertie;
$index++;
}
$data['features'] = $features;
return json_encode($data);
});
});
$klein->respond('GET', '/?', function ($request) {
$json = array();
$json['code'] = 200;
$json['message'] = "Welcome to GIS API!";
return json_encode($json);
});
$klein->respond('404', function ($request) {
$json = array();
$json['code'] = 404;
$json['message'] = "Page Not Found!";
return json_encode($json);
});
//marker/{jenis}/{kategori}/?region=
$klein->dispatch();
?>