GeoserverService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace GeoserverBundle\Services;
  3. use Symfony\Component\DependencyInjection\ContainerInterface;
  4. class GeoserverService
  5. {
  6. /**
  7. * @var ContainerInterface
  8. */
  9. protected $serviceContainer;
  10. protected $user;
  11. protected $pass;
  12. protected $host;
  13. protected $port;
  14. protected $pathShapes;
  15. protected $url;
  16. /**
  17. * @param ContainerInterface $serviceContainer
  18. */
  19. public function __construct(ContainerInterface $serviceContainer)
  20. {
  21. $this->serviceContainer = $serviceContainer;
  22. if($this->serviceContainer->getParameter("geoserver_service")) {
  23. $this->user = $this->serviceContainer->getParameter("geoserver_user");
  24. $this->pass = $this->serviceContainer->getParameter("geoserver_pass");
  25. $this->host = $this->serviceContainer->getParameter("geoserver_host");
  26. $this->port = $this->serviceContainer->getParameter("geoserver_port");
  27. $this->pathShapes = $this->serviceContainer->getParameter("geoserver_path_shapes");
  28. $this->url = "http://{$this->host}:{$this->port}/geoserver";
  29. }
  30. }
  31. // curl -v -u admin:geoserver -XPOST -H "Content-type: text/xml" -d "<workspace><name>workspaceName</name></workspace>" http://127.0.0.1:8081/geoserver/rest/workspaces
  32. public function createWorkspace($workspace)
  33. {
  34. $ch = curl_init();
  35. $url = "{$this->getUrlRest()}/workspaces";
  36. curl_setopt($ch, CURLOPT_URL, $url);
  37. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  38. curl_setopt($ch, CURLOPT_POSTFIELDS, "<workspace><name>{$workspace}</name></workspace>");
  39. curl_setopt($ch, CURLOPT_POST, 1);
  40. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
  41. $headers = array();
  42. $headers[] = "Content-Type: text/xml";
  43. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  44. $result = curl_exec($ch);
  45. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  46. curl_close($ch);
  47. }
  48. // curl -v -u admin:geoserver -XPUT -H "Content-type: text/plain" -d "file:/home/maxi/Escritorio/aux/" "http://localhost:8080/geoserver/rest/workspaces/workspaceName/datastores/shapefiles/external.shp?configure=all"
  49. public function updateShape($workspace, $shapeName = null)
  50. {
  51. $ch = curl_init();
  52. $path = $this->pathShapes.DIRECTORY_SEPARATOR.$workspace.DIRECTORY_SEPARATOR;
  53. $url = "{$this->getUrlRest()}/workspace/{$workspace}/datastores/shapefiles/external.shp?configure=all";
  54. curl_setopt($ch, CURLOPT_URL, $url);
  55. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  56. curl_setopt($ch, CURLOPT_POSTFIELDS, "file:{$path}");
  57. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  58. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
  59. $headers = array();
  60. $headers[] = "Content-type: text/plain";
  61. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  62. $result = curl_exec($ch);
  63. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  64. curl_close ($ch);
  65. }
  66. /* public function getLayers($workspace)
  67. {
  68. $nameWorkspace = "{$workspace}";
  69. $ch = curl_init();
  70. curl_setopt($ch, CURLOPT_URL, "http://{$this->host}:{$this->port}/geoserver/rest/workspaces/{$nameWorkspace}/datastores/shapefiles/featuretypes.json");
  71. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  72. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  73. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}" . ":" . "{$this->pass}");
  74. $headers = array();
  75. $headers[] = "Accept: application/json";
  76. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  77. $result = curl_exec($ch);
  78. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  79. curl_close($ch);
  80. $data = json_decode($result,true);
  81. if(is_array($data))
  82. return $data;
  83. return array();
  84. } */
  85. // ELIMINAMOS EL SHAPE - curl -v -u admin:geoserver -XDELETE "http://localhost:8080/geoserver/rest/layers/workspaceName:shapeName.json"
  86. // ELIMINAMOS EL REGISTRO EN DATASTORE - curl -v -u admin:geoserver -XDELETE "http://localhost:8080/geoserver/rest/workspaces/workspaceName/datastores/shapefiles/featuretypes/shapeName.json"
  87. public function deleteShape($workspace, $shape)
  88. {
  89. $ch = curl_init();
  90. $urlLayer = "{$this->getUrlRest()}/layers/{$workspace}:{$shape}.json";
  91. $urlWorkspace = "{$this->getUrlRest()}/workspaces/{$workspace}/datastores/shapefiles/featuretypes/{$shape}.json";
  92. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  93. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
  94. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
  95. curl_setopt($ch, CURLOPT_URL, $urlLayer);
  96. $result = curl_exec($ch);
  97. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  98. curl_setopt($ch, CURLOPT_URL, $urlWorkspace);
  99. $result = curl_exec($ch);
  100. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  101. curl_close ($ch);
  102. }
  103. /* public function createUser($user)
  104. {
  105. $nameUser = "user{$user->getId()}";
  106. $passUser = $user->getGeoPass();
  107. $ch = curl_init();
  108. $url = "http://{$this->host}:{$this->port}/geoserver/rest/security/usergroup/default/users";
  109. curl_setopt($ch, CURLOPT_URL, $url);
  110. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  111. curl_setopt($ch, CURLOPT_POSTFIELDS, "<user><userName>{$nameUser}</userName><password>{$passUser}</password><enabled>true</enabled></user>");
  112. curl_setopt($ch, CURLOPT_POST, 1);
  113. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}" . ":" . "{$this->pass}");
  114. $headers = array();
  115. $headers[] = "Content-Type: text/xml";
  116. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  117. $result = curl_exec($ch);
  118. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  119. $file = fopen(LOG_FILE,"a+");
  120. fwrite($file,"CREATE USER: ".$url.PHP_EOL);
  121. fwrite($file,"return - CREATE USER: ".$result.PHP_EOL);
  122. fclose($file);
  123. curl_close($ch);
  124. } */
  125. public function putShape($rest, $data)
  126. {
  127. $ch = curl_init();
  128. $url = "{$this->getUrlRest()}/{$rest}";
  129. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  130. curl_setopt($ch, CURLOPT_URL, $url);
  131. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  132. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  133. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
  134. $headers = array();
  135. $headers[] = "Content-Type: text/xml";
  136. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  137. $result = curl_exec($ch);
  138. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  139. curl_close($ch);
  140. }
  141. public function getUrlRest()
  142. {
  143. $url = "{$this->url}/rest";
  144. return $url;
  145. }
  146. public function getUrlWms($workspace)
  147. {
  148. $url = "{$this->url}/{$workspace}/wms";
  149. return $url;
  150. }
  151. /*
  152. public function existResource($url)
  153. {
  154. $ch = curl_init();
  155. curl_setopt($ch, CURLOPT_URL, $url);
  156. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  157. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  158. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}" . ":" . "{$this->pass}");
  159. $headers = array();
  160. $headers[] = "Accept: application/json";
  161. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  162. $result = curl_exec($ch);
  163. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  164. curl_close($ch);
  165. $data = json_decode($result,true);
  166. if(is_array($data))
  167. return $data;
  168. return array();
  169. }
  170. public function getLayerData($workspace, $shape)
  171. {
  172. $nameWorkspace = "{$workspace}";
  173. $ch = curl_init();
  174. curl_setopt($ch, CURLOPT_URL, "http://{$this->host}:{$this->port}/geoserver/rest/workspaces/{$nameWorkspace}/datastores/shapefiles/featuretypes/{$shape}.json");
  175. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  176. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  177. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}" . ":" . "{$this->pass}");
  178. $headers = array();
  179. $headers[] = "Accept: application/json";
  180. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  181. $result = curl_exec($ch);
  182. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  183. curl_close($ch);
  184. $data = json_decode($result,true);
  185. if(is_array($data))
  186. return $data;
  187. return array();
  188. }
  189. public function getFeature($workspace, $params)
  190. {
  191. $ch = curl_init();
  192. $url = "http://{$this->host}:{$this->port}/geoserver/{$workspace}/wms?".http_build_query($params);
  193. $file = fopen(LOG_FILE,"a+");
  194. fwrite($file,"GET FEATURE: ".$url.PHP_EOL);
  195. fclose($file);
  196. curl_setopt($ch, CURLOPT_URL, $url);
  197. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  198. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  199. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}" . ":" . "{$this->pass}");
  200. $headers = array();
  201. $headers[] = "Accept: application/json";
  202. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  203. $result = curl_exec($ch);
  204. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  205. curl_close($ch);
  206. print_r($result);
  207. die;
  208. return $result;
  209. }
  210. public function getImage($workspace, $params)
  211. {
  212. $ch = curl_init();
  213. $url = "http://{$this->host}:{$this->port}/geoserver/{$workspace}/wms?".http_build_query($params);
  214. $file = fopen(LOG_FILE,"a+");
  215. fwrite($file,"GET IMAGE: ".$url.PHP_EOL);
  216. fclose($file);
  217. curl_setopt($ch, CURLOPT_URL, $url);
  218. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  219. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  220. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}" . ":" . "{$this->pass}");
  221. $headers = array();
  222. //$headers[] = "Accept: application/json";
  223. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  224. $result = curl_exec($ch);
  225. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  226. curl_close($ch);
  227. return $result;
  228. }
  229. */
  230. }