/// <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); } } } }