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

📄 typeseditoraction.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, availible at the root
 * application directory.
 */
package org.vfny.geoserver.action.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.geotools.feature.AttributeType;
import org.geotools.feature.FeatureType;
import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.NoSuchAuthorityCodeException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;
import org.vfny.geoserver.action.ConfigAction;
import org.vfny.geoserver.action.HTMLEncoder;
import org.vfny.geoserver.config.AttributeTypeInfoConfig;
import org.vfny.geoserver.config.ConfigRequests;
import org.vfny.geoserver.config.DataConfig;
import org.vfny.geoserver.config.DataStoreConfig;
import org.vfny.geoserver.config.FeatureTypeConfig;
import org.vfny.geoserver.form.data.AttributeForm;
import org.vfny.geoserver.form.data.TypesEditorForm;
import org.vfny.geoserver.global.MetaDataLink;
import org.vfny.geoserver.global.UserContainer;
import org.vfny.geoserver.util.DataStoreUtils;

import com.vividsolutions.jts.geom.Envelope;


/**
 * These Action handles all the buttons for the FeatureType Editor.
 *
 * <p>
 * This one is more complicated then usual since not all the actions require
 * the form bean to be validated! I am going to have to hack a little bit to
 * make that happen, I may end up making the form bean validation differ
 * depending on the selected action.
 * </p>
 *
 * <p>
 * Buttons that make this action go:
 *
 * <ul>
 * <li>
 * Submit: update the FeatureTypeConfig held by the user, punt it back into
 * DataConfig and return to the FeatureTypeSelect screen.
 * </li>
 * <li>
 * Up and Down (for each attribute): not quite sure how to make these work yet
 * - I hope I dont have to give them different names.
 * </li>
 * </ul>
 *
 * As usual we will have to uninternationlize the action name provided to us.
 * </p>
 *
 * @author Richard Gould
 * @author Jody Garnett
 */
public class TypesEditorAction extends ConfigAction {
    public ActionForward execute(ActionMapping mapping, ActionForm form, UserContainer user,
        HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
        if (LOGGER.isLoggable(Level.FINER)) {
            LOGGER.finer(new StringBuffer("form bean:").append(form.getClass().getName()).toString());
        }

        TypesEditorForm typeForm = (TypesEditorForm) form;

        String action = typeForm.getAction();

        if (LOGGER.isLoggable(Level.FINER)) {
            LOGGER.finer(new StringBuffer("TypesEditorAction is ").append(action).toString());
        }

        Locale locale = (Locale) request.getLocale();
        MessageResources messages = getResources(request);
        final String SUBMIT = HTMLEncoder.decode(messages.getMessage(locale, "label.submit"));
        final String ADD = HTMLEncoder.decode(messages.getMessage(locale, "label.add"));
        final String BBOX = HTMLEncoder.decode(messages.getMessage(locale,
                    "config.data.calculateBoundingBox.label"));
        final String LOOKUP_SRS = HTMLEncoder.decode(messages.getMessage(locale,
                    "config.data.lookupSRS.label"));

        if (LOGGER.isLoggable(Level.FINER)) {
            LOGGER.finer(new StringBuffer("BBOX: ").append(BBOX).toString());
        }

        final String NEWSLD = HTMLEncoder.decode(messages.getMessage(locale,
                    "config.data.sldWizard.label"));

        if (typeForm.getAutoGenerateExtent().equals("true")) {
            if ((typeForm.getSRS() == null) || typeForm.getSRS().trim().equals("0")) {
                executeLookupSRS(mapping, typeForm, user, request);
            }

            executeBBox(mapping, typeForm, user, request);

            return executeSubmit(mapping, typeForm, user, request);
        }

        if (SUBMIT.equals(action)) {
            return executeSubmit(mapping, typeForm, user, request);
        }

        if (action.equals(BBOX)) {
            return executeBBox(mapping, typeForm, user, request);
        }

        if (action.equals(LOOKUP_SRS)) {
            return executeLookupSRS(mapping, typeForm, user, request);
        }

        if (action.equals(NEWSLD)) { // if the SLDWizard button was hit

            return mapping.findForward("SLDWizard");
        }

        List attributes = typeForm.getAttributes();

        if (action.startsWith("up_")) {
            int index = Integer.parseInt(action.substring(3));
            Object attribute = attributes.remove(index);
            attributes.add(index - 1, attribute);
        } else if (action.startsWith("down_")) {
            int index = Integer.parseInt(action.substring(5));
            Object attribute = attributes.remove(index);
            attributes.add(index + 1, attribute);
        } else if (action.startsWith("delete_")) {
            int index = Integer.parseInt(action.substring(7));
            attributes.remove(index);
        } else if (action.equals(ADD)) {
            executeAdd(mapping, typeForm, user, request);
        }

        // Update, Up, Down, Add, Remove need to resync
        sync(typeForm, user.getFeatureTypeConfig(), request);
        form.reset(mapping, request);

        return mapping.findForward("config.data.type.editor");
    }

    private ActionForward executeLookupSRS(ActionMapping mapping, TypesEditorForm typeForm,
        UserContainer user, HttpServletRequest request)
        throws IOException, ServletException {
        DataConfig dataConfig = getDataConfig();
        DataStoreConfig dsConfig = dataConfig.getDataStore(typeForm.getDataStoreId());
        DataStore dataStore = null;
        try {
            dsConfig.findDataStore(request.getSession().getServletContext());
            FeatureType featureType = dataStore.getSchema(typeForm.getTypeName());
            FeatureSource fs = dataStore.getFeatureSource(featureType.getTypeName());

            CoordinateReferenceSystem crs = fs.getSchema().getDefaultGeometry().getCoordinateSystem();
            String s = CRS.lookupIdentifier(crs, true);

            if (s == null) {
                typeForm.setSRS("UNKNOWN");
            } else if (s.indexOf(':') != -1) {
                typeForm.setSRS(s.substring(s.indexOf(':') + 1));
            } else {
                typeForm.setSRS(s);
            }
        } catch (Exception e) {
            typeForm.setSRS("UNKNOWN");
        } finally {
            if(dataStore != null) dataStore.dispose();
        }

        return mapping.findForward("config.data.type.editor");
    }

    /**
     * Populate the bounding box fields from the source and pass control back
     * to the UI
     *
     * @param mapping DOCUMENT ME!
     * @param typeForm DOCUMENT ME!
     * @param user DOCUMENT ME!
     * @param request DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     *
     * @throws IOException DOCUMENT ME!
     * @throws ServletException DOCUMENT ME!
     */
    private ActionForward executeBBox(ActionMapping mapping, TypesEditorForm typeForm,
        UserContainer user, HttpServletRequest request)
        throws IOException, ServletException {
        DataConfig dataConfig = getDataConfig();
        DataStoreConfig dsConfig = dataConfig.getDataStore(typeForm.getDataStoreId());
        DataStore dataStore = null;
        try {
            dataStore = dsConfig.findDataStore(request.getSession().getServletContext());
            FeatureType featureType = dataStore.getSchema(typeForm.getTypeName());
            FeatureSource fs = dataStore.getFeatureSource(featureType.getTypeName());
    
            if (LOGGER.isLoggable(Level.FINE)) {
                LOGGER.fine(new StringBuffer("calculating bbox for their dataset").toString());
            }
    
            Envelope envelope = DataStoreUtils.getBoundingBoxEnvelope(fs);
    
            if (envelope.isNull()) // there's no data in the featuretype!!
             {
                if (LOGGER.isLoggable(Level.FINE)) {
                    LOGGER.fine(new StringBuffer("FeatureType '").append(featureType.getTypeName())
                                                                 .append("' has a null bounding box")
                                                                 .toString());
                }
    
                ActionErrors errors = new ActionErrors();
                errors.add(ActionErrors.GLOBAL_ERROR,
                    new ActionError("error.data.nullBBOX", featureType.getTypeName()));
                saveErrors(request, errors);
    
                return mapping.findForward("config.data.type.editor");
            }
    
            // do a translation from the data's coordinate system to lat/long
    
            //TODO: DJB: NOTE: 1/2 of the config stuff has the srs as an int, 1/2 as string!!  We should be more consistent!
            String srs = typeForm.getSRS(); // what the user typed in for the srs in the form
    
            if (srs.indexOf(':') == -1) { // check to see if its of the form "EPSG:#" (or some such thing)
                srs = "EPSG:" + srs; //assume they wanted to use an EPSG number
            }

            CoordinateReferenceSystem crsDeclared = CRS.decode(srs);
            CoordinateReferenceSystem original = null;

            if (featureType.getDefaultGeometry() != null) {
                original = featureType.getDefaultGeometry().getCoordinateSystem();
            }

            if (original == null) {
                original = crsDeclared;
            }

            CoordinateReferenceSystem crsLatLong = CRS.decode("EPSG:4326"); // latlong

            // let's show coordinates in the declared crs, not in the native one, to
            // avoid confusion (since on screen we do have the declared one, the native is
            // not visible)
            Envelope declaredEnvelope = envelope;

            if (!CRS.equalsIgnoreMetadata(original, crsDeclared)) {
                MathTransform xform = CRS.findMathTransform(original, crsDeclared, true);
                declaredEnvelope = JTS.transform(envelope, null, xform, 10); //convert data bbox to lat/long
            }

            LOGGER.finer("Seeting form's data envelope: " + declaredEnvelope);
            typeForm.setDataMinX(Double.toString(declaredEnvelope.getMinX()));
            typeForm.setDataMaxX(Double.toString(declaredEnvelope.getMaxX()));
            typeForm.setDataMinY(Double.toString(declaredEnvelope.getMinY()));
            typeForm.setDataMaxY(Double.toString(declaredEnvelope.getMaxY()));

            MathTransform xform = CRS.findMathTransform(original, crsLatLong, true);
            Envelope xformed_envelope = JTS.transform(envelope, xform); //convert data bbox to lat/long

            typeForm.setMinX(Double.toString(xformed_envelope.getMinX()));
            typeForm.setMaxX(Double.toString(xformed_envelope.getMaxX()));
            typeForm.setMinY(Double.toString(xformed_envelope.getMinY()));
            typeForm.setMaxY(Double.toString(xformed_envelope.getMaxY()));
        } catch (NoSuchAuthorityCodeException e) {
            if (LOGGER.isLoggable(Level.FINE)) {
                LOGGER.fine(e.getLocalizedMessage());
                LOGGER.fine(e.getStackTrace().toString());
            }

            ActionErrors errors = new ActionErrors();
            errors.add(ActionErrors.GLOBAL_ERROR,
                new ActionError("error.data.couldNotFindSRSAuthority", e.getLocalizedMessage(),

⌨️ 快捷键说明

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