In one of our current projects, there was a requirement to publish over 300 different layers from a database in GeoServer and to customize their names individually. The first thought of a developer with such a task is usually: “How can I solve my problem as automated and easily reproducible as possible?”. Because: Manually publishing and configuring about 300 layers in the GeoServer GUI is very time-consuming with this amount! Fortunately, GeoServer offers a REST interface, which we already use elsewhere in our SHOGun framework. With the interface, many operations that are performed via the user interface can also be automated. An overview of the REST interface is provided by the very good documentation of GeoServer, which also includes some practical examples and calls for the REST interface. Quickly the appropriate code was found to publish a layer of a specific datastore:
curl -v " -u user:password " -XPOST " -H "Content-type: text/xml" " -d "name_of_the_table" " http://myserver.com/geoserver/rest/workspaces/myws/datastores/myds/featuretypes
For the sake of simplicity, the command line tool curl is used in these examples, but the calls can easily be made in almost any programming language via an HTTP Request, or via the Java-based GeoServer Manager. With this call, a database table containing a geometry column can be published as a layer. The somewhat difficult part follows as soon as you want to edit the now published layer, especially the name. In our case, we did not want to use the name of the table as the layer name, but initially found no indication in the documentation how GeoServer distinguishes between the name for pure display and the name that is used as a reference to the database table. Examples or hints could not be found in the documentation. After a search in the source code of GeoServer via GitHub, two crucial hints were found, namely the methods getName() and getNativeName(). This logically resulted in the following line of code:
curl -v " -u user:password " -XPUT " -H "Content-type: text/xml" " -d "beautiful_displayname_of_the_table" " http://myserver.com/geoserver/rest/workspaces/myws/datastores/myds/featuretypes/name_of_the_table
The whole thing packaged in a loop in the code could then be used to publish and rename hundreds of layers at the “push of a button.”