Advertisement
Guest User

createUpdatePolygons.php

a guest
Dec 22nd, 2011
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.57 KB | None | 0 0
  1. <?php
  2. /**
  3.  * This script is distributed in the hope that it will be useful,
  4.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
  6.  *
  7.  * This script reads a list of tiles, which has been created by osm2pgsql after a differential update with parameter "-e" and "-o".
  8.  * The tilenumbers get converted to BoundingBoxes, and these are stored in a PostGIS DB.
  9.  * This information can then be used with MapProxy, to seed or cleanup specific regions (using an ogr datasource in the seed.yaml)
  10.  *
  11.  * If you need more Information or Details on how to use this script, you can email me anytime.
  12.  *
  13.  * Parts of the code (converting tiles to boundingboxes) come from http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
  14.  * You can find examples in lots of different programming languages there
  15.  *
  16.  * The script includes connection details for the PostGIS-database in the pgsql.conf.php which has to look like this:
  17.  *
  18.  * <?php
  19.  * $host = "the_host";
  20.  * $port = "5432";
  21.  * $dbname = "osm";
  22.  * $user = "username";
  23.  * $password = "pass";
  24.  * $connection = "host=$host port=$port dbname=$dbname user=$user password=$password";
  25.  * ?>
  26.  *
  27.  * @author J. Weskamm <weskamm@terrestris.de>
  28.  *
  29.  */
  30. include ("./pgsql.conf.php");
  31.  
  32. // Define the name of the table where the polygons should be written to
  33. // The table hast to have a "the_geom" column of type Geometry
  34. $updateTable = 'updatepolygons';
  35.  
  36. // Enter the correct path to the expire.list, which is genereated by osm2pgsql with option "-e" and "-o"
  37. $list = '/opt/diffs/expire.list';
  38.  
  39. if (!file_exists($list)) {
  40.     echo "ERROR! File expire.list not found, cancelling...";
  41.     exit ;
  42. }
  43.  
  44. $con = pg_connect($connection);
  45. $listentry = file($list);
  46.  
  47. // First we clean up the old data
  48. $cleanupsql = "DELETE from ".$updateTable;
  49. $resclean = pg_query($cleanupsql);
  50. if (!$resclean) {
  51.     echo "ERROR: Cleanup failed! Continuing anyway...";  
  52. }
  53.  
  54. foreach ($listentry as $line) {
  55.  
  56.     $parts = explode("/", $line);
  57.     $z = $parts[0];
  58.     $x = $parts[1];
  59.     $y = $parts[2];
  60.  
  61.     $bbox = tile2boundingBox($x, $y, $z);
  62.  
  63.     $insertsql = "INSERT INTO ".$updateTable."(the_geom) SELECT ST_Transform(ST_GeomFromText('POLYGON((";
  64.  
  65.     $insertsql .= $bbox[west] . " " . $bbox[south] . ", ";
  66.     $insertsql .= $bbox[west] . " " . $bbox[north] . ", ";
  67.     $insertsql .= $bbox[east] . " " . $bbox[north] . ", ";
  68.     $insertsql .= $bbox[east] . " " . $bbox[south] . ", ";
  69.     $insertsql .= $bbox[west] . " " . $bbox[south];
  70.  
  71.     $insertsql .= "))', 4326),900913)";
  72.  
  73.     $res = pg_query($insertsql);
  74.     if (!$res) {
  75.         echo "error on INSERT in DB, the SQL was: " . $insertsql . "     cancel operation....";
  76.         exit;
  77.     }
  78. }
  79.  
  80. echo "finished Postgis INSERTS...";
  81.  
  82. /**
  83.  
  84.  *
  85.  *
  86.  * @param object $x
  87.  * @param object $y
  88.  * @param object $zoom
  89.  * @return
  90.  */
  91. function tile2boundingBox($x, $y, $zoom) {
  92.  
  93.     $x = intval($x);
  94.     $y = intval($y);
  95.     $zoom = intval($zoom);
  96.  
  97.     $bb = array();
  98.     $bb['north'] = tile2lat($y, $zoom);
  99.     $bb['south'] = tile2lat($y + 1, $zoom);
  100.     $bb['west'] = tile2lon($x, $zoom);
  101.     $bb['east'] = tile2lon($x + 1, $zoom);
  102.  
  103.     return $bb;
  104. }
  105.  
  106. /**
  107.  *
  108.  * @param object $x
  109.  * @param object $z
  110.  * @return
  111.  */
  112. function tile2lon($x, $z) {
  113.  
  114.     $n = pow(2, $z);
  115.     $ret = $x / $n * 360.0 - 180.0;
  116.  
  117.     return $ret;
  118. }
  119.  
  120. /**
  121.  *
  122.  * @param object $y
  123.  * @param object $z
  124.  * @return
  125.  */
  126. function tile2lat($y, $z) {
  127.  
  128.     $n = pow(2, $z);
  129.     $ret = rad2deg(atan(sinh(pi() * (1 - 2 * $y / $n))));
  130.     return $ret;
  131. }
  132. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement