Trying to select the nearest point from one layer based on point in another

12-15-2011 10:26 AM
New Contributor III

I am working (repeatedly) with two point shapefiles.  For my current effort, one is based on a Surfer grid which has one point every 0.5 feet and measures 40 by 100 feet for a total of 16,200 points (the population).  The other point file has varying number of points (the sample) that fall within the footprint of the population.  What I want it to do is loop through the sample, and for each point, locate the closest point in the population and add each successive population point to a selected set which will later be exported to a new shapefile.

Thanks for your time and any suggestions.  Here is what I have so far.  There is a break in the code at my current problem.

    Protected Overrides Sub OnClick()

        'this routine assumes that the layers have been loaded as the first layer and the second layer in the map
        'the spatial references for both the layers need to be defined.
        'Its purpose is to select points from a population (Surfer grid) based on points from a sample population 
        'generated in Visual Sample Plan.  For example, in the Surfer grid of 16,200 points, it will cycle through
        'and choose the point closest to one of the VSP generated points.  Note, both grids should share the same 
        'basic footprint.

        'References the project to the active dataframe
        Dim pMxDoc As ESRI.ArcGIS.ArcMapUI.IMxDocument = My.ArcMap.Document
        Dim m_application As ESRI.ArcGIS.Framework.IApplication = CType(Hook, ESRI.ArcGIS.Framework.IApplication)
        Dim pMap As ESRI.ArcGIS.Carto.IMap
        Dim pFind As ESRI.ArcGIS.Geometry.PointClass = Nothing
        pMap = pMxDoc.FocusMap

        'Set things up for Layer 0, the Sample Population Layer (e.g., VSP from above)
        Dim pSampLayer As ESRI.ArcGIS.Carto.ILayer = pMap.Layer(0)
        Dim pSampFLayer As ESRI.ArcGIS.Carto.IFeatureLayer = pSampLayer
        Dim pSampFClass As ESRI.ArcGIS.Geodatabase.IFeatureClass = pSampFLayer.FeatureClass
        Dim pSampFCursor As ESRI.ArcGIS.Geodatabase.IFeatureCursor
        pSampFCursor = pSampFClass.Search(Nothing, True) ' getting the feature cursor for Layer A to cycle through it

        'Set things up for Layer 1, the Population Layer (e.g., Surfer from above)
        Dim pPopLayer As ESRI.ArcGIS.Carto.ILayer = pMap.Layer(1)

        Dim pPopFLayer As ESRI.ArcGIS.Carto.IFeatureLayer = pPopLayer
        Dim pPopFClass As ESRI.ArcGIS.Geodatabase.IFeatureClass = pPopFLayer.FeatureClass

Code failure happens on this next line.  Crashes ArcMap.
        Dim pPopNearest As ESRI.ArcGIS.Geometry.IProximityOperator = DirectCast(pPopLayer, ESRI.ArcGIS.Geometry.IProximityOperator)
        Dim pPopSpatRef As ESRI.ArcGIS.Geometry.ISpatialReference = GetSpatialReferenceFromDataset(pPopLayer)

        Dim pF As ESRI.ArcGIS.Geodatabase.IFeature = pSampFCursor.NextFeature
        'Dim pSelPoint As ESRI.ArcGIS.Geometry.IPoint = Nothing 'Initializes the selected point
        Dim pSelSet As ESRI.ArcGIS.Geometry.IPoint = New ESRI.ArcGIS.Geometry.PointClass
        pSelSet.SpatialReference = pPopSpatRef

        Do Until pF Is Nothing
            Dim pSampPointFeature As ESRI.ArcGIS.Geometry.IPoint = pF.Shape
            pPopNearest.QueryNearestPoint(pSampPointFeature, ESRI.ArcGIS.Geometry.esriSegmentExtension.esriNoExtension, pSelSet)
            pF = pSampFCursor.NextFeature

    End Sub

    Public Function GetMxDocument(ByVal application As ESRI.ArcGIS.Framework.IApplication) As ESRI.ArcGIS.ArcMapUI.IMxDocument

        If application Is Nothing Then
            Return Nothing
        End If

        Dim mxDocument As ESRI.ArcGIS.ArcMapUI.IMxDocument = (CType(application.Document, ESRI.ArcGIS.ArcMapUI.IMxDocument)) ' Explicit Cast

        Return mxDocument

    End Function

Thank you,

Dale Bridgford
0 Kudos
2 Replies
New Contributor III
I realized that I did not state what it was I was using.  ArcMap 10.0 SP3 and VB 2008.


0 Kudos
New Contributor III
Well, I solved it.  I ended up not using ESRI.ArcGIS.Geometry.IProximityOperator::QueryNearestPoint.  Instead I used ESRI.ArcGIS.Carto.IIndexQuery::NearestFeature with completely different code than what I have listed above.

Thanks for looking.

0 Kudos