10楼#
发布于:2005-09-06 11:42
<P 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71">本例实现的是如何新建一个指向Shape文件的lyr文件。利用IGxLayer,IGxFile和IFeatureLayer接口来实现该功能。通过GxLayer类实现IGxLayer接口对象。通过GxFile类实现IGxFile接口对象<p></p></P>
<P 0cm 0cm 0pt 21pt; TEXT-INDENT: -21pt; mso-list: l0 level1 lfo1; tab-stops: 9.0pt 18.0pt list 21.0pt">l 程序说明<p></p></P> <P 0cm 0cm 0pt 0.1pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; tab-stops: 9.0pt 27.0pt">函数OpenFeatureClass打开用来新建lyr文件的shape文件。参数sPath,sName指定shape文件的位置。过程CreateLyrFileFromShape创建lyr文件。<p></p></P> <P 0cm 0cm 0pt 21pt; TEXT-INDENT: -21pt; mso-list: l0 level1 lfo1; tab-stops: 9.0pt 18.0pt list 21.0pt">l 代码<p></p></P> <TABLE #cccccc; MARGIN: auto auto auto 9.05pt; BORDER-COLLAPSE: collapse; mso-padding-alt: 0cm 4.95pt 0cm 4.95pt" cellSpacing=0 cellPadding=0 border=0> <TR yes"> <TD #ece9d8; PADDING-RIGHT: 4.95pt; BORDER-TOP: #ece9d8; PADDING-LEFT: 4.95pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ece9d8; WIDTH: 354.4pt; PADDING-TOP: 0cm; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" vAlign=top width=473> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt">Option Explicit<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"><p> </p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt">Private Sub CreateLyrFileFromShape(sLyrFilePath As String, sShpFilePath As String, _<p></p></P> <P 0cm 0cm 0pt 126pt; TEXT-INDENT: 16pt; mso-char-indent-count: 2.0; mso-para-margin-left: 12.0gd; mso-line-height-alt: 0pt">sShpFileName As String)<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt">On Error GoTo ErrorHandler:<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Dim pGxLayer As IGxLayer<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Dim pGxFile As IGxFile<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Dim pFeatureLayer As IFeatureLayer<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Dim pMxDocument As IMxDocument<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"><p> </p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Set pGxLayer = New GxLayer<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Set pGxFile = pGxLayer<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> pGxFile.Path = sLyrFilePath<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> <p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Set pFeatureLayer = New FeatureLayer<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Set pFeatureLayer.FeatureClass = OpenFeatureClass(sShpFilePath, sShpFileName)<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> If pFeatureLayer.FeatureClass Is Nothing Then<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> GoTo ErrorHandler:<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> End If<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> <p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> pFeatureLayer.Name = "myCountryLyr"<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Set pGxLayer.Layer = pFeatureLayer<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> <p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Set pMxDocument = ThisDocument<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> pMxDocument.FocusMap.AddLayer pGxLayer.Layer<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> <p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Set pGxLayer = Nothing<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Set pGxFile = Nothing<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Exit Sub<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt">ErrorHandler:<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> MsgBox Err.Description<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt">End Sub<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"><p> </p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt">Function OpenFeatureClass(ByVal sPath As String, ByVal sName As String) As IFeatureClass<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt">On Error GoTo ErrorHandler:<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Dim pWorkspaceFactory As IWorkspaceFactory<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Set pWorkspaceFactory = New ShapefileWorkspaceFactory<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"><p> </p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Dim pFeatureWorkspace As IFeatureWorkspace<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Set pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(sPath, 0)<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Set OpenFeatureClass = pFeatureWorkspace.OpenFeatureClass(sName)<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Exit Function<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt">ErrorHandler:<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Set OpenFeatureClass = Nothing<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt">End Function<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"><p> </p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt">Private Sub UIButtonControl1_Click()<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Dim sDataFilePath As String<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Dim pVBProject As VBProject<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Dim nIndex As Integer<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> Set pVBProject = ThisDocument.VBProject<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> nIndex = InStrRev(pVBProject.FileName, "\")<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> sDataFilePath = Left(pVBProject.FileName, nIndex) ; "..\..\..\data\"<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> 'call AddLayerFileToMap<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt"> CreateLyrFileFromShape sDataFilePath ; "myCountry.lyr", sDataFilePath, "WorldCountries"<p></p></P> <P 0cm 0cm 0pt; mso-line-height-alt: 0pt">End Sub<p></p></P></TD></TR></TABLE> |
|
|
11楼#
发布于:2005-11-03 21:22
<P>那应如何保存呢?哪位人兄知道啊?</P>
|
|
上一页
下一页