首页 文章

在php中使用curl更改图层的默认样式的问题

提问于
浏览
0

我试图将栅格图层的默认样式从栅格更改为我定义的样式 .

我创建了一个sld文件并将其放在geoserver的data目录的styles文件夹中 .

接下来,我使用geoserver(http://docs.geoserver.org/latest/en/user/rest/examples/curl.html)文档中给出的以下命令创建了一个XML文件 .

curl -u admin:geoserver -XPOST -H 'Content-type: text/xml' -d '<style><name>aspect_style</name><filename>aspect.sld</filename></style>' http://localhost:8080/geoserver/rest/styles

然后我上传了文件,并使用以下命令将样式应用于图层 .

curl -u admin:geoserver -XPUT -H 'Content-type: application/vnd.ogc.sld+xml' -d @aspect.sld http://localhost:8080/geoserver/rest/styles/aspect_raster_style

curl -u admin:geoserver -XPUT -H 'Content-type: text/xml' -d '<layer><defaultStyle><name>aspect_raster_style</name></defaultStyle><enabled>true</enabled></layer>' http://localhost:8080/geoserver/rest/layers/dem:vinnu_aspect_raster

它通过CLI工作,当我在地理服务器中看到它时,样式得到了更新 . 当我用php做同样的事情时,我无法做到这一点 . 我得到一个错误,因为“输入源不包含数据” .

能够使用curl使用php创建工作区,coveragestore,图层 . 但我无法改变图层的样式 .

我的PHP代码如下 .

$file="F:/Vineendra/Images/abcd_aspect_qgis.tif";
$coverage_name="rast";
$workspace="medford";
// Open log file
$logfh = fopen("GeoserverPHP.log", 'w') or die("can't open log file");

// Initiate cURL session
$service = "http://localhost:8080/geoserver/"; 
$request = "rest/layers/".$workspace.":".$coverage_name."_raster"; // to add a new workspace
$url = $service . $request;
$ch = curl_init($url);

// Optional settings for debugging
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //option to return string
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $logfh); // logs curl messages

//Required POST request settings
curl_setopt($ch, CURLOPT_PUT, True);
$passwordStr = "admin:geoserver"; // replace with your username:password
curl_setopt($ch, CURLOPT_USERPWD, $passwordStr);

//POST data
curl_setopt($ch, CURLOPT_HTTPHEADER,
          array("Content-type:text/xml"));
$xmlStr = "<layer><defaultStyle><name>aspect_raster_style</name></defaultStyle></layer>";
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlStr);

//POST return code
$successCode = 201;

$buffer = curl_exec($ch); // Execute the curl request

// Check for errors and process results
$info = curl_getinfo($ch);
if ($info['http_code'] != $successCode) {
  $msgStr = "# Unsuccessful cURL request to ";
  $msgStr .= $url." [". $info['http_code']. "]\n";
  fwrite($logfh, $msgStr);
} else {
  $msgStr = "# Successful cURL request to ".$url."\n";
  fwrite($logfh, $msgStr);
}
fwrite($logfh, $buffer."\n");

curl_close($ CH);

由于我已经创建并上传了样式,我直接尝试使用php更改图层的默认样式 .

2 回答

  • 0

    不确定这篇文章是否仍然有效,但如果有人找到答案的地方(就像我做的那样),请确保你在HttpHeader中通知$ xmlString长度:

    curl_setopt($ch, CURLOPT_HTTPHEADER,
     array(
     "Content-type: application/xml",
     "Content-Length: ".strlen($xmlStr)
     )
    );
    

    在我的情况下,它解决了这个问题 .

  • 0

    不要使用 curl_setopt($ch, CURLOPT_PUT, True);

    使用 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");

相关问题