Harry Wood
home
>
maps
>
examples
> openlayers > db >
../maps/examples/openlayers/db/db2text.php
../maps/examples/openlayers/db/db2text.php
<?php /* * This PHP script reads points from a database and outputs a text formatted * as expected by the OpenLayers text parser (actually tab seperated variables) * In the absence of bbox param it reads all points from the database. * * You can point OpenLayers directly at this php script. Optionally you can then * use a BBOX strategy. This tells OpenLayers to add on a 'bbox' parameter, * which this script can use to limit the number of points returned. */ header('Content-Type:text/plain'); $bbox = isset($_GET['bbox']) ? $_GET['bbox'] : ""; if ($db = new SQLite3('example.db')) { $query = 'SELECT * FROM points '; if ($bbox!="") { //split out the comma sperated min/max lat/lon values $bbox_vars = explode(",", $bbox); $minlon = $bbox_vars[0]; $minlat = $bbox_vars[1]; $maxlon = $bbox_vars[2]; $maxlat = $bbox_vars[3]; //DB deals with integers $minx = round($minlon * 10000000); $miny = round($minlat * 10000000); $maxx = round($maxlon * 10000000); $maxy = round($maxlat * 10000000); $query .= "WHERE x>".$minx." AND x<".$maxx." AND y>".$miny." AND y<".$maxy; error_log("query=" .$query); sleep(3); //Artificial 3 second pause, to help see how the bboxes work } //Icon offset. For some reason this doesn't default to a sensible value for //the default marker graphic, so we output it in the text file. $icon_offset = "-10,-25"; $result = $db->query($query); if ($result === false) { die("Error during basic query:". $db->lastErrorMsg()); } else { print "lat lon title description iconOffset\n"; $count = 0; while($row = $result->fetchArray(SQLITE3_ASSOC)) { $count++; $id = $row['id']; $name = $row['name']; $x = $row['x']; $y = $row['y']; $lon = $x / 10000000; $lat = $y / 10000000; //output text file row which OpenLayers can understand print $lat."\t".$lon."\t".$name."\t".$name."\t".$icon_offset."\n"; } error_log("Returning " .$count. " points in a text file"); } } else { die($err); } ?>
view directly
Home
Blog
About
Maps
Other
Contact