Hello,
I am trying to write some code that will select a polygon from layer1 "SSESarea" and then select by location all polygons from layer2 "Parcels" with their centroid within the polygon selected from layer1.
I have the first part of the code complete and working but I could use some advice on the select by location part.
The following code selects the polygon that I need from Layer1, then opens the select by location tool in its own window like it does when you open it in arcmap . I would like to have the select by location tool automatically run instead of poping up on the screen.
Private Sub UIButtonControl1_Click()
' SELECT BY ATTRIBUTES
Dim pMxDoc As IMxDocument
Dim pMap As IMap
Dim pActiveView As IActiveView
Dim pFeatureLayer As IFeatureLayer
Dim pFeatureSelection As IFeatureSelection
Dim pQueryFilter As IQueryFilter
Set pMxDoc = Application.Document
Set pMap = pMxDoc.FocusMap
Set pActiveView = pMap
If Not TypeOf pMap.Layer(0) Is IFeatureLayer Then Exit Sub
Set pFeatureLayer = pMap.Layer(0)
Set pFeatureSelection = pFeatureLayer 'QI
'Create the query filter
Set pQueryFilter = New QueryFilter
pQueryFilter.WhereClause = "ProjectName = 'LSP - Arcadia'"
'Invalidate only the selection cache
'Flag the original selection
pActiveView.PartialRefresh esriViewGeoSelection, Nothing, Nothing
'Perform the selection
pFeatureSelection.SelectFeatures pQueryFilter, esriSelectionResultNew, False
'Flag the new selection
pActiveView.PartialRefresh esriViewGeoSelection, Nothing, Nothing
' ZOOM TO SELECTED FEATURES
Dim pCmdItem As ICommandItem
Set pCmdItem = Application.Document.CommandBars.Find(ArcID.Query_ZoomToSelected)
pCmdItem.Execute
' MAP SCALE
'Dim pMxDoc As IMxDocument
'Dim pMap As IMap
Set pMxDoc = ThisDocument
Set pMap = pMxDoc.FocusMap
pMap.MapScale = 3000
pMxDoc.ActiveView.Refresh
' EXPORT TO JPEG
'Dim pMxDoc As IMxDocument (it is necessary while using separately)
'Dim pActiveView As IActiveView (it is necessary while using separately)
Dim pExport As IExport
Dim pPixelBoundsEnv As IEnvelope
Dim exportRECT As tagRECT
Dim iOutputResolution As Integer
Dim iScreenResolution As Integer
Dim hDC As Long
Set pMxDoc = ThisDocument
Set pActiveView = pMxDoc.ActiveView
Set pExport = New ExportJPEG
pExport.ExportFileName = "C:\ExportMap." & Right(pExport.Filter, 3)
iScreenResolution = 150 'default screen resolution is usually 96dpi
iOutputResolution = 150
pExport.Resolution = iOutputResolution
With exportRECT
.Left = 0
.Top = 0
.Right = pActiveView.ExportFrame.Right * (iOutputResolution / iScreenResolution)
.bottom = pActiveView.ExportFrame.bottom * (iOutputResolution / iScreenResolution)
End With
Set pPixelBoundsEnv = New Envelope
pPixelBoundsEnv.PutCoords exportRECT.Left, exportRECT.Top, exportRECT.Right, exportRECT.bottom
pExport.PixelBounds = pPixelBoundsEnv
hDC = pExport.StartExporting
pActiveView.Output hDC, pExport.Resolution, exportRECT, Nothing, Nothing
pExport.FinishExporting
pExport.Cleanup
'*************
'Pops up select by location tool
Set pCmdItem = Application.Document.CommandBars.Find(Query_SelectByLayer)
pCmdItem.Execute
'**********************
End Sub
I found the following code on another forum and think it might be a start but I cannot figure out how to configure it.
************************************************************************
Since "have their center in" is unfortunately not an esri spatial relation that you can use with a spatial filter, I use a work around that will get the same results probably 99.99% of the time in arcobjects. Basically I get a small negative (inside) buffer on the geometry and then create a spatial filter using esriSpatialRelIntersects. The negative buffer distance is very, very small; ~1 meter. You may have to change this value depending on the map units of your data. You can see how this works in the code below, where pGeom is the polygon that I want to search within, and pFeatureLayer is a featurelayer that has the features you want to select
Dim pTopoOp as ITopologicalOperator
Set pTopoOp = pGeom
Dim pSpQuery as ISpatialFilter
Set pSpQuery = New SpatialFilter
With pSpQuery
'the value inside buffer should be very small map unit
Set .Geometry = pTopoOp.Buffer(-1)
.SpatialRel = esriSpatialRelIntersects
.GeometryField = pFeatureLayer.FeatureClass.ShapeFieldName
End With
Dim pFeatSelection as IFeatureSelection
Set pFeatSelection = pFeatureLayer
'Add features to current selection
pFeatSelection.SelectFeatures pSpQuery, esriSelectionResultsAdd, False
**************************************************************************