GeoserverService.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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}/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. $url = "{$this->getUrlRest()}/workspaces/{$workspace}/datastores/shapefiles/featuretypes.json";
  72. curl_setopt($ch, CURLOPT_URL, $url);
  73. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  74. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  75. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
  76. $headers = array();
  77. $headers[] = "Accept: application/json";
  78. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  79. $result = curl_exec($ch);
  80. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  81. curl_close($ch);
  82. $data = json_decode($result,true);
  83. if(is_array($data))
  84. return $data;
  85. return array();
  86. }
  87. // ELIMINAMOS EL SHAPE - curl -v -u admin:geoserver -XDELETE "http://localhost:8080/geoserver/rest/layers/workspaceName:shapeName.json"
  88. // ELIMINAMOS EL REGISTRO EN DATASTORE - curl -v -u admin:geoserver -XDELETE "http://localhost:8080/geoserver/rest/workspaces/workspaceName/datastores/shapefiles/featuretypes/shapeName.json"
  89. public function deleteShape($workspace, $shape)
  90. {
  91. $ch = curl_init();
  92. $urlLayer = "{$this->getUrlRest()}/layers/{$workspace}:{$shape}.json";
  93. $urlWorkspace = "{$this->getUrlRest()}/workspaces/{$workspace}/datastores/shapefiles/featuretypes/{$shape}.json";
  94. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  95. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
  96. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
  97. curl_setopt($ch, CURLOPT_URL, $urlLayer);
  98. $result = curl_exec($ch);
  99. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  100. curl_setopt($ch, CURLOPT_URL, $urlWorkspace);
  101. $result = curl_exec($ch);
  102. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  103. curl_close ($ch);
  104. }
  105. public function putData($rest, $data)
  106. {
  107. $ch = curl_init();
  108. $url = "{$this->getUrlRest()}/{$rest}";
  109. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  110. curl_setopt($ch, CURLOPT_URL, $url);
  111. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  112. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  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. curl_close($ch);
  120. }
  121. public function getUrlRest()
  122. {
  123. $url = "{$this->url}/rest";
  124. return $url;
  125. }
  126. public function getUrlWms($workspace)
  127. {
  128. $url = "{$this->url}/{$workspace}/wms";
  129. return $url;
  130. }
  131. public function existResource($url)
  132. {
  133. $ch = curl_init();
  134. curl_setopt($ch, CURLOPT_URL, $url);
  135. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  136. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  137. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
  138. $headers = array();
  139. $headers[] = "Accept: application/json";
  140. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  141. $result = curl_exec($ch);
  142. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  143. curl_close($ch);
  144. $data = json_decode($result,true);
  145. if(is_array($data))
  146. return $data;
  147. return array();
  148. }
  149. public function getLayerData($workspace, $shape)
  150. {
  151. $url = "{$this->getUrlRest()}/workspaces/{$workspace}/datastores/shapefiles/featuretypes/{$shape}.json";
  152. $ch = curl_init();
  153. curl_setopt($ch, CURLOPT_URL, $url);
  154. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  155. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  156. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
  157. $headers = array();
  158. $headers[] = "Accept: application/json";
  159. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  160. $result = curl_exec($ch);
  161. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  162. curl_close($ch);
  163. $data = json_decode($result,true);
  164. if(is_array($data))
  165. return $data;
  166. return array();
  167. }
  168. public function getFeature($workspace, $params)
  169. {
  170. $ch = curl_init();
  171. $url = "{$this->getUrlWms($workspace)}?".http_build_query($params);
  172. curl_setopt($ch, CURLOPT_URL, $url);
  173. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  174. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  175. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
  176. $headers = array();
  177. $headers[] = "Accept: application/json";
  178. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  179. $result = curl_exec($ch);
  180. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  181. curl_close($ch);
  182. return $result;
  183. }
  184. public function getImage($workspace, $params)
  185. {
  186. $ch = curl_init();
  187. $url = "{$this->getUrlWms($workspace)}?".http_build_query($params);
  188. curl_setopt($ch, CURLOPT_URL, $url);
  189. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  190. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  191. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
  192. $headers = array();
  193. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  194. $result = curl_exec($ch);
  195. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  196. curl_close($ch);
  197. return $result;
  198. }
  199. }