-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqlite_geojson.php
134 lines (125 loc) · 4.32 KB
/
sqlite_geojson.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
<?php
/**
* SQLite to GeoJSON (Requires https://github.com/phayes/geoPHP)
* Query a SQLite table or view (with a WKB GEOMETRY field) and return the results in GeoJSON format, suitable for use in OpenLayers, Leaflet, etc.
* @param string $filePath The SQLite file *REQUIRED*
* @param string $geotable The SQLite table name *REQUIRED*
* @param string $geomfield The WKB GEOMETRY field *REQUIRED*
* @param string $fields Fields to be returned *OPTIONAL (If omitted, all fields will be returned)*
* @param string $parameters SQL WHERE clause parameters *OPTIONAL*
* @param string $orderby SQL ORDER BY constraint *OPTIONAL*
* @param string $sort SQL ORDER BY sort order (ASC or DESC) *OPTIONAL*
* @param string $limit Limit number of results returned *OPTIONAL*
* @param string $offset Offset used in conjunction with limit *OPTIONAL*
* @return string resulting geojson string
*/
/* Include required geoPHP library and define wkb_to_json function
include_once('../geoPHP/geoPHP.inc');
function wkb_to_json($wkb) {
$geom = geoPHP::load($wkb,'wkb');
return $geom->out('json');
}*/
function escapeJsonString($value) { # list from www.json.org: (\b backspace, \f formfeed)
$escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c");
$replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b");
$result = str_replace($escapers, $replacements, $value);
return $result;
}
# Retrive URL variables
if (empty($_GET['filePath'])) {
echo "missing required parameter: <i>filePath</i>";
exit;
}else{
$filePath = $_GET['filePath'];
}
if (empty($_GET['geotable'])) {
echo "missing required parameter: <i>geotable</i>";
exit;
} else
$geotable = $_GET['geotable'];
/*if (empty($_GET['geomfield'])) {
echo "missing required parameter: <i>geomfield</i>";
exit;
} else
$geomfield = $_GET['geomfield'];
*/
if (empty($_GET['fields'])) {
$fields = '*';
} else
$fields = $_GET['fields'];
if (empty($_GET['parameters'])) {
$parameters = '';
} else
$parameters = $_GET['parameters'];
if (empty($_GET['orderby'])) {
$orderby = '';
} else
$orderby = $_GET['orderby'];
if (empty($_GET['sort'])) {
$sort = 'ASC';
} else
$sort = $_GET['sort'];
if (empty($_GET['limit'])) {
$limit = '';
} else
$limit = $_GET['limit'];
if (empty($_GET['offset'])) {
$offset = '';
} else
$offset = $_GET['offset'];
if (empty($_GET['groupby'])) {
$groupby = '';
} else
$groupby = $_GET['groupby'];
# Build SQL SELECT statement and return the geometry as a GeoJSON element
$sql = "SELECT " . SQLite3::escapeString($fields) . " FROM " . SQLite3::escapeString($geotable);
if (strlen(trim($parameters)) > 0) {
$sql .= " WHERE " . SQLite3::escapeString($parameters);
}
if (strlen(trim($groupby)) > 0) {
$sql .= " GROUP BY " . SQLite3::escapeString($groupby);
}
if (strlen(trim($orderby)) > 0) {
$sql .= " ORDER BY " . SQLite3::escapeString($orderby) . " " . $sort;
}
if (strlen(trim($limit)) > 0) {
$sql .= " LIMIT " . SQLite3::escapeString($limit);
}
if (strlen(trim($offset)) > 0) {
$sql .= " OFFSET " . SQLite3::escapeString($offset);
}
# Connect to SQLite database
$db = new PDO('sqlite:'.$filePath);
# Try query or error
$rs = $db->query($sql);
if (!$rs) {
echo $sql."\n";
echo "An SQL error occured.\n".$rs;
exit;
}
# Build GeoJSON
#die( $sql);
$output = '';
$rowOutput = '';
while ($row = $rs->fetch(PDO::FETCH_ASSOC)) {
$geom_from_lat_lng = '{"type": "Point","coordinates": ['.$row['lng'].','.$row['lat'].']}';
$rowOutput = (strlen($rowOutput) > 0 ? ',' : '') . '{"type": "Feature", "geometry": ' . $geom_from_lat_lng . ', "properties": {';
$props = '';
$id = '';
foreach ($row as $key => $val) {
if ($key != "geojson") {
$props .= (strlen($props) > 0 ? ',' : '') . '"' . $key . '":"' . escapeJsonString($val) . '"';
}
if ($key == "id") {
$id .= ',"id":"' . escapeJsonString($val) . '"';
}
}
$rowOutput .= $props . '}';
$rowOutput .= $id;
$rowOutput .= '}';
$output .= $rowOutput;
}
$output = '{ "type": "FeatureCollection", "features": [ ' . $output . ' ]}';
echo $output;
$db = NULL;
?>