⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 geoserverfeaturesource.java

📁 电子地图服务器,搭建自己的地图服务
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* Copyright (c) 2001 - 2007 TOPP - www.openplans.org.  All rights reserved.
 * This code is licensed under the GPL 2.0 license, available at the root
 * application directory.
 */
package org.vfny.geoserver.global;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;

import org.geotools.data.DataSourceException;
import org.geotools.data.DataStore;
import org.geotools.data.DefaultQuery;
import org.geotools.data.FeatureListener;
import org.geotools.data.FeatureLocking;
import org.geotools.data.FeatureSource;
import org.geotools.data.FeatureStore;
import org.geotools.data.Query;
import org.geotools.data.crs.ForceCoordinateSystemFeatureResults;
import org.geotools.data.crs.ReprojectFeatureResults;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureType;
import org.geotools.referencing.CRS;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import com.vividsolutions.jts.geom.Envelope;


/**
 * GeoServer wrapper for backend Geotools2 DataStore.
 *
 * <p>
 * Support FeatureSource decorator for FeatureTypeInfo that takes care of
 * mapping the FeatureTypeInfo's FeatureSource with the schema and definition
 * query configured for it.
 * </p>
 *
 * <p>
 * Because GeoServer requires that attributes always be returned in the same
 * order we need a way to smoothly inforce this. Could we use this class to do
 * so?
 * </p>
 *
 * @author Gabriel Rold�n
 * @version $Id: GeoServerFeatureSource.java 7746 2007-11-13 15:38:35Z aaime $
 */
public class GeoServerFeatureSource implements FeatureSource {
    /** Shared package logger */
    private static final Logger LOGGER = org.geotools.util.logging.Logging.getLogger("org.vfny.geoserver.global");

    /** FeatureSource being served up */
    protected FeatureSource source;

    /**
     * GeoTools2 Schema information
     *
     * <p>
     * Is this the same as source.getSchema() or is it used supply the order
     * that GeoServer requires attributes to be returned in?
     * </p>
     */
    private FeatureType schema;

    /** Used to constrain the Feature made available to GeoServer. */
    private Filter definitionQuery = Filter.INCLUDE;

    /** Geometries will be forced to this CRS (or null, if no forcing is needed) */
    private CoordinateReferenceSystem declaredCRS;

    /** How to handle SRS   */
    private int srsHandling;

    /**
     * Creates a new GeoServerFeatureSource object.
     *
     * @param source GeoTools2 FeatureSource
     * @param schema FeatureType returned by this FeatureSource
     * @param definitionQuery Filter used to limit results
     * @param declaredCRS Geometries will be forced or projected to this CRS
     */
    GeoServerFeatureSource(FeatureSource source, FeatureType schema, Filter definitionQuery,
        CoordinateReferenceSystem declaredCRS, int srsHandling) {
        this.source = source;
        this.schema = schema;
        this.definitionQuery = definitionQuery;
        this.declaredCRS = declaredCRS;
        this.srsHandling = srsHandling;

        if (this.definitionQuery == null) {
            this.definitionQuery = Filter.INCLUDE;
        }
    }

    /**
     * Factory that make the correct decorator for the provided featureSource.
     *
     * <p>
     * This factory method is public and will be used to create all required
     * subclasses. By comparison the constructors for this class have package
     * visibiliy.
     * </p>
     *
     * @param featureSource
     * @param schema DOCUMENT ME!
     * @param definitionQuery DOCUMENT ME!
     * @param declaredCRS 
     *
     * @return
     */
    public static GeoServerFeatureSource create(FeatureSource featureSource, FeatureType schema,
        Filter definitionQuery, CoordinateReferenceSystem declaredCRS, int srsHandling) {
        if (featureSource instanceof FeatureLocking) {
            return new GeoServerFeatureLocking((FeatureLocking) featureSource, schema,
                definitionQuery, declaredCRS, srsHandling);
        } else if (featureSource instanceof FeatureStore) {
            return new GeoServerFeatureStore((FeatureStore) featureSource, schema, definitionQuery,
                declaredCRS, srsHandling);
        }

        return new GeoServerFeatureSource(featureSource, schema, definitionQuery, declaredCRS, srsHandling);
    }

    /**
     * Takes a query and adapts it to match re definitionQuery filter
     * configured for a feature type.
     *
     * @param query Query against this DataStore
     *
     * @return Query restricted to the limits of definitionQuery
     *
     * @throws IOException See DataSourceException
     * @throws DataSourceException If query could not meet the restrictions of
     *         definitionQuery
     */
    protected Query makeDefinitionQuery(Query query) throws IOException {
        if ((query == Query.ALL) || query.equals(Query.ALL)) {
            return query;
        }

        try {
            String[] propNames = extractAllowedAttributes(query);
            Filter filter = query.getFilter();
            filter = makeDefinitionFilter(filter);

            DefaultQuery defQuery = new DefaultQuery(query);
            defQuery.setFilter(filter);
            defQuery.setPropertyNames(propNames);

            //set sort by
            if (query.getSortBy() != null) {
                defQuery.setSortBy(query.getSortBy());
            }

            return defQuery;
        } catch (Exception ex) {
            throw new DataSourceException(
                "Could not restrict the query to the definition criteria: " + ex.getMessage(), ex);
        }
    }

    /**
     * List of allowed attributes.
     *
     * <p>
     * Creates a list of FeatureTypeInfo's attribute names based on the
     * attributes requested by <code>query</code> and making sure they not
     * contain any non exposed attribute.
     * </p>
     *
     * <p>
     * Exposed attributes are those configured in the "attributes" element of
     * the FeatureTypeInfo's configuration
     * </p>
     *
     * @param query User's origional query
     *
     * @return List of allowed attribute types
     */
    private String[] extractAllowedAttributes(Query query) {
        String[] propNames = null;

        if (query.retrieveAllProperties()) {
            propNames = new String[schema.getAttributeCount()];

            for (int i = 0; i < schema.getAttributeCount(); i++) {
                propNames[i] = schema.getAttributeType(i).getName();
            }
        } else {
            String[] queriedAtts = query.getPropertyNames();
            int queriedAttCount = queriedAtts.length;
            List allowedAtts = new LinkedList();

            for (int i = 0; i < queriedAttCount; i++) {
                if (schema.getAttributeType(queriedAtts[i]) != null) {
                    allowedAtts.add(queriedAtts[i]);
                } else {
                    LOGGER.info("queried a not allowed property: " + queriedAtts[i]
                        + ". Ommitting it from query");
                }
            }

            propNames = (String[]) allowedAtts.toArray(new String[allowedAtts.size()]);
        }

        return propNames;
    }

    /**
     * If a definition query has been configured for the FeatureTypeInfo, makes
     * and return a new Filter that contains both the query's filter and the
     * layer's definition one, by logic AND'ing them.
     *
     * @param filter Origional user supplied Filter
     *
     * @return Filter adjusted to the limitations of definitionQuery
     *
     * @throws DataSourceException If the filter could not meet the limitations
     *         of definitionQuery
     */
    protected Filter makeDefinitionFilter(Filter filter)
        throws DataSourceException {
        Filter newFilter = filter;

        try {
            if (definitionQuery != Filter.INCLUDE) {
                FilterFactory ff = CommonFactoryFinder.getFilterFactory(null);
                newFilter = ff.and(definitionQuery, filter);
            }
        } catch (Exception ex) {
            throw new DataSourceException("Can't create the definition filter", ex);
        }

        return newFilter;
    }

    /**
     * Implement getDataStore.
     *
     * <p>
     * Description ...
     * </p>
     *
     * @return

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -