-
-
Save ns-1m/2288956 to your computer and use it in GitHub Desktop.
PHP SQLite to GeoJSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 $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.inc'); | |
function wkb_to_json($wkb) { | |
$geom = geoPHP::load($wkb,'wkb'); | |
return $geom->out('json'); | |
} | |
# Connect to SQLite database | |
$db = new PDO('sqlite:mydbfile.sqlite'); | |
# Retrive URL variables | |
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']; | |
# Build SQL SELECT statement and return the geometry as a GeoJSON element | |
$sql = "SELECT " . sqlite_escape_string($fields) . ", " . sqlite_escape_string($geomfield) . " AS geojson FROM " . sqlite_escape_string($geotable); | |
if (strlen(trim($parameters)) > 0) { | |
$sql .= " WHERE " . sqlite_escape_string($parameters); | |
} | |
if (strlen(trim($orderby)) > 0) { | |
$sql .= " ORDER BY " . sqlite_escape_string($orderby) . " " . $sort; | |
} | |
if (strlen(trim($limit)) > 0) { | |
$sql .= " LIMIT " . sqlite_escape_string($limit); | |
} | |
if (strlen(trim($offset)) > 0) { | |
$sql .= " OFFSET " . sqlite_escape_string($offset); | |
} | |
//echo $sql; | |
# Try query or error | |
$rs = $db->query($sql); | |
if (!$rs) { | |
echo "An SQL error occured.\n"; | |
exit; | |
} | |
# Build GeoJSON | |
$output = ''; | |
$rowOutput = ''; | |
while ($row = $rs->fetch(PDO::FETCH_ASSOC)) { | |
$rowOutput = (strlen($rowOutput) > 0 ? ',' : '') . '{"type": "Feature", "geometry": ' . wkb_to_json($row['geojson']) . ', "properties": {'; | |
$props = ''; | |
$id = ''; | |
foreach ($row as $key => $val) { | |
if ($key != "geojson") { | |
$props .= (strlen($props) > 0 ? ',' : '') . '"' . $key . '":"' . $val . '"'; | |
} | |
if ($key == "id") { | |
$id .= ',"id":"' . $val . '"'; | |
} | |
} | |
$rowOutput .= $props . '}'; | |
$rowOutput .= $id; | |
$rowOutput .= '}'; | |
$output .= $rowOutput; | |
} | |
$output = '{ "type": "FeatureCollection", "features": [ ' . $output . ' ]}'; | |
echo $output; | |
$db = NULL; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment