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 = "https://{$this->host}/geoserver";
}
}
// curl -v -u admin:geoserver -XPOST -H "Content-type: text/xml" -d "workspaceName" 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}");
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);
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;
}
}