📄 connectsde.java.svn-base
字号:
import java.io.IOException;
import java.net.UnknownHostException;
import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.carto.IFeatureLayer;
import com.esri.arcgis.datasourcesGDB.SdeWorkspaceFactory;
import com.esri.arcgis.geodatabase.Field;
import com.esri.arcgis.geodatabase.Fields;
import com.esri.arcgis.geodatabase.GeometryDef;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureDataset;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geodatabase.IFieldEdit;
import com.esri.arcgis.geodatabase.IFieldsEdit;
import com.esri.arcgis.geodatabase.IGeometryDefEdit;
import com.esri.arcgis.geodatabase.IWorkspaceFactory;
import com.esri.arcgis.geoprocessing.GeoProcessor;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.PropertySet;
import com.esri.arcgis.system.UID;
import com.esri.arcgis.system.esriLicenseProductCode;
public class ConnectSDE {
/*
* 1. Create a WorkspaceFactory object within the Server's context.
*
* 2. Create an instance of a PropertySet for an Oracle ArcSDE connection.
* The PropertySet acts as an array of keyed values that ArcSDE will use to
* collect the connection values from:
*
* 3. Open the ArcSDE workspace and get a handle to it through the
* WorkspaceFactory, passing in the PropertySet:
*
* 4. You now have a connection to the database through a Workspace object .
* In this example, the feature datasets are listed out:
*
*/
public static void connectSDE()
throws com.esri.arcgis.interop.AutomationException,
java.io.IOException {
com.esri.arcgis.geodatabase.IWorkspaceFactory pWorkspaceFactory = new SdeWorkspaceFactory();
com.esri.arcgis.system.PropertySet propSet = new PropertySet();
//com.esri.arcgis.geodatabase.id
GeoProcessor gp=new GeoProcessor();
propSet.setProperty("SERVER", "192.168.1.220");
propSet.setProperty("INSTANCE", "5151");
//propSet.setProperty("DATABASE", "sde");
propSet.setProperty("USER", "sde");
propSet.setProperty("PASSWORD", "sde");
propSet.setProperty("VERSION", "SDE.DEFAULT");
com.esri.arcgis.geodatabase.IWorkspace ws = pWorkspaceFactory.open(
propSet, 0);
SdeWorkspaceFactory swf=new SdeWorkspaceFactory();
IFeatureWorkspace ifw=(IFeatureWorkspace)swf.open(propSet, 0);
IFeatureDataset ifd= ifw.openFeatureDataset("datasettest");
//create the geometry field
IGeometryDefEdit igde=(IGeometryDefEdit)new GeometryDef();
//assign the spatial reference
/*ISpatialReference isr= new UnknownCoordinateSystem();
isr.setDomain(0, 21474.83645, 0, 21474.83645);
isr.setFalseOriginAndUnits(0, 0, 100000);*/
//设为Line Feature
igde.setGeometryType(com.esri.arcgis.geometry.esriGeometryType.esriGeometryPolyline);
/*igde.setGridCount(1);
igde.setGridSize(0, 10);
igde.setAvgNumPoints(2);*/
/*igde.setHasM(false);//M值存放管道,线路数据
igde.setHasZ(false);//Z值存放3D数据
*/
//设置列集合
IFieldsEdit pFieldsEdit = new Fields();
//设置列
IFieldEdit pFieldEdit = new Field();
IFieldEdit shp = new Field();
//列名
pFieldEdit.setName("ObjectID");
//类型
pFieldEdit.setType(com.esri.arcgis.geodatabase.esriFieldType.esriFieldTypeOID);
shp.setName("SHP");
shp.setType(com.esri.arcgis.geodatabase.esriFieldType.esriFieldTypeGeometry);
//Geometry类型的列需要定义Geometry的类型.点/线/面 等等
shp.setGeometryDefByRef(igde);
//将列加入到列集合
pFieldsEdit.addField(pFieldEdit);
pFieldsEdit.addField(shp);
/**
* 设置FeatureClass的cLSID
* value与createFeatureClass(String name,IFields fields,IUID cLSID,IUID eXTCLSID,
* int featureType,String shapeFieldName,String configKeyword)的 featureType 参数对应关系
* esriFTSimple -- esriGeoDatabase.Feature
* esriFTSimpleJunction -- esriGeoDatabase.SimpleJunctionFeature
* esriFTComplexJunction -- esriGeoDatabase.ComplexJunctionFeature
* esriFTSimpleEdge -- esriGeoDatabase.SimpleEdgeFeature
* esriFTComplexEdge -- esriGeoDatabase.ComplexEdgeFeature
* esriFTAnnotation -- 无
* */
UID id=new UID();
id.setValue("esriGeoDatabase.Feature");
//下面循环取出Geometry列的列名,如果已知列名可以不做这个循环
String fieldName="";
for(int i=0;i<pFieldsEdit.getFieldCount()-1;i++){
if(pFieldsEdit.getField(i).getType()==com.esri.arcgis.geodatabase.esriFieldType.esriFieldTypeGeometry){
fieldName=pFieldsEdit.getField(i).getName();
}
}
/**
* createFeatureClass(..)方法中的shapeFieldName参数
* 必须与IFieldsEdit中设置的Geometry类型列的列名相同
* */
ifd.createFeatureClass("testfeature3", pFieldsEdit, id, null,
com.esri.arcgis.geodatabase.esriFeatureType.esriFTSimple, "SHP", "");
/*com.esri.arcgis.geodatabase.IEnumDataset dsenum = ws
.getDatasets(com.esri.arcgis.geodatabase.esriDatasetType.esriDTFeatureDataset);
com.esri.arcgis.geodatabase.IDataset ds = dsenum.next();
while (ds != null) {
System.out.println(ds.getName());
ds = dsenum.next();
}*/
}
/*
* 2、加载SDE数据库 什么是SDE数据库?这个问题要详细地讲解将花费大量的时间, 但我可以告诉你SDE数据数据库可以是任何关系数据库。
* ESRI公司为了使空间数据能保存在关系数据库中, 并且能很好的查询相关的空间属性而开发的一个中间件,
* 使用SDE能很好的将空间数据保存在关系数据库中。 如Orcale SQL Server 等。 SDE具体细节的了解请查找相关的资料,
* 这里只介绍怎么连接SDE数据库。 SDE数据库的联机分为直接连接和通过SDE连接。
* 当服务器的性能比较好的时候可以采用SDE连接,否则采用直接连接, 这样可以减轻服务器的任务。建议采用直接连接,
* 其实,SDE连接方式和直接连接的方式只是一个属性参数设置的问题。 跟个人数据库采用属性连接的方式一样,先定义一个属性对象, 然后设置属性参数,
* 接着定义一个工作空间并用SdeWorkspaceFactoryClass()实例化它,
* 接着加在加载图层,至于加载图层的代码,与加载个人数据库中图层的方法一样,
* 其实不只加载这两种数据类型,加载其他类型的数据时也是采用相同的方法加载图层, 只是工作空间采用不同的实例而已,下边为完整的对吗”//”后的为注析:
*/
public static void AddSDELayer(boolean ChkSdeLinkModle) {
try {
// 定义一个属性
com.esri.arcgis.system.IPropertySet Propset = new PropertySet();
if (ChkSdeLinkModle == true) // 采用SDE连接
{
Propset.setProperty("SERVER", "gctz");// 设置数据库服务器名
Propset.setProperty("INSTANCE", "5151");// 设置SDE的端口,这是安装时指定的,默认安装时"port:5151"
Propset.setProperty("USER", "sde");// SDE的用户名
Propset.setProperty("PASSWORD", "sde");// 密码
Propset.setProperty("DATABASE", "sde");// 设置数据库的名字,只有SQL Server Informix 数据库才需要设置
Propset.setProperty("VERSION", "SDE.DEFAULT");// SDE的版本,在这为默认版本
} else // 直接连接
{
Propset.setProperty("INSTANCE", "5151");// 设置数据库服务器名,如果是本机可以用"sde:sqlserver:."
Propset.setProperty("USER", "sde");// SDE的用户名
Propset.setProperty("PASSWORD", "sde");// 密码
// 设置数据库的名字,只有SQL Server Informix 数据库才需要设置
Propset.setProperty("DATABASE", "sde");
// SDE的版本,在这为默认版本
Propset.setProperty("VERSION", "SDE.DEFAULT");
}
IWorkspaceFactory Fact = new SdeWorkspaceFactory();// 定义一个工作空间,并实例化为SDE的工作空间
IFeatureWorkspace Workspace = (IFeatureWorkspace) Fact.open(Propset, 0);// 打开SDE工作空间,并转化为地物工作空间
/*
* 定义一个地物类,并打开SDE中的管点地物类,写的时候一定要写全.如SDE中有一个管点层,你不能写成IFeatureClass Fcls =
* Workspace.OpenFeatureClass ("管点");这样,一定要写成下边的样子.
*/
IFeatureClass Fcls = Workspace.openFeatureClass("SDE.IANN120279");
IFeatureLayer Fly = new FeatureLayer();
//Fly.FeatureClass = Fcls;
Fly.setFeatureClassByRef(Fcls);
System.out.println(Fly.toString());
//MapCtr.Map.AddLayer(Fly);
//MapCtr.ActiveView.Refresh();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AutomationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/* 不知道注意到了没有,直接连接跟SDE连接的最大的不同是直接连接不要设置端口,同时他们的参数设置也不一样,好好注意参数的设置。 */
public static void main(String args[]){
try {
EngineInitializer.initializeEngine();
final AoInitialize aoInit = new AoInitialize();
aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);
//
connectSDE();
//AddSDELayer(true);
} catch (AutomationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -