Package org.geoserver.wfs.response

Source Code of org.geoserver.wfs.response.RemappingFeatureCollection$RemappingIterator

/* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
* This code is licensed under the GPL 2.0 license, availible at the root
* application directory.
*/
package org.geoserver.wfs.response;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.collection.DecoratingFeatureCollection;
import org.geotools.feature.collection.DelegateFeatureIterator;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeDescriptor;
import org.opengis.feature.type.GeometryDescriptor;
/**
* FeatureCollection that remaps attribute names using a given map.
*
* @author Mauro Bartolomeoli, mbarto@infosia.it
*
*/
public class RemappingFeatureCollection extends DecoratingFeatureCollection<SimpleFeatureType, SimpleFeature> {

    Map<String,String> attributesMapping;
   
    public RemappingFeatureCollection(FeatureCollection<SimpleFeatureType, SimpleFeature> delegate,Map<String,String> attributesMapping) {
        super(delegate);
        this.attributesMapping=attributesMapping;      
    }
   
    public SimpleFeatureType getSchema() {
        return remapSchema(delegate.getSchema());
    }
   
    /**
     * Builds an inverted version of the given map.
     * Inversion means that key->value becomes value->key
     * @param map
     * @return
     */
    static Map<String,String> invertMappings(Map<String,String> map) {
        Map<String,String> result=new HashMap<String,String>();
        for(String key:map.keySet())
            result.put(map.get(key),key);
        return result;
    }
   
    /**
     * Gets a new schema, built remapping attribute names via
     * the attributeMappings map.
     * @param schema
     * @return
     */
    private SimpleFeatureType remapSchema(SimpleFeatureType schema) {
        SimpleFeatureTypeBuilder builder=new SimpleFeatureTypeBuilder();
        builder.setName(schema.getName());
        for(AttributeDescriptor attDesc : schema.getAttributeDescriptors()) {
            if(attDesc instanceof GeometryDescriptor) {
                GeometryDescriptor geoDesc=(GeometryDescriptor)attDesc;
                builder.add(attributesMapping.get(attDesc.getLocalName()),attDesc.getType().getBinding(),geoDesc.getCoordinateReferenceSystem());           
            } else
                builder.add(attributesMapping.get(attDesc.getLocalName()),attDesc.getType().getBinding());
        }
        return builder.buildFeatureType();
    }

    public Iterator<SimpleFeature> iterator() {
        return new RemappingIterator(delegate.iterator(), attributesMapping,getSchema());
    }

    public void close(Iterator<SimpleFeature> iterator) {
        RemappingIterator remapping = (RemappingIterator) iterator;
        delegate.close(remapping.delegate);
    }

    public FeatureIterator<SimpleFeature> features() {
        return new DelegateFeatureIterator<SimpleFeature>(this, iterator());
    }

    public void close(FeatureIterator<SimpleFeature> iterator) {
        DelegateFeatureIterator<SimpleFeature> delegate = (DelegateFeatureIterator<SimpleFeature>) iterator;
        delegate.close();
    }

       
    /**
     * Remaps a SimpleFeature, using the given mappings (oldname -> mappedname).
     * The builder uses the mapped schema.
     *
     * @param source
     * @param attributeMappings
     * @param builder
     * @return
     */
    static SimpleFeature remap(SimpleFeature source, Map<String,String> attributeMappings,SimpleFeatureBuilder builder) {
        SimpleFeatureType target = builder.getFeatureType();
        for (int i = 0; i < target.getAttributeCount(); i++) {
            AttributeDescriptor attributeType = target.getDescriptor(i);
            Object value = null;
            String mappedName=attributeMappings.get(attributeType.getLocalName());
            if (source.getFeatureType().getDescriptor(mappedName) != null) {
                value = source.getAttribute(mappedName);
            }

            builder.add(value);
        }
       
        return builder.buildFeature(source.getIdentifier().getID());
    }

   
    public static class RemappingIterator implements Iterator<SimpleFeature> {
        Map<String,String> attributesMapping;
        Iterator<SimpleFeature> delegate;
        SimpleFeatureBuilder builder;

        public RemappingIterator(Iterator<SimpleFeature> delegate, Map attributesMapping,SimpleFeatureType schema) {
            this.delegate = delegate;
            this.attributesMapping = RemappingFeatureCollection.invertMappings(attributesMapping);
            this.builder = new SimpleFeatureBuilder(schema);
        }

        public boolean hasNext() {
            return delegate.hasNext();
        }

        public SimpleFeature next() {
            return RemappingFeatureCollection.remap(delegate.next(), attributesMapping,builder);
        }

        public void remove() {
            delegate.remove();
        }
    }

   


}
TOP

Related Classes of org.geoserver.wfs.response.RemappingFeatureCollection$RemappingIterator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.