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

📄 singlebandrasterelevationcalculator.java

📁 esri的ArcGIS Server超级学习模板程序(for java)
💻 JAVA
字号:
package com.esri.solutions.jitk.services.elevation;

import java.io.IOException;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.esri.arcgis.datasourcesraster.IRaster2;
import com.esri.arcgis.datasourcesraster.IRaster2Proxy;
import com.esri.arcgis.geodatabase.IFeature;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureClassProxy;
import com.esri.arcgis.geodatabase.IFeatureCursor;
import com.esri.arcgis.geodatabase.IRasterCatalog;
import com.esri.arcgis.geodatabase.IRasterDataset;
import com.esri.arcgis.geodatabase.ISpatialFilter;
import com.esri.arcgis.geodatabase.RasterCatalogItem;
import com.esri.arcgis.geodatabase.SpatialFilter;
import com.esri.arcgis.geodatabase.esriSpatialRelEnum;
import com.esri.arcgis.geometry.IPoint;
import com.esri.arcgis.geometry.ISpatialReference;
import com.esri.arcgis.geometry.ISpatialReferenceFactory2;
import com.esri.arcgis.geometry.SpatialReferenceEnvironment;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.server.IServerContext;
import com.esri.solutions.jitk.services.common.IServerContextFactory;
import com.esri.solutions.jitk.services.common.ServicesException;

/**
 * Implementation of {@link IElevationCalculator} that calculates
 * elevation from a Single Band Raster within a Raster Catalog.  This
 * implementation will perform a Spatial Search to find the Raster
 * that contains the specified map point.  Once the raster is found, then
 * the map point is converted to a row and column in the raster.  The
 * value at that row and column in the raster is returned as the elevation.
 * The elevation unit will be in METERS.
 * 
 * <p>
 * This implementation requires {@link IRasterCatalogFactory} and
 * {@link IServerContextFactory} instances.  These are needed to get
 * the Raster Catalog and to get a Server Context.  Not specifying these
 * instances via {@link #setRasterCatalogFactory(IRasterCatalogFactory)} and
 * {@link #setServerContextFactory(IServerContextFactory)} will result
 * in an {@link IllegalStateException} being thrown from {@link #calculateElevation(Point)}.
 * </p>
 *
 */
public class SingleBandRasterElevationCalculator implements
		IElevationCalculator {

	private static final Logger LOG = LogManager.getLogger(SingleBandRasterElevationCalculator.class);

	private IRasterCatalogFactory m_rcFactory;
	private IServerContextFactory m_ctxFactory;
	
	public Elevation calculateElevation(Point pt) throws ServicesException {
		
		if (m_rcFactory == null) {
			throw new IllegalStateException();
		}
		if (m_ctxFactory == null) {
			throw new IllegalStateException();
		}
		
		IServerContext ctx = null;
		try {
			ctx = m_ctxFactory.createServerContext();
			
			IPoint point = (IPoint) ctx.createObject(com.esri.arcgis.geometry.Point.getClsid());
			point.setX(pt.getX());
			point.setY(pt.getY());
			ISpatialReferenceFactory2 srFactory = (ISpatialReferenceFactory2) ctx.createObject(SpatialReferenceEnvironment.getClsid());
			
			ISpatialReference sr = srFactory.createSpatialReference(pt.getSrsId());
			
			point.setSpatialReferenceByRef(sr);
			
			IRasterCatalog rc = m_rcFactory.createRasterCatalog(ctx);
			
			if (rc != null) {
				// Convert Point to WGS 1984 (EPSG: 4326)
				convertPoint (point, rc);
				
				IRaster2 raster = findRaster(point, rc, ctx);
				
				if (raster != null) {
					int col = raster.toPixelColumn(point.getX());
					int row = raster.toPixelRow(point.getY());
				
					short elevationValue = (Short)raster.getPixelValue(0, col, row);
				
					Elevation elevation = Elevation.elevationAsMeters(elevationValue);
				
					return elevation;
				}
				
				throw new ServicesException ("NO_ELEVATION_DATA");
			}
			
			throw new ServicesException ("MISSING_RASTER_CATALOG");
			
		} catch (AutomationException e) {
			throw new ServicesException ("AGS_AUTOMATION_EXCEPTION", e);
		} catch (IOException e) {
			throw new ServicesException ("AGS_IO_EXCEPTION", e);
		} finally {
			if (ctx != null) {
				try {
					ctx.releaseContext();
				} catch (AutomationException e) {
					LOG.warn("Unable to release ServerContext", e);
				} catch (IOException e) {
					LOG.warn("Unable to release ServerContext", e);
				}
			}
		}
	}
	
	/**
	 * Sets the instance of {@link IRasterCatalogFactory} that is required
	 * for this object.
	 * 
	 * @param factory	Required to create the appropriate RasterCatalog, cannot
	 * 					be <code>null</code>.
	 * @throws NullPointerException		Thrown if the <code>factory</code>
	 * 									parameter is <code>null</code>.
	 */
	public void setRasterCatalogFactory (IRasterCatalogFactory factory) {
		if (factory == null) {
			throw new NullPointerException();
		}
		
		m_rcFactory = factory;
	}

	/**
	 * Sets the instance of {@link IServerContextFactory} that is required
	 * for this object.
	 * 
	 * @param ctxFactory	Required to create an {@link IServerContext}, cannot
	 * 						be <code>null</code>.
	 * @throws NullPointerException	Thrown if the <code>ctxFactory</code> parameter
	 * 								is <code>null</code>.
	 */
	public void setServerContextFactory (IServerContextFactory ctxFactory) {
		if (ctxFactory == null) {
			throw new NullPointerException ();
		}
		
		m_ctxFactory = ctxFactory;
	}
	
	private void convertPoint (IPoint point, IRasterCatalog rc) throws AutomationException, IOException {
		//ISpatialReferenceFactory2 srFactory = new ISpatialReferenceFactory2Proxy(
		//												m_ctx.createObject(
		//														SpatialReferenceEnvironment.getClsid()));
		//ISpatialReference srWGS1984 = new ISpatialReferenceProxy(
		//												srFactory.createGeographicCoordinateSystem(
		//														esriSRGeoCSType.esriSRGeoCS_WGS1984));

		point.project(rc.getRasterSpatialReference());
	}
	
	private IRaster2 findRaster (IPoint point, IRasterCatalog rc, IServerContext ctx) throws AutomationException, IOException {
		IRaster2 raster = null;
		ISpatialFilter filter = (ISpatialFilter) ctx.createObject(SpatialFilter.getClsid());
		IFeatureClass fClass = new IFeatureClassProxy(rc);
		
		filter.setGeometryByRef(point);
		filter.setSpatialRel(esriSpatialRelEnum.esriSpatialRelIntersects);
		filter.setGeometryField("Shape");
		
		IFeatureCursor cursor = fClass.search(filter, false);
		IFeature tempFeature = null;
		tempFeature = cursor.nextFeature();
		if (tempFeature != null) {
			RasterCatalogItem rci = (RasterCatalogItem)tempFeature;
			
			IRasterDataset rd = rci.getRasterDataset();
			
			raster = new IRaster2Proxy(rd.createDefaultRaster());
		}
		
		return raster;
	}
}

⌨️ 快捷键说明

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