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

📄 getmapkvpreader.java

📁 电子地图服务器,搭建自己的地图服务
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
	public void parseMandatoryParameters(GetMapRequest request,
			boolean parseStylesLayers) throws WmsException {
		try {
			int width = Integer.parseInt(getValue("WIDTH"));
			int height = Integer.parseInt(getValue("HEIGHT"));
			request.setWidth(width);
			request.setHeight(height);
		} catch (NumberFormatException ex) {
			throw new WmsException("WIDTH and HEIGHT incorrectly specified");
		}

		String format = getValue("FORMAT");

		if (format == null) {
			throw new WmsException("parameter FORMAT is required");
		}

		request.setFormat(format);

		Envelope bbox = parseBbox(getValue("BBOX"));
		request.setBbox(bbox);

		// let styles and layers parsing for the end to give more trivial
		// parameters
		// a chance to fail before incurring in retrieving the SLD or SLD_BODY
		if (parseStylesLayers) {
			parseLayersAndStyles(request);
		}
	}

	protected Envelope parseBbox(String bboxParam) throws WmsException {
		// overridden to throw the right exception for this context
		try {
			return super.parseBbox(bboxParam);
		} catch (ServiceException e) {
			throw new WmsException(e);
		}
	}

	/**
	 * creates a list of requested attributes, wich must be a valid attribute
	 * name or one of the following special attributes:
	 * 
	 * <ul>
	 * <li> <b>#FID</b>: a map producer capable of handling attributes (such as
	 * SVGMapResponse), will write the feature id of each feature </li>
	 * <li> <b>#BOUNDS</b>: a map producer capable of handling attributes (such
	 * as SVGMapResponse), will write the bounding box of each feature </li>
	 * </ul>
	 * 
	 * 
	 * @param layers
	 *            info about the requested map layers
	 * 
	 * @return an empty list if no attributes was requested, or a
	 *         <code>List&lt;List&lt;String&gt;&gt;</code> with an entry for
	 *         each requested layer, where each of them consists of a List of
	 *         the attribute names requested
	 * 
	 * @throws WmsException
	 *             if:
	 *             <ul>
	 *             <li>the number of attribute sets requested is not equal to
	 *             the number of layers requested.</li>
	 *             <li>an illegal attribute name was requested</li>
	 *             <li>an IOException occurs while fetching a FeatureType
	 *             schema to ask it for propper attribute names</li>
	 *             </ul>
	 */
	private List parseAttributes(FeatureTypeInfo[] layers) throws WmsException {
		String rawAtts = getValue("ATTRIBUTES");

		if (LOGGER.isLoggable(Level.FINER)) {
			LOGGER.finer(new StringBuffer("parsing attributes ")
					.append(rawAtts).toString());
		}

		if ((rawAtts == null) || "".equals(rawAtts)) {
			return Collections.EMPTY_LIST;
		}

		// raw list of attributes for each feature type requested
		List byFeatureTypes = readFlat(rawAtts, "|");
		int nLayers = layers.length;

		if (byFeatureTypes.size() != nLayers) {
			throw new WmsException(byFeatureTypes.size()
					+ " lists of attributes specified, expected "
					+ layers.length, getClass().getName()
					+ "::parseAttributes()");
		}

		// fill byFeatureTypes with the split of its raw attributes requested
		// separated by commas, and check for the validity of each att name
		FeatureType schema;
		List atts;
		String attName;

		for (int i = 0; i < nLayers; i++) {
			rawAtts = (String) byFeatureTypes.get(i);

			atts = readFlat(rawAtts, ",");
			byFeatureTypes.set(i, atts);

			// FeatureType schema = layers[i].getSchema();
			try {
				schema = layers[i].getFeatureType();

				// verify that propper attributes has been requested
				for (Iterator attIt = atts.iterator(); attIt.hasNext();) {
					attName = (String) attIt.next();

					if (attName.length() > 0) {
						if (LOGGER.isLoggable(Level.FINER)) {
							LOGGER.finer(new StringBuffer("checking that ")
									.append(attName).append(" is valid")
									.toString());
						}

						if ("#FID".equalsIgnoreCase(attName)
								|| "#BOUNDS".equalsIgnoreCase(attName)) {
							if (LOGGER.isLoggable(Level.FINER)) {
								LOGGER.finer(new StringBuffer(
										"special attribute name requested: ")
										.append(attName).toString());
							}

							continue;
						}

						if (schema.getAttributeType(attName) == null) {
							throw new WmsException("Attribute '" + attName
									+ "' requested for layer "
									+ schema.getTypeName() + " does not exists");
						}
					} else {
						if (LOGGER.isLoggable(Level.FINEST)) {
							LOGGER
									.finest("removing empty attribute name from request");
						}

						attIt.remove();
					}
				}

				if (LOGGER.isLoggable(Level.FINEST)) {
					LOGGER.finest(new StringBuffer("attributes requested for ")
							.append(schema.getTypeName()).append(" checked: ")
							.append(rawAtts).toString());
				}
			} catch (java.io.IOException e) {
				throw new WmsException(e);
			}
		}

		return byFeatureTypes;
	}

	/**
	 * Parses the list of style names requested for each requested layer and
	 * looks up the actual Style objects, which are returned in an ordered list.
	 * 
	 * <p>
	 * A client _may_ request teh default Style using a null value (as in
	 * "STYLES="). If several layers are requested with a mixture of named and
	 * default styles, the STYLES parameter includes null values between commas
	 * (as in "STYLES=style1,,style2,,"). If all layers are to be shown using
	 * the default style, either the form "STYLES=" or "STYLES=,,," is valid.
	 * </p>
	 * 
	 * @param request
	 *            DOCUMENT ME!
	 * @param layers
	 *            the requested feature types
	 * 
	 * @return a full <code>List</code> of the style names requested for the
	 *         requiered layers with no null style names.
	 * 
	 * @throws WmsException
	 *             if some of the requested styles does not exist or its number
	 *             if greater than zero and distinct of the number of requested
	 *             layers
	 */
	protected List parseStylesParam(GetMapRequest request, MapLayerInfo[] layers)
			throws WmsException {
		String rawStyles = getValue("STYLES");
		List styles = new ArrayList(layers.length);

		int numLayers = layers.length;

		if ("".equals(rawStyles)) {
			if (LOGGER.isLoggable(Level.FINER)) {
				LOGGER
						.finer("Assigning default style to all the requested layers");
			}

			for (int i = 0; i < numLayers; i++)
				if (layers[i].getType() == MapLayerInfo.TYPE_VECTOR) {
					styles.add(layers[i].getFeature().getDefaultStyle());
				} else if (layers[i].getType() == MapLayerInfo.TYPE_RASTER) {
					styles.add(layers[i].getCoverage().getDefaultStyle());
				}
		} else {
			List styleNames = readFlat(rawStyles, INNER_DELIMETER);

			if (numLayers != styleNames.size()) {
				String msg = numLayers
						+ " layers requested, but found "
						+ styleNames.size()
						+ " styles specified. "
						+ "Since SLD parameter is not yet implemented, the STYLES parameter "
						+ "is mandatory and MUST have exactly one value per requested layer";
				throw new WmsException(msg, getClass().getName());
			}

			String currStyleName;
			Style currStyle;
			MapLayerInfo currLayer;

			for (int i = 0; i < numLayers; i++) {
				currStyleName = (String) styleNames.get(i);
				currLayer = layers[i];

				if (currLayer.getType() == MapLayerInfo.TYPE_VECTOR) {
					if ((null == currStyleName) || "".equals(currStyleName)) {
						currStyle = currLayer.getFeature().getDefaultStyle();
					} else {
						currStyle = findStyle(request, currStyleName);

						if (currStyle == null) {
							String msg = "No default style has been defined for "
									+ currLayer.getName();
							throw new WmsException(msg, "StyleNotDefined");
						}
					}

					try {
						checkStyle(currStyle, layers[i].getFeature()
								.getFeatureType());
					} catch (IOException e) {
						throw new WmsException(
								"Error obtaining FeatureType for layer "
										+ layers[i].getName());
					}

					if (LOGGER.isLoggable(Level.FINE)) {
						LOGGER.fine(new StringBuffer("establishing ").append(
								currStyleName).append(" style for ").append(
								layers[i].getName()).toString());
					}

					styles.add(currStyle);
				} else if (currLayer.getType() == MapLayerInfo.TYPE_RASTER) {
					if ((null == currStyleName) || "".equals(currStyleName)) {
						currStyle = currLayer.getCoverage().getDefaultStyle();
					} else {
						currStyle = findStyle(request, currStyleName);

						if (currStyle == null) {
							String msg = "No default style has been defined for "
									+ currLayer.getName();
							throw new WmsException(msg,
									"GetMapKvpReader::parseStyles()");
						}
					}

					/**
					 * @task TODO: Check for Style Coverage Compatibility ...
					 */
					styles.add(currStyle);
				}
			}
		}

		return styles;
	}

	/**
	 * DOCUMENT ME!
	 * 
	 * @param request
	 * @param currStyleName
	 * 
	 * @return the configured style named <code>currStyleName</code> or
	 *         <code>null</code> if such a style does not exists on this
	 *         server.
	 */
	public static Style findStyle(GetMapRequest request, String currStyleName) {
		Style currStyle;
		Map configuredStyles = request.getWMS().getData().getStyles();

		currStyle = (Style) configuredStyles.get(currStyleName);

		return currStyle;
	}

	/**
	 * Method to initialize a user layer which contains inline features.
	 * 
	 * @param request
	 *            The request
	 * @param mapLayer
	 *            The map layer.
	 * 
	 * @throws Exception
	 */

	// JD: the reason this method is static is to share logic among the xml
	// and kvp reader, ugh...
	public static void initializeInlineFeatureLayer(
			GetMapRequest getMapRequest, UserLayer ul, MapLayerInfo currLayer)
			throws Exception {
		// SPECIAL CASE - we make the temporary version
		currLayer.setFeature(new TemporaryFeatureTypeInfo(ul
				.getInlineFeatureDatastore()));

		// what if they didn't put an "srsName" on their geometry in their
		// inlinefeature?
		// I guess we should assume they mean their geometry to exist in the
		// output SRS of the
		// request they're making.
		if (ul.getInlineFeatureType().getDefaultGeometry()
				.getCoordinateSystem() == null) {
			LOGGER
					.warning("No CRS set on inline features default geometry.  Assuming the requestor has their inlinefeatures in the boundingbox CRS.");

			FeatureType currFt = ul.getInlineFeatureType();
			Query q = new DefaultQuery(currFt.getTypeName(), Filter.INCLUDE);
			FeatureReader ilReader = ul.getInlineFeatureDatastore()
					.getFeatureReader(q, Transaction.AUTO_COMMIT);
			CoordinateReferenceSystem crs = (getMapRequest.getCrs() == null) ? DefaultGeographicCRS.WGS84
					: getMapRequest.getCrs();
			MemoryDataStore reTypedDS = new MemoryDataStore(
					new ForceCoordinateSystemFeatureReader(ilReader, crs));
			currLayer.setFeature(new TemporaryFeatureTypeInfo(reTypedDS));
		}
	}

	/**
	 * Checks to make sure that the style passed in can process the FeatureType.
	 * 
	 * @param style
	 *            The style to check
	 * @param fType
	 *            The source requested.
	 * 
	 * @throws WmsException
	 *             DOCUMENT ME!
	 */
	private void checkStyle(Style style, FeatureType fType) throws WmsException {
		StyleAttributeExtractor sae = new StyleAttributeExtractor();
		sae.visit(style);

		String[] styleAttributes = sae.getAttributeNames();
		String attName;
		final int length = styleAttributes.length;

		for (int i = 0; i < length; i++) {
			attName = styleAttributes[i];

			if (fType.getAttributeType(attName) == null) {
				throw new WmsException(
						"The requested Style can not be used with "
								+ "this featureType.  The style specifies an attribute of "
								+ attName
								+ " and the featureType definition is: "
								+ fType);
			}
		}
	}

	/**
	 * DOCUMENT ME!
	 * 
	 * @param request
	 *            DOCUMENT ME!
	 * 
	 * @throws WmsException
	 *             DOCUMENT ME!
	 */
	protected void parseLayersAndStyles(GetMapRequest request)
			throws WmsException {
		String sldParam = getValue("SLD");
		String sldBodyParam = getValue("SLD_BODY");

		if (sldBodyParam != null) {
			if (LOGGER.isLoggable(Level.FINE)) {
				LOGGER.fine("Getting layers and styles from SLD_BODY");
			}

			parseSldBodyParam(request);
		} else if (sldParam != null) {
			if (LOGGER.isLoggable(Level.FINE)) {
				LOGGER.fine("Getting layers and styles from reomte SLD");
			}

			parseSldParam(request);
		} else {
			MapLayerInfo[] featureTypes = null;
			List styles = null;
			featureTypes = parseLayersParam(request);

			request.setLayers(featureTypes);

			if (isStylesRquired()) {
				styles = parseStylesParam(request, featureTypes);

				if (isStylesRquired()) {
					request.setStyles(styles);
				}
			}
		}
	}

	/**
	 * Takes the SLD_BODY parameter value and parses it to a geotools'
	 * <code>StyledLayerDescriptor</code>, then takes the layers and styles
	 * to use in the map composition from there.
	 * 
	 * @param request
	 *            DOCUMENT ME!
	 * 
	 * @throws WmsException
	 *             DOCUMENT ME!
	 */
	protected void parseSldBodyParam(GetMapRequest request) throws WmsException {
		final String sldBody = getValue("SLD_BODY");

		if (LOGGER.isLoggable(Level.FINE)) {
			LOGGER.fine(new StringBuffer("About to parse SLD body: ").append(
					sldBody).toString());
		}

		if (getValue("VALIDATESCHEMA") != null) {
			// Get a reader from the given string
			Reader reader = getReaderFromString(sldBody);

			// -InputStream in = new StringBufferInputStream(sldBody);
			// user requested to validate the schema.
			SLDValidator validator = new SLDValidator();
			List errors = null;

			// Create a sax input source from the reader
			InputSource in = new InputSource(reader);
			errors = validator.validateSLD(in, request.getHttpServletRequest()
					.getSession().getServletContext());

			if (errors.size() != 0) {
				reader = getReaderFromString(sldBody);
				throw new WmsException(SLDValidator.getErrorMessage(reader,
						errors));
			}

			// - errors = validator.validateSLD(in,
			// request.getHttpServletRequest().getSession().getServletContext());
			// - try{
			// - in.close();
			// - }
			// - catch(Exception e)
			// - {
			// - // do nothing

⌨️ 快捷键说明

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