本文整理了Java中org.geotools.feature.Feature
类的一些代码示例,展示了Feature
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Feature
类的具体详情如下:
包路径:org.geotools.feature.Feature
类名称:Feature
[英]Represents a feature of arbitrary complexity.
This interface answers the question: How do we store feature attributes? (The answer to the more useful question, How do we access feature attribute, is contained in the Expression class.
Warning: We are revising the Feature Model to be more complete in the next round of GeoTools. If you do any data access in your classes please try and make use of Expression to access your information, if you do this you will not be affected by the change (beyond a few search and replace operations).
The most generic approach would be to pass all feature attributes as objects and use Java variable and method references to access them. However, this is also the most useless approach because it establishes no unified methods for getting attribute information (since it is totally Object dependent), without elaborate reflection/introspection, which is inconvenient to use. Unlike its FeatureType counterpart, this interface does not attempt to serve as a typing framework. Rather, multiple implementations of this interface should generally be for performance reasons.
This interface serves two important purposes. Most obviously, it gives users of features a unified, consistent framework for accessing and manipulating feature data. Perhaps more importantly, the FeatureType
and Feature
interfaces also work together to give implementers a framework for constraining and enforcing constraints (respectively) on allowed feature types. As such, this interface is as general as possible in terms of the types of objects to which it provides access. Keep in mind that creating new features is relatively difficult and should only be done to optimize performance for highly constrained schema types. For the vast majority of schemas, the generic feature implementation will work fine.
Notes for Feature Clients:
Clients should always use feature accessor methods (getAttribute and setAttribute) to modify the state of internal attribute objects. It is possible that some feature implementations will allow object state changes by clients outside of the class, but this is strongly discouraged. In general, feature implementations will make defensive copies of objects passed to clients and it is therefore not guaranteed that client state changes that take place outside of the feature will be reflected in the internal state of the feature object! For this reason, clients should always use the set methods to change feature attribute object states!
Notes for Feature Implementers:
It is the responsibility of the implementing class to ensure that the Feature
attributes stay synchronized with its FeatureType definition. Features should never get out of synch with their declared schemas and should never alter their schemas! There are four conventions of which implementers of this interface must be aware in order to successfully manage a Feature
:
FeatureType
) schema reference and this reference should not be altered after a feature has been created. To ensure this, is is strongly recommended that features take a valid reference to an existing immutable schema in its constructor and declare that reference final.FeatureType
and is therefore immmutable.Feature
, they are handled specially by the Feature
methods, in that their attributes may be accessed directly by their containing feature. All other object variables and methods must be accessed through the objects themselves. It is up to implementers of Feature
to make sure that each attribute value conforms to its internal schema. A feature should never reach a state where its attributes (or sub-attributes) do not conform to their FeatureType
definitions. There are three ways to implement this. The first is to simply make features immutable; however, given the ubiquity and required flexibility of features, this is likely not possible. The second (and second easiest), is to make all feature attributes immutable. For most cases, this is probably the best way to handle this issue. The third way, is to never give out a reference that would allow a client to change an attribute object's class (most obviously, an array reference). Generally speaking, features should attempt to minimize external object references by attempting to clone incoming attributes before adding them and outgoing attributes before sending them. For features with non-cloneable attributes, of course, this is not possible, so this is left to the discretion of the implementor.FeatureType
和Feature
接口还协同工作,为实现者提供了一个框架,分别对允许的功能类型进行约束和强制约束。因此,就其提供访问的对象类型而言,该接口尽可能通用。请记住,创建新功能相对来说比较困难,应该只在高度受限的模式类型中优化性能。对于绝大多数模式,通用特性实现可以很好地工作。Feature
属性与其FeatureType定义保持同步。特性不应该与其声明的模式不同步,也不应该更改其模式!为了成功管理Feature
,此接口的实现者必须了解四个约定:FeatureType
)架构引用,并且在创建功能后不应更改此引用。为了确保这一点,强烈建议特性在其构造函数中对现有的不可变模式进行有效引用,并将该引用声明为final。FeatureType
的一部分,因此不可更改。Feature
的实例,则它们由Feature
方法专门处理,因为它们的属性可以通过其包含功能直接访问。必须通过对象本身访问所有其他对象变量和方法。Feature
的实现者需要确保每个属性值符合其内部模式。特征不应达到其属性(或子属性)与其FeatureType
定义不一致的状态。有三种方法可以实现这一点。第一个是简单地使特性不可变;然而,考虑到功能的普遍性和所需的灵活性,这可能是不可能的。第二个(也是第二个最简单的)是使所有特征属性都不可变。在大多数情况下,这可能是处理此问题的最佳方法。第三种方法是永远不要给出允许客户端更改属性对象类的引用(最明显的是数组引用)。一般来说,功能应该通过在添加传入属性之前尝试克隆传入属性和在发送属性之前尝试克隆传出属性来尽量减少外部对象引用。当然,对于具有不可克隆属性的特性,这是不可能的,因此这由实现者自行决定。代码示例来源:origin: org.geotools/gt2-main
if (!feature2.getFeatureType().equals(feature1.getFeatureType())) {
return false;
for (int i = 0, ii = feature1.getNumberOfAttributes(); i < ii; i++) {
Object otherAtt = feature2.getAttribute(i);
if (feature1.getAttribute(i) == null) {
if (otherAtt != null) {
return false;
if (!feature1.getAttribute(i).equals(otherAtt)) {
if (feature1.getAttribute(i) instanceof Geometry
&& otherAtt instanceof Geometry) {
if (!((Geometry) feature1.getAttribute(i)).equals(
(Geometry) otherAtt)) {
return false;
代码示例来源:origin: org.geotools/gt2-postgis-versioned
throw new IllegalArgumentException("Both features are null, that's not a diff!");
this.ID = oldFeature != null ? oldFeature.getID() : newFeature.getID();
this.feature = newFeature;
this.oldFeature = oldFeature;
this.state = UPDATED;
List changedAttributes = new ArrayList();
for (int i = 0; i < oldFeature.getNumberOfAttributes(); i++) {
String attName = oldFeature.getFeatureType().getAttributeType(i).getName();
Object toAttribute = newFeature.getAttribute(attName);
Object fromAttribute = oldFeature.getAttribute(attName);
if (!DataUtilities.attributesEqual(fromAttribute, toAttribute)) {
changedAttributes.add(attName);
代码示例来源:origin: org.geotools/gt2-demo-property
public void remove() throws IOException {
if( live == null){
throw new IOException( "No current feature to remove");
}
if( origional != null ){
store.listenerManager.fireFeaturesRemoved(live.getFeatureType().getTypeName(), Transaction.AUTO_COMMIT,
origional.getBounds(), false);
}
origional = null;
live = null; // prevent live and remove from being written out
}
public void close() throws IOException {
代码示例来源:origin: org.geotools/gt2-xml-xsd
public Object getImmediateNode() {
return index != -1 ? feature.getAttribute( index ) : feature.getID();
}
代码示例来源:origin: org.geotools/gt2-demo-property
private void writeImplementation( Feature f ) throws IOException{
writer.next();
writer.writeFeatureID( f.getID() );
for( int i=0; i<f.getNumberOfAttributes(); i++){
writer.write( i, f.getAttribute( i ));
}
}
public Feature next() throws IOException {
代码示例来源:origin: org.geotools/gt2-main
protected void addToSpatialIndex(Feature f) {
if (f.getDefaultGeometry() != null) {
Envelope bounds = f.getBounds();
if( !bounds.isNull() )
spatialIndex.insert(bounds, f);
}
}
代码示例来源:origin: org.geotools/gt2-postgis-versioned
for (int i = 0; i < liveFeature.getNumberOfAttributes(); i++) {
AttributeType at = liveFeature.getFeatureType().getAttributeType(i);
Object newValue = liveFeature.getAttribute(at.getName());
Object oldValue = oldFeature.getAttribute(at.getName());
newFeature.setAttribute(at.getName(), newValue);
if (!DataUtilities.attributesEqual(newValue, oldValue)) {
dirty = true;
String typeName = liveFeature.getFeatureType().getTypeName();
String fid = liveFeature.getID();
dirtyFeature = state.isFidDirty(typeName, fid);
for (int i = 0; i < liveFeature.getNumberOfAttributes(); i++) {
AttributeType at = liveFeature.getFeatureType().getAttributeType(i);
oldFeature.setAttribute(at.getName(), liveFeature.getAttribute(at.getName()));
for (int i = 0; i < liveFeature.getNumberOfAttributes(); i++) {
AttributeType at = liveFeature.getFeatureType().getAttributeType(i);
newFeature.setAttribute(at.getName(), liveFeature.getAttribute(at.getName()));
newFeature.setAttribute("expired", NON_EXPIRED);
newFeature.setAttribute("revision", new Long(state.getRevision()));
newFeature.setAttribute("created", oldFeature.getAttribute("created"));
} else {
newFeature.setAttribute("created", new Long(state.getRevision()));
id = mapper.createVersionedFid(liveFeature.getID(), state.getRevision());
newFeature.setAttribute("created", oldFeature.getAttribute("created"));
代码示例来源:origin: org.geotools/gt2-main
public Feature duplicate(Feature original) throws IllegalAttributeException{
if( original == null ) return null;
FeatureType featureType = original.getFeatureType();
if (!featureType.equals(this)) {
throw new IllegalAttributeException("Feature type " + featureType
+ " does not match " + this);
}
String id = original.getID();
int numAtts = featureType.getAttributeCount();
Object attributes[] = new Object[numAtts];
for (int i = 0; i < numAtts; i++) {
AttributeType curAttType = getAttributeType(i);
attributes[i] = curAttType.duplicate(original.getAttribute(i));
}
return featureType.create(attributes, id );
}
/**
代码示例来源:origin: org.geotools/gt2-main
public FeatureType getFeatureType() {
return features[0].getFeatureType();
}
代码示例来源:origin: org.geotools/gt2-main
/**
* Applies transform to all geometry attribute.
*
* @param feature Feature to be transformed
* @param schema Schema for target transformation - transform( schema, crs )
* @param transform MathTransform used to transform coordinates - reproject( crs, crs )
* @return transformed Feature of type schema
* @throws TransformException
* @throws MismatchedDimensionException
* @throws IllegalAttributeException
*/
public static Feature transform( Feature feature, FeatureType schema, MathTransform transform )
throws MismatchedDimensionException, TransformException, IllegalAttributeException {
feature = schema.create(feature.getAttributes(null), feature.getID());
GeometryAttributeType geomType = schema.getDefaultGeometry();
Geometry geom = (Geometry) feature.getAttribute(geomType.getName());
geom = JTS.transform(geom, transform);
try {
feature.setAttribute(geomType.getName(), geom);
} catch (IllegalAttributeException shouldNotHappen) {
// we are expecting the transform to return the same geometry type
}
return feature;
}
代码示例来源:origin: org.geotools/gt2-xml-gml3
public Element encode(Object object, Document document, Element value)
throws Exception {
Feature feature = (Feature) object;
FeatureType featureType = feature.getFeatureType();
String namespace = featureType.getNamespace().toString();
String typeName = featureType.getTypeName();
Element encoding = document.createElementNS(namespace, typeName);
encoding.setAttributeNS(GML.NAMESPACE, "id", feature.getID());
return encoding;
}
代码示例来源:origin: org.geotools/gt2-main
/**
* Gets the value of this attribute from the passed feature.
*
* @param feature Feature from which to extract attribute value.
*/
public Object evaluate(Feature feature) {
return feature.getAttribute(attPath);
}
代码示例来源:origin: org.geotools/gt2-main
public void testGetFeaturesWriterModify()
throws IOException, IllegalAttributeException {
FeatureWriter writer = data.getFeatureWriter("ROAD",
Transaction.AUTO_COMMIT);
Feature feature;
while (writer.hasNext()) {
feature = writer.next();
if (feature.getID().equals(roadFeatures[0].getID())) {
feature.setAttribute("NAME", "changed");
writer.write();
}
}
feature = null;
FeatureReader reader = data.getFeatureReader(new DefaultQuery("ROAD",
rd1Filter), Transaction.AUTO_COMMIT);
if (reader.hasNext()) {
feature = reader.next();
}
// feature = (Feature) data.features("ROAD").get("road.rd1");
assertEquals("changed", feature.getAttribute("NAME"));
}
代码示例来源:origin: org.geotools/gt2-postgis-versioned
public String createID(Connection conn, Feature feature, Statement statement)
throws IOException {
if (feature.getAttribute(colNames[1]) == null) {
try {
String id = autoIncrementMapper.createID(conn, feature, statement);
feature.setAttribute(colNames[1], new Long(id));
} catch (Exception e) {
throw new DataSourceException("Could not generate key for the "
+ "unset primary key column " + colNames[1], e);
}
}
return super.createID(conn, feature, statement);
}
代码示例来源:origin: org.geotools/gt2-main
public int compare(Object o1, Object o2) {
Feature f1 = (Feature) o1;
Feature f2 = (Feature) o2;
return f1.getID().compareTo(f2.getID());
}
};
代码示例来源:origin: org.fudaa.framework.ctulu/ctulu-gis
while (it.hasNext()) {
final Feature f = it.next();
geom.add(f.getDefaultGeometry());
for (int i = 0; i < finalAttributeCount; i++) {
values[i].add(f.getAttribute(idxInFeature.getQuick(i)));
代码示例来源:origin: org.geotools/gt2-postgis-versioned
writer = wrapped.getFeatureWriterAppend(VersionedPostgisDataStore.TBL_CHANGESETS, t);
f = writer.next();
f.setAttribute("author", author);
f.setAttribute("message", message);
f.setAttribute("date", new Date());
f.setDefaultGeometry(toLatLonRectange(bbox));
writer.write();
} catch (IllegalAttributeException e) {
return ((Long) f.getAttribute("revision")).longValue();
代码示例来源:origin: org.geotools/gt2-jdbc
protected void doUpdate(Feature live, Feature current)
throws IOException, SQLException {
try {
// Can we create for array getAttributes more efficiently?
for (int i = 0; i < current.getNumberOfAttributes(); i++) {
Object currAtt = current.getAttribute(i);
Object liveAtt = live.getAttribute(i);
if ((live == null)
|| !DataUtilities.attributesEqual(liveAtt, currAtt)) {
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.info("modifying att# " + i + " to " + currAtt);
}
queryData.write(i, currAtt);
}
}
} catch (IOException ioe) {
String message = "problem modifying row";
if (queryData.getTransaction() != Transaction.AUTO_COMMIT) {
queryData.getTransaction().rollback();
message += "(transaction canceled)";
}
throw ioe;
}
queryData.updateRow();
}
代码示例来源:origin: org.geotools/gt2-main
/**
* @see org.geotools.data.FeatureWriter#remove()
*/
public void remove() throws IOException {
if (live != null) {
// mark live as removed
diff.remove(live.getID());
fireNotification(FeatureEvent.FEATURES_REMOVED, live.getBounds());
live = null;
current = null;
} else if (current != null) {
// cancel additional content
current = null;
}
}
代码示例来源:origin: org.geotools/gt2-jdbc
LOGGER.finer("Calling next on writer");
} catch (IllegalAttributeException e) {
throw new DataSourceException("Unable to edit " + live.getID()
+ " of " + featureType.getTypeName(), e);
temp.getAttributes(
new Object[temp.getNumberOfAttributes()]), null);
内容来源于网络,如有侵权,请联系作者删除!