📄 joinrelationship.java.svn-base
字号:
import java.io.IOException;
import java.net.UnknownHostException;
import com.esri.arcgis.carto.AnnotationVBScriptEngine;
import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.carto.IAnnotateLayerProperties;
import com.esri.arcgis.carto.IAnnotateLayerPropertiesCollection;
import com.esri.arcgis.carto.IAnnotationExpressionEngine;
import com.esri.arcgis.carto.IDisplayRelationshipClass;
import com.esri.arcgis.carto.IDisplayRelationshipClassProxy;
import com.esri.arcgis.carto.IDisplayTable;
import com.esri.arcgis.carto.IFeatureLayer;
import com.esri.arcgis.carto.IGeoFeatureLayer;
import com.esri.arcgis.carto.ILabelEngineLayerProperties;
import com.esri.arcgis.carto.IMap;
import com.esri.arcgis.carto.ITableCollection;
import com.esri.arcgis.carto.LabelEngineLayerProperties;
import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.FeatureClass;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geodatabase.IFields;
import com.esri.arcgis.geodatabase.IMemoryRelationshipClassFactory;
import com.esri.arcgis.geodatabase.IObjectClass;
import com.esri.arcgis.geodatabase.IRelationshipClass;
import com.esri.arcgis.geodatabase.ITable;
import com.esri.arcgis.geodatabase.IWorkspace;
import com.esri.arcgis.geodatabase.IWorkspaceFactory;
import com.esri.arcgis.geodatabase.MemoryRelationshipClassFactory;
import com.esri.arcgis.geodatabase.ObjectClass;
import com.esri.arcgis.geodatabase.Table;
import com.esri.arcgis.geodatabase.esriJoinType;
import com.esri.arcgis.geodatabase.esriRelCardinality;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
public class JoinRelationship {
/**
* 把两个 属性表关联起来
* @param pForeignTable
* @throws AutomationException
* @throws IOException
*/
private void joinRelationship(IFeatureClass featureClass,ITable pForeignTable) throws AutomationException, IOException{
IFeatureLayer mainLayer = new FeatureLayer();
mainLayer.setFeatureClassByRef(featureClass);
ITable pPrimaryTable = new Table(featureClass);
String sFieldName = "school_id";
IMemoryRelationshipClassFactory pMemoryRelationshipCF = new MemoryRelationshipClassFactory();
IRelationshipClass pRelationshipClass = pMemoryRelationshipCF.open("TabletoLayer", new ObjectClass(pPrimaryTable),
sFieldName,new ObjectClass(pForeignTable), sFieldName, "Forward", "Backward",
esriRelCardinality.esriRelCardinalityOneToOne);
IDisplayRelationshipClass pDisplayRelationshipC = (IDisplayRelationshipClass)mainLayer;
pDisplayRelationshipC.displayRelationshipClass(pRelationshipClass, esriJoinType.esriLeftOuterJoin);
System.out.println(pDisplayRelationshipC.toString());
ITable table = new Table(pDisplayRelationshipC); //组合以后的 属性表
System.out.println("22222222!!!!!!!!!!!!!!!"+table.getFields().getFieldCount());
System.out.println(table.getFields().getField(1).getName());
System.out.println(table.getFields().getField(2).getName());
System.out.println(table.getFields().getField(4).getName());
System.out.println(table.getFields().getField(6).getName());
IFeatureLayer labelLayer = new FeatureLayer();
labelLayer.setFeatureClassByRef((FeatureClass)table);
}
/**
* 根据一个 shape文件的路径 和 shape文件的名字 返回他对应的ITable
* @param shapePath
* @param shapeName
* @return
* @throws UnknownHostException
* @throws IOException
*/
private ITable loadShape(String shapePath,String shapeName) throws UnknownHostException, IOException{
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
IWorkspace pWorkspace = pWorkspaceFactory.openFromFile(shapePath, 0);
IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspace;
ITable pForeignTable = pFeatureWorkspace.openTable(shapeName);
return pForeignTable;
}
/**
* 根据一个 shape文件的路径 和 shape文件的名字 返回他对应的ITable
* @param shapePath
* @param shapeName
* @return
* @throws UnknownHostException
* @throws IOException
*/
private IFeatureClass loadFeatureClass(String shapePath,String shapeName) throws UnknownHostException, IOException{
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
IWorkspace pWorkspace = pWorkspaceFactory.openFromFile(shapePath, 0);
IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspace;
//ITable pPrimaryTable = pFeatureWorkspace.openTable("school");
IFeatureClass featureClass = pFeatureWorkspace.openFeatureClass(shapeName);
return featureClass;
}
/**
public void JoinlLabel(){
//Dim pmxdoc As IMxDocument
//Set pmxdoc = ThisDocument
IFeatureLayer pFeatLayer = null;
pFeatLayer = JoinToTable(pmxdoc);
pFeatLayer.setName("test");
IGeoFeatureLayer pGeoLayer = null;
pGeoLayer = JoinToTable(pmxdoc)
//'pmxdoc.FocusMap.AddLayer pFeatLayer
System.out.println(pGeoLayer.getDisplayFeatureClass().findField("复件 city.县区名"));
IFields pFields = null;
pFields = pGeoLayer.getDisplayFeatureClass().getFields();
System.out.println(pFields.getFieldCount());
IAnnotateLayerPropertiesCollection pAnnoColl = null;
pAnnoColl = pGeoLayer.getAnnotationProperties();
System.out.println( "AnnoCollection Count:" + pAnnoColl.getCount());
pAnnoColl.clear();
IAnnotateLayerProperties pAnnoProp = null;
ILabelEngineLayerProperties pLabelEngine = null;
pLabelEngine = new LabelEngineLayerProperties();
pLabelEngine.setIsExpressionSimple(true);
pLabelEngine.setExpression("[复件 city.县区名]");
pAnnoColl.add(pLabelEngine);
//'pGeoLayer.DisplayField = "复件 city.县区名"
pGeoLayer.setDisplayAnnotation(true);
//pmxdoc.ActiveView.Refresh
}
private IDisplayRelationshipClass JoinToTable(IMxDocument pmxdoc){
IMap pMap = null;
IGeoFeatureLayer pLayer = null;
IDisplayRelationshipClass pDpyRC = null;
ITableCollection pTableColl = null;
ITable pTable = null;
IMemoryRelationshipClassFactory pMemRCFact = null;
IRelationshipClass pRelClass = null;
// Set pMap = pmxdoc.FocusMap
// ' get a reference to the layer
pLayer = (IGeoFeatureLayer) pMap.getLayer(0);
pDpyRC = (IDisplayRelationshipClass) pLayer;
// ' get a reference to the table
pTableColl = pMap;
pTable = pTableColl.getTable(0);
System.out.print(pTable.findField("县区名"));
// ' create a relationship class in memory
pMemRCFact = new MemoryRelationshipClassFactory();
pRelClass = pMemRCFact.open("cityJoin", new ObjectClass(pLayer
.getFeatureClass()), "FID", new ObjectClass(pTable),
"OID", "Forwards", "Backwards",
esriRelCardinality.esriRelCardinalityOneToOne);
// ' perform the join
pDpyRC.displayRelationshipClass(pRelClass,
esriJoinType.esriLeftOuterJoin);
return pDpyRC;
}
*/
public static void main(String[] args) throws UnknownHostException, IOException{
EngineInitializer.initializeEngine();
final AoInitialize aoInit =new AoInitialize();
try{
//aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);
aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
JoinRelationship object = new JoinRelationship();
IFeatureClass tableSchoolLayer = object.loadFeatureClass("C:\\gis_ex09\\ex05","school");
ITable tableStudent = object.loadShape("C:\\gis_ex09\\ex05","student");
object.joinRelationship(tableSchoolLayer,tableStudent);
}catch(Exception e){
e.printStackTrace();
}finally{
aoInit.shutdown();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -