300 likes | 502 Views
Adding shapefiles as feature layers Adding coverages as feature layers Adding grids as raster layers Adding tables as table windows Adding text files as table windows Getting projection information Getting statistics about a field Creating a new shapefile theme. SpatialQuery Filter.
E N D
Adding shapefiles as feature layers • Adding coverages as feature layers • Adding grids as raster layers • Adding tables as table windows • Adding text files as table windows • Getting projection information • Getting statistics about a field • Creating a new shapefile theme
IWorkspaceFactory • To create a new workspace • To connect to an existing workspace • To find information about a workspace
Sub add_shapefile() Dim pmxdoc As IMxDocument Set pmxdoc = ThisDocument Dim pmap As IMap Set pmap = pmxdoc.FocusMap 'Objects for getting data from disk: Dim pworkspacefactory As IWorkspaceFactory Set pworkspacefactory = New ShapefileWorkspaceFactory Dim pworkspace As IFeatureWorkspace Set pworkspace = pworkspacefactory.OpenFromFile("C:\test", 0) 'Get shapefile from feature workspace: Dim pFeatureClass As IFeatureClass Set pFeatureClass = pworkspace.OpenFeatureClass("towns")
'Set the feature class to the arcmap Feature Layer Dim pFeatureLayer As IFeatureLayer Set pFeatureLayer = New FeatureLayer Set pFeatureLayer.FeatureClass = pFeatureClass 'Name the theme for table of contents, add it to pmap and refresh: pFeatureLayer.Name = "Alaska Villages and Towns" pmap.AddLayer pFeatureLayer pmxdoc.ActiveView.Refresh pmxdoc.UpdateContents
Sub add_arcinfo_polygon_coverage() Dim pmxdoc As IMxDocument Set pmxdoc = ThisDocument Dim pmap As IMap Set pmap = pmxdoc.FocusMap 'Objects for getting data from disk: Dim pworkspacefactory As IWorkspaceFactory Set pworkspacefactory = New ArcInfoWorkspaceFactory Dim pworkspace As IFeatureWorkspace Set pworkspace = pworkspacefactory.OpenFromFile("C:\test", 0) 'Get polygons in quad_polys coverage from feature workspace: Dim pFeatureClass As IFeatureClass Set pFeatureClass = pworkspace.OpenFeatureClass("quad_polys:polygon")
'Set the feature class to the arcmap Feature Layer Dim pFeatureLayer As IFeatureLayer Set pFeatureLayer = New FeatureLayer Set pFeatureLayer.FeatureClass = pFeatureClass 'Name the theme for table of contents, add it to pmap and refresh: pFeatureLayer.Name = "Alaska 1:63360 Quadrangles" pmap.AddLayer pFeatureLayer pmxdoc.ActiveView.Refresh pmxdoc.UpdateContents
Sub add_grid() Dim pmxdoc As IMxDocument Set pmxdoc = ThisDocument Dim pmap As IMap Set pmap = pmxdoc.FocusMap 'Objects for getting data from disk: Dim pworkspacefactory As IWorkspaceFactory Set pworkspacefactory = New RasterWorkspaceFactory Dim pRasterFolder As IRasterWorkspace Set pRasterFolder = pworkspacefactory.OpenFromFile("C:\test\", 0) 'Get raster grid from raster workspace: Dim pRDataSet As IRasterDataset Set pRDataSet = pRasterFolder.OpenRasterDataset("elevation")
'Set raster data set to Arcmap raster layer: Dim pRasterLayer As IRasterLayer Set pRasterLayer = New RasterLayer pRasterLayer.CreateFromDataset pRDataSet 'Name the grid layer for table of contents, add it to pmap and refresh: pRasterLayer.Name = "Alaska Elevation Grid" pmap.AddLayer pRasterLayer pmxdoc.ActiveView.Refresh pmxdoc.UpdateContents End Sub
Adding A Table • Create new workspace name • Specify pathname • Specify workspace factory program ID
Sub add_table() Dim pmxdoc As IMxDocument Set pmxdoc = ThisDocument Dim pmap As IMap Set pmap = pmxdoc.FocusMap 'Set workspacename object: Dim pWorkspacename As IWorkspaceName Set pWorkspacename = New WorkspaceName pWorkspacename.WorkspaceFactoryProgID = "esriDataSourcesFile.ShapefileWorkspaceFactory" pWorkspacename.PathName = "c:\test"
Adding A Table(continued..) • Create new workspace name • Specify pathname • Specify workspace factory program ID
'Set Dataset name workspace and name Dim pDatasetname As IDatasetName Set pDatasetname = New TableName Set pDatasetname.WorkspaceName = pWorkspacename pDatasetname.Name = "salmon.dbf" ‘Open table: Dim pTable As ITable Dim pname As IName Set pname = pDatasetname ‘QueryInterface Set pTable = pname.Open
'Add table to collection of pmap tables... Dim ptablecollection As ITableCollection Set ptablecollection = pmap ptablecollection.AddTable pTable 'Open the table in a arcmap table-window: Dim ptablewindow As ITableWindow Set ptablewindow = New TableWindow Set ptablewindow.Table = pTable Set ptablewindow.Application = Application ptablewindow.Show True End Sub
Adding A Text Table • Create new workspace name • Specify pathname • Specify workspace factory program ID
Sub add_text table() Dim pmxdoc As IMxDocument Set pmxdoc = ThisDocument Dim pmap As IMap Set pmap = pmxdoc.FocusMap 'Set workspacename object: Dim pWorkspacename As IWorkspaceName Set pWorkspacename = NewWorkspaceName pWorkspacename.WorkspaceFactoryProgID = "esriDataSourcesOleDB.TextFileWorkspaceFactory " pWorkspacename.PathName = "c:\test"
'Set table object and open table.. Dim pDatasetname As IDatasetName Set pDatasetname = New TableName Set pDatasetname.WorkspaceName = pWorkspacename pDatasetname.Name = "salmon.txt" Dim pTable As ITable Dim pname As IName Set pname = pDatasetname 'query interface Set pTable = pname.Open 'Open the table in a arcmap table-window: Dim ptablewindow As ITableWindow Set ptablewindow = New TableWindow Set ptablewindow.Table = pTable Set ptablewindow.Application = Application ptablewindow.Show True 'Add table to collection of pmap tables... Dim ptablecollection As ITableCollection Set ptablecollection = pmap ptablecollection.AddTable pTable End sub
Sub projection_info() Dim pmxdoc As IMxDocument Set pmxdoc = ThisDocument Dim pstatusbar As IStatusBar Set pstatusbar = Application.StatusBar Dim pmap As IMap Set pmap = pmxdoc.FocusMap Dim pflayer As IFeatureLayer Set pflayer = pmap.Layer(0) 'Get spatial reference info: Dim pGeoDataset As IGeoDataset Set pGeoDataset = pflayer Dim pSpatialRef As ISpatialReference Set pSpatialRef = pGeoDataset.SpatialReference
'Get projected coordinate system information: Dim pprojection As IProjectedCoordinateSystem Set pprojection = pSpatialRef Dim p_xyunit As ILinearUnit Set p_xyunit = pprojection.CoordinateUnit Dim p_spheroid As IGeographicCoordinateSystem Set p_spheroid = pprojection.GeographicCoordinateSystem Dim pDatum As IDatum Set pDatum = p_spheroid.Datum ‘Display projection information on status bar pane#0 pstatusbar.Message(0) = "Projection: " & pSpatialRef.Name & _ " Datum: " & pDatum.Name & _ " X,Y units: " & p_xyunit.Name End Sub
Sub TEST_STATS() Dim pMxDoc As IMxDocument, pFLayer As IFeatureLayer Set pMxDoc = ThisDocument Set pFLayer = pMxDoc.FocusMap.Layer(0) ‘Set cursor to all feature layer rows: Dim pCursor As ICursor Set pCursor = pFLayer.Search(Nothing, False) ‘Specify field and rows for statisitics: Dim pData As IDataStatistics, pStatResults As IStatisticsResults Set pData = New DataStatistics pData.Field = "hectares" 'field to get statistics from Set pData.Cursor = pCursor 'all rows in feature layer ‘Get the statistics results and output to statusbar the mean, count properties: Set pStatResults = pData.Statistics Dim pstatusbar As IStatusBar Set pstatusbar = Application.StatusBar pstatusbar.Message(0) = "Hectares stats...Mean = " & pStatResults.Mean & " N = " & pStatResults.Count End Sub
Creating a New Shapefile With 3 Fields Sub CreateShapefile() Const strFolder As String = "c:\test" Const strName As String = "polygon_theme" ' Dont include .shp extension Const strShapeFieldName As String = "Shape" ' Open the folder to contain the shapefile as a workspace Dim pFWS As IFeatureWorkspace Dim pWorkspaceFactory As IWorkspaceFactory Set pWorkspaceFactory = New ShapefileWorkspaceFactory Set pFWS = pWorkspaceFactory.OpenFromFile(strFolder, 0) ' Set up a fields collection that will contain the three attribute fields Dim pFields As IFields Dim pFieldsEdit As IFieldsEdit Set pFields = New Fields Set pFieldsEdit = pFields
'Create three attribute fields: a shape field, a character string field, a double field Dim pField As IField Dim pFieldEdit As IFieldEdit ' Make the shape field ' it will need a geometry definition, with a spatial reference Set pField = New Field Set pFieldEdit = pField pFieldEdit.Name = strShapeFieldName pFieldEdit.Type = esriFieldTypeGeometry Dim pGeomDef As IGeometryDef Dim pGeomDefEdit As IGeometryDefEdit Set pGeomDef = New GeometryDef Set pGeomDefEdit = pGeomDef With pGeomDefEdit .GeometryType = esriGeometryPolygon Set .SpatialReference = New UnknownCoordinateSystem End With Set pFieldEdit.GeometryDef = pGeomDef pFieldsEdit.AddField pField
' Add a character string field that could hold up to 4 characters: Set pField = New Field Set pFieldEdit = pField With pFieldEdit .Length = 4 .Name = "Char_Field" .Type = esriFieldTypeString End With pFieldsEdit.AddField pField ' Add a double field that could hold area: Set pField = New Field Set pFieldEdit = pField With pFieldEdit .Length = 8 '8 bytes .Name = "Area" .Type = esriFieldTypeDouble .Precision = 16 'Total number of digits .Scale = 3 'Number of digits beyond decimal point End With pFieldsEdit.AddField pField ' Create the shapefile (some parameters apply to geodatabase ---coded to Nothing) Dim pFeatClass As IFeatureClass Set pFeatClass = pFWS.CreateFeatureClass(strName, pFields, Nothing, Nothing, _ esriFTSimple, strShapeFieldName, "") End Sub