Last month I asked vb.net arcobjects spatial query get fields value of desired layer only.

Now there is some change in the requirement, I need to get value of 2 layers after use "Select Features" tool in arcmap, 1 polyline and 1 polygon.

Can I use the same method?

One thing to highlight is the editable variables in attribute table of the 2 layers are the same.

My code is here:

    pMxDoc = My.ArcMap.Application.Document
    pMap = pMxDoc.FocusMap
    pActiveView = pMxDoc.FocusMap

    Dim layerNum = GetIndexNumberFromLayerName(pActiveView, "test_db.DBO.TestTable")
    Dim layerNum2 = GetIndexNumberFromLayerName(pActiveView, "test_db.DBO.TestTable2")

    Dim pFeatureLayer As IFeatureLayer
    Dim pFeatureLayer2 As IFeatureLayer

    pFeatureLayer = pMap.Layer(layerNum)
    pFeatureLayer2 = pMap.Layer(layerNum2)

    Dim pFeatureSelection As IFeatureSelection
    pFeatureSelection = pFeatureLayer
    logger.Info("pFeatureLayer = " + pFeatureLayer.Name)

    Dim pSelectionSet As ISelectionSet
    pSelectionSet = pFeatureSelection.SelectionSet
    logger.Info("pSelection number = " & pSelectionSet.Count)

    Dim pFeatureCursor As IFeatureCursor = Nothing
    pSelectionSet.Search(Nothing, True, pFeatureCursor)

    Dim tempArr As New List(Of String)
    Dim pFeature As IFeature
    pFeature = pFeatureCursor.NextFeature

    Do While (Not pFeature Is Nothing)
        pFeature = pFeatureCursor.NextFeature

Sorry of the unclear requirement of the whole process. After performing the spatial query, I will retrieve data related to features of these 2 layers (from database) and allow user to select and zoom to any results from the spatial query.


You can use the Spatial Join method for this.
Spatial Join

You should look at Merge Rules and the Field Mapping Control using these links. I recommend doing this through ArcMap without your script first to see how the field mapping control works. This provides a powerful way of renaming fields so they do not collide or merging them (as a Sum or Average etc.) and keeping the same name. There are lots of good options for how to treat fields with the same name (or to merge fields with different names). They have provided a powerful way for you to do this.

Edit: You will only get the one (target) layer back but you will get the FIDs of the polyline layer as one of the fields in the polygon so I believe you can use that information to join data to the polyline. You could alternately do an intersection of the lines inside of the polygons which would give you the information on the polyline layer.

  • Hi jbchurchill, thanks for the reply. Can this spatial join applicable for different kinds of geometry? I have tried the spatial join gp in arcmap. After join a polyline layer and a polygon layer, I only get a result layer only contain the polygon. – ppLily Sep 8 '14 at 1:21
  • It is true that you will only get back the one layer (the target layer). I assumed that you only needed the "values" for the two layers. I will amend my answer and add some more information about the potential to join data back to the polyline layer or the option to do an intersect. – jbchurchill Sep 8 '14 at 1:36
  • Thanks a lot jbchurchill, I think it is my fault not make it clear, in my case after I perform the spatial query, I also need to select and zoom to a feature on either of the 2 layers. I want to confirm whether it is possible. – ppLily Sep 8 '14 at 1:46
  • It seems to me that selecting and zooming to a feature is a second step unrelated to the spatial query. Yes you can select a feature and zoom to it if you have something to base your selection on. – jbchurchill Sep 8 '14 at 13:11
  • Hi jbchurchill, unfortunately in y case select and zoom to a feature is related to the spatial query. The workflow in my case is: (1) perform spatial query, (2) display the selected features attribute of from the 2 layers in a datagird, (3) from the datagrid select a record, perform 'select and zoom' to the selected row in the datagrid related to a feature. – ppLily Sep 9 '14 at 3:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.