📄 wcsmapresource.java
字号:
package com.esri.solutions.jitk.web.wcs.data;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletContext;
import org.apache.log4j.Logger;
import com.esri.adf.web.data.GISResource;
import com.esri.adf.web.data.WebContext;
import com.esri.adf.web.data.geometry.WebExtent;
import com.esri.adf.web.data.geometry.WebSpatialReference;
import com.esri.adf.web.util.WebUtil;
import com.esri.solutions.jitk.datasources.exceptions.ClientNotFoundException;
import com.esri.solutions.jitk.datasources.exceptions.WCSException;
import com.esri.solutions.jitk.datasources.ogc.wcs.IOAgent;
import com.esri.solutions.jitk.datasources.ogc.wcs.IWCSClient;
import com.esri.solutions.jitk.datasources.ogc.wcs.WCSClientFactory;
import com.esri.solutions.jitk.datasources.ogc.wcs.WCSHTTPClientBase;
import com.esri.solutions.jitk.datasources.ogc.wcs.info.WCSBoundingBox;
import com.esri.solutions.jitk.datasources.ogc.wcs.info.WCSCapabilities;
import com.esri.solutions.jitk.datasources.ogc.wcs.info.WCSCoverageInfo;
@SuppressWarnings("serial")
public class WCSMapResource extends GISResource {
@SuppressWarnings("unused")
private static final Logger logger = Logger.getLogger(WCSMapResource.class.getName());
private WCSCapabilities wcsCapabilities;
private String coverage = null;
private WebSpatialReference spatialReference;
private String endPointURL = null;
private String epsgId = null;
private String format = null;
private int dimension = 2;
private String noMapImage;
private String noOverviewImage;
private String chkStr(String s, String defaultVal) {
s = (s == null) ? "" : s.trim();
return (s.length()==0) ? defaultVal : s;
}
private int chkInt(String s, int defaultVal) {
int n = defaultVal;
try { n = Integer.parseInt(s); } catch(Exception e) {}
return n;
}
/**
* Set end point URL
* @param endPointURL
*/
public void setEndPointURL(String endPointURL) {
this.endPointURL = endPointURL;
}
/**
* get end point URL
* @return endPointURL
*/
public String getEndPointURL() {
return this.endPointURL;
}
/**
* set epsg id
* @param epsgId
*/
public void setEpsgId(String epsgId) {
this.epsgId = epsgId;
}
/**
* get epsg id
* @return epsgId
*/
public String getEpsgId() {
return this.epsgId;
}
/**
* set coverage name
* @param coverage
*/
public void setCoverage(String coverage) {
this.coverage = coverage;
}
/**
* get coverage name
* @return coverage
*/
public String getCoverage() {
return this.coverage;
}
/**
* set format
* @param format
*/
public void setFormat(String format) {
this.format = format;
}
/**
* get format
* @return format
*/
public String getFormat() {
return this.format;
}
/**
* set dimension
* @param dimension
*/
public void setDimension(int dimension) {
this.dimension = dimension;
}
/**
* get dimension
* @return dimension
*/
public int getDimension(){
return this.dimension;
}
/**
* get WCSCapabilitiesType
* @return WCSCapabilitiesType
*/
public WCSCapabilities getWCSCapabilities() {
return this.wcsCapabilities;
}
public void setWCSCapabilities(WCSCapabilities wcsCapabilities) {
if(wcsCapabilities != null) {
this.setAlias(chkStr(wcsCapabilities.getName(), wcsCapabilities.getLabel()));
this.setEndPointURL(wcsCapabilities.getGetCapabilitiesURL());
} else {
this.setAlias("");
}
this.wcsCapabilities = wcsCapabilities;
}
/**
* This method is called by the WebContext to initialize the resource.
*/
public void init(WebContext context) {
initResource();
super.init(context);
}
/**
* The cleanup (final) chores of the resource like releasing held resources must be performed in this method.
*/
public void destroy() {
super.destroy();
}
/**
* This method is called by the associated WebContext when the context itself is being activated.
*/
public void activate() {
super.activate();
}
/**
* This method is called by the associated WebContext when the context itself is being passivated.
*/
public void passivate() {
super.passivate();
}
/**
* Returns the spatial reference associated with this resource.
*/
public WebSpatialReference getDefaultSpatialReference() {
return this.spatialReference;
}
protected WCSCapabilities initWCSCapabilities() throws WCSException {
if(this.wcsCapabilities == null) {
setWCSCapabilities(new WCSHTTPClientBase().getCapabilities(this.endPointURL));
}
return this.wcsCapabilities;
}
protected WCSCoverageInfo getWCSCoverageInfo() throws ClientNotFoundException, WCSException {
if(this.coverage != null && this.coverage.length() > 0 && initWCSCapabilities() != null && this.wcsCapabilities.getCoverageInfos().containsKey(this.coverage)) {
IWCSClient wcsClient = WCSClientFactory.getInstance(wcsCapabilities.getVersion());
List<WCSCoverageInfo> infos = wcsClient.describeCoverage(this.wcsCapabilities.getDescribeCoverageURL(), new String[] {this.coverage});
if(infos != null) {
for(Iterator<WCSCoverageInfo> i = infos.iterator(); i.hasNext();) {
WCSCoverageInfo info = i.next();
if(info.getId().equalsIgnoreCase(this.coverage)) {
return info;
}
}
}
}
return null;
}
public void initResource() {
try {
WCSCoverageInfo info = getWCSCoverageInfo();
if(info != null) {
this.wcsCapabilities.getCoverageInfos().put(this.coverage, info);
} else {
throw new IllegalStateException("No coverage '" + this.coverage + "' found.");
}
this.epsgId = this.wcsCapabilities.getDefaultProjectionId(this.coverage);
if(this.epsgId != null) {
this.spatialReference = WebSpatialReference.getWebSpatialReference(Integer.parseInt(this.epsgId));
} else {
throw new IllegalStateException("Coverage default spatial reference is invalid.");
}
this.format = this.wcsCapabilities.getDefaultFormat(this.coverage);
if(this.format == null) {
throw new IllegalStateException("Coverage format is not supported.");
}
this.dimension = this.wcsCapabilities.getDefaultDimension(this.coverage);
} catch(ClientNotFoundException ex) {
throw new IllegalStateException("No WCS client found.");
} catch(WCSException ex) {
throw new IllegalStateException(ex.getMessage());
}
}
public WebExtent getDefaultWebExtent() {
if(this.wcsCapabilities != null) {
WCSBoundingBox bbox = this.wcsCapabilities.getDefaultBoundingBox(this.coverage, this.epsgId);
if(bbox != null) {
return new WebExtent(bbox.getMinX(), bbox.getMinY(), bbox.getMaxX(), bbox.getMaxY(), WebSpatialReference.getWebSpatialReference(chkInt(this.epsgId, 4326)));
}
}
return null;
}
protected InputStream getImageStream(String fn) {
try {
return new ByteArrayInputStream(IOAgent.readIntoByteArray(new File(((ServletContext) WebUtil.getExternalContext().getContext()).getRealPath(fn))));
} catch(IOException e) {}
return null;
}
public String getNoMapImage() {
return noMapImage;
}
public void setNoMapImage(String noMapImage) {
this.noMapImage = noMapImage;
}
public String getNoOverviewImage() {
return noOverviewImage;
}
public void setNoOverviewImage(String noOverviewImage) {
this.noOverviewImage = noOverviewImage;
}
protected InputStream getNoMapImageStream() {
return getImageStream(this.noMapImage);
}
protected InputStream getNoOverviewImageStream() {
return getImageStream(this.noOverviewImage);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -