123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- <?php
- namespace GeoserverBundle\Services;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- class GeoserverService
- {
- /**
- * @var ContainerInterface
- */
- protected $serviceContainer;
- protected $user;
- protected $pass;
- protected $host;
- protected $port;
- protected $pathShapes;
- protected $url;
- /**
- * @param ContainerInterface $serviceContainer
- */
- public function __construct(ContainerInterface $serviceContainer)
- {
- $this->serviceContainer = $serviceContainer;
- if($this->serviceContainer->getParameter("geoserver_service")) {
- $this->user = $this->serviceContainer->getParameter("geoserver_user");
- $this->pass = $this->serviceContainer->getParameter("geoserver_pass");
- $this->host = $this->serviceContainer->getParameter("geoserver_host");
- $this->port = $this->serviceContainer->getParameter("geoserver_port");
-
- $this->pathShapes = $this->serviceContainer->getParameter("geoserver_path_shapes");
- $this->url = "http://{$this->host}:{$this->port}/geoserver";
- }
- }
- // 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
- public function createWorkspace($workspace)
- {
- $ch = curl_init();
- $url = "{$this->getUrlRest()}/workspaces";
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, "<workspace><name>{$workspace}</name></workspace>");
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
- $headers = array();
- $headers[] = "Content-Type: text/xml";
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- $result = curl_exec($ch);
- if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
- curl_close($ch);
- }
- // 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"
- public function updateShape($workspace)
- {
- $ch = curl_init();
- $path = $this->pathShapes.DIRECTORY_SEPARATOR.$workspace.DIRECTORY_SEPARATOR;
- $url = "{$this->getUrlRest()}/workspaces/{$workspace}/datastores/shapefiles/external.shp?configure=all";
- // http://200.50.168.118:8081/geoserver/rest/workspaces/deviceServer_1/datastores/shapefiles/external.shp?configure=all
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, "file:{$path}");
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
- curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
- $headers = array();
- $headers[] = "Content-type: text/plain";
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- $result = curl_exec($ch);
- if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
- curl_close ($ch);
- }
- public function getLayers($workspace)
- {
- $nameWorkspace = "{$workspace}";
- $ch = curl_init();
- $url = "{$this->getUrlRest()}/workspaces/{$workspace}/datastores/shapefiles/featuretypes.json";
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
- curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
- $headers = array();
- $headers[] = "Accept: application/json";
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- $result = curl_exec($ch);
- if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
- curl_close($ch);
-
- $data = json_decode($result,true);
- if(is_array($data))
- return $data;
- return array();
- }
- // ELIMINAMOS EL SHAPE - curl -v -u admin:geoserver -XDELETE "http://localhost:8080/geoserver/rest/layers/workspaceName:shapeName.json"
- // ELIMINAMOS EL REGISTRO EN DATASTORE - curl -v -u admin:geoserver -XDELETE "http://localhost:8080/geoserver/rest/workspaces/workspaceName/datastores/shapefiles/featuretypes/shapeName.json"
- public function deleteShape($workspace, $shape)
- {
- $ch = curl_init();
- $urlLayer = "{$this->getUrlRest()}/layers/{$workspace}:{$shape}.json";
- $urlWorkspace = "{$this->getUrlRest()}/workspaces/{$workspace}/datastores/shapefiles/featuretypes/{$shape}.json";
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
- curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
- curl_setopt($ch, CURLOPT_URL, $urlLayer);
- $result = curl_exec($ch);
- if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
-
- curl_setopt($ch, CURLOPT_URL, $urlWorkspace);
- $result = curl_exec($ch);
- if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
- curl_close ($ch);
- }
- public function putData($rest, $data)
- {
- $ch = curl_init();
- $url = "{$this->getUrlRest()}/{$rest}";
-
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
- $headers = array();
- $headers[] = "Content-Type: text/xml";
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- $result = curl_exec($ch);
- if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
-
- curl_close($ch);
- }
- public function getUrlRest()
- {
- $url = "{$this->url}/rest";
-
- return $url;
- }
-
- public function getUrlWms($workspace)
- {
- $url = "{$this->url}/{$workspace}/wms";
-
- return $url;
- }
-
- public function existResource($url)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
- curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
- $headers = array();
- $headers[] = "Accept: application/json";
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- $result = curl_exec($ch);
- if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
- curl_close($ch);
-
- $data = json_decode($result,true);
- if(is_array($data))
- return $data;
- return array();
- }
- public function getLayerData($workspace, $shape)
- {
- $url = "{$this->getUrlRest()}/workspaces/{$workspace}/datastores/shapefiles/featuretypes/{$shape}.json";
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
- curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
- $headers = array();
- $headers[] = "Accept: application/json";
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- $result = curl_exec($ch);
- if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
- curl_close($ch);
-
- $data = json_decode($result,true);
- if(is_array($data))
- return $data;
- return array();
- }
- public function getFeature($workspace, $params)
- {
- $ch = curl_init();
- $url = "{$this->getUrlWms($workspace)}?".http_build_query($params);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
- curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
- $headers = array();
- $headers[] = "Accept: application/json";
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- $result = curl_exec($ch);
- if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
- curl_close($ch);
-
- //print_r($result);
- //die;
- return $result;
-
- }
- public function getImage($workspace, $params)
- {
- $ch = curl_init();
- $url = "{$this->getUrlWms($workspace)}?".http_build_query($params);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
- curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
- $headers = array();
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- $result = curl_exec($ch);
- if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
- curl_close($ch);
-
- return $result;
- }
- }
|