0

Lets say I have just an URL to a Layer in OpenLayers format:

http://www.geoservicos.ibge.gov.br/geoserver/CREN/wms?service=WMS&version=1.1.0&request=GetMap&layers=HidrogeologiaRegiaoNE

Is it possible to create a Layer in my Geoserver pointing to this external layer?

1) Using my Geoserver I've tried to use Web Feature Server (NG) but I don't want to list all layers before add it (GetCapabilities).

2) I need to do it using geotools. Again, GetCapabilities will be time spending because I already know the layer I want to add.

I can use Geotools or Geoserver Manager (REST API). I've searched everywhere for examples to add (publish) a WMS layer from a external geoserver layer but found nothing.

In other words, how can I do this using GeoserverManager API?
http://geoserver.geo-solutions.it/edu/en/adding_data/add_wfscascade.html

1

To access a remote WMS, it is necessary to load it as a store in GeoServer. GeoServer must be able to access the capabilities document of the remote WMS for the store to be successfully loaded. You can't just proxy a layer without creating a store.

The easiest way to get the REST file to upload (PUT) is to create the store once by hand (using the normal UI), then get that file (from the GeoServer data directory), modify it as needed, then upload. Creating a layer is a bit easier, but the same process may still be useful.

  • I can't do it. My application will allow the user to add arbitrary layers from other places. I'm already doing with GEOTIFF and SHP (easy as the GeoserverManager Publisher class offers methods to do it). Having a link like my example, I'm currently storing it in my database and send it right to the OpenLayers. But I need to prepare the system to work even when the user have no acces to the internet, just to its own Geoserver. My idea is to import that layer to the Geoserver and it will become a proxy. – Magno C Jul 5 '16 at 17:23
  • By the way, I'm able to create stores by using GeoserverManager REST API, but I don't know HOW to do it (what kind of store and how to bind a layer URL to it). – Magno C Jul 5 '16 at 17:25
  • So you want to copy / cache the data, instead of just proxying it? – BradHards Jul 5 '16 at 20:55
  • No. Sometimes my client may have no access to internet. Just to its Geoserver. This Geoserver always have internet access. The users may catch some layers around ( GeoTIFF, SHP, or a URL in the WMS format <serverURL> <getMap> <layerName> <blah> <foo> ). This URL I'm currently storing in my database to pass directly to OpenLayers. All fine. Now, I must look ahead if the user have no access to internet, so OpenLayers will nod be able to open the external layer by URL. I want to store the layer reference in my geoserver as if it were its own layer. – Magno C Jul 6 '16 at 15:30
  • ...cont : so I just change the <serverURL> part of the URL from external to internal Geoserver. See github.com/icemagno/geoexplorer/blob/master/src/main/webapp/js/… – Magno C Jul 6 '16 at 15:31
0

Found it!

Following this post I've discovered the data to send to the REST API: Adding an external WMS service using REST service with Geoserver

POST /rest/workspaces/<ws>/wmsstores

<wmsStore>
   <name>wms</name>
   <capabilitiesURL>http://somehost/wms?</capabilitiesURL>
   <workspace>sf</workspace>
</wmsStore>

Now, I need to know HOW. Found this: http://boundlessgeo.com/2012/10/adding-layers-to-geoserver-using-the-rest-api/ with a lot of CURL examples to talk to the Geoserver REST API. None of them is what I need.

Now I just put the two parts together: The correct data with correct protocol, something like:

    curl -u admin:geoserver -v -XPOST -H 'Content-type:text/xml' -d '<wmsStore>
       <name>wms</name><capabilitiesURL>http://somehost/wms?</capabilitiesURL>
<workspace>sf</workspace></wmsStore>' 
http://mygeoserver/rest/workspaces/target_workspace/wmsstores

But I need it in Java...

The caller:

    try {

        String workspace = "test";
        String myServer = "http://10.5.115.22/geoserver/rest/workspaces/"+workspace+"/wmsstores";

        String urlToAdd = "http://www.geoservicos.ibge.gov.br/geoserver/wms?";
        String wsToAdd = "CREN";

        StringBuilder postData = new StringBuilder();
        postData.append("<wmsStore>");
        postData.append("<name>wms</name>");
        postData.append("<capabilitiesURL>" + urlToAdd + "</capabilitiesURL>");
        postData.append("<workspace>" + wsToAdd + "</workspace>");
        postData.append("</wmsStore>");

        WebClient wc = new WebClient();
        wc.doPostStream(myServer, postData.toString() );


    } catch ( Exception e ) {
        e.printStackTrace();
    } 

... and the connection (CURL Java like):

public void doPostStream( String url, String content ) throws Exception {
    String encodedAuth = new String( Base64.encodeBase64( "admin:geoserver".getBytes() ) );
    HttpURLConnection con = (HttpURLConnection) new URL( url ).openConnection();
    con.setRequestMethod("POST");
    con.setRequestProperty("Authorization", "Basic " + encodedAuth );
    con.setRequestProperty("User-Agent", USER_AGENT);
    con.setRequestProperty("Content-type", "text/xml");

    con.setDoOutput(true);
    con.getOutputStream().write( content.getBytes("UTF-8") );

    InputStream inputStream = con.getInputStream();
    StringWriter writer = new StringWriter();
    IOUtils.copy(inputStream, writer, "UTF-8");
    String theString = writer.toString();
    System.out.println( theString );    

    con.disconnect();
    inputStream.close();

}

Now, my WMS Store was created! But instead the type of the store I have an interrogation:

enter image description here

I think it is because the URL http://www.geoservicos.ibge.gov.br/geoserver/wms? is not in proper format to this kind of store.

Investigating...

EDIT: The answer is in the same post, @tareq comment:

You can add <type>WMS</type> into wmsStore-tag so in the datasources overview in geoserver the type of this datasource gets specified instead of default "?"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.