Sfoglia il codice sorgente

Servicio para la api de geoserver

Maximiliano Schvindt 7 anni fa
parent
commit
1648592f41
2 ha cambiato i file con 327 aggiunte e 0 eliminazioni
  1. 4 0
      Resources/config/services.yml
  2. 323 0
      Services/GeoserverService.php

+ 4 - 0
Resources/config/services.yml

@@ -0,0 +1,4 @@
+services:
+    geoserver.api:
+       class: GeoserverBundle\Services\GeoserverService
+       arguments: ["@service_container"]

+ 323 - 0
Services/GeoserverService.php

@@ -0,0 +1,323 @@
+<?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:/home/maxi/Escritorio/aux/" "http://localhost:8080/geoserver/rest/workspaces/workspaceName/datastores/shapefiles/external.shp?configure=all"
+    public function updateShape($workspace, $shapeName = null)
+    {
+        $ch = curl_init();
+        $path = $this->pathShapes.DIRECTORY_SEPARATOR.$workspace.DIRECTORY_SEPARATOR;
+        $url = "{$this->getUrlRest()}/workspace/{$workspace}/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();
+
+        curl_setopt($ch, CURLOPT_URL, "http://{$this->host}:{$this->port}/geoserver/rest/workspaces/{$nameWorkspace}/datastores/shapefiles/featuretypes.json");
+        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 createUser($user) 
+    {
+        
+        $nameUser = "user{$user->getId()}";
+        $passUser = $user->getGeoPass();
+        $ch = curl_init();
+
+        $url = "http://{$this->host}:{$this->port}/geoserver/rest/security/usergroup/default/users";
+        
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+        curl_setopt($ch, CURLOPT_POSTFIELDS, "<user><userName>{$nameUser}</userName><password>{$passUser}</password><enabled>true</enabled></user>");
+        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);}
+        
+        $file = fopen(LOG_FILE,"a+");
+        fwrite($file,"CREATE USER: ".$url.PHP_EOL);
+        fwrite($file,"return - CREATE USER: ".$result.PHP_EOL);
+        fclose($file);
+
+        curl_close($ch);
+    } */
+
+    public function putShape($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) 
+    {
+
+        $nameWorkspace = "{$workspace}";
+        $ch = curl_init();
+
+        curl_setopt($ch, CURLOPT_URL, "http://{$this->host}:{$this->port}/geoserver/rest/workspaces/{$nameWorkspace}/datastores/shapefiles/featuretypes/{$shape}.json");
+        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 = "http://{$this->host}:{$this->port}/geoserver/{$workspace}/wms?".http_build_query($params);
+
+        $file = fopen(LOG_FILE,"a+");
+        fwrite($file,"GET FEATURE: ".$url.PHP_EOL);
+        fclose($file);
+
+        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 = "http://{$this->host}:{$this->port}/geoserver/{$workspace}/wms?".http_build_query($params);
+
+        $file = fopen(LOG_FILE,"a+");
+        fwrite($file,"GET IMAGE: ".$url.PHP_EOL);
+        fclose($file);
+
+        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);
+        
+        return $result;
+
+    }
+
+    */
+
+
+}