阅读:2010回复:3
[原创]求助ao的空间查询代码
求助ao开发代码(vb或者c#皆可),如何实现通过鼠标点击查询实体的空间属性,获得空间数据库(geodatabase)的内容?
|
|
1楼#
发布于:2007-02-27 11:12
<P>首先点击获得Feature</P>
<P>代码:</P> <P> Public Shared Sub SelectMouseDown(ByVal pixSize As Integer, ByVal x As Long, ByVal y As Long, ByVal pLayer As IFeatureLayer, ByVal pMap As IMap, ByVal justOne As Boolean)<BR> Dim pRefreshArea As IInvalidArea<BR> pRefreshArea = New InvalidArea</P> <P> Dim pFeatureLayer As IFeatureLayer<BR> Dim pFeatureClass As IFeatureClass<BR> Dim pSpatialFilter As ISpatialFilter<BR> Dim pFilter As IQueryFilter<BR> Dim pActiveView As IActiveView<BR> Dim pGeometry As IGeometry<BR> Dim pPoint As IPoint</P> <P><BR> '将所有选择集合加入到pInvalidArea中,刷新时能清除原有的选择集合<BR> Dim pOrigEnumFeature As IEnumFeature<BR> Dim pOrigFeature As IFeature<BR> pOrigEnumFeature = pMap.FeatureSelection<BR> pOrigFeature = pOrigEnumFeature.Next<BR> While pOrigFeature IsNot Nothing<BR> pRefreshArea.Add(pOrigFeature)<BR> pOrigFeature = pOrigEnumFeature.Next<BR> End While</P> <P> '初始话pLayer层的选择SelectionSet<BR> Dim pFeatureSelect As IFeatureSelection<BR> Dim pSelectionSet As ISelectionSet<BR> pFeatureSelect = pLayer<BR> pFeatureSelect.Clear()<BR> pSelectionSet = pFeatureSelect.SelectionSet</P> <P> Try<BR> pMap.ClearSelection() '首先清除以前选择项</P> <P> If pLayer Is Nothing Then Exit Sub<BR> If Not TypeOf pLayer Is IGeoFeatureLayer Then Exit Sub</P> <P> ' Get the feature layer and class of the current layer<BR> pFeatureLayer = pLayer<BR> pFeatureClass = pFeatureLayer.FeatureClass<BR> If pFeatureClass Is Nothing Then Exit Sub</P> <P> ' Get the mouse down position in map coordinates<BR> pActiveView = pMap<BR> pPoint = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y)<BR> pGeometry = pPoint</P> <P> ' Use a four pixel buffer around the cursor for feature search<BR> Dim length As Double<BR> length = ConvertPixelsToMapUnits(pMap, pixSize)<BR> Dim pTopo As ITopologicalOperator<BR> pTopo = pGeometry<BR> Dim pBuffer As IGeometry<BR> pBuffer = pTopo.Buffer(length)<BR> pGeometry = pBuffer.Envelope</P> <P> ' Set up a Filter specific to this layer<BR> pSpatialFilter = New SpatialFilter<BR> pSpatialFilter.Geometry = pGeometry<BR> Select Case pFeatureClass.ShapeType<BR> Case esriGeometryPoint<BR> pSpatialFilter.SpatialRel = esriSpatialRelIntersects<BR> Case esriGeometryPolyline<BR> pSpatialFilter.SpatialRel = esriSpatialRelIntersects<BR> Case esriGeometryPolygon<BR> pSpatialFilter.SpatialRel = esriSpatialRelIntersects<BR> End Select<BR> pSpatialFilter.GeometryField = pFeatureClass.ShapeFieldName<BR> pFilter = pSpatialFilter</P> <P> ' Do the search<BR> Dim pCursor As IFeatureCursor<BR> pCursor = pFeatureLayer.Search(pFilter, True)</P> <P> ' and select the features on the map<BR> Dim pFeature As IFeature<BR> pFeature = pCursor.NextFeature<BR> If pFeature IsNot Nothing Then<BR> '如果justOne为真只添加一个</P> <P> If justOne Then<BR> pMap.SelectFeature(pLayer, pFeature)<BR> pRefreshArea.Add(pFeature)<BR> pSelectionSet.Add(pFeature.OID)<BR> Else<BR> While Not pFeature Is Nothing<BR> pMap.SelectFeature(pLayer, pFeature)<BR> pRefreshArea.Add(pFeature)<BR> pSelectionSet.Add(pFeature.OID)<BR> pFeature = pCursor.NextFeature<BR> End While<BR> End If<BR> End If</P> <P> '更新Map中的选择集合,并触发selectionChanged事件<BR> pMap.ClearSelection()<BR> pFeatureSelect.SelectionSet = pSelectionSet<BR> pFeatureSelect.SelectionChanged()</P> <P> '刷新屏幕<BR> pRefreshArea.Display = pActiveView.ScreenDisplay<BR> pRefreshArea.Invalidate(esriAllScreenCaches)<BR> Catch ex As Exception<BR> MsgBox(ex.Message)<BR> End Try<BR> End Sub<BR></P> <P>由于是GDB,可以通过Shape_Length、Shape_Area获得长度、面积等信息。</P> |
|
|
2楼#
发布于:2007-02-27 11:40
<P>谢谢楼上的几次帮忙,很感谢</P>
|
|
3楼#
发布于:2007-02-27 11:42
<TABLE fixed; WORD-BREAK: break-all" height="85%" width="95%" align=center border=0>
<TR> <TD 9pt; LINE-HEIGHT: 12pt" vAlign=top width=* height="100%"><IMG src="http://www.gisempire.com/bbs/Skins/Default/topicface/face1.gif"> <B></B><BR> <P>谢谢楼上的几次帮忙,很感谢</P></TD></TR></TABLE> |
|