我是靠谱客的博主 俏皮耳机,最近开发中收集的这篇文章主要介绍Java调用arcgis导入shape,java for arcgis 之——将shapefile导入SDE,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

环境:1.安装arcgis客户端(百度有)

2.在该路径下ArcGISDesktop10.2javalib 找到arcobject.jar  这是java对arcgis二次开发所需的jar包,添加到工程中(百度有)

3.SDE本人采取的直连oracle的方式,也是查询资料后推荐的方式(需要本地安装oracle客户端,具体见百度)

以上环境准备好直接上代码:

注意:!!!!!

1.这里是很关键的地方,java在使用arcobject.jar时需要在VMOptions中添加一个java命令,目的是告诉jvm去寻找arcgis的dll组件,因为java不能像C#完美调用本地客户端服务

-Djava.library.path="C:Program Files (x86)ArcGISDesktop10.2bin"  -Dfile.encoding=UTF8

路径写自己的arcgis安装路径。

2.java调用arcgis服务时必须先初始化ArcEngine环境,这会在代码中体现,个人猜测应该是调用组件的引擎

代码:

先讲下代码思路:shapefile-->shapefile的featureClass,SDE-->SDE的featureClass,shpFeatureClass插入到SDEFeatureClass中

初始化ArcEngine:

private staticAoInitialize ao= null;/*** 初始化ArcEngne* java操作arcgis必须先初始化ArcEngne环境 否则报错**/public static voidinitializeArcGISLincenses(){

/*所有AE10的与AE10.1以后版本的对应关系esriLicenseProductCodeArcInfo ==》esriLicenseProductCodeAdvancedesriLicenseProductCodeArcEditor==》esriLicenseProductCodeStandardesriLicenseProductCodeArcView ==》esriLicenseProductCodeBasic*/EngineInitializer.initializeVisualBeans();EngineInitializer.initializeEngine();try{

ao= newAoInitialize();if(ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeAdvanced) == esriLicenseStatus.esriLicenseAvailable){

ao.initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced);}else{

System.out.print("lincense failed");ao.checkOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCode3DAnalyst);ao.checkOutExtension(com.esri.arcgis.system.esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);ao.checkOutExtension(com.esri.arcgis.system.esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);ao.initialize(esriLicenseProductCode.esriLicenseProductCodeStandard);ao.checkOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);System.out.println("arcgis>>>...");}

}catch(Exception e){

e.printStackTrace();}

}

连接SDE:

public classConnectSDE {

/*** 连接SDE 返回SDE工作空间*@return*@throwsIOException*/public staticIWorkspace openSDE() throwsIOException{

IPropertySet iPropertySet = newPropertySet();iPropertySet.setProperty("AUTHENTICATION_MODE","DBMS");iPropertySet.setProperty("DB_CLIENT","ORCL");iPropertySet.setProperty("IS_GEODATABSE","true");iPropertySet.setProperty("DB_CONNECTION_PROPERTIES","ORCL");iPropertySet.setProperty("SERVER","192.168.80.35");//1、oracle$后是客户端配置的实例名时 IP(SERVER)可以不设置 随便设置一个IP也可以//2、oracle$后是服务器IP【:端口(如1521)】/服务器实例名 时 IP(SERVER)一定要填且必须对// sde:oracle$orcl(客户端配置的实例名) 或者 sde:oracle$127.0.0.1[:1521]/orcl(默认端口不报错)iPropertySet.setProperty("INSTANCE","sde:oracle:ORCL_192.168.80.35");iPropertySet.setProperty("USER","dsnjgeo");iPropertySet.setProperty("PASSWORD","gtis");//pPropertySet.SetProperty("VERSION", "SDE.DEFAULT");//默认版本可以不设置IWorkspaceFactory workspaceFactory = newSdeWorkspaceFactory();IWorkspace workspace = workspaceFactory.open(iPropertySet,0);if(workspace.exists()){

returnworkspace;}else{

return null;}

}

}

获取shapefile要素集:

/*** 获取shapefile要素集*@parampath //shapefile文件dizhi*@paramname featureClass的名称*@return*@throwsIOException*/private staticFeatureClass getFeatureClass(String path,String name) throwsIOException{

FeatureClass featureClass = null;try{

ShapefileWorkspaceFactory swf = newShapefileWorkspaceFactory();Workspace workspace = newWorkspace(swf.openFromFile(path,0));featureClass = newFeatureClass(workspace.openFeatureClass(name));

}catch(IOException e){

System.out.println("Couldn't access feature class :"+ name + " in "+ path);throwe;}

returnfeatureClass;}

开始导入:

/*** 批量导入*@paramshapePath*@paramfeatureClassName*@paramtargetFeatureName SDE中目标表名称*@throwsIOException*/public static voidshfToSDE(String shapePath,String featureClassName,String targetFeatureName) throwsIOException{

//初始化AE环境initializeArcGISLincenses();//获取shapefile要素集IFeatureClass shpFeature = getFeatureClass(shapePath,featureClassName);//获取SDE工作空间IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)ConnectSDE.openSDE();//指定SDE要素集IFeatureClass sdefeatureClass = featureWorkspace.openFeatureClass(targetFeatureName);//IFeatureCursor接口可用来访问要素类的一系列要素IFeatureCursor featureCursor = shpFeature.search(null,true);//NextFeature方法第一次调用时,实际指向第一条记录,之后每一次调用都是指向下一条记录//获取第一个featureIFeature feature = featureCursor.nextFeature();IFeatureCursor sdeFeatureCursor = sdefeatureClass.IFeatureClass_insert(true);IFeatureBuffer sdeFeatureBuffer = null;while(feature != null){

sdeFeatureBuffer = sdefeatureClass.createFeatureBuffer();IField shpField = null;IFields shpFields = feature.getFields();for(inti=0;i; i++){

shpField = shpFields.getField(i);intindex = sdeFeatureBuffer.getFields().findField(shpField.getName());if(index != -1){

sdeFeatureBuffer.setValue(index,feature.getValue(i));}

}

sdeFeatureCursor.insertFeature(sdeFeatureBuffer);sdeFeatureCursor.flush();feature = featureCursor.nextFeature();}

featureWorkspace = null;}

最后

以上就是俏皮耳机为你收集整理的Java调用arcgis导入shape,java for arcgis 之——将shapefile导入SDE的全部内容,希望文章能够帮你解决Java调用arcgis导入shape,java for arcgis 之——将shapefile导入SDE所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部