Sunday 10 February 2019

geoserver - Using Geotools or GeoserverManager to add a layer using just an URL


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



Answer



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//wmsstores


wms
http://somehost/wms?

sf


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 '
wmshttp://somehost/wms?
sf
'
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("");
postData.append("wms");
postData.append("" + urlToAdd + "");
postData.append("" + wsToAdd + "");
postData.append("
");

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 WMS into wmsStore-tag so in the datasources overview in geoserver the type of this datasource gets specified instead of default "?"



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...