ThinkGeo.com    |     Documentation    |     Premium Support

Problem of intersect of 2 features

Im letting the user draw polygons on the map by manipulating winformsMap1_MapClick. They click on the map to form x number of points, then when they click on the first point (to close the polygon), the polygon should be drawn. The points plotted on the screen is declared as:


            InMemoryFeatureLayer markerLayer = new InMemoryFeatureLayer();
            markerLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle.PointType = PointType.Bitmap;
            markerLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle.Image = new GeoImage("dot.gif");


When i compare the first and last points (on the map they do overlap), it always evaluate to false:

BaseShape currentShape = myFeature.GetShape();
            BaseShape firstShape = drawLayer.InternalFeatures[0].GetShape();
            if (currentShape.Intersects(firstShape))



Someone know why is that so?

Ric, 
  
 The PointShapeA.Intersects(PointShapeB) returns true only if PointShapeA and PointShapeB are identical, that’s almost impossible by adding points manually by our hands, even it seems they are totally override, but in fact they are not. 
  
 I suggest to get the screen distance between the 2 points and if it is smaller than a threshold, we say it is override.  Here is the sample code: 
 
  private void winformsMap1_MapClick(object sender, WinformsMapClickEventArgs e)
        {
            InMemoryFeatureLayer markerLayer = (InMemoryFeatureLayer)winformsMap1.DynamicOverlay.Layers[“markerLayer”]; 
            markerLayer.InternalFeatures.Add(new Feature(e.WorldLocation));
            winformsMap1.RefreshDynamic();

            PointShape firstShape = (PointShape)markerLayer.InternalFeatures[0].GetShape();
            ScreenPointF firstScreenPointF = ExtentHelper.ToScreenCoordinate(winformsMap1.CurrentExtent, firstShape, winformsMap1.Width, winformsMap1.Height);

            // Get the screen distance between the 2 points, sure you can get rid of the Sqrt to make it more efficient
            double distance = Math.Sqrt(Math.Pow(e.X - firstScreenPointF.X, 2) + Math.Pow(e.Y - firstScreenPointF.Y, 2));

            if (distance < 2)
            {
                MessageBox.Show(“Overlap!”);
            }
        }
 
 Let me know if you have any issues. 
  
 Thanks, 
  
 Ben

Ben, 



Your solution worked, the polygon is drawn correctly as i expected. But i have a side issue though. The markers were drawn with labels that i read in the forum: 

InMemoryFeatureLayer markerLabelLayer = new InMemoryFeatureLayer(); 

markerLabelLayer.Columns.Add(new FeatureSourceColumn("Label1")); 

Feature testFeature = new Feature(new PointShape(50,-50)); 

testFeature.ColumnValues.Add("Label1", "This is my label"); 



The labels were displayed, but they overlapped with the markers. How can i adjust the position of labels to move further right or left, up or down? 



Ric



Ric, 



There are a couple ways to do that. First, TextStyle has a property called “PointPlacement” which is an enumeration you can set the position of the label, such as UpperLeft or LowerRight. Also, TextStyle has properties XoffsetInPixel and YoffsetInPixel with which you can directly customize the offset of the label. Please have a look the sample Labeling ->”Change the label placement for points” for detail. 



Thanks, 



Ben



It worked, thanks Ben.

That’s great! Ric.