arcgis server 服务

package com.hdmap.fymas;

import java.io.IOException;

import com.esri.arcgis.geodatabase.Feature;
import com.esri.arcgis.geodatabase.FeatureClass;
import com.esri.arcgis.geodatabase.FeatureCursor;
import com.esri.arcgis.geodatabase.Field;
import com.esri.arcgis.geodatabase.Fields;
import com.esri.arcgis.geodatabase.IDataset;
import com.esri.arcgis.geodatabase.IDatasetName;
import com.esri.arcgis.geodatabase.IEnumDataset;
import com.esri.arcgis.geodatabase.IEnumDatasetName;
import com.esri.arcgis.geodatabase.IFeatureBuffer;
import com.esri.arcgis.geodatabase.IFeatureCursor;
import com.esri.arcgis.geodatabase.IWorkspace;
import com.esri.arcgis.geodatabase.esriDatasetType;
import com.esri.arcgis.geodatabase.esriFieldType;
import com.esri.arcgis.geodatabasedistributed.GeoDataServer;
import com.esri.arcgis.geometry.IPoint;
import com.esri.arcgis.geometry.Point;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.server.IServerContext;
import com.esri.arcgis.server.IServerObjectManager;
import com.esri.arcgis.server.ServerConnection;
import com.esri.arcgis.system.Cleaner;
import com.esri.arcgis.system.ServerInitializer;

public class GeoDataServerMain {

	
	private static String user = "cmt";// 
	private static String password = "a";// 
	private static String domain = "172.16.17.156";
	private static String host = "172.16.17.156";
	private static String servicesName = "qin/usaqiny";
	private static ServerConnection conn = null;
	private static IServerContext serverContext = null;
	private static IServerObjectManager som = null;
	private static GeoDataServer gdbserver = null;
	private static IWorkspace workspace = null;

	public static void initGeoDataServer() {
		/*** 连接到ArcGISSever */
		try {
			new ServerInitializer().initializeServer(domain, user, password);
			conn = new ServerConnection();
			conn.connect(host);
			som = conn.getServerObjectManager();
			serverContext = som.createServerContext(servicesName,
					"GeoDataServer");
			gdbserver = (GeoDataServer) serverContext.getServerObject();

			workspace = gdbserver.getDefaultWorkingWorkspace();
			System.out.print("已连接到ArcGIS Sever");
		} catch (Exception e) {
			e.printStackTrace();
			gdbserver = null;
			workspace = null;
			som = null;
		}
	}

	public static void main(String[] args) throws Exception {
		// GeoDataServerMain it = new GeoDataServerMain();
		initGeoDataServer();
		browseFileGDB(workspace);
		insertFeaturesUsingCursor(workspace);
		browseFileGDB(workspace);
		serverContext.releaseContext();
	}

	/**
	 * 显示所有的图层
	 * @param workspace
	 */
	public static void browseFileGDB(IWorkspace workspace) {
		try {

			IEnumDatasetName enumDatasetName = workspace
					.getDatasetNames(esriDatasetType.esriDTAny);

			IDatasetName dsName = enumDatasetName.next();
			while (dsName != null) {
				System.out.println("Dataset Name: " + dsName.getName());

				dsName = enumDatasetName.next();
			}

			IEnumDataset enumDataset = workspace
					.getDatasets(esriDatasetType.esriDTFeatureClass);

			IDataset ds = enumDataset.next();
			while (ds != null) {
				FeatureClass fClass = new FeatureClass(ds);
				query(fClass);
				int fCount = fClass.featureCount(null);

				System.out.println("FeatureClass " + fClass.getAliasName()
						+ " has " + fCount + " features.");

				ds = enumDataset.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**查询数据
	 * @param featureClass
	 * @throws AutomationException
	 * @throws IOException
	 */
	public static void query(FeatureClass featureClass)
			throws AutomationException, IOException {

		FeatureCursor featureCursor = new FeatureCursor(featureClass.search(
				null, true));
		Feature feature = (Feature) featureCursor.nextFeature();
		Fields fields = (Fields) featureCursor.getFields();
		int fieldCount = fields.getFieldCount();

		for (int index = 0; index < fieldCount; index++) {
			Field field = (Field) fields.getField(index);
			String fieldName = field.getName();
			System.out.print(fieldName + "\t");
		}
		System.out.println();
		while (feature != null) {
			StringBuffer row = new StringBuffer();

			for (int index = 0; index < fieldCount; index++) {
				int fieldType = feature.getFields().getField(index).getType();

				switch (fieldType) {
				case esriFieldType.esriFieldTypeDate:
				case esriFieldType.esriFieldTypeDouble:
				case esriFieldType.esriFieldTypeGlobalID:
				case esriFieldType.esriFieldTypeGUID:
				case esriFieldType.esriFieldTypeInteger:
				case esriFieldType.esriFieldTypeOID:
				case esriFieldType.esriFieldTypeSingle:
				case esriFieldType.esriFieldTypeSmallInteger:
				case esriFieldType.esriFieldTypeString:
					row.append(feature.getValue(index) + "\t");
					break;

				case esriFieldType.esriFieldTypeBlob:
					row.append("(blob)" + "\t");
					break;

				case esriFieldType.esriFieldTypeGeometry:
					row.append("(geometry)" + "\t");
					break;

				case esriFieldType.esriFieldTypeRaster:
					row.append("(raster)" + "\t");
					break;
				}
			}

			if (row.length() > 0) {
				System.out.println(row);
			}

			feature = (Feature) featureCursor.nextFeature();
		}
	}

	/**
	 * 插入数据
	 * @param workspace
	 * @throws Exception
	 */
	private static void insertFeaturesUsingCursor(IWorkspace workspace)
			throws Exception {
		IEnumDataset enumDataset = workspace
				.getDatasets(esriDatasetType.esriDTFeatureClass);
		IDataset ds = enumDataset.next();// 这里就有一个图层,所以就这么写了,应该获得图层然后进行操作 
		
		while (ds != null) {
			
			FeatureClass featureClass = new FeatureClass(ds);
			IFeatureBuffer featureBuffer = featureClass.createFeatureBuffer();

			IFeatureCursor insertCursor = featureClass
					.IFeatureClass_insert(false);
            // 这里是关键
			// 不能  Point point= new Point();
			IPoint point = (IPoint) serverContext
					.createObject(Point.getClsid());
			point.setX(100);
			point.setY(200);
			featureBuffer.setShapeByRef(point);
			insertCursor.insertFeature(featureBuffer);
			ds = enumDataset.next();
			insertCursor.flush();
			Cleaner.release(insertCursor);
		}
	}
}

 

package com.hdmap.fymas;

import java.io.IOException;
import java.util.List;

import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.carto.IFeatureLayer;
import com.esri.arcgis.geodatabase.Feature;
import com.esri.arcgis.geodatabase.FeatureClass;
import com.esri.arcgis.geodatabase.FeatureCursor;
import com.esri.arcgis.geodatabase.Field;
import com.esri.arcgis.geodatabase.Fields;
import com.esri.arcgis.geodatabase.IDataset;
import com.esri.arcgis.geodatabase.IDatasetName;
import com.esri.arcgis.geodatabase.IEnumDataset;
import com.esri.arcgis.geodatabase.IEnumDatasetName;
import com.esri.arcgis.geodatabase.IFeatureBuffer;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureCursor;
import com.esri.arcgis.geodatabase.IWorkspace;
import com.esri.arcgis.geodatabase.esriDatasetType;
import com.esri.arcgis.geodatabase.esriFieldType;
import com.esri.arcgis.geodatabasedistributed.GeoDataServer;
import com.esri.arcgis.geometry.IPoint;
import com.esri.arcgis.geometry.Point;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.server.IServerContext;
import com.esri.arcgis.server.IServerObjectManager;
import com.esri.arcgis.server.ServerConnection;
import com.esri.arcgis.system.Cleaner;
import com.esri.arcgis.system.ServerInitializer;
import com.hdmap.fymas.fire.bean.FirePoint;
import com.hdmap.fymas.utils.ReadFromFileUtil;

public class GeoDataServerMain {

	private static String user = "cmt";//
	private static String password = "a";//
	private static String domain = "172.16.17.156";
	private static String host = "172.16.17.156";
	private static String servicesName = "qin/usaqiny";
	private static ServerConnection conn = null;
	private static IServerContext serverContext = null;
	private static IServerObjectManager som = null;
	private static GeoDataServer gdbserver = null;
	private static IWorkspace workspace = null;

	public static void initGeoDataServer() {
		/*** 连接到ArcGISSever */
		try {
			new ServerInitializer().initializeServer(domain, user, password);
			conn = new ServerConnection();
			conn.connect(host);
			som = conn.getServerObjectManager();
			serverContext = som.createServerContext(servicesName,
					"GeoDataServer");
			gdbserver = (GeoDataServer) serverContext.getServerObject();

			workspace = gdbserver.getDefaultWorkingWorkspace();
			System.out.print("已连接到ArcGIS Sever");
		} catch (Exception e) {
			e.printStackTrace();
			gdbserver = null;
			workspace = null;
			som = null;
		}
	}

	public static void main(String[] args) throws Exception {
		// GeoDataServerMain it = new GeoDataServerMain();
		initGeoDataServer();
		browseFileGDB(workspace);
		String fileName = "D:/FIR_PLST_FY3A_MERSI_1000M_EJ_P001_20110426024500.TXT";
		List alist = ReadFromFileUtil.readFirePointByLines(fileName);
		System.out.println(alist);

		// insertFeaturesUsingCursor(workspace);
		insertFirePoint(workspace, alist);
		browseFileGDB(workspace);
		serverContext.releaseContext();
	}

	/**
	 * 插入数据
	 * 
	 * @param workspace
	 * @throws Exception
	 */
	@SuppressWarnings("unused")
	private static void insertFeaturesUsingCursor(IWorkspace workspace,
			String featureName) throws Exception {
		FeatureClass featureClass = getFeatureClassByName(workspace,
				featureName);
		IFeatureBuffer featureBuffer = featureClass.createFeatureBuffer();
		IFeatureCursor insertCursor = featureClass.IFeatureClass_insert(false);
		// 这里是关键
		// 不能 Point point= new Point();
		IPoint point = (IPoint) serverContext.createObject(Point.getClsid());
		point.setX(100);
		point.setY(200);
		featureBuffer.setShapeByRef(point);
		insertCursor.insertFeature(featureBuffer);

		insertCursor.flush();
		Cleaner.release(insertCursor);

	}

	/**
	 * 根据FeatureClassName 获得FeatureClass
	 * 
	 * @param workspace
	 * @param FeatureClassName
	 * @return
	 * @throws AutomationException
	 * @throws IOException
	 */
	public static FeatureClass getFeatureClassByName(IWorkspace workspace,
			String FeatureClassName) throws AutomationException, IOException {

		IEnumDataset enumDataset = workspace
				.getDatasets(esriDatasetType.esriDTFeatureClass);
		IDataset ds = enumDataset.next();//
		FeatureClass featureClass = null;
		while (ds != null) {
			if (FeatureClassName.equals(ds.getName())) {
				featureClass = new FeatureClass(ds);

			}
			ds = enumDataset.next();
		}
		return featureClass;
	}

	/**
	 * 显示所有的图层
	 * 
	 * @param workspace
	 */
	public static void browseFileGDB(IWorkspace workspace) {
		try {

			IEnumDatasetName enumDatasetName = workspace
					.getDatasetNames(esriDatasetType.esriDTAny);

			IDatasetName dsName = enumDatasetName.next();
			while (dsName != null) {
				System.out.println("Dataset Name: " + dsName.getName());

				dsName = enumDatasetName.next();
			}

			IEnumDataset enumDataset = workspace
					.getDatasets(esriDatasetType.esriDTFeatureClass);

			IDataset ds = enumDataset.next();
			while (ds != null) {
				FeatureClass fClass = new FeatureClass(ds);
				query(fClass);
				int fCount = fClass.featureCount(null);

				System.out.println("FeatureClass " + fClass.getAliasName()
						+ " has " + fCount + " features.");

				ds = enumDataset.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 查询数据
	 * 
	 * @param featureClass
	 * @throws AutomationException
	 * @throws IOException
	 */
	public static void query(FeatureClass featureClass)
			throws AutomationException, IOException {

		FeatureCursor featureCursor = new FeatureCursor(featureClass.search(
				null, true));
		Feature feature = (Feature) featureCursor.nextFeature();
		Fields fields = (Fields) featureCursor.getFields();
		int fieldCount = fields.getFieldCount();

		for (int index = 0; index < fieldCount; index++) {
			Field field = (Field) fields.getField(index);
			String fieldName = field.getName();
			System.out.print(fieldName + "\t");
		}
		System.out.println();
		while (feature != null) {
			StringBuffer row = new StringBuffer();
			for (int index = 0; index < fieldCount; index++) {
				int fieldType = feature.getFields().getField(index).getType();

				switch (fieldType) {
				case esriFieldType.esriFieldTypeDate:
				case esriFieldType.esriFieldTypeDouble:
				case esriFieldType.esriFieldTypeGlobalID:
				case esriFieldType.esriFieldTypeGUID:
				case esriFieldType.esriFieldTypeInteger:
				case esriFieldType.esriFieldTypeOID:
				case esriFieldType.esriFieldTypeSingle:
				case esriFieldType.esriFieldTypeSmallInteger:
				case esriFieldType.esriFieldTypeString:
					row.append(feature.getValue(index) + "\t");
					break;

				case esriFieldType.esriFieldTypeBlob:
					row.append("(blob)" + "\t");
					break;

				case esriFieldType.esriFieldTypeGeometry:
					row.append("(geometry)" + "\t");
					break;

				case esriFieldType.esriFieldTypeRaster:
					row.append("(raster)" + "\t");
					break;
				}
			}

			if (row.length() > 0) {
				System.out.println(row);
			}

			feature = (Feature) featureCursor.nextFeature();
		}
	}

	/**
	 * 插入火点数据
	 * 
	 * @param workspace
	 * @throws Exception
	 */
	@SuppressWarnings("unused")
	private static void insertFirePoint(IWorkspace workspace,
			List<FirePoint> fpList) throws Exception {
		// IEnumDataset enumDataset = workspace
		// .getDatasets(esriDatasetType.esriDTFeatureClass);
		// IDataset ds = enumDataset.next();// 这里就有一个图层,所以就这么写了,应该获得图层然后进行操作

		String fName = "fire_p";
		// while (ds != null) {
		FeatureClass featureClass = getFeatureClassByName(workspace, fName);
		// FeatureClass featureClass = new FeatureClass(ds);
		IFeatureBuffer featureBuffer = featureClass.createFeatureBuffer();

		IFeatureCursor insertCursor = featureClass.IFeatureClass_insert(false);

		for (int i = 0; i < fpList.size(); i++) {
			// 这里是关键
			FirePoint fp = fpList.get(i);
			IPoint point = (IPoint) serverContext
					.createObject(Point.getClsid());

			point.setX(fp.getX());
			point.setY(fp.getY());
			featureBuffer.setShapeByRef(point);
			insertCursor.insertFeature(featureBuffer);
		}

		// ds = enumDataset.next();
		insertCursor.flush();

		Cleaner.release(insertCursor);
		// }
	}
}

 

你可能感兴趣的:(server)