2191: Geo Utility enhancements: update json parser to retrieve z-axis property

Task-Url: https://support.d4science.org/issues/2191

Added method to return zAxis bean

git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/spatial-data/geo-utility@124012 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2016-02-09 17:05:32 +00:00
parent c6df35cdc8
commit 8ab2bab5c8
3 changed files with 124 additions and 46 deletions

View File

@ -9,11 +9,13 @@ import java.util.Map;
import org.apache.log4j.Logger;
import org.gcube.spatial.data.geoutility.bean.NcWmsLayerMetadata;
import org.gcube.spatial.data.geoutility.bean.NcWmsLayerMetadata.METADATA;
import org.gcube.spatial.data.geoutility.bean.WmsParameters;
import org.gcube.spatial.data.geoutility.wms.NcWmsGetMetadata;
import org.gcube.spatial.data.geoutility.wms.NcWmsGetMetadataRequest;
import org.gcube.spatial.data.geoutility.wms.WmsGetStyles;
import org.gcube.spatial.data.geoutility.wms.WmsUrlValidator;
import org.gcube.spatial.data.geoutility.wms.ZAxis;
/**
@ -173,9 +175,16 @@ public class GeoGetStylesUtility {
logger.debug("Wms GetStyles not found, Trying to get styles by 'NcWmsGetMetadata'");
isNcWms = true;
try{
NcWmsLayerMetadata ncMetadata = NcWmsGetMetadata.getMetadata(uriWMSService, layerName, connectionTimeout);
METADATA[] meta = new METADATA[3];
meta[0] = METADATA.SUPPORTEDSTYLES;
meta[1] = METADATA.DEFAULTPALETTE;
meta[2] = METADATA.PALETTES;
NcWmsLayerMetadata ncMetadata = NcWmsGetMetadata.getMetadata(uriWMSService, layerName, connectionTimeout, meta);
if(ncMetadata!=null && ncMetadata.getResponseCode()==200){
//STYLES
if(ncMetadata.getSupportedStyles().size()>0){
styles.add(ncMetadata.getSupportedStyles().get(0)+"/"+ncMetadata.getDefaultPalette()); //DEFAULT STYLE
logger.debug("added ncWms default style: "+ncMetadata.getSupportedStyles().get(0)+"/"+ncMetadata.getDefaultPalette());
@ -190,6 +199,10 @@ public class GeoGetStylesUtility {
}
}
}
//Z-AXIS
if(ncMetadata.getZAxis()!=null){
}
}
}catch(Exception e){
logger.error(e);
@ -203,6 +216,30 @@ public class GeoGetStylesUtility {
this.geoStyles = styles;
}
/**
* Load z axis values for a ncWMS layer
*
* @param connectionTimeout the connection timeout
* @return the z axis
*/
public ZAxis loadZAxis(int connectionTimeout){
String uriWMSService = validator.getBaseWmsServiceUrl();
String layerName = validator.getValueOfParsedWMSParameter(WmsParameters.LAYERS);
METADATA[] meta = new METADATA[1];
meta[0] = METADATA.Z_AXIS;
try {
NcWmsLayerMetadata ncMetadata = NcWmsGetMetadata.getMetadata(uriWMSService, layerName, connectionTimeout, meta);
return ncMetadata.getZAxis();
}
catch (Exception e) {
logger.error("Exception during ncWMS get zAxis: "+e);
return null;
}
}
/**
* Gets the geo styles.
*

View File

@ -23,10 +23,32 @@ public class NcWmsLayerMetadata implements Serializable {
private static final long serialVersionUID = 5111586382138532571L;
//GET PARAMETERS
public static final String SUPPORTEDSTYLES = "supportedStyles";
public static final String PALETTES = "palettes";
public static final String DEFAULTPALETTE = "defaultPalette";
public static final String Z_AXIS = "zaxis";
// public static final String SUPPORTEDSTYLES = "supportedStyles";
// public static final String PALETTES = "palettes";
// public static final String DEFAULTPALETTE = "defaultPalette";
// public static final String Z_AXIS = "zaxis";
public static enum METADATA {
SUPPORTEDSTYLES("supportedStyles"),
PALETTES("palettes"),
DEFAULTPALETTE("defaultPalette"),
Z_AXIS("zaxis");
private String key;
METADATA(String key){
this.key = key;
}
/**
* @return the key
*/
public String getKey() {
return key;
}
};
private String defaultPalette;
private List<String> supportedStyles;

View File

@ -11,12 +11,14 @@ import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.gcube.spatial.data.geoutility.bean.NcWmsLayerMetadata;
import org.gcube.spatial.data.geoutility.bean.NcWmsLayerMetadata.METADATA;
import org.gcube.spatial.data.geoutility.bean.WmsParameters;
import org.gcube.spatial.data.geoutility.util.UrlEncoderUtil;
import org.json.JSONArray;
@ -44,19 +46,21 @@ public class NcWmsGetMetadata {
* @throws Exception the exception
*/
public static NcWmsLayerMetadata getMetadata(String wmsServerUri, String layerName) throws Exception {
return getMetadata(wmsServerUri, layerName, CONNECTION_TIMEOUT);
return getMetadata(wmsServerUri, layerName, CONNECTION_TIMEOUT, METADATA.values());
}
/**
* Gets the metadata.
*
* @param wmsServerUri the wms server uri
* @param layerName the layer name
* @param connectionTimeout the connection timeout sets a specified timeout value, in milliseconds, to be used when opening URLConnection.
* @param connectionTimeout the connection timeout
* @param meta the meta
* @return the metadata
* @throws Exception the exception
*/
public static NcWmsLayerMetadata getMetadata(String wmsServerUri, String layerName, int connectionTimeout) throws Exception {
public static NcWmsLayerMetadata getMetadata(String wmsServerUri, String layerName, int connectionTimeout, NcWmsLayerMetadata.METADATA...meta) throws Exception {
if(wmsServerUri==null || wmsServerUri.isEmpty())
throw new Exception("Invalid wms server uri");
@ -70,7 +74,7 @@ public class NcWmsGetMetadata {
try {
String query = UrlEncoderUtil.encodeQuery(parameters);
return openConnectionGetMetadata(wmsServerUri, query, connectionTimeout);
return openConnectionGetMetadata(wmsServerUri, query, connectionTimeout, meta);
}catch (Exception e) {
logger.error("Error Exception with url " + wmsServerUri);
@ -86,9 +90,10 @@ public class NcWmsGetMetadata {
* @param urlConn the url conn
* @param query the query
* @param connectionTimeout the connection timeout sets a specified timeout value, in milliseconds, to be used when opening URLConnection.
* @param meta the meta
* @return the nc wms layer metadata
*/
private static NcWmsLayerMetadata openConnectionGetMetadata(String urlConn, String query, int connectionTimeout) {
private static NcWmsLayerMetadata openConnectionGetMetadata(String urlConn, String query, int connectionTimeout, NcWmsLayerMetadata.METADATA...meta) {
URL url;
NcWmsLayerMetadata metadata = null;
@ -113,7 +118,7 @@ public class NcWmsGetMetadata {
int code = httpConnection.getResponseCode();
if (code == 200) {
metadata = getMetadata(httpConnection);
metadata = getMetadata(httpConnection, meta);
metadata.setResponseCode(code);
}else{
logger.warn("openConnectionGetMetadata error, code = " + code +", returning");
@ -147,10 +152,11 @@ public class NcWmsGetMetadata {
* Gets the metadata.
*
* @param httpConnection the http connection
* @param meta the meta
* @return the metadata
* @throws IOException Signals that an I/O exception has occurred.
*/
private static NcWmsLayerMetadata getMetadata(HttpURLConnection httpConnection) throws IOException{
private static NcWmsLayerMetadata getMetadata(HttpURLConnection httpConnection, METADATA[] meta) throws IOException{
InputStream source = null;
NcWmsLayerMetadata metadata = new NcWmsLayerMetadata();
@ -160,44 +166,57 @@ public class NcWmsGetMetadata {
String jsonTxt = IOUtils.toString(source);
metadata.setRawJson(jsonTxt);
if(meta==null)
meta = METADATA.values();
List<METADATA> listMeta = Arrays.asList(meta);
JSONObject json = new JSONObject(jsonTxt);
JSONArray supportedStyles = json.getJSONArray(NcWmsLayerMetadata.SUPPORTEDSTYLES);
if(supportedStyles!=null){
List<String> s = new ArrayList<String>(supportedStyles.length());
for (int i=0; i<supportedStyles.length(); i++) {
s.add(supportedStyles.getString(i));
}
metadata.setSupportedStyles(s);
}
JSONArray palettes = json.getJSONArray(NcWmsLayerMetadata.PALETTES);
if(palettes!=null){
List<String> s = new ArrayList<String>(palettes.length());
for (int i=0; i<palettes.length(); i++) {
s.add(palettes.getString(i));
}
metadata.setPalettes(s);
}
metadata.setDefaultPalette(json.getString(NcWmsLayerMetadata.DEFAULTPALETTE));
JSONObject zaxis = json.getJSONObject(NcWmsLayerMetadata.Z_AXIS);
if(zaxis!=null){
System.out.println(zaxis);
ZAxis zAxis = new ZAxis();
zAxis.setUnits(zaxis.getString(ZAxis.UNITS));
zAxis.setPositive(zaxis.getBoolean(ZAxis.POSITIVE));
JSONArray values = zaxis.getJSONArray(ZAxis.VALUES);
System.out.println(values);
if(values!=null){
List<Integer> s = new ArrayList<Integer>(values.length());
for (int i=0; i<values.length(); i++) {
s.add(values.getInt(i));
if(listMeta.contains(NcWmsLayerMetadata.METADATA.SUPPORTEDSTYLES)){
JSONArray supportedStyles = json.getJSONArray(NcWmsLayerMetadata.METADATA.SUPPORTEDSTYLES.getKey());
if(supportedStyles!=null){
List<String> s = new ArrayList<String>(supportedStyles.length());
for (int i=0; i<supportedStyles.length(); i++) {
s.add(supportedStyles.getString(i));
}
zAxis.setValues(s);
metadata.setSupportedStyles(s);
}
}
if(listMeta.contains(NcWmsLayerMetadata.METADATA.PALETTES)){
JSONArray palettes = json.getJSONArray(NcWmsLayerMetadata.METADATA.PALETTES.getKey());
if(palettes!=null){
List<String> s = new ArrayList<String>(palettes.length());
for (int i=0; i<palettes.length(); i++) {
s.add(palettes.getString(i));
}
metadata.setPalettes(s);
}
}
if(listMeta.contains(NcWmsLayerMetadata.METADATA.DEFAULTPALETTE)){
metadata.setDefaultPalette(json.getString(NcWmsLayerMetadata.METADATA.DEFAULTPALETTE.getKey()));
}
if(listMeta.contains(NcWmsLayerMetadata.METADATA.Z_AXIS)){
JSONObject zaxis = json.getJSONObject(NcWmsLayerMetadata.METADATA.Z_AXIS.getKey());
if(zaxis!=null){
System.out.println(zaxis);
ZAxis zAxis = new ZAxis();
zAxis.setUnits(zaxis.getString(ZAxis.UNITS));
zAxis.setPositive(zaxis.getBoolean(ZAxis.POSITIVE));
JSONArray values = zaxis.getJSONArray(ZAxis.VALUES);
System.out.println(values);
if(values!=null){
List<Integer> s = new ArrayList<Integer>(values.length());
for (int i=0; i<values.length(); i++) {
s.add(values.getInt(i));
}
zAxis.setValues(s);
}
metadata.setZAxis(zAxis);
}
metadata.setZAxis(zAxis);
}
logger.trace("returning: "+metadata.toString());