Selection by polygon

Hello,

I tried to use the polygon selection but I could not trigger the end event with a ctrl + left mouse click
The event never goes in the selectboxfinal… but I don’t understand because I have defined SendSelectBoxFinal = true

Should I do something special?

Thanks for your help,

Can you show some code to reproduce it?

Hello,

Thank you for your reply
The code :

public partial class Form : System.Windows.Forms.Form
{
    public Form()
    {
        InitializeComponent();

        axMap.SendSelectBoxFinal = true;
    }

    private void buttonSelectByPolygon_Click(object sender, EventArgs e)
    {
        axMap.CursorMode = tkCursorMode.cmSelectByPolygon;
    }

    private void buttonIdentify_Click(object sender, EventArgs e)
    {
        axMap.CursorMode = tkCursorMode.cmIdentify;

        axMap.Identifier.IdentifierMode = tkIdentifierMode.imAllLayers;
    }

    private void buttonLoad_Click(object sender, EventArgs e)
    {
        LoadLayer(@"C:\SHP\CARRY_ORANGE_VACANCIEL-FAC.shp");
        LoadLayer(@"C:\SHP\CARRY_ORANGE_VACANCIEL-POT.shp");

        // ShapeIdentified : Does not work with a polyline shp ?
        LoadLayer(@"C:\SHP\CARRY_ORANGE_VACANCIEL-CABLE.shp");
    }

    private void axMap_SelectBoxFinal(object sender, AxMapWinGIS._DMapEvents_SelectBoxFinalEvent e)
    {
        // Never go here
    }

    private void axMap_ShapeIdentified(object sender, AxMapWinGIS._DMapEvents_ShapeIdentifiedEvent e)
    {
        // Do not work with polylines
        // e.layerHandle = -1 ...

        Shapefile shp = (Shapefile)axMap.get_GetObject(e.layerHandle);
    }

    private void axMap_ShapesIdentified(object sender, AxMapWinGIS._DMapEvents_ShapesIdentifiedEvent e)
    {
        // Never go here
    }

    public void LoadLayer(string layerLocation)
    {
        try
        {
            int layerHandle = axMap.AddLayerFromFilename(layerLocation, tkFileOpenStrategy.fosAutoDetect, true);

            Shapefile sf = axMap.get_Shapefile(layerHandle);

            // Ajout des étiquettes
            if (sf != null)
            {
                sf.Volatile = true;
                sf.CollisionMode = tkCollisionMode.AllowCollisions;
                sf.Labels.Generate("[ETIQUETTE]", tkLabelPositioning.lpCentroid, false);
                sf.Labels.AvoidCollisions = false;
                sf.Identifiable = true;

                // Pour la sélection dans l'interface graphique
                Utils utils = new Utils();

                LabelCategory ct = sf.Labels.AddCategory("Selected");
                ct.FrameBackColor = utils.ColorByName(tkMapColor.Yellow);
            }
        }
        catch
        {
            
        }
    }
}

Hello @Anthony

Using the cmSelection tool will invoke the SelectBoxFinal. The cmSelectByPolgon, on the other hand, will invoke the ChooseLayer event to allow you specify which layer (or layers) you wish to select. See the documentation here:

tkCursorMode

Let us know if you need more clarification.

Regards.
Jerry.

Hi,
I’ve noticed a similar problem after updating the references to MapWinGIS 5.0.
My CursorMode is set to cmSelection and SendSelectBoxFinal is set to True but SelectBoxFinal is never raised. The same code works fine with MapWinGIS 4.9.

Thanks

Hello @jerryfaust

Sorry for the late answer I was on another project.
I tried to follow your advice (see below) however I have two questions:

    private void axMap_ChooseLayer(object sender, AxMapWinGIS._DMapEvents_ChooseLayerEvent e)
    {
        e.layerHandle = 0;
    }

    private void axMap_SelectionChanged(object sender, AxMapWinGIS._DMapEvents_SelectionChangedEvent e)
    {

        Shapefile shp = (Shapefile)axMap.get_GetObject(e.layerHandle);
    }

First:
How should we do if we want to use multiple layers for the same selection?

Second:
Is it normal that the cursor “cmIdentify” does not work if a point is on a line? I still get:
:
layerHandle = -1
shapeIndex = -1

Thanks

Thank you for the feedback, @GKing.

You’re right. There is an issue in v5.0 that is related to the new Shapefile.Selectable attribute (being able to select multiple layers concurrently by setting their Selectable property = True).

However, the behavior already existed in the case of single-layer selection. If you specifed a layer handle in the ChooseLayer event, even in v4.9, AND any shapes were selected, the SelectionChanged event was raised, but the SelectBoxFinal event was not raised. It was only raised if you did not specify a layer handle in the ChooseLayer event OR no shapes were selected.

Even so, a bug now exists in v5.0, such that the SelectBoxFinal event is not raised in either case.

Please consider the following, and feel free to advise (I am including @pmeems in the discussion).

  1. Note that historically, the SelectBoxFinal event was not raised if a layer was marked for selection AND shapes were actually selected.

  2. Based on the documentation, if you do not specify a layer handle in the ChooseLayer event, the operation will not be performed. It does not indicate whether or not the SelectBoxFinal event should still be raised.

  3. The documentation for the SelectBoxFinal event puts no conditions on whether or not it should be raised.

  4. QUESTION: Should the SelectBoxFinal event always be raised?, or as in the past, only when no shapes were actually selected?

Regards.
Jerry.

Hello @Anthony

In order to select from multiple layers in the same selection, do not specify a layer handle in ChooseLayer, and instead, set the Selectable property = True for each layer that you would like to be selectable. Perhaps I should make it more clear in the documentation.

I’m not sure I understand your second question. The cmIdentify tool is capable of selecting both point and line shapes just by clicking on them. Are you wanting to select both a point and a line in a single selection? You can do that based on the answer to your first question. Let me know if your question is still unanswered.

Regards.
Jerry.

Hello,

Thank you for you precious help @jerryfaust !
I finally understood how polygon selection works.
The “cmIdentify” tool does not work in the specific case, if I select the point that is on the line (see attached image)

Identify

    private void axMap_ShapeIdentified(object sender, AxMapWinGIS._DMapEvents_ShapeIdentifiedEvent e)
    {
        // if the point is present on a line
        // e.layerHandle = -1
        // e.shapeIndex = -1
    }

Is it a bug?

Hello @Anthony

I apologize for any confusion in this discussion - since there are two discussions going on in this thread. One is about shape ‘Selection’ and the other is about shape ‘Identification’. I’ve realized that in my previous response to you, I was mixing up the two concepts. You were asking about the ‘Identify’ tool, and I was giving an answer about the ‘Selection’ tool.

Regarding your sample with the cmIdentify tool, can you please give more context (more of your code)? Here’s what we’re looking for:

  1. Are you trying to Identify a single layer (IdentifierMode = imSingleLayer) or all layers (IdentifierMode = imAllLayers)?
  2. If you set the mode to imSingleLayer, then you have to set the ActiveLayer property, and if you set the mode to imAllLayers, then you have to set individual layer properties to ‘Identifiable’ in order for them to be Identified, and you would then fetch them out of the SelectionList.

Thank you.
Jerry.

Hello @jerryfaust ,

Thank your precious help, the object “SelectionList” solves my problem.
On the other hand I do not find the selection super convenient for polylines. I have to zoom out in order to identify a polyline (see screenshot)

Finally for polylines I used the “selection” tool.

Thank you,
Anthony,

Hello!
Regarding the selection of objects on the map polygon. I did as follows.
If possible, please insert this code into the MapWinGis documentation in the “examples” section. Thanks you!

#region ----------Selected layer----------
//Select By Polygon
private void btnSelectByPolygon_Click(object sender, EventArgs e)
{
axMap1.CursorMode = tkCursorMode.cmSelectByPolygon;
}
//Duble Click to Finish Select By Polygon
private void axMap1_DblClick(object sender, EventArgs e)
{
axMap1.ShapeEditor.SaveChanges();
}
//Select by Rectangle
private void btnSelection_Click(object sender, EventArgs e)
{
axMap1.CursorMode = tkCursorMode.cmSelection;
}
//layer selection
private void axMap1_ChooseLayer(object sender, DMapEvents_ChooseLayerEvent e)
{
e.layerHandle = 0;
}
//
private void axMap1_SelectionChanged(object sender, DMapEvents_SelectionChangedEvent e)
{
get_selected_attribute(e.layerHandle);
}
//Get attribute from selected layer
private void get_selected_attribute(int layerHandle)
{
try
{
var sf = new Shapefile();
sf = axMap1.get_Shapefile(layerHandle);
//string expression = “”;
string data
= “”;
/for (int i = 1; i < sf.NumFields; i++)
{
expression += “[” + sf.Field[i].Name + “]”;
}
/
int fieldIndex = sf.Table.FieldIndexByName[“num”];
if (fieldIndex != -1)
{
for (int i = 0; i < sf.NumSelected; i++)
{
data
+= “[” + (string)sf.CellValue[fieldIndex, i].ToString() + “]”;
}
}
MessageBox.Show("Objects: " + sf.NumSelected.ToString() + "\n Value: " + data_);
//MessageBox.Show("Objects: “+sf.NumSelected.ToString()+”,\n Fields: “+expression+”,\n Value: "+ data_);
}
catch (Exception ex)
{
MessageBox.Show("Ошибка: " + ex, “Ошибка”, MessageBoxButtons.OK);
return;
}
}
#endregion

if you need to select the desired layer, then I did it

>private void axMap1_ChooseLayer(object sender, _DMapEvents_ChooseLayerEvent e)
    {
        int handle = 0;
        int thishandle = 0;
        int result = 0;
        while (handle < axMap1.Layers.Count)
        {
            if (axMap1.Layers[handle].Name == "Example Point")
            {
                thishandle = handle;
                result = 1;
            }
            handle++;
        }
        if (result > 0)
        {
            e.layerHandle = thishandle;
        }
    }

Give an example of an identification code cmIdentify, thanks!