Guest User

Untitled

a guest
Apr 13th, 2018
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>Testing SpatiaLite on PHP</title>
  4. </head>
  5. <body>
  6. <h1>testing SpatiaLite on PHP</h1>
  7.  
  8. <?php
  9. # connecting some SQLite DB
  10. # we'll actually use an IN-MEMORY DB
  11. # so to avoid any further complexity;
  12. # an IN-MEMORY DB simply is a temp-DB
  13. $db = new SQLite3(':memory:');
  14.  
  15. # loading SpatiaLite as an extension
  16. # dl('mod_spatialite.so');
  17. //$db->loadExtension('mod_spatialite.so');
  18. //sqlite3_enable_load_extension($db, 1);
  19. $db->loadExtension('mod_spatialite.so');
  20. //$db->exec("SELECT load_extension('mod_spatialite.so', 'sqlite3_modspatialite_init')");
  21.  
  22. # enabling Spatial Metadata
  23. # using v.2.4.0 this automatically initializes SPATIAL_REF_SYS
  24. # and GEOMETRY_COLUMNS
  25. $db->exec("SELECT InitSpatialMetadata(1)");
  26.  
  27. # reporting some version info
  28. $rs = $db->query('SELECT sqlite_version()');
  29. while ($row = $rs->fetchArray())
  30. {
  31. print "<h3>SQLite version: $row[0]</h3>";
  32. }
  33. $rs = $db->query('SELECT spatialite_version()');
  34. while ($row = $rs->fetchArray())
  35. {
  36. print "<h3>SpatiaLite version: $row[0]</h3>";
  37. }
  38.  
  39. # creating a POINT table
  40. $sql = "CREATE TABLE test_pt (";
  41. $sql .= "id INTEGER NOT NULL PRIMARY KEY,";
  42. $sql .= "name TEXT NOT NULL)";
  43. $db->exec($sql);
  44. # creating a POINT Geometry column
  45. $sql = "SELECT AddGeometryColumn('test_pt', ";
  46. $sql .= "'geom', 4326, 'POINT', 'XY')";
  47. $db->exec($sql);
  48.  
  49. # creating a LINESTRING table
  50. $sql = "CREATE TABLE test_ln (";
  51. $sql .= "id INTEGER NOT NULL PRIMARY KEY,";
  52. $sql .= "name TEXT NOT NULL)";
  53. $db->exec($sql);
  54. # creating a LINESTRING Geometry column
  55. $sql = "SELECT AddGeometryColumn('test_ln', ";
  56. $sql .= "'geom', 4326, 'LINESTRING', 'XY')";
  57. $db->exec($sql);
  58.  
  59. # creating a POLYGON table
  60. $sql = "CREATE TABLE test_pg (";
  61. $sql .= "id INTEGER NOT NULL PRIMARY KEY,";
  62. $sql .= "name TEXT NOT NULL)";
  63. $db->exec($sql);
  64. # creating a POLYGON Geometry column
  65. $sql = "SELECT AddGeometryColumn('test_pg', ";
  66. $sql .= "'geom', 4326, 'POLYGON', 'XY')";
  67. $db->exec($sql);
  68.  
  69. # inserting some POINTs
  70. # please note well: SQLite is ACID and Transactional
  71. # so (to get best performance) the whole insert cycle
  72. # will be handled as a single TRANSACTION
  73. $db->exec("BEGIN");
  74. for ($i = 0; $i < 10000; $i++)
  75. {
  76. # for POINTs we'll use full text sql statements
  77. $sql = "INSERT INTO test_pt (id, name, geom) VALUES (";
  78. $sql .= $i + 1;
  79. $sql .= ", 'test POINT #";
  80. $sql .= $i + 1;
  81. $sql .= "', GeomFromText('POINT(";
  82. $sql .= $i / 1000.0;
  83. $sql .= " ";
  84. $sql .= $i / 1000.0;
  85. $sql .= ")', 4326))";
  86. $db->exec($sql);
  87. }
  88. $db->exec("COMMIT");
  89.  
  90. # checking POINTs
  91. $sql = "SELECT DISTINCT Count(*), ST_GeometryType(geom), ";
  92. $sql .= "ST_Srid(geom) FROM test_pt";
  93. $rs = $db->query($sql);
  94. while ($row = $rs->fetchArray())
  95. {
  96. # read the result set
  97. $msg = "Inserted ";
  98. $msg .= $row[0];
  99. $msg .= " entities of type ";
  100. $msg .= $row[1];
  101. $msg .= " SRID=";
  102. $msg .= $row[2];
  103. print "<h3>$msg</h3>";
  104. }
  105.  
  106. # inserting some LINESTRINGs
  107. # this time we'll use a Prepared Statement
  108. $sql = "INSERT INTO test_ln (id, name, geom) ";
  109. $sql .= "VALUES (?, ?, GeomFromText(?, 4326))";
  110. $stmt = $db->prepare($sql);
  111. $db->exec("BEGIN");
  112. for ($i = 0; $i < 10000; $i++)
  113. {
  114. # setting up values / binding
  115. $name = "test LINESTRING #";
  116. $name .= $i + 1;
  117. $geom = "LINESTRING(";
  118. if (($i%2) == 1)
  119. {
  120. # odd row: five points
  121. $geom .= "-180.0 -90.0, ";
  122. $geom .= -10.0 - ($i / 1000.0);
  123. $geom .= " ";
  124. $geom .= -10.0 - ($i / 1000.0);
  125. $geom .= ", ";
  126. $geom .= -10.0 - ($i / 1000.0);
  127. $geom .= " ";
  128. $geom .= 10.0 + ($i / 1000.0);
  129. $geom .= ", ";
  130. $geom .= 10.0 + ($i / 1000.0);
  131. $geom .= " ";
  132. $geom .= 10.0 + ($i / 1000.0);
  133. $geom .= ", 180.0 90.0";
  134. }
  135. else
  136. {
  137. # even row: two points
  138. $geom .= -10.0 - ($i / 1000.0);
  139. $geom .= " ";
  140. $geom .= -10.0 - ($i / 1000.0);
  141. $geom .= ", ";
  142. $geom .= 10.0 + ($i / 1000.0);
  143. $geom .= " ";
  144. $geom .= 10.0 + ($i / 1000.0);
  145. }
  146. $geom .= ")";
  147.  
  148. $stmt->reset();
  149. $stmt->clear();
  150. $stmt->bindValue(1, $i+1, SQLITE3_INTEGER);
  151. $stmt->bindValue(2, $name, SQLITE3_TEXT);
  152. $stmt->bindValue(3, $geom, SQLITE3_TEXT);
  153. $stmt->execute();
  154. }
  155. $db->exec("COMMIT");
  156.  
  157. # checking LINESTRINGs
  158. $sql = "SELECT DISTINCT Count(*), ST_GeometryType(geom), ";
  159. $sql .= "ST_Srid(geom) FROM test_ln";
  160. $rs = $db->query($sql);
  161. while ($row = $rs->fetchArray())
  162. {
  163. # read the result set
  164. $msg = "Inserted ";
  165. $msg .= $row[0];
  166. $msg .= " entities of type ";
  167. $msg .= $row[1];
  168. $msg .= " SRID=";
  169. $msg .= $row[2];
  170. print "<h3>$msg</h3>";
  171. }
  172.  
  173. # insering some POLYGONs
  174. # this time too we'll use a Prepared Statement
  175. $sql = "INSERT INTO test_pg (id, name, geom) ";
  176. $sql .= "VALUES (?, ?, GeomFromText(?, 4326))";
  177. $stmt = $db->prepare($sql);
  178. $db->exec("BEGIN");
  179. for ($i = 0; $i < 10000; $i++)
  180. {
  181. # setting up values / binding
  182. $name = "test POLYGON #";
  183. $name .= $i + 1;
  184. $geom = "POLYGON((";
  185. $geom .= -10.0 - ($i / 1000.0);
  186. $geom .= " ";
  187. $geom .= -10.0 - ($i / 1000.0);
  188. $geom .= ", ";
  189. $geom .= 10.0 + ($i / 1000.0);
  190. $geom .= " ";
  191. $geom .= -10.0 - ($i / 1000.0);
  192. $geom .= ", ";
  193. $geom .= 10.0 + ($i / 1000.0);
  194. $geom .= " ";
  195. $geom .= 10.0 + ($i / 1000.0);
  196. $geom .= ", ";
  197. $geom .= -10.0 - ($i / 1000.0);
  198. $geom .= " ";
  199. $geom .= 10.0 + ($i / 1000.0);
  200. $geom .= ", ";
  201. $geom .= -10.0 - ($i / 1000.0);
  202. $geom .= " ";
  203. $geom .= -10.0 - ($i / 1000.0);
  204. $geom .= "))";
  205.  
  206. $stmt->reset();
  207. $stmt->clear();
  208. $stmt->bindValue(1, $i+1, SQLITE3_INTEGER);
  209. $stmt->bindValue(2, $name, SQLITE3_TEXT);
  210. $stmt->bindValue(3, $geom, SQLITE3_TEXT);
  211. $stmt->execute();
  212. }
  213. $db->exec("COMMIT");
  214.  
  215. # checking POLYGONs
  216. $sql = "SELECT DISTINCT Count(*), ST_GeometryType(geom), ";
  217. $sql .= "ST_Srid(geom) FROM test_pg";
  218. $rs = $db->query($sql);
  219. while ($row = $rs->fetchArray())
  220. {
  221. # read the result set
  222. $msg = "Inserted ";
  223. $msg .= $row[0];
  224. $msg .= " entities of type ";
  225. $msg .= $row[1];
  226. $msg .= " SRID=";
  227. $msg .= $row[2];
  228. print "<h3>$msg</h3>";
  229. }
  230.  
  231. # closing the DB connection
  232. $db->close();
  233. ?>
  234.  
  235. </body>
  236. </html>
Advertisement
Add Comment
Please, Sign In to add comment