/// <summary> /// 将带有x、y字段的ITable对象转化为IFeatureClass /// </summary> /// <param name="pTable">带有x、y字段的ITable数据源</param> /// <param name="xName">X字段名称</param> /// <param name="yName">y字段名称</param> /// <param name="zName">z字段名称(可为空)</param> /// <param name="spatialReference">空间坐标参考</param> /// <returns></returns> private IFeatureClass OpenXYData(ITable pTable, string xName, string yName, string zName, ISpatialReference spatialReference) { // 将带有x、y字段的ITable对象转化为IFeatureClass,确定X、Y、Z字段以及空间参考坐标 // xy字段属性描述 IXYEvent2FieldsProperties pXYEventFieldsPro = new XYEvent2FieldsPropertiesClass(); pXYEventFieldsPro.XFieldName = xName; pXYEventFieldsPro.YFieldName = yName; pXYEventFieldsPro.ZFieldName = zName; //XY事件 数据表名称描述 IXYEventSourceName pXYEventSourceName = new XYEventSourceNameClass(); pXYEventSourceName.EventProperties = pXYEventFieldsPro; IDataset ds = pTable as IDataset; IName tname = ds.FullName; pXYEventSourceName.SpatialReference = spatialReference; pXYEventSourceName.EventTableName = tname; IName xyname = pXYEventSourceName as IName; IXYEventSource xysrc = (IXYEventSource)xyname.Open(); return(xysrc as IFeatureClass); }
/// <summary> /// Creates the XY event source from a table which can be used to dynamically create a point feature class. /// </summary> /// <param name="xyTable">The table containing fields for X and Y coordinates.</param> /// <param name="xFieldName">Name of the x coordinate field.</param> /// <param name="yFieldName">Name of the y coordinate field.</param> /// <param name="spatialReference">The spatial reference of the points contained in the table.</param> /// <returns></returns> /// <remarks>An <see cref="IXYEventSource"/> is a dynmanic point feature class which generates features /// based on X and Y coordinates in a table.<para/> /// Since XYEventSource inherits from FeatureClass, it implements the same interfaces and can be treated like any other point feature class.</remarks> public static IXYEventSource CreateXYEventSourceFromTable(ITable xyTable, string xFieldName, string yFieldName, ISpatialReference spatialReference) { // Validate arguments if (xyTable == null) { throw new ArgumentNullException("xyTable", "XY table has not been set."); } if (string.IsNullOrEmpty(xFieldName)) { throw new ArgumentNullException("xFieldName", "X field name has not been set."); } if (string.IsNullOrEmpty(yFieldName)) { throw new ArgumentNullException("yFieldName", "Y field name has not been set."); } if (spatialReference == null) { throw new ArgumentNullException("spatialReference", "Spatial reference has not been set."); } // QI to get the dataset IDataset dataset = xyTable as IDataset; // Verify fields exist and are of a numeric type Int32 index = xyTable.FindField(xFieldName); if (index == -1) { throw new ArgumentException(string.Format("{0} field does not exist in table {1}.", xFieldName, dataset.Name)); } index = xyTable.FindField(yFieldName); if (index == -1) { throw new ArgumentException(string.Format("{0} field does not exist in table {1}.", yFieldName, dataset.Name)); } // Set the field properties for the XY event theme IXYEvent2FieldsProperties xyEvent2FieldsProperties = new XYEvent2FieldsPropertiesClass(); xyEvent2FieldsProperties.XFieldName = xFieldName; xyEvent2FieldsProperties.YFieldName = yFieldName; xyEvent2FieldsProperties.ZFieldName = string.Empty; // Create XY event theme IXYEventSourceName xyEventSourceName = new XYEventSourceNameClass(); xyEventSourceName.EventProperties = xyEvent2FieldsProperties; xyEventSourceName.SpatialReference = spatialReference; xyEventSourceName.EventTableName = dataset.FullName; // Create XY event source. IName name = (xyEventSourceName as IName); IXYEventSource xyEventSource = (name.Open() as IXYEventSource); // Return value return(xyEventSource); }
private static IFeatureClass CreateXYEventFeature(ITable xyTable, string xField, string yField, ISpatialReference spatialReference) { IXYEvent2FieldsProperties xyEventProperties = new XYEvent2FieldsPropertiesClass { XFieldName = xField, YFieldName = yField }; IXYEventSourceName xyEventSourceName = new XYEventSourceNameClass { EventProperties = xyEventProperties, EventTableName = ((IDataset) xyTable).FullName, SpatialReference = spatialReference }; IName name = xyEventSourceName as IName; IXYEventSource xyEventSource = name.Open() as IXYEventSource; return xyEventSource as IFeatureClass; }
/// <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); } } } }
private void btnSelect_Click(object sender, EventArgs e) { REngine pEngine = mForm.pEngine; clsSnippet pSnippet = new clsSnippet(); string strBLLayerName = cboTargetLayer.Text; string strXFieldName = cboXField.Text; string strYFieldName = cboYField.Text; string strSALayerName = cboStudyArea.Text; int intLIndex = pSnippet.GetIndexNumberFromLayerName(pActiveView, strSALayerName); int intBLIndex = pSnippet.GetIndexNumberFromLayerName(pActiveView, strBLLayerName); ILayer pSALayer = mForm.axMapControl1.get_Layer(intLIndex); IFeatureLayer pSAFLayer = (IFeatureLayer)pSALayer; IFeatureClass pSAFClass = pSAFLayer.FeatureClass; ILayer pBLLayer = mForm.axMapControl1.get_Layer(intBLIndex); IFeatureLayer pBLFLayer = (IFeatureLayer)pBLLayer; IGeoFeatureLayer pBLGeoFLayer = (IGeoFeatureLayer)pBLFLayer; IAttributeTable pBLAttTable = (IAttributeTable)pBLGeoFLayer; ITable pBLTable = (ITable)pBLAttTable; //Get File Name and Path IDataLayer pSADLayer = (IDataLayer)pSALayer; IDatasetName pSADSName = (IDatasetName)pSADLayer.DataSourceName; string strSAFCName = pSADSName.Name; string strSAFCPath = pSADSName.WorkspaceName.PathName; string strSAFullName = strSAFCPath + "\\" + strSAFCName + ".shp"; IDataLayer pBLDLayer = (IDataLayer)pBLLayer; IDatasetName pBLDSName = (IDatasetName)pBLDLayer.DataSourceName; string strBLFCName = pBLDSName.Name; string strBLFCPath = pBLDSName.WorkspaceName.PathName; string strBLFullName = strBLFCPath + "\\" + strBLFCName + "1.dbf";; mForm = System.Windows.Forms.Application.OpenForms["MainForm"] as MainForm; pActiveView = mForm.axMapControl1.ActiveView; IMap pMap = pActiveView.FocusMap; int intXFieldIdx = pBLFLayer.FeatureClass.Fields.FindField(strXFieldName) - 1; //-1 to use for R idx int intYFieldIdx = pBLFLayer.FeatureClass.Fields.FindField(strYFieldName) - 1; //StringBuilder plotCommmand = new StringBuilder(); //plotCommmand.Append("library(circular);library(maptools);library(spdep);library(foreign);"); //plotCommmand.Append("city.shp <- readShapePoly(" + strSAFullName + ");"); //plotCommmand.Append("polycoords <- as.matrix(city.shp@polygons[[1]]@Polygons[[1]]@coords);"); //plotCommmand.Append("bll.df <- as.data.frame(read.dbf(" + strBLFullName + ");"); //plotCommmand.Append("del.row <- which(point.in.polygon(bll.df[,6],bll.df[,7],polycoords[,1],polycoords[,2])==0);"); //plotCommmand.Append("hist(bll.df[,6])"); //pEngine.Evaluate(plotCommmand.ToString()); pEngine.Evaluate(@"library(circular);library(maptools);library(spdep);library(foreign)"); pEngine.Evaluate(@"city.shp <- readShapePoly(" + strSAFullName + ")"); //DataFrame pGV =pEngine.Evaluate(@"bll.df <- as.data.frame(read.dbf(" + strBLFullName + ")").AsDataFrame(); //DataFrame pBLLdf = pEngine. pEngine.Evaluate("bll.df <- as.data.frame(read.dbf(\"" + strBLFullName + "\")"); pEngine.Evaluate("hist(bll.df[,6])"); // 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 == "syracuse-bll.csv") { 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 = new XYEvent2FieldsPropertiesClass(); pXYEvent2FieldsProperties.XFieldName = cboXField.Text; pXYEvent2FieldsProperties.YFieldName = cboYField.Text; pXYEvent2FieldsProperties.ZFieldName = ""; // Create the XY name object and set it's properties IXYEventSourceName pXYEventSourceName = new XYEventSourceNameClass(); pXYEventSourceName.EventProperties = pXYEvent2FieldsProperties; IGeoDataset pGeoDataset = (IGeoDataset)pBLDLayer; pXYEventSourceName.SpatialReference = pGeoDataset.SpatialReference; //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 = "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); }