📄 featuretypeinfo.java
字号:
* This is the full type name with namespace prefix.
*
* @return String the FeatureTypeInfo name - should be unique for the
* parent Data instance.
*/
public String getName() {
return getPrefix() + ":" + typeName;
}
/**
* getFeatureSource purpose.
*
* <p>
* Returns a real FeatureSource.
* </p>
*
* @return FeatureSource the feature source represented by this info class
*
* @throws IOException when an error occurs.
*/
public FeatureSource getFeatureSource() throws IOException {
return getFeatureSource(false);
}
/**
* If this layers has been setup to reproject data, skipReproject = true will
* disable reprojection. This method is build especially for the rendering subsystem
* that should be able to perform a full reprojection on its own, and do generalization
* before reprojection (thus avoid to reproject all of the original coordinates)
*/
public FeatureSource getFeatureSource(boolean skipReproject) throws IOException {
if (!isEnabled() || (getDataStoreInfo().getDataStore() == null)) {
throw new IOException("featureType: " + getName()
+ " does not have a properly configured " + "datastore");
}
DataStore dataStore = data.getDataStoreInfo(dataStoreId).getDataStore();
FeatureSource realSource = dataStore.getFeatureSource(typeName);
// avoid reprojection if the calling code can do it better
int localSrsHandling = srsHandling;
if(srsHandling == REPROJECT && skipReproject)
localSrsHandling = LEAVE;
if (((schema == null) || schema.isEmpty())) { // &&
//(ftc.getDefinitionQuery() == null || ftc.getDefinitionQuery().equals( Query.ALL ))){
return realSource;
} else {
// support versioning only if it is in the classpath, use reflection to invoke
// methods so that we don't get a compile time dependency
try {
if (implementsInterface(realSource.getClass(),
"org.geotools.data.VersioningFeatureSource")) {
Class clazz = Class.forName(
"org.vfny.geoserver.global.GeoServerVersioningFeatureSource");
Method m = clazz.getMethod("create",
new Class[] {
Class.forName("org.geotools.data.VersioningFeatureSource"),
FeatureType.class, Filter.class, CoordinateReferenceSystem.class, int.class
});
return (FeatureSource) m.invoke(null,
new Object[] {
realSource, getFeatureType(realSource), getDefinitionQuery(),
getSRS(SRS), new Integer(localSrsHandling)
});
}
} catch (Exception e) {
throw new DataSourceException("Creation of a versioning wrapper failed", e);
}
return GeoServerFeatureLocking.create(realSource, getFeatureType(realSource),
getDefinitionQuery(), getSRS(SRS), localSrsHandling);
}
}
/**
* Checks if a interface is implemented by looking at implemented interfaces using reflection
* @param realSource
* @param string
* @return
*/
private boolean implementsInterface(Class clazz, String interfaceName) {
if (clazz.getName().equals(interfaceName)) {
return true;
}
final Class[] ifaces = clazz.getInterfaces();
for (int i = 0; i < ifaces.length; i++) {
if (ifaces[i].getName().equals(interfaceName)) {
return true;
} else if (implementsInterface(ifaces[i], interfaceName)) {
return true;
}
}
if (clazz.getSuperclass() == null) {
return false;
} else {
return implementsInterface(clazz.getSuperclass(), interfaceName);
}
}
/*public static FeatureSource reTypeSource(FeatureSource source,
FeatureTypeInfoDTO ftc) throws SchemaException {
AttributeType[] attributes = new AttributeType[ftc.getSchemaAttributes()
.size()];
List attributeDefinitions = ftc.getSchemaAttributes();
int index = 0;
FeatureType ft = source.getSchema();
for (int i = 0; i < attributes.length; i++) {
AttributeTypeInfoDTO attributeDTO = (AttributeTypeInfoDTO) ftc.getSchemaAttributes()
.get(i);
String xpath = attributeDTO.getName();
attributes[i] = ft.getAttributeType(xpath);
if (attributes[i] == null) {
throw new NullPointerException("Error finding " + xpath
+ " specified in you schema.xml file for " + ftc.getName()
+ "FeatureType.");
}
}
FeatureType myType = FeatureTypeFactory.newFeatureType(attributes,
ftc.getName());
return GeoServerFeatureLocking.create(source, myType,
ftc.getDefinitionQuery());
}*/
/**
* Returns the FeatureType's envelope in its native CRS (or user
* declared CRS, if any).
* <p>
* Note the Envelope is cached in order to avoid a potential
* performance penalty every time this value is requires (for example,
* at every GetCapabilities request)
* </p>
*
* @return Envelope of the feature source bounds.
*
* @throws IOException when an error occurs
*/
public Envelope getBoundingBox() throws IOException {
CoordinateReferenceSystem declaredCRS = getDeclaredCRS();
CoordinateReferenceSystem nativeCRS = getNativeCRS();
if ((nativeBBox == null) || nativeBBox.isNull()) {
CoordinateReferenceSystem crs = srsHandling == LEAVE ? nativeCRS : declaredCRS;
nativeBBox = getBoundingBox(crs);
}
if (!(nativeBBox instanceof ReferencedEnvelope)) {
CoordinateReferenceSystem crs = srsHandling == LEAVE ? nativeCRS : declaredCRS;
nativeBBox = new ReferencedEnvelope(nativeBBox, crs);
}
if(srsHandling == REPROJECT) {
try {
ReferencedEnvelope re = (ReferencedEnvelope) nativeBBox;
nativeBBox = re.transform(declaredCRS, true);
} catch(Exception e) {
LOGGER.warning("Issues trying to transform native CRS");
}
}
return nativeBBox;
}
private Envelope getBoundingBox(CoordinateReferenceSystem targetCrs)
throws IOException {
// compute original bounding box
DataStore dataStore = data.getDataStoreInfo(dataStoreId).getDataStore();
FeatureSource realSource = dataStore.getFeatureSource(typeName);
Envelope bbox = FeatureSourceUtils.getBoundingBoxEnvelope(realSource);
// check if the original CRS is not the declared one
GeometryAttributeType defaultGeometry = realSource.getSchema().getDefaultGeometry();
CoordinateReferenceSystem originalCRS = defaultGeometry.getCoordinateSystem();
try {
if (targetCrs != null && !CRS.equalsIgnoreMetadata(originalCRS, targetCrs)) {
MathTransform xform = CRS.findMathTransform(originalCRS, targetCrs, true);
// bbox = JTS.transform(bbox, null, xform, 10);
if (bbox instanceof ReferencedEnvelope) {
bbox = ((ReferencedEnvelope) bbox).transform(targetCrs, true, 10);
} else {
bbox = new ReferencedEnvelope(JTS.transform(bbox, null, xform, 10), targetCrs);
}
}
} catch (Exception e) {
LOGGER.severe(
"Could not turn the original envelope in one into the declared CRS for type "
+ typeName);
LOGGER.severe("Original CRS is " + originalCRS);
LOGGER.severe("Declared CRS is " + targetCrs);
}
return new ReferencedEnvelope(bbox, targetCrs);
}
/**
* getDefinitionQuery purpose.
*
* <p>
* Returns the definition query for this feature source
* </p>
*
* @return Filter the definition query
*/
public Filter getDefinitionQuery() {
return definitionQuery;
}
/**
* getLatLongBoundingBox purpose.
*
* <p>
* The feature source lat/long bounds.
* </p>
*
* @return Envelope the feature source lat/long bounds.
*
* @throws IOException when an error occurs
*/
public Envelope getLatLongBoundingBox() throws IOException {
if (latLongBBox == null) {
latLongBBox = getBoundingBox(getSRS(4326));
}
return latLongBBox;
}
/**
* getSRS purpose.
*
* <p>
* Proprietary identifier number
* </p>
*
* @return int the SRS number.
*/
public String getSRS() {
return SRS + "";
}
/**
* Returns the declared CRS, that is, the CRS specified in the feature type
* editor form
*/
public CoordinateReferenceSystem getDeclaredCRS() {
return getSRS(SRS);
}
public CoordinateReferenceSystem getNativeCRS() throws IOException {
GeometryAttributeType dg = getDefaultGeometry();
if (dg == null) {
return null;
}
return dg.getCoordinateSystem();
}
/**
* Returns the default geometry for this feature type
* @return
* @throws IOException if the layer is not properly configured
*/
GeometryAttributeType getDefaultGeometry() throws IOException {
if (getDataStoreInfo().getDataStore() == null) {
throw new IOException("featureType: " + getName()
+ " does not have a properly configured " + "datastore");
}
DataStore dataStore = data.getDataStoreInfo(dataStoreId).getDataStore();
FeatureSource realSource = dataStore.getFeatureSource(typeName);
return realSource.getSchema().getDefaultGeometry();
}
/**
* If true, the layer does not have a default geometry
* @return
* @throws IOException
*/
public boolean isGeometryless() throws IOException {
return getDefaultGeometry() == null;
}
/**
* Get XMLSchema for this FeatureType.
*
* <p>
* Note this may require connection to the real geotools2 DataStore and as
* such is subject to IOExceptions.
* </p>
*
* <p>
* You have been warned.
* </p>
*
* @return XMLFragment
*
* @throws IOException DOCUMENT ME!
*/
/* public synchronized String getXMLSchema(){
if (xmlSchemaFrag == null) {
StringWriter sw = new StringWriter();
try {
FeatureTypeInfoDTO dto = getGeneratedDTO();
XMLConfigWriter.storeFeatureSchema(dto, sw);
} catch (ConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
xmlSchemaFrag = null;
}
xmlSchemaFrag = sw.toString();
try{
sw.close();
}catch(IOException e){}
}
return xmlSchemaFrag;
}*/
/**
* Will return our delegate with all information filled out
*
* <p>
* This is a hack because we cache our DTO delegate, this method combines
* or ftc delegate with possibly generated schema information for use by
* XMLConfigWriter among others.
* </p>
*
* <p>
* Call this method to receive a complete featureTypeInfoDTO that incldues
* all schema information.
* </p>
*
* @return
*
* @throws IOException DOCUMENT ME!
*/
private synchronized FeatureTypeInfoDTO getGeneratedDTO()
throws IOException {
return DataTransferObjectFactory.create(dataStoreId, getFeatureType());
}
/**
* getAttribute purpose.
*
* <p>
* XLM helper method.
* </p>
*
* @param elem The element to work on.
* @param attName The attribute name to find
* @param mandatory true is an exception is be thrown when the attr is not
* found.
*
* @return String the Attr value
*
* @throws ConfigurationException thrown when an error occurs.
*/
protected String getAttribute(Element elem, String attName, boolean mandatory)
throws ConfigurationException {
Attr att = elem.getAttributeNode(attName);
String value = null;
if (att != null) {
value = att.getValue();
}
if (mandatory) {
if (att == null) {
throw new ConfigurationException("element " + elem.getNodeName()
+ " does not contains an attribute named " + attName);
} else if ("".equals(value)) {
throw new ConfigurationException("attribute " + attName + "in element "
+ elem.getNodeName() + " is empty");
}
}
return value;
}
/*private FeatureType getSchema(String schema) throws ConfigurationException{
try{
return getSchema(loadConfig(new StringReader(schema)));
}catch(IOException e){
throw new ConfigurationException("",e);
}
}*/
/**
* loadConfig purpose.
*
* <p>
* Parses the specified file into a DOM tree.
* </p>
*
* @param fromSrId The file to parse int a DOM tree.
* @param bbox DOCUMENT ME!
*
* @return the resulting DOM tree
*/
/*public static Element loadConfig(Reader fis) throws ConfigurationException {
try {
InputSource in = new InputSource(fis);
DocumentBuilderFactory dfactory = DocumentBuilderFactory
.newInstance();
/*set as optimizations and hacks for geoserver schema config files
* @HACK should make documents ALL namespace friendly, and validated. Some documents are XML fragments.
* @TODO change the following config for the parser and modify config files to avoid XML fragmentation.
*/
/* dfactory.setNamespaceAware(false);
dfactory.setValidating(false);
dfactory.setIgnoringComments(true);
dfactory.setCoalescing(true);
dfactory.setIgnoringElementContentWhitespace(true);
Document serviceDoc = dfactory.newDocumentBuilder().parse(in);
Element configElem = serviceDoc.getDocumentElement();
return configElem;
} catch (IOException ioe) {
String message = "problem reading file " + "due to: "
+ ioe.getMessage();
LOGGER.warning(message);
throw new ConfigurationException(message, ioe);
} catch (ParserConfigurationException pce) {
String message =
"trouble with parser to read org.vfny.geoserver.config.org.vfny.geoserver.config.xml, make sure class"
+ "path is correct, reading file ";
LOGGER.warning(message);
throw new ConfigurationException(message, pce);
} catch (SAXException saxe) {
String message = "trouble parsing XML " + ": " + saxe.getMessage();
LOGGER.warning(message);
throw new ConfigurationException(message, saxe);
}
}*/
/**
* here we must make the transformation. Crhis: do you know how to do it? I
* don't know. Ask martin or geotools devel. This will be better when
* our geometries actually have their srs objects. And I think that we
* may need some MS Access database, not sure, but I saw some stuff about
* that on the list. Hopefully they'll do it all in java soon. I'm sorta
* tempted to just have users define for now.
*
* @param fromSrId
* @param bbox Envelope
*
* @return Envelope
*/
private static Envelope getLatLongBBox(String fromSrId, Envelope bbox) {
return bbox;
}
/**
* Get abstract (description) of FeatureType.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -