arcgis关于sde的操作

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.esri.sde.sdk.client.SDEPoint;
import com.esri.sde.sdk.client.SeColumnDefinition;
import com.esri.sde.sdk.client.SeConnection;
import com.esri.sde.sdk.client.SeCoordinateReference;
import com.esri.sde.sdk.client.SeDelete;
import com.esri.sde.sdk.client.SeException;
import com.esri.sde.sdk.client.SeExtent;
import com.esri.sde.sdk.client.SeInsert;
import com.esri.sde.sdk.client.SeLayer;
import com.esri.sde.sdk.client.SeObjectId;
import com.esri.sde.sdk.client.SeQuery;
import com.esri.sde.sdk.client.SeRow;
import com.esri.sde.sdk.client.SeShape;
import com.esri.sde.sdk.client.SeSqlConstruct;
import com.esri.sde.sdk.client.SeUpdate;
import com.nankang.webgis.vo.WebgisAnnotationVO;
import com.nankang.framework.util.StringUtil;
import com.nankang.framework.util.XmlUtil;


public class CopyOfSDEUtil {

	/**
	 * 构造函数. 
	 */
	private CopyOfSDEUtil() {
		super();
	}
	//连接池配置(未实现同步)
	private static String  server,database,user,password;   
	//标注层,区县层,样本区域层,楼幢层 
	private static int instance,maxCount;       
	private static ArrayList connPool = new ArrayList();
	private static MySdeConnection  tempConn = null;
	//区县坐标系
	private static SeCoordinateReference  regionCoordref = null;
	//区县图形map(根据区县编号获取后)
	private static HashMap regionShapeList = new HashMap();
	static
	{
		try { 
			InputStream is = CopyOfSDEUtil.class.getResourceAsStream("/sdeconfig.xml");	
			InputStreamReader reader = new InputStreamReader(is);
			Document document = XmlUtil.parseXml(reader, "GBK");
			NodeList dataTypeList = document.getElementsByTagName("sde");
			Element node = (Element) dataTypeList.item(0);
			server = StringUtil.format(WebgisUtil.getSubNodeValue(node,"server").trim());
			database = StringUtil.format(WebgisUtil.getSubNodeValue(node,"database").trim());
			user = StringUtil.format(WebgisUtil.getSubNodeValue(node,"user").trim());
			password = StringUtil.format(WebgisUtil.getSubNodeValue(node,"password").trim());
			instance = StringUtil.getIntValue(WebgisUtil.getSubNodeValue(node,"instance").trim());
			maxCount = StringUtil.getIntValue(WebgisUtil.getSubNodeValue(node,"maxcount").trim());
			for(int i=0;i<maxCount;i++)
			{
				tempConn = new MySdeConnection(server, instance, database, user, password); 
				connPool.add(tempConn);
			}
		}catch (SeException e) { 
			System.out.println("无法创建连接,请检查数据库参数配置文件sdeconfig.xml!");
			e.printStackTrace();    
		}
	}
	public static void init(){
		
	}

	public static boolean testConnecttion()
	{
		try{
			MySdeConnection t_conn = (MySdeConnection)getSeConnection();
			if(t_conn!=null)
			{
				t_conn.close();
				return true;
			}else
				return false;
		}catch(Exception e)
		{
			return false;
		}
	}
	
	public static MySdeConnection  getSeConnection()
	{
		for(int i=0;i<maxCount;i++)
		{
			if(!tempConn.isBusy())
			{
				tempConn = (MySdeConnection)connPool.get(i); 
				tempConn.setIsBusy();
				return tempConn;
			}
		}
        System.out.println("无可用的连接,请检查是否正确关闭连接!");
        return null;
	}
    
	/**
	 * 增加图层对象. 
	 * @param numParts  对象个数,通常为1111111111222211111111
	 * @param offsets  每个对象的坐标偏移量,即从offsets[i]开始第i个对象的坐标
	 */
    public static boolean addObject(WebgisAnnotationVO anno,int numParts,int [] offsets,String[] x,String[] y){
    	MySdeConnection conn = getSeConnection();
    	SeInsert insert = null;
    	try {
    		conn.startTransaction();
			SeLayer annotation = new SeLayer(conn,new SeObjectId(anno.getSdeID()));
            //需要修改的列
			String[] columns = anno.getColumns().split(",");
			String[] cols = new String[columns.length+1];
			System.arraycopy(columns,0,cols,0,columns.length);
			cols[columns.length] = annotation.getSpatialColumn();
			//列值
			String[] values = anno.getValues();
			insert = new SeInsert(conn);
			insert.intoTable(annotation.getName(),cols);
			insert.setWriteMode(true);
			SeRow row = insert.getRowToSet();
			SeColumnDefinition[] col = row.getColumns();
			for(int i=0;i<cols.length-1;i++)
                row.setNString(i,StringUtil.format(values[i]));
			//shape处理
            SeShape shape = new SeShape(annotation.getCoordRef());
            SDEPoint [] points = new SDEPoint [x.length];
            for(int i=0;i<points.length;i++)
            	points[i] = new SDEPoint(StringUtil.getDoubleValue(x[i]),StringUtil.getDoubleValue(y[i]));
            if(anno.getAnnotype()==WebgisCommon.GIS_ANNO_POINT)
            {
                shape.generatePoint(1,points);
            }
            if(anno.getAnnotype()==WebgisCommon.GIS_ANNO_POLYGON)
            {
                shape.generatePolygon(points.length,1,offsets,points);
            }
            row.setShape(cols.length-1,shape);
            insert.execute();
            conn.commitTransaction();
		} catch (SeException e) {
			try {
				conn.rollbackTransaction();
			} catch (SeException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
			return false;
		}finally
		{
			try {
				conn.close();
				insert.close();
			} catch (SeException e1) {
				e1.printStackTrace();
			}
		}
        return true;
    }
    
    
    //修改图层对象(numParts)
    public static boolean updateObject(String objectID,WebgisAnnotationVO anno,int numParts,int [] offsets,String[] x,String[] y,boolean withMap){
    	MySdeConnection conn = getSeConnection();
    	SeUpdate update = null;
    	try {
    		conn.startTransaction();
			SeLayer annotation = new SeLayer(conn,new SeObjectId(anno.getSdeID()));
			
			//需要修改的列
			String[] columns = anno.getColumns().split(",");
			int colsLen = withMap?columns.length+1:columns.length;
			String[] cols = new String[colsLen];
			System.arraycopy(columns,0,cols,0,columns.length);
			if(withMap) cols[columns.length] = annotation.getSpatialColumn();
			//列值
			String[] values = anno.getValues();
			update = new SeUpdate(conn);
			update.toTable(annotation.getName(),cols,"OBJECTID="+objectID);
			update.setWriteMode(true);
			SeRow row = update.getRowToSet();
			SeColumnDefinition[] col = row.getColumns();
			for(int i=0;i<columns.length;i++)
                row.setNString(i,StringUtil.format(values[i]));
			//shape处理
			if(withMap){
	            SeShape shape = new SeShape(annotation.getCoordRef());
	            SDEPoint [] points = new SDEPoint [x.length];
	            for(int i=0;i<points.length;i++)
	            	points[i] = new SDEPoint(StringUtil.getDoubleValue(x[i]),StringUtil.getDoubleValue(y[i]));
	            if(anno.getAnnotype()==WebgisCommon.GIS_ANNO_POINT)
	            {
	                shape.generatePoint(1,points);
	            }
	            if(anno.getAnnotype()==WebgisCommon.GIS_ANNO_POLYGON)
	            {
	            	shape.generatePolygon(points.length,1,offsets,points);
	            }
	            row.setShape(col.length-1,shape);
			}
            update.execute();
            conn.commitTransaction();
		} catch (SeException e) {
			try {
				conn.rollbackTransaction();
			} catch (SeException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
			return false;
		}finally
		{
			try {
				conn.close();
				update.close();
			} catch (SeException e1) {
				e1.printStackTrace();
			}
		}
        return true;
    }
    
    //删除
    public static boolean deleteObject(String objectID,WebgisAnnotationVO anno){
    	MySdeConnection conn = getSeConnection();
    	SeDelete delete = null;
    	try {
    		conn.startTransaction();
			SeLayer annotation = new SeLayer(conn,new SeObjectId(anno.getSdeID()));
			delete = new SeDelete(conn);
			delete.byId(annotation.getName(),new SeObjectId(Long.parseLong(objectID)));
                          //delete.execute();  此处为arcgis的bug,9.19.1之后的版本可能会修复	
			conn.commitTransaction();
		} catch (SeException e) {
			try {
				conn.rollbackTransaction();
			} catch (SeException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
			return false;
		}finally
		{
			try {
				conn.close();
				delete.close();
			} catch (SeException e1) {
				e1.printStackTrace();
			}
		}
        return true;
    }
	
   
    public static SeCoordinateReference getRegionSeCoordinateReference(){
        return regionCoordref;
    }
    
    
    //根据坐标获得所在区县编号
    public static String getRegionID(double x,double y){
    	Iterator it = regionShapeList.keySet().iterator();
    	String code;
    	SeShape shape;
    	try {
	        while(it.hasNext())
	        {
	        	code = StringUtil.format(it.next());
	        	shape = (SeShape)regionShapeList.get(code);
	        	SeShape point;
				
					point = new SeShape(shape.getCoordRef());
				
				point.generatePoint(1,new SDEPoint []{new SDEPoint(x,y)});
	        	if(point.isWithin(shape))
	        		return code;
        }
        return "";
    	} catch (SeException e) {
			e.printStackTrace();
			return "";
		}
    }
    
    public static void main(String [] args) throws SeException
    {
    }  
}
class MySdeConnection extends SeConnection
{
	private boolean isBusy = false;
	public MySdeConnection(String server, int instance, String database, String user, String password) throws SeException
	{
		super(server, instance, database, user, password);
	}
	public void close()
	{
		isBusy = false;
	}
	public void setIsBusy()
	{
		isBusy = true;
	}
	public boolean isBusy()
	{
		return isBusy;
	}
}

猜你喜欢

转载自liggway.iteye.com/blog/617448