GeoserverService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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:/dir_con_shapes/" "http://localhost:8080/geoserver/rest/workspaces/workspaceName/datastores/shapefiles/external.shp?configure=all"
  49. public function updateShape($workspace)
  50. {
  51. $ch = curl_init();
  52. $path = $this->pathShapes.DIRECTORY_SEPARATOR.$workspace.DIRECTORY_SEPARATOR;
  53. $url = "{$this->getUrlRest()}/workspaces/{$workspace}/datastores/shapefiles/external.shp?configure=all";
  54. // http://200.50.168.118:8081/geoserver/rest/workspaces/deviceServer_1/datastores/shapefiles/external.shp?configure=all
  55. curl_setopt($ch, CURLOPT_URL, $url);
  56. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  57. curl_setopt($ch, CURLOPT_POSTFIELDS, "file:{$path}");
  58. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  59. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
  60. $headers = array();
  61. $headers[] = "Content-type: text/plain";
  62. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  63. $result = curl_exec($ch);
  64. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  65. curl_close ($ch);
  66. }
  67. /* public function getLayers($workspace)
  68. {
  69. $nameWorkspace = "{$workspace}";
  70. $ch = curl_init();
  71. curl_setopt($ch, CURLOPT_URL, "http://{$this->host}:{$this->port}/geoserver/rest/workspaces/{$nameWorkspace}/datastores/shapefiles/featuretypes.json");
  72. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  73. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  74. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}" . ":" . "{$this->pass}");
  75. $headers = array();
  76. $headers[] = "Accept: application/json";
  77. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  78. $result = curl_exec($ch);
  79. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  80. curl_close($ch);
  81. $data = json_decode($result,true);
  82. if(is_array($data))
  83. return $data;
  84. return array();
  85. } */
  86. // ELIMINAMOS EL SHAPE - curl -v -u admin:geoserver -XDELETE "http://localhost:8080/geoserver/rest/layers/workspaceName:shapeName.json"
  87. // ELIMINAMOS EL REGISTRO EN DATASTORE - curl -v -u admin:geoserver -XDELETE "http://localhost:8080/geoserver/rest/workspaces/workspaceName/datastores/shapefiles/featuretypes/shapeName.json"
  88. public function deleteShape($workspace, $shape)
  89. {
  90. $ch = curl_init();
  91. $urlLayer = "{$this->getUrlRest()}/layers/{$workspace}:{$shape}.json";
  92. $urlWorkspace = "{$this->getUrlRest()}/workspaces/{$workspace}/datastores/shapefiles/featuretypes/{$shape}.json";
  93. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  94. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
  95. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
  96. curl_setopt($ch, CURLOPT_URL, $urlLayer);
  97. $result = curl_exec($ch);
  98. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  99. curl_setopt($ch, CURLOPT_URL, $urlWorkspace);
  100. $result = curl_exec($ch);
  101. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  102. curl_close ($ch);
  103. }
  104. /* public function createUser($user)
  105. {
  106. $nameUser = "user{$user->getId()}";
  107. $passUser = $user->getGeoPass();
  108. $ch = curl_init();
  109. $url = "http://{$this->host}:{$this->port}/geoserver/rest/security/usergroup/default/users";
  110. curl_setopt($ch, CURLOPT_URL, $url);
  111. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  112. curl_setopt($ch, CURLOPT_POSTFIELDS, "<user><userName>{$nameUser}</userName><password>{$passUser}</password><enabled>true</enabled></user>");
  113. curl_setopt($ch, CURLOPT_POST, 1);
  114. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}" . ":" . "{$this->pass}");
  115. $headers = array();
  116. $headers[] = "Content-Type: text/xml";
  117. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  118. $result = curl_exec($ch);
  119. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  120. $file = fopen(LOG_FILE,"a+");
  121. fwrite($file,"CREATE USER: ".$url.PHP_EOL);
  122. fwrite($file,"return - CREATE USER: ".$result.PHP_EOL);
  123. fclose($file);
  124. curl_close($ch);
  125. } */
  126. public function putShape($rest, $data)
  127. {
  128. $ch = curl_init();
  129. $url = "{$this->getUrlRest()}/{$rest}";
  130. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  131. curl_setopt($ch, CURLOPT_URL, $url);
  132. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  133. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  134. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
  135. $headers = array();
  136. $headers[] = "Content-Type: text/xml";
  137. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  138. $result = curl_exec($ch);
  139. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  140. curl_close($ch);
  141. }
  142. public function getUrlRest()
  143. {
  144. $url = "{$this->url}/rest";
  145. return $url;
  146. }
  147. public function getUrlWms($workspace)
  148. {
  149. $url = "{$this->url}/{$workspace}/wms";
  150. return $url;
  151. }
  152. /*
  153. public function existResource($url)
  154. {
  155. $ch = curl_init();
  156. curl_setopt($ch, CURLOPT_URL, $url);
  157. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  158. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  159. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}" . ":" . "{$this->pass}");
  160. $headers = array();
  161. $headers[] = "Accept: application/json";
  162. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  163. $result = curl_exec($ch);
  164. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  165. curl_close($ch);
  166. $data = json_decode($result,true);
  167. if(is_array($data))
  168. return $data;
  169. return array();
  170. }
  171. public function getLayerData($workspace, $shape)
  172. {
  173. $nameWorkspace = "{$workspace}";
  174. $ch = curl_init();
  175. curl_setopt($ch, CURLOPT_URL, "http://{$this->host}:{$this->port}/geoserver/rest/workspaces/{$nameWorkspace}/datastores/shapefiles/featuretypes/{$shape}.json");
  176. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  177. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  178. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}" . ":" . "{$this->pass}");
  179. $headers = array();
  180. $headers[] = "Accept: application/json";
  181. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  182. $result = curl_exec($ch);
  183. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  184. curl_close($ch);
  185. $data = json_decode($result,true);
  186. if(is_array($data))
  187. return $data;
  188. return array();
  189. }
  190. public function getFeature($workspace, $params)
  191. {
  192. $ch = curl_init();
  193. $url = "http://{$this->host}:{$this->port}/geoserver/{$workspace}/wms?".http_build_query($params);
  194. $file = fopen(LOG_FILE,"a+");
  195. fwrite($file,"GET FEATURE: ".$url.PHP_EOL);
  196. fclose($file);
  197. curl_setopt($ch, CURLOPT_URL, $url);
  198. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  199. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  200. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}" . ":" . "{$this->pass}");
  201. $headers = array();
  202. $headers[] = "Accept: application/json";
  203. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  204. $result = curl_exec($ch);
  205. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  206. curl_close($ch);
  207. print_r($result);
  208. die;
  209. return $result;
  210. }
  211. public function getImage($workspace, $params)
  212. {
  213. $ch = curl_init();
  214. $url = "http://{$this->host}:{$this->port}/geoserver/{$workspace}/wms?".http_build_query($params);
  215. $file = fopen(LOG_FILE,"a+");
  216. fwrite($file,"GET IMAGE: ".$url.PHP_EOL);
  217. fclose($file);
  218. curl_setopt($ch, CURLOPT_URL, $url);
  219. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  220. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  221. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}" . ":" . "{$this->pass}");
  222. $headers = array();
  223. //$headers[] = "Accept: application/json";
  224. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  225. $result = curl_exec($ch);
  226. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  227. curl_close($ch);
  228. return $result;
  229. }
  230. */
  231. }