How to detect the self-intersection of one polygon after drawing(mapwingis))

When I use mapwingis to develop my program. I can not find the source code in the Mapwindow5 source code on the self-intersection. When I draw one polygon in the map control, right-click the item Finish Operation, if the polygon has self-intersection, the messagebox show the message “Validation failed: self-intersection [X,Y]”. But when I code it in my program, I can not find the function. Who can help me?

Who can tell me how the detect the self-intersection problem?

code as the follow (c#)

        private void btn_SavePolygon_Click(object sender, EventArgs e)
        {
            string shpfilename = "PolygonTest02.shp"; 
            if (drawShp == true)
            {
                Shapefile tempShapfile = new Shapefile();
                bool result = tempShapfile.CreateNewWithShapeID("", ShpfileType.SHP_POLYGON);
                if (!result)
                {
                    MessageBox.Show(tempShapfile.ErrorMsg[tempShapfile.LastErrorCode]);
                }
                else
                {                
                    Shape shp = new Shape();
                    shp.Create(ShpfileType.SHP_POLYGON);
                    int index = 0;
                    for (int i = 0; i < ListPoint.Count; i++)
                    {                       
                        shp.InsertPoint(ListPoint[i], ref i);
                    }
                    System.Diagnostics.Debug.Print(shp.Extents.ToDebugString());
            
                    tempShapfile.EditInsertShape(shp, ref index);
                    
                    tempShapfile.GeoProjection = axMap1.GeoProjection.Clone();
                   
                    tempShapfile.SaveAs(@"D:\" + shpfilename, null);
                }
            }
        }

Hello @yixing, and welcome.

If I understand your question, and you want to see the code that detects the self-intersection, you will find it in the GEOS library, which is what is used within MapWinGIS to perform various geometry operations.

MapWinGIS methods IsValid and IsValidReason are mapped into the GEOS library. So you would have to look in the GEOS source code to find the validation code.

Is that what you were asking?

Regards,
Jerry.

Yes. Thanks for the reply. When I get the GEOS library, I found it (source code C++) is hard for me. So now I need the DLL (C#) of GEOS and the self-intersection function position in mapwindow5 source code, I want to get the function not the source code now ^_^. can I directly get geos DLLs from mapwindow5 source code, where is it (T﹏T)? or How to compile the GEOS library to get the DLL(C#)?

No need to wrestle with the GEOS code.
Just use shp.IsValid() and shp.IsValidReason() (which should be name InvalidReason)

Or even better, use MapWinGIS: ShapeEditor Class Reference
It will do the most validations for you and this is what MW5 is using.

This might be a useful link as well: MapWinGIS: Validation

Thanks for the reply! I had used the MapWinGIS methods IsValid and HasInvalidShapes(), but when I have drawn the polygon (see the picture)
01
When I click the save button, the bool value is false (the IsValid) and the bool value is true (the HasInvalidShapes() method), but the polygon can be saved as in the hard disk and can be open in the ArcGIS software and the shp file is no problem. The strange question is the polygon is valid, but it(IsValid or HasInvalidShapes()) shows the incorrect result, the polygon is invalid? The code is below:

    private void btn_SavePolygon_Click(object sender, EventArgs e)
    {
        string shpfilename = "PolygonTest08.shp";
        if (drawShp == true)
        {
            Shapefile tempShapfile = new Shapefile();
            bool result = tempShapfile.CreateNewWithShapeID("", ShpfileType.SHP_POLYGON);
            if (!result)
            {
                MessageBox.Show(tempShapfile.ErrorMsg[tempShapfile.LastErrorCode]);
            }
            else
            {
               
                Shape shp = new Shape();
                shp.Create(ShpfileType.SHP_POLYGON);
                int index = 0;
                for (int i = 0; i < ListPoint.Count; i++)
                {                       
                    shp.InsertPoint(ListPoint[i], ref i);
                    i = i + 1;
                }
                
                tempShapfile.EditInsertShape(shp, ref index); 
               
                tempShapfile.GeoProjection = axMap1.GeoProjection.Clone();
                bool flag01 = tempShapfile.HasInvalidShapes();
                bool flag02=shp.IsValid;

                tempShapfile.SaveAs(@"D:\" + shpfilename, null);
            }
        }
    }

Hi yixing,

why do you think the code is not working?

It is absolutely possible in any gis software to generate and save an invalid shape.
That is the reason, why this method to check the validity is there, cause if it wouldn’t be possible to do so, this method wouldn’t be needed.
Since it depends a lot of what you want to do with your geometry if it is invalid for your purposes, you normaly can save even horribly invalid ones.

Cheers
Stefan

Hi Stefan,
Thanks for your reply!
I know the code is working for saving as the shapefile polygon in the hard disk (even the invalid polygon), and can be open in the ArcGIS.
But My question is when I draw one polygon which is valid but the code returns the incorrect result “invalid”.

Hi yixing,

what does the method .isValidReason() return?
…as Paul wrote, this returns, why MapWinGIS thinks that this polygon is invalid?

Have you tested the same polygon in ArcGIS or QGis with their validation tools?
…sorry may have missed your statements about this.

I add the code .isValidReason().
The return is “The first and the last point of the polygon part must be the same”.

…and have you checked, what the validation functions of other gis software return?

I try it now . it is a good idea.

I check the polygon (by arcgis) created by my code. But I do not know whether it is correct.

i cannot help you with arcgis, - don’t like it.
you can import that shapefile to postgres/postgis and there you have the same two functions: isvalid and validreason, - you have also the functions startpoint/endpoint that allow you to get the coordinates of the start- and endpoints to find out, if mapwingis is correct or not.
In QGis you can find the tool in the toolbox underneath Vectorgeometry → check validity.

ok, Thanks I will try it later. I do not have a Qgis, I will download it. :grin:

… it’s dangerous, maybe you like it more than arcgis…

ArcGIS is a shit. very slow. I do not like it. The mapwindow5 is very beautiful. But our school, company choose ArcGIS.

In addition, I do not understand why I draw a polygon, the points number is twice the actual points. When I press the mouse down, It looks like triggers twice events.

if it is an event handling problem i have to fold, maybe pmeems knows more about that.
If you have a sensible, maybe gaming mouse, might be worth a try to pull down its dpi rate, - but this is only guess-work.

I will check it. Thanks :joy:

I know the reason isvalid or .isValidReason() return. First, I add points but lost one point for the start- and endpoints. Then I did not clockwise draw the polygon, when I draw the polygon clockwise, the return is right. But Why I must clockwise draw the polygon? Who can tell me ? :joy: