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

📄 personalizationgeometryconverter.java

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

import java.util.List;

import org.apache.log4j.Logger;

import com.esri.adf.web.data.geometry.WebGeometry;
import com.esri.adf.web.data.geometry.WebPoint;
import com.esri.adf.web.data.geometry.WebPolygon;
import com.esri.adf.web.data.geometry.WebRing;

import com.esri.solutions.jitk.personalization.data.beans.v1.ArrayOfPoint;
import com.esri.solutions.jitk.personalization.data.beans.v1.ArrayOfRing;
import com.esri.solutions.jitk.personalization.data.beans.v1.ArrayOfRingType;
import com.esri.solutions.jitk.personalization.data.beans.v1.GeometryType;
import com.esri.solutions.jitk.personalization.data.beans.v1.ObjectFactory;
import com.esri.solutions.jitk.personalization.data.beans.v1.PointN;
import com.esri.solutions.jitk.personalization.data.beans.v1.PolygonN;
import com.esri.solutions.jitk.personalization.data.beans.v1.Ring;
import com.esri.solutions.jitk.personalization.data.beans.v1.SpatialReferenceType;
import com.esri.solutions.jitk.web.tasks.mapcomp.DefaultADFToBeanConverter;

@SuppressWarnings("deprecation")
public class PersonalizationGeometryConverter {
	private static final Logger _logger = Logger.getLogger(PersonalizationGeometryConverter.class);
	private ObjectFactory factory = null;
	
	public PersonalizationGeometryConverter() {
		try {
			factory = new ObjectFactory();
		} catch(Exception ex){
			_logger.error("PersonalizationGeometryConverter: Error creating object factory");
		}
	}
	
	/**
	 * Convert from Personalization Geometry to WebGeometry
	 */
	public WebGeometry toWebGeometry(GeometryType personalGeom)
		throws Exception {	
		WebGeometry geom = null;
					
		if (personalGeom instanceof PolygonN) {
			_logger.debug("Converting Personalization Polygon");
			geom = toWebGeo((PolygonN) personalGeom);
		} else if (personalGeom instanceof PointN){
			_logger.debug("Converting Personalization Point");
			geom = toWebGeo((PointN) personalGeom);
		} else {
			_logger.warn("Unsupported geometry type encountered");
			throw new Exception("Unsupported geomtry type encountered");
		}
		
		return geom;	
	}
	
	/**
	 * Convert from WebGeometry to Personalization Geometry
	 */
	public GeometryType fromWebGeometry(WebGeometry webGeo) throws Exception {		
		PolygonN polyN = null;
		PointN pointN = null;
					
		if (webGeo instanceof WebPolygon) {
			_logger.debug("Converting WebPolygon");
			polyN = toPersonalGeo((WebPolygon) webGeo);
			return polyN;
		} else if (webGeo instanceof WebPoint) {
			_logger.debug("Converting WebPoint");
			pointN = toPersonalGeo((WebPoint) webGeo);
			return pointN;
		} else {
			_logger.warn("Unsupported geometry type encountered");
			throw new Exception("Unsupported geomtry type encountered");
		}
	}
	
	/**
	 * Convert WebPolygon to Personalization Polygon
	 */
	private PolygonN toPersonalGeo(WebPolygon wPoly) {
	   try {
		   DefaultADFToBeanConverter converter = new DefaultADFToBeanConverter();
		   PolygonN poly = factory.createPolygonN();
		   wPoly.getSpatialReference();
		
		   if (wPoly.getSpatialReference() != null) {
			   SpatialReferenceType srType = converter.createSpatialReference(
							wPoly.getSpatialReference());
			
			   poly.setSpatialReference(srType);
		   }
		
		   toPersonalGeo(wPoly, poly);
	
		   return poly;
	   	} catch (Exception ex){
	   		_logger.warn(ex);
	   		return null;
	   	}
	}
	
	/**
	 * Convert WebPoint to Personalization Point
	 */
	private PointN toPersonalGeo(WebPoint wPoint){
	   try {
		   	PointN pointN = factory.createPointN();
		
		   	if (wPoint.getSpatialReference() != null){
			   DefaultADFToBeanConverter converter = new DefaultADFToBeanConverter();
		
			   pointN.setSpatialReference(converter.createSpatialReference(
					wPoint.getSpatialReference()));
		   	}
		
		   	pointN.setX(wPoint.getX());
		   	pointN.setY(wPoint.getY());
		
		   	return pointN;
	   	} catch (Exception ex) {
	   		_logger.warn(ex);
		  
		  	return null;
	   	}
	}
	
	/**
	 * Convert Personalization Polygon to WebPolygon
	 */
	private WebPolygon toWebGeo(PolygonN poly){
		try {
			WebPolygon webPoly = new WebPolygon();
		
			toWebGeo(poly, webPoly);
		
			return webPoly;
		} catch (Exception ex){
			_logger.warn(ex);
		  	return null;
		}
	}
	
	/**
	 * Convert Personalization Point to WebPoint
	 */
	private WebPoint toWebGeo(PointN pointN){
		try {
			WebPoint webPoint = new WebPoint();
		
			webPoint.setX(pointN.getX());
			webPoint.setY(pointN.getY());
		
			return webPoint;
		} catch (Exception ex){
			_logger.warn(ex);
		  	return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	private void toPersonalGeo(WebPolygon webPoly, PolygonN poly){
		try {
			List<WebRing> rings = webPoly.getRings();
			ArrayOfRing arrayOfRing = factory.createArrayOfRing();
			poly.setRingArray(arrayOfRing);
			
			for (WebRing ring : rings) {
				Ring pRing = factory.createRing();
				poly.getRingArray().getRing().add(pRing);
				ArrayOfPoint pointArray = factory.createArrayOfPoint();
				pRing.setPointArray(pointArray);
				
				toPersonalGeo(ring, pRing);
			}
		} catch(Exception ex){
			_logger.error("Error converting WebPolygon to PolygonN");
		}
	}
	
	@SuppressWarnings("unchecked")
	private void toWebGeo(PolygonN poly, WebPolygon webPoly){
		
		ArrayOfRingType rings = (ArrayOfRingType)poly.getRingArray();
		List<Ring> _ring = rings.getRing();
		
		for (Ring ring : _ring) {
			WebRing wRing = new WebRing();
			webPoly.addRing(wRing);
				
			toWebGeo(ring, wRing);
			
		}
	}
	
	@SuppressWarnings("unchecked")
	public void toPersonalGeo(WebRing ring, Ring pRing) {
		try {
			List<WebPoint> points = ring.getPoints();
			
			//If the first vertex is the same object as the last vertex, do 
			//not move it twice
			boolean lastVertexEqualToFirst = isLastVertexEqualToFirst(ring);
			int vertexCount = ring.getPoints().size();
			
			//int stopVertexIndex = lastVertexEqualToFirst ? vertexCount - 2 : vertexCount - 1;
			int stopVertexIndex = vertexCount - 1;
			
			if (lastVertexEqualToFirst) {
				_logger.debug("First vertex of WebRing equals the last vertex");
			} else {
				_logger.debug("First vertex of WebRing does not equal the last vertex");
			}
				
			for (int vertexIdx = 0; vertexIdx <= stopVertexIndex; vertexIdx++) {
				WebPoint wPnt = points.get(vertexIdx);
				pRing.getPointArray().getPointN().add(toPersonalGeo(wPnt));
			}
		} catch(Exception ex) {
			_logger.error("toPersonalGeo: Error creating Points");
		}
	}	
	
	@SuppressWarnings("unchecked")
	public void toWebGeo(Ring ring, WebRing wRing) {
		try {
			List<PointN> points = ring.getPointArray().getPointN();
			
			//If the first vertex is the same object as the last vertex, do 
			//not move it twice
			boolean lastVertexEqualToFirst = isLastVertexEqualToFirst(ring);
			int vertexCount = ring.getPointArray().getPointN().size();
			int stopVertexIndex = lastVertexEqualToFirst ? vertexCount - 2 : vertexCount - 1;
			
			if (lastVertexEqualToFirst) {
				_logger.debug("First vertex of WebRing equals the last vertex");
			} else {
				_logger.debug("First vertex of WebRing does not equal the last vertex");
			}
			
			for (int vertexIdx = 0; vertexIdx <= stopVertexIndex; vertexIdx++) {
				PointN pnt = points.get(vertexIdx);	
				wRing.addPoint(toWebGeo(pnt));
			}
		} catch(Exception ex){
			_logger.error("Unable to convert to WebPoint");
		}
	}
	
	public boolean isLastVertexEqualToFirst(WebRing ring) {
		int vertexCount = ring.getPoints().size();
		WebPoint first = ring.getPoints().get(0);
		WebPoint last = ring.getPoints().get(vertexCount - 1);
		
		return first == last;
	}
	
	public boolean isLastVertexEqualToFirst(Ring ring) {
		int vertexCount = ring.getPointArray().getPointN().size();
		PointN first = (PointN)ring.getPointArray().getPointN().get(0);
		PointN last = (PointN)ring.getPointArray().getPointN().get(vertexCount - 1);
		
		return first == last;
	}
}

⌨️ 快捷键说明

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