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);
}
}
}
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.
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#)?
Thanks for the reply! I had used the MapWinGIS methods IsValid and HasInvalidShapes(), but when I have drawn the polygon (see the picture)
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);
}
}
}
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.
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”.
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.
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 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 ?