首页 > 代码库 > ArcSDE SDK For Java二次开发介绍、示例

ArcSDE SDK For Java二次开发介绍、示例

在一个工作中,遇到了需要java后台来查询ArcGIS 中用到的Oracle数据库空间数据,由于对ArcGIS空间数据首次接触,只知道Oracle可以使用ST_GEOMETRY字段存储,如下图


但是查询时会发现这个ST_GEOMETRY字段会在结果中出现个多个子的字段,对于arcgis地理知识了解甚少,不知道单独查询一个坐标怎么弄了,有些朋友说需要配置监听文件。

不管怎么说,至少参考了一个大牛的文章,http://blog.csdn.net/linghe301/article/details/8058806 根据 ArcSDE For Java的API中,可以对其进行增删改查等操作。


对于ArcSDE for Java的介绍请参考大牛文章,就不重复写了。下面的示例代码中,经过修改,加了一些注释,还是不错的

package cn.sde.test;

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.SeException;
import com.esri.sde.sdk.client.SeFilter;
import com.esri.sde.sdk.client.SeInstance;
import com.esri.sde.sdk.client.SeLayer;
import com.esri.sde.sdk.client.SeQuery;
import com.esri.sde.sdk.client.SeQueryInfo;
import com.esri.sde.sdk.client.SeRelease;
import com.esri.sde.sdk.client.SeRow;
import com.esri.sde.sdk.client.SeShape;
import com.esri.sde.sdk.client.SeShapeFilter;
import com.esri.sde.sdk.client.SeSqlConstruct;
import com.esri.sde.sdk.client.SeTable;

public class ArcSDE_Test3 {

	private static SeConnection conn = null;
	private static String server = "172.25.0.253"; // sde服务器
	private static String instance = "5151"; // sde端口
	private static String database = "orcl"; // sde数据库
	private static String username = "sde"; // sde用户名
	private static String password = "sde"; // sde密码

	// 获得ArcSDE连接
	private static SeConnection getConn() {
		if (conn == null) {
			try {
				conn = new SeConnection(server, instance, database, username, password);
			} catch (SeException ex) {
				ex.printStackTrace();
			}
		}
		return conn;
	}

	/**
	 * ArcSDE管理
	 */
	public static void GetArcSDEInfo() {
		try {
			SeInstance instance = new SeInstance(server, "5151");
			SeInstance.SeInstanceStatus status = instance.getStatus();
			System.out.println("连接数:" + status.getNumConnections());
			System.out.println("可以连接:" + status.isAccepting());

			System.out.println("------------------------------------------------");

			SeInstance.SeInstanceConfiguration config = instance.getConfiguration();
			System.out.println("最大连接数:" + config.getMaxConnections());

			System.out.println("------------------------------------------------");

			SeInstance.SeInstanceStats[] stats = instance.getStats();
			for (int i = 0; i < stats.length; i++) {
				System.out.println("操作数:" + stats[i].getOperationCount());
			}

			System.out.println("------------------------------------------------");

			SeInstance.SeInstanceUsers[] users = instance.getUsers();
			for (int j = 0; j < users.length; j++) {
				System.out.println("用户名:" + users[j].getUserName());
				System.out.println("系统名:" + users[j].getSysName());
				System.out.println("服务器开始时间:" + users[j].getServerStartTime());
				System.out.println("服务器PID:" + users[j].getServerPid());
				System.out.println("*****************************");
			}

			System.out.println("------------------------------------------------");
			System.out.println("系统名:" + instance.getServerName());
			System.out.println("------------------------------------------------");

			System.out.println("------------------------------------------------");
			SeInstance.SeInstanceTableLocks[] tablelocks = instance.getTableLocks();
			for (int i = 0; i < tablelocks.length; i++) {
				System.out.println("表级别锁类型:" + tablelocks[i].getLockType());
				System.out.println("表级别锁PID:" + tablelocks[i].getPid());
				System.out.println("表级别锁注册ID:" + tablelocks[i].getRegistrationId());
				System.out.println("*****************************");
			}
			System.out.println("------------------------------------------------");
		} catch (SeException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 空间条件查询
	 * 目前存在的问题是,实际应该返回2条,但是,只返回了一条记录,有知道解决方法的朋友请告诉我,留言  跪谢!
	 */
	public static void SpatialQuery_Hz() {
		try {
			String tbname = "EZGISDATA.DCQWG_HB";	// 表名
			String[] cols = new String[7];	// 要查询字段的数组
			cols[0] = "JDNAME";
			cols[1] = "JDCODE";
			cols[2] = "SQNAME";
			cols[3] = "SQCODE";
			cols[4] = "WGNAME";
			cols[5] = "WGCODE";
			cols[6] = "TYPE";
			SeConnection conn = getConn();
			SeLayer layer = new SeLayer(conn, tbname, "SHAPE");	// shape为要查询的表中的空间数据字段

			SeCoordinateReference cr = layer.getCoordRef();
			SeFilter[] filters = new SeFilter[1];
			SeShape shape = new SeShape(cr);

			double x = 507638.3125000;	// x坐标
			double y = 303547.9687500;	// y坐标
			double xs = 0.000000001;

			double x1 = x - xs;
			double y1 = y + xs;

			double x2 = x + xs;
			double y2 = y + xs;

			double x3 = x + xs;
			double y3 = y - xs;

			double x4 = x - xs;
			double y4 = y - xs;
			String  polygonstr = "POLYGON((" + Double.toString(x1) + " " + Double.toString(y1);
					polygonstr = polygonstr + "," + Double.toString(x2) + " " + Double.toString(y2);
					polygonstr = polygonstr + "," + Double.toString(x3) + " " + Double.toString(y3);
					polygonstr = polygonstr + "," + Double.toString(x4) + " " + Double.toString(y4);
					polygonstr = polygonstr + "," + Double.toString(x1) + " " + Double.toString(y1);
					polygonstr = polygonstr + "))";
			shape.generateFromText(polygonstr);		// 设置查询参数,根据polygon查询

			// SDEPoint pt = new SDEPoint(507638.3125000,303547.9687500);//X,Y
			// shape.generatePoint(1, new SDEPoint[] { pt });
			SeFilter filter = new SeShapeFilter(tbname, layer.getSpatialColumn(), shape, SeShapeFilter.METHOD_AI);	// 设置过滤
			filters[0] = filter;

			SeSqlConstruct sqlCons = new SeSqlConstruct(tbname);
			SeQuery query = new SeQuery(conn, cols, sqlCons);
			query.prepareQuery();
			query.setSpatialConstraints(SeQuery.SE_OPTIMIZE, false, filters);
			query.execute();
			SeRow row = query.fetch();	// 类似数据库游标的功能
			int reccount = 0;
			while (row != null) {
				reccount = reccount + 1;
				String info = Integer.toString(reccount);
				for (int i = 0; i < cols.length; i++) {	// 将字段和结果同时输出
					info = info + " : " + cols[i] + ":" + row.getObject(i).toString();
				}
				System.out.println(info);
				row = query.fetch();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 *  属性条件查询
	 */
	public static void AttributeQuery() {
		try {
			String tbname = "EZGISDATA.DCQWG_PY";
			String[] cols = new String[4];
			cols[0] = "NEWSQMC";
			cols[1] = "NEWSQBM";
			cols[2] = "NEWWGMC";
			cols[3] = "NEWWGBM";
			SeConnection conn = getConn();
			SeSqlConstruct sqlCons = new SeSqlConstruct(tbname);
			sqlCons.setWhere("JDNAME='和平里'");		// 设置条件,类似sql语句
			SeQuery query = new SeQuery(conn, cols, sqlCons);

			query.prepareQuery(cols, sqlCons);
			query.execute();
			SeRow row = query.fetch();
			int reccount = 0;
			while (row != null) {
				reccount = reccount + 1;
				System.out.println(Integer.toString(reccount) + " : " + row.getObject(0).toString() + " : " + row.getObject(1).toString() 
						+ " : " + row.getObject(2).toString() + " : " + row.getObject(3).toString());
				row = query.fetch();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 *  普通查询
	 */
	public static void CommonQuery() {
		try {
			SeConnection conn = getConn();
			SeTable table = new SeTable(conn, "EZGISDATA.DCQWG_PY");
			SeColumnDefinition[] tableDef = table.describe();
			String[] cols = new String[tableDef.length];
			for (int j = 0; j < cols.length; j++) {
				cols[j] = tableDef[j].getName();
			}
			SeSqlConstruct sqlCons = new SeSqlConstruct("EZGISDATA.DCQWG_PY");
			SeQuery query = new SeQuery(conn, cols, sqlCons);

			SeQueryInfo queryInfo = new SeQueryInfo();
			queryInfo.setQueryType(SeQueryInfo.SE_QUERYTYPE_ATTRIBUTE_FIRST);
			queryInfo.setColumns(cols);
			queryInfo.setConstruct(sqlCons);
			query.prepareQueryInfo(queryInfo);
			query.execute();
			SeRow row = query.fetch();
			while (row != null) {
				System.out.println(row.getObject(0).toString() + " : " + row.getObject(1).toString() + " : " + row.getObject(3).toString() + " : " + row.getObject(4).toString() + " : "
						+ row.getObject(5).toString() + " : " + row.getObject(6).toString() + " : " + row.getObject(7).toString() + " : " + row.getObject(8).toString());
				row = query.fetch();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 *  获得ArcSDE版本信息
	 */
	public static void GetVersion() {
		SeConnection conn = getConn();
		SeRelease release = conn.getRelease();
		System.out.println(release.getBugFix());
		System.out.println(release.getDesc());
		System.out.println(release.getRelease());
		System.out.println(release.getMajor());
		System.out.println(release.getMinor());
	}

	public static void main(String[] args) {
//		 GetArcSDEInfo();
//		 GetVersion();
//		 CommonQuery();
//		 AttributeQuery();
		 SpatialQuery_Hz();	// 空间查询
	}

}
结果:

------------------------------------------------
1 : JDNAME:东花市 : JDCODE:13 : SQNAME:忠实里社区 : SQCODE:1303 : WGNAME:忠实里社区3号网格 : WGCODE:13033-0152 : TYPE:SG





ArcSDE SDK For Java二次开发介绍、示例