/* Package GeoTools/OGC Implementation
* Copyright (C) 2001 Cameron Shorter (camerons@users.sourceforge.net)
* Artur Hefczyc (kobit@users.sourceforge.net)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id: RemoteOGC.java,v 1.18 2001/12/21 11:46:47 ianturton Exp $
* $Author: ianturton $
* $Date: 2001/12/21 11:46:47 $
*/
package uk.ac.leeds.ccg.ogc;
import java.util.*;
import java.io.*;
import java.net.*;
/**
* Class RemoteOGC
provides an interface for the standard
* protocols defined by the
* Open GIS Consortium, eg a Web Map Server
* (WMS). It is an bridge between a remote OGC Services
* and GeoTools
client. Implemented methods of this
* class convert OGC Services data representation to
* GeoTools
client data representation.
*
* For application which use GeoTools
and this ogc package
* RemoteOGC
acts as bridge between GeoTools
* map data reprezentation and all Web Map Server specific logic.
* So all cals for Web Map Server data should be performed by
* this class.
* You can think about it as a special kind of proxy class which during
* realisation of user calls creates necessary objects, doing calculatiion
* downloading and processing proper data becouse it knows how, when and
* what to do to return data to user.
*
RemoteOGC
instance. This constructor will
* trigger a GETCAPABILITIES to determine the remainder of its parameters.
*
* @param server an URL
value
* @param proxy an URL
value
*/
public RemoteOGC(URL server, URL proxy)
{
this.server = server;
this.proxy = proxy;
// Trigger GETCAPABILITIES to get SRS and version.
capabilities=null;
}
/**
* Creates a new RemoteOGC
instance. This constructor will
* trigger a GETCAPABILITIES to determine the remainder of its parameters.
*
* @param server an URL
value
* @param proxy an URL
value
*/
public RemoteOGC(URL server)
{
this.server = server;
this.proxy = null;
// Trigger GETCAPABILITIES to get SRS and version.
capabilities=null;
}
/**
* Creates a new RemoteOGC
instance. This constructor will
* does not require a GETCAPABILITIES call to obtain remaining parameters.
*
* @param server an URL
value
* @param proxy an URL
value
* @param SRS the Spacial Reference System. See OGC Spec for description.
* @param Version the version of the OGC spec being used. See OGC Spec for
* description.
*/
public RemoteOGC(URL server, URL proxy, String SRS, String version)
{
this.server = server;
this.proxy = proxy;
this.SRS = SRS;
this.version = version;
}
/**
* Creates a new RemoteOGC
instance. This constructor will
* does not require a GETCAPABILITIES call to obtain remaining parameters.
*
* @param server an URL
value
* @param proxy an URL
value
* @param SRS the Spacial Reference System. See OGC Spec for description.
* @param Version the version of the OGC spec being used. See OGC Spec for
* description.
*/
public RemoteOGC(URL server, String SRS, String version)
{
this.server = server;
this.proxy = null;
this.SRS = SRS;
this.version = version;
}
protected void initConnection()
{
if(proxy!=null)
remoteConnection = new RemoteConnection(server.toString(), proxy.toString());
else
remoteConnection = new RemoteConnection(server.toString(), null);
}
/**
* getURL
method builds a URL based on the proxy, the OGC
* service URL, and the parameters for the service. The URL will look
* something like
* http://proxy.org?service=http://service?request=getcapabilities
*
* @return a URL
of proxy service with the OGC service and
* params as a parameter.
* @param params an array of (name1,value1,name2,value2,...) params to build
* into a URL.
*/
public URL getURL(String[] params)
{
URL newURL;
// Build server URL
StringBuffer serverURL =
new StringBuffer(server.toString()+"?");
if(this.SRS!=null) serverURL.append("SRS="+this.SRS);
else serverURL.append("SRS=EPSG:4326");
serverURL.append("&WMTVER="+this.version);
for (int i=0;igetAvailableFormats
method returns ArrayList
* with String
representation of formats supported by remote
* Web Map Server
*
* @return an ArrayList
value
* @exception IOException if an error occurs
*/
public ArrayList getAvailableFormats(String service_type)
throws IOException {
if(capabilities == null)
getCapabilities();
if(capabilities == null) return null;
return capabilities.getAvailableFormats(service_type);
}
/* ********* WebMapService
proxy methods END *********** */
/**
* getCapabilities
method is first method which
* should be called after object creating. This is done automatically
* during first call for data from remote WMS.Thread
. This is "future feature".
* And on every step should be called function from objects similiar
* to JobProgresHandler
.OGCCapabilities
value
* @exception IOException if an error occurs
*/
protected OGCCapabilities getCapabilities()
throws IOException
{
int query_result = -1;
/* First try to read capabilities from disk (DTD still will be
downloaded from the net) */
if(capabilities == null)
try {
String h = (new URL(server.toString())).getHost();
if(h == null || h.equals(""))
h = "_CAP_";
File ian = new File(h+"_capabilities.xml");
if(ian.exists()){
prln("Try to read from local disk: "+h+"_capabilities.xml");
capabilities = new OGCCapabilities(new FileInputStream(h+"_capabilities.xml"));
prln("Capabilities version: "+capabilities.getCapabilitiesVersion());
}
//services = capabilities.getAvailableServices();
//for(int i = 0; i < services.length; i++) {
// prln(""+(i+1)+". "+services[i].toString());
// if(services[i] instanceof OGCWebMapService) {
// webMap = (OGCWebMapService)services[i];
// }
//}
} catch(Exception e) {
capabilities = null;
prln("Caught exception -- trying for network");
}
/* If there isn't capabilities on local disk download it from the net */
if(capabilities == null) {
if(remoteConnection == null)
initConnection();
try {
query_result =
remoteConnection.sendQuery(OGCCapabilities.capabilitiesQuery(version));
} catch(MalformedURLException mue) {
query_result = -1;
mue.printStackTrace();
} catch(Exception e) {
prln("Help - unexpected exception "+e+" in send Query");
prln("server "+server.toString());
e.printStackTrace();
}
prln("Content result="+query_result);
switch(query_result) {
case RemoteConnection.RESPONSE_ERROR :
break;
case RemoteConnection.CONTENT_BASIC_XML :
case RemoteConnection.CONTENT_CAPABILITIES :
case RemoteConnection.CONTENT_UNKNOWN :
case RemoteConnection.CONTENT_HTML : /* apache returns this for
HEAD requests on xml files - Ian*/
try {
/* Create capabilities objecr and parse data */
capabilities = new
OGCCapabilities(remoteConnection.getInputStream());
prln("Capabilities version: "+capabilities.getCapabilitiesVersion());
/* Get all services instances from capabilities */
//services = capabilities.getAvailableServices();
//for(int i = 0; i < services.length; i++) {
// prln(""+(i+1)+". "+services[i].toString());
// if(services[i] instanceof OGCWebMapService) {
// /* WebMap service found. */
// webMap = (OGCWebMapService)services[i];
// }
//}
/* Save capabilities to disk for future use */
/* !!! Creating names should be changed, there can be many WMS on one
virtual host on the net !!! */
String host = (new URL(server.toString())).getHost();
if(host == null || host.equals(""))
host = "_CAP_";
FileOutputStream fos =
new FileOutputStream(host+"_capabilities.xml");
capabilities.printCapabilitiesXML(fos);
fos.close();
} catch(Exception e) {
capabilities = null;
e.printStackTrace();
}
break;
case RemoteConnection.CONTENT_GML :
break;
case RemoteConnection.CONTENT_SE :
break;
default :
/* It should never happen!! */
}
}
return capabilities;
}
/*
public static void main(String argc[])
throws Exception
{
URL u = new URL("file:sample.xml");
for(int i = 0; i < argc.length; i++) {
if(argc[i].equals("-u")) {
u = new URL(argc[i+1]);
i++;
}
}
RemoteOGC remote = new RemoteOGC(u, null);
remote.getCapabilities();
}
*/
protected static void prln(String str) {
System.err.println(str);
}
protected static void pr(String str) {
System.err.print(str);
}
}
/*
* Changes in file:
*
* $Log: RemoteOGC.java,v $
* Revision 1.18 2001/12/21 11:46:47 ianturton
* added some code to getImageStream to handle the connection timing out.
*
* Revision 1.17 2001/12/20 15:27:23 ianturton
* various fixes and small mods to make WMSExample actually run. Can now
* display an image if you remember to choose gif or jpg as output. Still
* crashes if you zoom in.
*
* Revision 1.16 2001/11/21 13:48:19 kobit
* Added support for creating ImageLayer with InputStream as image data source and full support for downaloding map data from WMS with jprotocols package
*
* Revision 1.15 2001/11/20 17:01:45 kobit
* Added byte[] getContentData() support
*
* Revision 1.14 2001/11/19 10:03:33 kobit
* *** empty log message ***
*
* Revision 1.13 2001/11/18 15:57:54 kobit
* Use of simple-jprotocols package instead of java.net, DTD validating switched OFF, default version if WMS implementation is set to 1.0.0
*
* Revision 1.12 2001/10/29 15:32:33 ianturton
* added default SRS EPSG:4326 and some checking to skip null or empty
* parameters
*
* Revision 1.11 2001/10/24 09:42:24 kobit
* Some minor changes for working according to new design
*
*
*/