protected override void OnClick() { string tablePath = Path.Combine(DataPath, @"File-Based\MajorCities.csv"); string tableName = Path.GetFileName(tablePath); Type factoryType = Type.GetTypeFromProgID("esriDataSourcesFile.TextFileWorkspaceFactory"); IWorkspaceFactory workspaceFactory = Activator.CreateInstance(factoryType) as IWorkspaceFactory; IWorkspace workspace = workspaceFactory.OpenFromFile(Path.GetDirectoryName(tablePath), 0); ITable table = ((IFeatureWorkspace) workspace).OpenTable(tableName); ISpatialReference sRef = CreateSpatialReference(esriSRGeoCSType.esriSRGeoCS_WGS1984); IFeatureClass featureClass = CreateXYEventFeature(table, "POINT_X", "POINT_y", sRef); IFeatureLayer featureLayer = new FeatureLayerClass { FeatureClass = featureClass, Name = "CSV XY Event Table" }; IFeatureLayerSourcePageExtension sourcePageExtension = new XYDataSourcePageExtensionClass(); ((ILayerExtensions) featureLayer).AddExtension(sourcePageExtension); ArcMap.Document.FocusMap.AddLayer(featureLayer); ArcMap.Document.UpdateContents(); }
public void CreateXYLayer() { try { //IMxDocument pMxDoc; //IMap pMap = axMapControl.Map; ////IApplication m_app; //IApplication m_app = default(IApplication); ////IApplication m_app = Application; // //IMxApplication m_appsd; ////pMxDoc = (IMxDocument)m_app.Document; ////pMap = pMxDoc.FocusMap; //int i =pMap.MapSurroundCount; IApplication m_app = axMapControl.ActiveView.ScreenDisplay as IApplication; //IApplication m_app = ESRI.ArcGIS.ArcMap.Application; //IApplication m_app = axMapControl.app IMxDocument pMxDoc; IMap pMap; pMxDoc = (IMxDocument)m_app.Document; pMap = pMxDoc.FocusMap; // Get the table named XYSample.txt IStandaloneTableCollection pStTabCol; IStandaloneTable pStandaloneTable; ITable pTable = null; pStTabCol = (IStandaloneTableCollection) pMap; for (int intCount = 0; intCount < pStTabCol.StandaloneTableCount; intCount++) { pStandaloneTable = (IStandaloneTable) pStTabCol.get_StandaloneTable(intCount); if (pStandaloneTable.Name == @"c:\XYSample.txt") { pTable = pStandaloneTable.Table; break; } } if (pTable == null) { MessageBox.Show("The table was not found"); return; } // Get the table name object IDataset pDataSet; IName pTableName; pDataSet = (IDataset) pTable; pTableName = pDataSet.FullName; // Specify the X and Y fields IXYEvent2FieldsProperties pXYEvent2FieldsProperties; pXYEvent2FieldsProperties = new XYEvent2FieldsPropertiesClass(); pXYEvent2FieldsProperties.XFieldName = "x"; pXYEvent2FieldsProperties.YFieldName = "y"; pXYEvent2FieldsProperties.ZFieldName = ""; // Specify the projection ISpatialReferenceFactory pSpatialReferenceFactory; pSpatialReferenceFactory = new SpatialReferenceEnvironmentClass(); IProjectedCoordinateSystem pProjectedCoordinateSystem; pProjectedCoordinateSystem = pSpatialReferenceFactory.CreateProjectedCoordinateSystem(26911); // esriSRProjCS_NAD1983UTM_11N // Create the XY name object and set it's properties IXYEventSourceName pXYEventSourceName = new XYEventSourceNameClass(); IName pXYName; IXYEventSource pXYEventSource; pXYEventSourceName.EventProperties = pXYEvent2FieldsProperties; pXYEventSourceName.SpatialReference = pProjectedCoordinateSystem; pXYEventSourceName.EventTableName = pTableName; pXYName = (IName) pXYEventSourceName; pXYEventSource = (IXYEventSource) pXYName.Open(); // Create a new Map Layer IFeatureLayer pFLayer = new FeatureLayerClass(); pFLayer.FeatureClass = (IFeatureClass) pXYEventSource; pFLayer.Name = "Sample XY Event layer"; // Add the layer extension (this is done so that when you edit // the layer's Source properties and click the Set Data Source // button, the Add XY Events Dialog appears) ILayerExtensions pLayerExt; IFeatureLayerSourcePageExtension pRESPageExt = new XYDataSourcePageExtensionClass(); pLayerExt = (ILayerExtensions) pFLayer; pLayerExt.AddExtension(pRESPageExt); pMap.AddLayer(pFLayer); } catch (System.Exception SysEx) { MessageBox.Show(SysEx.Message,".NET Error: ",MessageBoxButtons.OK,MessageBoxIcon.Warning); } }
//使用addXY进行经纬度描点 public void CreateXYLayer() { try { IMxDocument pMxDoc; IMap pMap = axMapControl.Map; IApplication m_app; IDocument doc = new MxDocumentClass(); m_app = doc.Parent; pMxDoc = (IMxDocument)m_app.Document; pMap = pMxDoc.FocusMap; IStandaloneTableCollection pStTabCol; IStandaloneTable pStandaloneTable; ITable pTable = null; pStTabCol = (IStandaloneTableCollection)pMap; for (int intCount = 0; intCount < pStTabCol.StandaloneTableCount; intCount++) { pStandaloneTable = (IStandaloneTable)pStTabCol.get_StandaloneTable(intCount); if (pStandaloneTable.Name == @"e:\XYSample.txt") { pTable = pStandaloneTable.Table; break; } } if (pTable == null) { MessageBox.Show("The table was not found"); return; } IDataset pDataSet; IName pTableName; pDataSet = (IDataset)pTable; pTableName = pDataSet.FullName; IXYEvent2FieldsProperties pXYEvent2FieldsProperties; pXYEvent2FieldsProperties = new XYEvent2FieldsPropertiesClass(); pXYEvent2FieldsProperties.XFieldName = "x"; pXYEvent2FieldsProperties.YFieldName = "y"; pXYEvent2FieldsProperties.ZFieldName = ""; ISpatialReferenceFactory pSpatialReferenceFactory; pSpatialReferenceFactory = new SpatialReferenceEnvironmentClass(); IProjectedCoordinateSystem pProjectedCoordinateSystem; pProjectedCoordinateSystem = pSpatialReferenceFactory.CreateProjectedCoordinateSystem(26911); // esriSRProjCS_NAD1983UTM_11N // Create the XY name object and set it's properties IXYEventSourceName pXYEventSourceName = new XYEventSourceNameClass(); IName pXYName; IXYEventSource pXYEventSource; pXYEventSourceName.EventProperties = pXYEvent2FieldsProperties; pXYEventSourceName.SpatialReference = pProjectedCoordinateSystem; pXYEventSourceName.EventTableName = pTableName; pXYName = (IName)pXYEventSourceName; pXYEventSource = (IXYEventSource)pXYName.Open(); // Create a new Map Layer IFeatureLayer pFLayer = new FeatureLayerClass(); pFLayer.FeatureClass = (IFeatureClass)pXYEventSource; pFLayer.Name = "Sample XY Event layer"; // Add the layer extension (this is done so that when you edit // the layer's Source properties and click the Set Data Source // button, the Add XY Events Dialog appears) ILayerExtensions pLayerExt; IFeatureLayerSourcePageExtension pRESPageExt = new XYDataSourcePageExtensionClass(); pLayerExt = (ILayerExtensions)pFLayer; pLayerExt.AddExtension(pRESPageExt); pMap.AddLayer(pFLayer); } catch (System.Exception SysEx) { MessageBox.Show(SysEx.Message, ".NET Error: ", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }