/// <summary> /// this function takes a table and creates a shapefile (XY events) layer /// that is then added to the map /// </summary> /// <param name="pMxDoc">the map doc</param> /// <param name="sWorkspacePath">where to put the shapefile that is generated from the xy events / and where the dbf lives</param> /// <param name="sTableName">the name of the dbf to open</param> /// <param name="pSpatRef">the spatial ref for the prj file for the shapefile</param>\ /// <changelog> /// /// comments created. /// /// </changelog> public void addXYEvents(IMxDocument pMxDoc, string sWorkspacePath, string sTableName, ISpatialReference pSpatRef, string xField, string yField, string zField) { IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass(); IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWSF.OpenFromFile(sWorkspacePath, 0); ITable pTable = (ITable)pFeatureWorkspace.OpenTable(sTableName); if (pTable == null) { MessageBox.Show("The table was not found"); return; } // Create a new standalone table and add it // to the collection of the focus map IStandaloneTable pStTab = new StandaloneTableClass(); pStTab.Table = (ITable)pTable; //sortTable(ref pTable); // Get the table name object IDataset pDataSet = (IDataset)pStTab; IName pTableName = pDataSet.FullName; // Specify the X and Y fields IXYEvent2FieldsProperties pXYEvent2FieldsProperties = new XYEvent2FieldsPropertiesClass(); pXYEvent2FieldsProperties.XFieldName = xField; pXYEvent2FieldsProperties.YFieldName = yField; pXYEvent2FieldsProperties.ZFieldName = zField; // Create the XY name object and set it's properties IXYEventSourceName pXYEventSourceName = new XYEventSourceNameClass(); pXYEventSourceName.EventProperties = pXYEvent2FieldsProperties; if (pSpatRef != null) { pXYEventSourceName.SpatialReference = pSpatRef; } pXYEventSourceName.EventTableName = pTableName; IName pXYName = (IName)pXYEventSourceName; IXYEventSource pXYEventSource = (IXYEventSource)pXYName.Open(); // Create a new Map Layer IFeatureLayer pFLayer = new FeatureLayerClass(); pFLayer.FeatureClass = (IFeatureClass)pXYEventSource; pFLayer.Name = sTableName; //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) XYDataSourcePageExtension pRESPageExt = new XYDataSourcePageExtension(); ILayerExtensions pLayerExt = (ILayerExtensions)pFLayer; pLayerExt.AddExtension(pRESPageExt); //Get the FcName from the featureclass IFeatureClass pFc = pFLayer.FeatureClass; pDataSet = (IDataset)pFc; IFeatureClassName pINFeatureClassName = (IFeatureClassName)pDataSet.FullName; IDatasetName pInDsName = (IDatasetName)pINFeatureClassName; //Get the selection set IFeatureSelection pFSel = (IFeatureSelection)pFLayer; ISelectionSet pSelSet = (ISelectionSet)pFSel.SelectionSet; //Define the output feature class name IFeatureClassName pFeatureClassName = new FeatureClassNameClass(); IDatasetName pOutDatasetName = (IDatasetName)pFeatureClassName; //string sDSName = ensureDataName(pDataSet.Name, sWorkspacePath); string sDSName = pDataSet.Name; // +DateTime.UtcNow.DayOfYear + DateTime.UtcNow.Hour + DateTime.UtcNow.Minute + DateTime.UtcNow.Second; pOutDatasetName.Name = sDSName; IWorkspaceName pWorkspaceName = new WorkspaceNameClass(); pWorkspaceName.PathName = sWorkspacePath; pWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.ShapeFileWorkspaceFactory"; pOutDatasetName.WorkspaceName = pWorkspaceName; pFeatureClassName.FeatureType = esriFeatureType.esriFTSimple; pFeatureClassName.ShapeType = esriGeometryType.esriGeometryPoint; pFeatureClassName.ShapeFieldName = "Shape"; //Export IExportOperation pExportOp = new ExportOperationClass(); pExportOp.ExportFeatureClass(pInDsName, null, null, null, pOutDatasetName as IFeatureClassName, 0); IFeatureClass pClass = (IFeatureClass)pFeatureWorkspace.OpenFeatureClass(sDSName); IFeatureLayer pLayer = new FeatureLayerClass(); pLayer.FeatureClass = pClass; pLayer.Name = sDSName;//pClass.AliasName; pMxDoc.AddLayer(pLayer); pMxDoc.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, pLayer, null); }
/// <summary> /// The add XY event layer. /// </summary> /// <param name="table"> /// The table. /// </param> /// <param name="query"> /// The query. /// </param> /// <param name="tweetShow"> /// The tweet show. /// </param> public static void AddXyEventLayer(ITable table, string query, bool tweetShow = true) { var mxdoc = ArcMap.Application.Document as IMxDocument; if (mxdoc != null) { var map = mxdoc.FocusMap; // Get the table named XYSample.txt var stTableCollection = map as IStandaloneTableCollection; // Get the table name object var dataset = table as IDataset; var tableName = dataset.FullName; // Specify the X and Y fields var xyEvent2FieldsProperties = new XYEvent2FieldsProperties() as IXYEvent2FieldsProperties; if (xyEvent2FieldsProperties != null) { xyEvent2FieldsProperties.XFieldName = "x"; xyEvent2FieldsProperties.YFieldName = "y"; xyEvent2FieldsProperties.ZFieldName = string.Empty; // Specify the projection var spatialReferenceFactory = new SpatialReferenceEnvironment() as ISpatialReferenceFactory; var projectedCoordinateSystem = spatialReferenceFactory.CreateGeographicCoordinateSystem( (int) esriSRGeoCSType.esriSRGeoCS_WGS1984); // Create the XY name object as set it's properties var xyEventSourceName = new XYEventSourceName() as IXYEventSourceName; xyEventSourceName.EventProperties = xyEvent2FieldsProperties; xyEventSourceName.SpatialReference = projectedCoordinateSystem; xyEventSourceName.EventTableName = tableName; IName xyName = xyEventSourceName as IName; IXYEventSource xyEventSource = xyName.Open() as IXYEventSource; // Create a new Map Layer IFeatureLayer featureLayer = new FeatureLayer() as IFeatureLayer; featureLayer.FeatureClass = xyEventSource as IFeatureClass; featureLayer.Name = query; // 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 layerExtensions = featureLayer as ILayerExtensions; XYDataSourcePageExtension resPageExtension = new XYDataSourcePageExtension(); layerExtensions.AddExtension(resPageExtension); IGeoFeatureLayer geoLayer = (IGeoFeatureLayer) featureLayer; ISimpleRenderer simpleRenderer = (ISimpleRenderer) geoLayer.Renderer; var randomNumber = NumberRandom.Next(0, Colors.Count - 1); var color = Colors[randomNumber]; IRgbColor rgbColor = new RgbColorClass(); rgbColor.Blue = color.B; rgbColor.Red = color.R; rgbColor.Green = color.G; IMarkerSymbol markerSymbol = new SimpleMarkerSymbolClass(); markerSymbol.Color = rgbColor; markerSymbol.Size = 5; simpleRenderer.Symbol = (ISymbol) markerSymbol; try { map.AddLayer(featureLayer); } catch (Exception error) { Console.WriteLine(error.Message); } } } }
/// <summary> /// The add XY event layer. /// </summary> /// <param name="table"> /// The table. /// </param> /// <param name="query"> /// The query. /// </param> /// <param name="tweetShow"> /// The tweet show. /// </param> public static void AddXyEventLayer(ITable table, string query, bool tweetShow = true) { var mxdoc = ArcMap.Application.Document as IMxDocument; if (mxdoc != null) { var map = mxdoc.FocusMap; // Get the table named XYSample.txt var stTableCollection = map as IStandaloneTableCollection; // Get the table name object var dataset = table as IDataset; var tableName = dataset.FullName; // Specify the X and Y fields var xyEvent2FieldsProperties = new XYEvent2FieldsProperties() as IXYEvent2FieldsProperties; if (xyEvent2FieldsProperties != null) { xyEvent2FieldsProperties.XFieldName = "x"; xyEvent2FieldsProperties.YFieldName = "y"; xyEvent2FieldsProperties.ZFieldName = string.Empty; // Specify the projection var spatialReferenceFactory = new SpatialReferenceEnvironment() as ISpatialReferenceFactory; var projectedCoordinateSystem = spatialReferenceFactory.CreateGeographicCoordinateSystem( (int)esriSRGeoCSType.esriSRGeoCS_WGS1984); // Create the XY name object as set it's properties var xyEventSourceName = new XYEventSourceName() as IXYEventSourceName; xyEventSourceName.EventProperties = xyEvent2FieldsProperties; xyEventSourceName.SpatialReference = projectedCoordinateSystem; xyEventSourceName.EventTableName = tableName; IName xyName = xyEventSourceName as IName; IXYEventSource xyEventSource = xyName.Open() as IXYEventSource; // Create a new Map Layer IFeatureLayer featureLayer = new FeatureLayer() as IFeatureLayer; featureLayer.FeatureClass = xyEventSource as IFeatureClass; featureLayer.Name = query; // 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 layerExtensions = featureLayer as ILayerExtensions; XYDataSourcePageExtension resPageExtension = new XYDataSourcePageExtension(); layerExtensions.AddExtension(resPageExtension); IGeoFeatureLayer geoLayer = (IGeoFeatureLayer)featureLayer; ISimpleRenderer simpleRenderer = (ISimpleRenderer)geoLayer.Renderer; var randomNumber = NumberRandom.Next(0, Colors.Count - 1); var color = Colors[randomNumber]; IRgbColor rgbColor = new RgbColorClass(); rgbColor.Blue = color.B; rgbColor.Red = color.R; rgbColor.Green = color.G; IMarkerSymbol markerSymbol = new SimpleMarkerSymbolClass(); markerSymbol.Color = rgbColor; markerSymbol.Size = 5; simpleRenderer.Symbol = (ISymbol)markerSymbol; try { map.AddLayer(featureLayer); } catch (Exception error) { Console.WriteLine(error.Message); } } } }