需要批量导入osm样式文件,原始的为.sh格式的bash代码,由于只有在本机windows上搭建的geoserver,因此需要改写为bat批处理文件。

主要涉及到:

  1. 变量的定义使用SET
  2. for循环语句的语法
  3. 使用%~ni(%%~ni)来代替原本的basename命令
  4. 多行连接符使用^
  5. 变量的引用使用%various%

改写后代码如下:

: 改编自 SLD_create.sh
@echo off
:  script to add layer/style information
:  for every SLD file in our collection
SET restapi=http://localhost:8080/geoserver/rest
SET login=admin:geoserver
SET workspace=osm
SET store=openstreetmap

for %%i in (*.sld) do (
: create a new featuretype in the store, assuming the table
: already exists in the database and is named $layername
: this step automatically creates a layer of the same name
: as a side effect
: 末尾的^为多行连接符,下一行开头不可有空格或缩进
curl -v -u %login% -XPOST -H "Content-type: text/xml" ^
-d "<featureType><name>%%~ni</name></featureType>" ^
%restapi%/workspaces/%workspace%/datastores/%store%/featuretypes?recalculate=nativebbox,latlonbbox

: create an empty style object in the workspace, using the same name
curl -v -u %login% -XPOST -H "Content-type: text/xml" ^
-d "<style><name>%%~ni</name><filename>%%i</filename></style>" ^
%restapi%/workspaces/%workspace%/styles

: upload the SLD definition to the style
curl -v -u %login% -XPUT -H "Content-type: application/vnd.ogc.sld+xml" ^
-d @%%i ^
%restapi%/workspaces/%workspace%/styles/%%~ni

: associate the style with the layer as the default style
curl -v -u %login% -XPUT -H "Content-type: text/xml" ^
-d "<layer><enabled>true</enabled><defaultStyle><name>%%~ni</name><workspace>%workspace%</workspace></defaultStyle></layer>" ^
%restapi%/workspaces/%workspace%:%%~ni

)

curl -v -u %login% -XPOST -d@layergroup.xml -H "Content-type: text/xml" ^
%restapi%/workspaces/%workspace%/layergroups
pause

参考:

  1. curl for Windows
  2. curl for windows安装
  3. Windows bat脚本——for循环用法详解
  4. windows下的批处理bat文件和Linux下的shell文件的互相转换
  5. 学习笔记之 curl 命令用法详解

附:原始代码如下

#  script to add layer/style information
#  for every SLD file in our collection
#
restapi=http://localhost:8080/geoserver/rest
login=admin:geoserver
workspace=osm
store=openstreetmap

for sldfile in *.sld; do

  # strip the extension from the filename to use for layer/style names
  layername=`basename $sldfile .sld`

  # create a new featuretype in the store, assuming the table
  # already exists in the database and is named $layername
  # this step automatically creates a layer of the same name
  # as a side effect
  curl -v -u $login -XPOST -H "Content-type: text/xml" \
    -d "<featureType><name>$layername</name></featureType>" \
    $restapi/workspaces/$workspace/datastores/$store/featuretypes?recalculate=nativebbox,latlonbbox

  # create an empty style object in the workspace, using the same name
  curl -v -u $login -XPOST -H "Content-type: text/xml" \
    -d "<style><name>$layername</name><filename>$sldfile</filename></style>" \
    $restapi/workspaces/$workspace/styles

  # upload the SLD definition to the style
  curl -v -u $login -XPUT -H "Content-type: application/vnd.ogc.sld+xml" \
    -d @$sldfile \
    $restapi/workspaces/$workspace/styles/$layername

  # associate the style with the layer as the default style
  curl -v -u $login -XPUT -H "Content-type: text/xml" \
    -d "<layer><enabled>true</enabled><defaultStyle><name>$layername</name><workspace>$workspace</workspace></defaultStyle></layer>" \
    $restapi/layers/$workspace:$layername

done


curl -v -u admin:@pp\$4boundleSS -XPOST -d@layergroup.xml -H "Content-type: text/xml" \
  http://apps.opengeo.org/geoserver/rest/workspaces/osm/layergroups