org.geotools.feature.Feature类的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(15.9k)|赞(0)|评价(0)|浏览(276)

本文整理了Java中org.geotools.feature.Feature类的一些代码示例,展示了Feature类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Feature类的具体详情如下:
包路径:org.geotools.feature.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:

  1. FeatureType Reference
    Features must always hold a single (immutable: see 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.
  2. Default Geometry
    Each feature must have a default geometry, but this primary geometry may be null. This means that a feature may contain no geometries, but it must always have a method for accessing a geometry object (even if it is null). It also means that a feature with multiple geometries must pick one as its default geometry. Note that the designation of the default geometry is stored as part of the FeatureType and is therefore immmutable.
  3. Attributes
    All features contain zero or more attributes, which can have one or more occurrences inside the feature. Attributes may be any valid Java object. If attributes are instances of 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.
  4. Constructors
    Constructors should take arguments with enough information to create a valid representation of the feature. They should also always include a valid schema that can be used to check the proposed attributes. This is necessary to ensure that the feature is always in a valid state, relative to its schema.
  5. hashCode() and equals(Object other)
    Determining equality and equivalence for Feature instances is of utmost importance. This must be done in a constistent manner, as many other areas of geotools will rely on these relations. See java.lang.Object for details.
    [中]表示任意复杂度的特征。
    此界面回答了以下问题:如何存储特征属性?(更有用的问题“如何访问feature属性”的答案包含在Expression类中。
    警告:我们正在修改特征模型,以便在下一轮GeoTools中更加完整。如果您在类中进行任何数据访问,请尝试使用表达式访问您的信息,如果您这样做,您将不会受到更改的影响(除了一些搜索和替换操作)。
    最通用的方法是将所有特性属性作为对象传递,并使用Java变量和方法引用来访问它们。然而,这也是最无用的方法,因为它没有建立获取属性信息的统一方法(因为它完全依赖于对象),没有详细的反射/内省,这不方便使用。与FeatureType对应物不同,此接口不尝试用作类型框架。相反,此接口的多个实现通常应该是出于性能原因。
    此接口有两个重要用途。最明显的是,它为特征用户提供了一个统一、一致的框架,用于访问和操作特征数据。也许更重要的是,FeatureTypeFeature接口还协同工作,为实现者提供了一个框架,分别对允许的功能类型进行约束和强制约束。因此,就其提供访问的对象类型而言,该接口尽可能通用。请记住,创建新功能相对来说比较困难,应该只在高度受限的模式类型中优化性能。对于绝大多数模式,通用特性实现可以很好地工作。
    功能客户端注意事项:
    客户端应始终使用功能访问器方法(getAttribute和setAttribute)来修改内部属性对象的状态。有些功能实现可能允许类外的客户端更改对象状态,但强烈建议不要这样做。通常,功能实现会对传递给客户端的对象进行防御性复制,因此不能保证在功能外部发生的客户端状态更改会反映在功能对象的内部状态中!因此,客户端应始终使用set方法来更改要素属性对象状态!
    功能实现者注意事项:
    实现类负责确保Feature属性与其FeatureType定义保持同步。特性不应该与其声明的模式不同步,也不应该更改其模式!为了成功管理Feature,此接口的实现者必须了解四个约定:
    1.特征类型参考
    功能必须始终保持单个(不可变:请参见FeatureType)架构引用,并且在创建功能后不应更改此引用。为了确保这一点,强烈建议特性在其构造函数中对现有的不可变模式进行有效引用,并将该引用声明为final。
    1.默认几何图形
    每个要素都必须具有默认几何图形,但此主几何图形可能为空。这意味着要素可能不包含几何图形,但它必须始终具有访问几何图形对象的方法(即使它为null)。这还意味着具有多个几何图形的特征必须选择一个作为其默认几何图形。请注意,默认几何图形的指定存储为FeatureType的一部分,因此不可更改。
    1.属性
    所有要素都包含零个或多个属性,这些属性在要素内部可以有一个或多个引用。属性可以是任何有效的Java对象。如果属性是Feature的实例,则它们由Feature方法专门处理,因为它们的属性可以通过其包含功能直接访问。必须通过对象本身访问所有其他对象变量和方法。Feature的实现者需要确保每个属性值符合其内部模式。特征不应达到其属性(或子属性)与其FeatureType定义不一致的状态。有三种方法可以实现这一点。第一个是简单地使特性不可变;然而,考虑到功能的普遍性和所需的灵活性,这可能是不可能的。第二个(也是第二个最简单的)是使所有特征属性都不可变。在大多数情况下,这可能是处理此问题的最佳方法。第三种方法是永远不要给出允许客户端更改属性对象类的引用(最明显的是数组引用)。一般来说,功能应该通过在添加传入属性之前尝试克隆传入属性和在发送属性之前尝试克隆传出属性来尽量减少外部对象引用。当然,对于具有不可克隆属性的特性,这是不可能的,因此这由实现者自行决定。
    1.施工人员
    构造函数应该使用具有足够信息的参数来创建功能的有效表示形式。它们还应该始终包含一个有效的模式,用于检查建议的属性。这对于确保功能相对于其架构始终处于有效状态是必要的。
    1.hashCode()和等于(对象其他)
    确定特征实例的相等性和等价性至关重要。这必须以一致的方式完成,因为地理工具的许多其他领域将依赖于这些关系。参见java。lang.对象以获取详细信息。

代码示例

代码示例来源: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);

相关文章