Java中org.geotools.feature.AttributeBuilder.setDescriptor方法 用法示例代碼
Java setDescriptor方法屬于org.geotools.feature.AttributeBuilder類。
使用說明:設(shè)置正在構(gòu)建的屬性的描述符。
在構(gòu)建復雜屬性時,此類型用作引用來獲取所包含屬性的類型。
本文搜集整理了關(guān)于Java中org.geotools.feature.AttributeBuilder.setDescriptor方法 用法示例代碼,并附有代碼來源和完整的源代碼,希望對您的程序開發(fā)有幫助。
本文末尾還列舉了關(guān)于setDescriptor方法的其它相關(guān)的方法列表供您參考。
AttributeBuilderTest.setDescriptor_validDescriptor_typeIsSetToDescriptorsType()
@Test
public void setDescriptor_validDescriptor_typeIsSetToDescriptorsType() {
// Act
builder.setDescriptor(FakeTypes.Mine.mineNAME_DESCRIPTOR);
// Assert
Assert.assertSame(FakeTypes.Mine.mineNAME_DESCRIPTOR.getType(), builder.getType());
}
代碼來源:geotools/geotools
GenericRecordBuilder.buildNode(...)
/**
* Helper method for building feature from tree node
*
* @param node the node
* @return list of attributes to be added to feature
*/
private Attribute buildNode(TreeNode node) {
if (node instanceof TreeLeaf) {
if (node instanceof ComplexTreeLeaf) {
ComplexTreeLeaf leaf = (ComplexTreeLeaf) node;
ComplexType type = (ComplexType) node.descriptor.getType();
ab.setDescriptor(node.descriptor);
for (Entry<String, Object> entry : leaf.value.entrySet()) {
PropertyDescriptor descriptor = Types.findDescriptor(type, entry.getKey());
if (descriptor == null) {
throw new IllegalArgumentException(
"Cannot find descriptor for attribute "
+ entry.getKey()
+ " in type "
+ type.getName().toString());
}
ab.add(null, entry.getValue(), descriptor.getName());
}
Attribute att = ab.build();
if (leaf.userData != null) {
for (Entry<String, Object> entry : leaf.value.entrySet()) {
((ComplexAttribute) att)
.getProperty(entry.getKey())
.getUserData()
.putAll(leaf.userData);
}
}
return att;
} else {
SimpleTreeLeaf leaf = (SimpleTreeLeaf) node;
ab.setDescriptor(node.descriptor);
Attribute att = ab.buildSimple(null, leaf.value);
if (leaf.userData != null) {
att.getUserData().putAll(leaf.userData);
}
return att;
}
} else if (node instanceof TreeBranch) {
List<Attribute> list = new ArrayList<Attribute>();
for (List<TreeNode> nodes : ((TreeBranch) node).children.values()) {
for (TreeNode child : nodes) {
list.add(buildNode(child));
}
}
return ab.createComplexAttribute(list, null, node.descriptor, null);
}
return null;
}
}
代碼來源:org.geoserver.csw/gs-csw-api
XPath.setValue(...)
@SuppressWarnings("unchecked")
private Attribute setValue(final AttributeDescriptor descriptor, final String id,
final Object value, final int index, final Attribute parent,
final AttributeType targetNodeType, boolean isXlinkRef) {
Object convertedValue;
Map <Object, Object> simpleContentProperties = null;
if (isFeatureChainedSimpleContent(descriptor, value)) {
// get the simple content attribute from the value
Collection<Property> simpleContentList = extractSimpleContent(value);
convertedValue = simpleContentList;
// and also get the client properties from the feature that wraps the simple content
// and it will be merged with the created attribute's client properties later
if (!simpleContentList.isEmpty()) {
simpleContentProperties = simpleContentList.iterator().next().getUserData();
}
} else {
// adapt value to context
convertedValue = convertValue(descriptor, value);
}
Attribute leafAttribute = null;
final Name attributeName = descriptor.getName();
if (!isXlinkRef) {
// skip this process if the attribute would only contain xlink:ref
// that is chained, because it won't contain any values, and we
// want to create a new empty leaf attribute
if (parent instanceof ComplexAttribute) {
Object currStepValue = ((ComplexAttribute) parent).getProperties(attributeName);
if (currStepValue instanceof Collection) {
List<Attribute> values = new ArrayList((Collection) currStepValue);
if (!values.isEmpty()) {
if (isEmpty(convertedValue)) {
// when attribute is empty, it is probably just a parent of a leaf
// attribute
// it could already exist from another attribute mapping for a different
// leaf
// e.g. 2 different attribute mappings:
// sa:relatedObservation/om:Observation/om:parameter[2]/swe:Time/swe:uom
// sa:relatedObservation/om:Observation/om:parameter[2]/swe:Time/swe:value
// and this could be processing om:parameter[2] the second time for
// swe:value
// so we need to find it if it already exists
if (index > -1) {
// get the attribute of specified index
int valueIndex = 1;
for (Attribute stepValue : values) {
Object mappedIndex = stepValue.getUserData().get(
ComplexFeatureConstants.MAPPED_ATTRIBUTE_INDEX);
if (mappedIndex == null) {
mappedIndex = valueIndex;
}
if (index == Integer.parseInt(String.valueOf(mappedIndex))) {
leafAttribute = stepValue;
}
valueIndex++;
}
} else {
// get the last existing node
leafAttribute = values.get(values.size() - 1);
}
} else {
for (Attribute stepValue : values) {
// eliminate duplicates in case the values come from denormalized
// view..
boolean sameIndex = true;
if (index > -1) {
if (stepValue.getUserData().containsKey(
ComplexFeatureConstants.MAPPED_ATTRIBUTE_INDEX)) {
sameIndex = (index == Integer.parseInt(
String.valueOf(stepValue.getUserData().get(
ComplexFeatureConstants.MAPPED_ATTRIBUTE_INDEX))));
}
}
if (sameIndex && stepValue.getValue().equals(convertedValue)) {
leafAttribute = stepValue;
}
}
}
}
} else if (currStepValue instanceof Attribute) {
leafAttribute = (Attribute) currStepValue;
} else if (currStepValue != null) {
throw new IllegalStateException("Unknown addressed object. Xpath:"
+ attributeName + ", addressed: " + currStepValue.getClass().getName()
+ " [" + currStepValue.toString() + "]");
}
}
}
if (leafAttribute == null) {
AttributeBuilder builder = new AttributeBuilder(featureFactory);
if (crs != null) {
builder.setCRS(crs);
}
builder.setDescriptor(parent.getDescriptor());
// check for mapped type override
builder.setType(parent.getType());
if (targetNodeType != null) {
if (parent.getType().getName().equals(XSSchema.ANYTYPE_TYPE.getName())) {
// special handling for casting any type since there's no attributes in its
// schema
leafAttribute = builder.addAnyTypeValue(convertedValue, targetNodeType,
descriptor, id);
} else {
leafAttribute = builder.add(id, convertedValue, attributeName, targetNodeType);
}
} else if (descriptor.getType().getName().equals(XSSchema.ANYTYPE_TYPE.getName())
&& (value == null || (value instanceof Collection && ((Collection) value)
.isEmpty()))) {
// casting anyType as a complex attribute so we can set xlink:href
leafAttribute = builder.addComplexAnyTypeAttribute(convertedValue, descriptor, id);
} else {
leafAttribute = builder.add(id, convertedValue, attributeName);
}
if (index > -1) {
// set attribute index if specified so it can be retrieved later for grouping
leafAttribute.getUserData().put(ComplexFeatureConstants.MAPPED_ATTRIBUTE_INDEX,
index);
}
List newValue = new ArrayList();
newValue.addAll((Collection) parent.getValue());
newValue.add(leafAttribute);
parent.setValue(newValue);
}
if (!isEmpty(convertedValue)) {
leafAttribute.setValue(convertedValue);
}
if (simpleContentProperties != null) {
mergeClientProperties(leafAttribute, simpleContentProperties);
}
return leafAttribute;
}
代碼來源:org.geotools/gt-app-schema
DataAccessMappingFeatureIterator.computeNext()
protected Feature computeNext() throws IOException {
String id = getNextFeatureId();
List<Feature> sources = getSources(id);
final Name targetNodeName = targetFeature.getName();
AttributeBuilder builder = new AttributeBuilder(attf);
builder.setDescriptor(targetFeature);
Feature target = (Feature) builder.build(id);
for (AttributeMapping attMapping : selectedMapping) {
try {
if (skipTopElement(targetNodeName, attMapping.getTargetXPath(), targetFeature.getType())) {
// ignore the top level mapping for the Feature itself
// as it was already set
continue;
}
if (attMapping.isList()) {
Attribute instance = setAttributeValue(target, null, sources.get(0),
attMapping, null, null, selectedProperties.get(attMapping));
if (sources.size() > 1 && instance != null) {
List<Object> values = new ArrayList<Object>();
Expression sourceExpr = attMapping.getSourceExpression();
for (Feature source : sources) {
values.add(getValue(sourceExpr, source));
}
String valueString = StringUtils.join(values.iterator(), " ");
StepList fullPath = attMapping.getTargetXPath();
StepList leafPath = fullPath.subList(fullPath.size() - 1, fullPath.size());
if (instance instanceof ComplexAttributeImpl) {
// xpath builder will work out the leaf attribute to set values on
xpathAttributeBuilder.set(instance, leafPath, valueString, null, null,
false, sourceExpr);
} else {
// simple attributes
instance.setValue(valueString);
}
}
} else if (attMapping.isMultiValued()) {
// extract the values from multiple source features of the same id
// and set them to one built feature
for (Feature source : sources) {
setAttributeValue(target, null, source, attMapping, null, null, selectedProperties.get(attMapping));
}
} else {
String indexString = attMapping.getSourceIndex();
// if not specified, get the first row by default
int index = 0;
if (indexString != null) {
if (ComplexFeatureConstants.LAST_INDEX.equals(indexString)) {
index = sources.size() - 1;
} else {
index = Integer.parseInt(indexString);
}
}
setAttributeValue(target, null, sources.get(index), attMapping, null, null, selectedProperties.get(attMapping));
// When a feature is not multi-valued but still has multiple rows with the same ID in
// a denormalised table, by default app-schema only takes the first row and ignores
// the rest (see above). The following line is to make sure that the cursors in the
// 'joining nested mappings'skip any extra rows that were linked to those rows that are being ignored.
// Otherwise the cursor will stay there in the wrong spot and none of the following feature chaining
// will work. That can really only occur if the foreign key is not unique for the ID of the parent
// feature (otherwise all of those rows would be already passed when creating the feature based on
// the first row). This never really occurs in practice I have noticed, but it is a theoretic
// possibility, as there is no requirement for the foreign key to be unique per id.
skipNestedMapping(attMapping, sources.subList(1, sources.size()));
}
} catch (Exception e) {
throw new RuntimeException("Error applying mapping with targetAttribute "
+ attMapping.getTargetXPath(), e);
}
}
cleanEmptyElements(target);
return target;
}
代碼來源:org.geotools/gt-app-schema
AbstractMappingStore.mapPropertiesToComplex(...)
/**
* Performs the common mappings, subclasses can override to add more
*
* @param builder
* @param fi
*/
protected void mapPropertiesToComplex(ComplexFeatureBuilder builder, SimpleFeature fi) {
AttributeBuilder ab = new AttributeBuilder(FEATURE_FACTORY);
for (PropertyDescriptor pd : schema.getDescriptors()) {
if (!(pd instanceof AttributeDescriptor)) {
continue;
}
String localName = (String) pd.getUserData().get(JDBCOpenSearchAccess.SOURCE_ATTRIBUTE);
if (localName == null) {
continue;
}
Object value = fi.getAttribute(localName);
if (value == null) {
continue;
}
ab.setDescriptor((AttributeDescriptor) pd);
Attribute attribute = ab.buildSimple(null, value);
builder.append(pd.getName(), attribute);
}
// handle joined metadata
Object metadataValue = fi.getAttribute("metadata");
if (metadataValue instanceof SimpleFeature) {
SimpleFeature metadataFeature = (SimpleFeature) metadataValue;
ab.setDescriptor((AttributeDescriptor) schema.getDescriptor(METADATA_PROPERTY_NAME));
Attribute attribute = ab.buildSimple(null, metadataFeature.getAttribute("metadata"));
builder.append(METADATA_PROPERTY_NAME, attribute);
}
// handle joined layer if any
Object layerValue = fi.getAttribute(OpenSearchAccess.LAYER);
if (layerValue instanceof SimpleFeature) {
SimpleFeature layerFeature = (SimpleFeature) layerValue;
SimpleFeature retyped = retypeLayerFeature(layerFeature);
ab.setDescriptor((AttributeDescriptor) schema.getDescriptor(LAYER_PROPERTY_NAME));
final Collection<Property> properties = retyped.getProperties();
Attribute attribute = ab.buildSimple(retyped.getID(), properties);
builder.append(LAYER_PROPERTY_NAME, attribute);
}
}
代碼來源:org.geoserver.community/gs-oseo-core
JDBCProductFeatureStore.mapPropertiesToComplex(...)
@Override
protected void mapPropertiesToComplex(ComplexFeatureBuilder builder, SimpleFeature fi) {
// basic mappings
super.mapPropertiesToComplex(builder, fi);
// quicklook extraction
Object metadataValue = fi.getAttribute("quicklook");
if (metadataValue instanceof SimpleFeature) {
SimpleFeature quicklookFeature = (SimpleFeature) metadataValue;
AttributeBuilder ab = new AttributeBuilder(CommonFactoryFinder.getFeatureFactory(null));
ab.setDescriptor(
(AttributeDescriptor)
schema.getDescriptor(OpenSearchAccess.QUICKLOOK_PROPERTY_NAME));
Attribute attribute = ab.buildSimple(null, quicklookFeature.getAttribute("thumb"));
builder.append(OpenSearchAccess.QUICKLOOK_PROPERTY_NAME, attribute);
}
}
代碼來源:org.geoserver.community/gs-oseo-core
XmlMappingFeatureIterator.populateFeatureData()
protected Feature populateFeatureData() throws IOException {
final AttributeDescriptor targetNode = mapping.getTargetFeature();
AttributeBuilder builder = new AttributeBuilder(attf);
builder.setDescriptor(targetNode);
Feature target = (Feature) builder.build(extractIdForAttribute(mapping
.getFeatureIdExpression(), null));
if (attOrderedTypeList == null) {
initialiseAttributeLists(mapping.getAttributeMappings());
}
// create required elements
PathAttributeList elements = populateAttributeList(target);
setAttributeValues(elements, target);
indexCounter++;
return target;
}
代碼來源:org.geotools/gt-app-schema
AbstractOpenSearchController.simpleToComplex(...)
/**
* Converts the simple feature representatin of a collection into a complex feature suitable for
* OpenSearchAccess usage
*
* @param feature
* @return
* @throws IOException
*/
protected Feature simpleToComplex(
SimpleFeature feature, FeatureType targetSch, Collection<String> ignoredAttributes)
throws IOException {
ComplexFeatureBuilder builder = new ComplexFeatureBuilder(targetSch);
AttributeBuilder ab = new AttributeBuilder(FEATURE_FACTORY);
for (AttributeDescriptor ad : feature.getType().getAttributeDescriptors()) {
String sourceName = ad.getLocalName();
// ignore links
if (ignoredAttributes.contains(sourceName)) {
continue;
}
// map to complex feature attribute and check
Name pname = toName(sourceName, targetSch.getName().getNamespaceURI());
PropertyDescriptor pd = targetSch.getDescriptor(pname);
if (pd == null) {
throw new RestException(
"Unexpected attribute found: '" + sourceName + "'", HttpStatus.BAD_REQUEST);
}
ab.setDescriptor((AttributeDescriptor) pd);
Object originalValue = feature.getAttribute(sourceName);
Object converted = convert(originalValue, pd.getType().getBinding());
Attribute attribute = ab.buildSimple(null, converted);
builder.append(pd.getName(), attribute);
}
Feature collectionFeature = builder.buildFeature(feature.getID());
return collectionFeature;
}
代碼來源:org.geoserver.community/gs-oseo-rest
相關(guān)方法:
org.geotools.feature.AttributeBuilder.<init>
org.geotools.feature.AttributeBuilder.build
org.geotools.feature.AttributeBuilder.add
org.geotools.feature.AttributeBuilder.setType
org.geotools.feature.AttributeBuilder.buildSimple
org.geotools.feature.AttributeBuilder.createComplexAttribute
org.geotools.feature.AttributeBuilder.init
org.geotools.feature.AttributeBuilder.associate
org.geotools.feature.AttributeBuilder.associationDescriptor
org.geotools.feature.AttributeBuilder.attributeDescriptor
org.geotools.feature.AttributeBuilder.create
org.geotools.feature.AttributeBuilder.getCRS
org.geotools.feature.AttributeBuilder.create
org.geotools.feature.AttributeBuilder.getCRS
org.geotools.feature.AttributeBuilder.properties
org.geotools.feature.AttributeBuilder.addAnyTypeValue
org.geotools.feature.AttributeBuilder.addComplexAnyTypeAttribute
org.geotools.feature.AttributeBuilder.getDescriptor
org.geotools.feature.AttributeBuilder.getProperties
org.geotools.feature.AttributeBuilder.getType
org.geotools.feature.AttributeBuilder.setCRS
本文內(nèi)容根據(jù)網(wǎng)絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。