Shapefile creating issue - Plotting circle around point.

Jul 22, 2012 at 7:03 PM

Hi,

I am currently trying to have my application plot a circle of a certain radis around a specified point.

Basically working out the geometric centre, then plotting a circle with the radius as standard deviation, however am having issues.

The layer appears in the legend as a polygon layer, but nothing is showing on the map.

Currenlty saying IsValid is False and IsClockwise as False.

Giving the following ISValidReason : Too few points in geometry component[54 54]

 

Can anyone see any erros within the code?

Any help would be grealy appreciated.

 

Private Sub btnStdDist_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStdDist.Click
        Dim hndl, hand As Integer
        Dim sf As MapWinGIS.Shapefile
        Dim x2 As Double, y2 As Double
        Dim i, j, k As Integer
        Dim numShp As Integer
        Dim Totalx, Totaly, CentX, CentY As Double
        Dim GeomCent As New MapWinGIS.Shapefile
        Dim ShapeCent As New MapWinGIS.Shape
        Dim PointCent As New MapWinGIS.Point
        Dim field As New MapWinGIS.Field
        Dim indx As Integer
        Dim intPartRef As Integer = 0
        Dim result As Boolean = GeomCent.CreateNewWithShapeID("", MapWinGIS.ShpfileType.SHP_POLYGON)

        'Ensure analysis takes place on selected layer
        hndl = MapView.Legend1.SelectedLayer
        sf = MapView.Map.get_GetObject(hndl)
        numShp = sf.NumShapes
        indx = sf.Table.FieldIndexByName(cboWeight.Text)

      ' Loop through points to created centroid
        For i = 0 To numShp - 1
            Totalx += sf.Shape(i).Center.x.ToString()
            Totaly += sf.Shape(i).Center.y.ToString()
        Next i
        CentX = Totalx / numShp
        CentY = Totaly / numShp
        'calculate sum of sqaures
        For j = 0 To numShp - 1
            x2 += (sf.Shape(j).Center.x.ToString() - CentX) ^ 2
            y2 += (sf.Shape(j).Center.y.ToString() - CentY) ^ 2
        Next j
        'calculate std distance
        Dim StdDist As Double = Math.Sqrt((x2 + y2) / numShp)
        'Create new shapefile and shape

        ShapeCent.InsertPart(0, intPartRef)
        ShapeCent.Create(MapWinGIS.ShpfileType.SHP_POLYGON)
        Dim a As Double = 2 / 36



        'create circle polygon
        For k = 0 To 35
            PointCent.x += CentX + (StdDist * (Math.Cos(k * (Math.PI * a))))
            PointCent.y += CentY + StdDist * (Math.Sin(k * (Math.PI * a)))
            ShapeCent.InsertPoint(PointCent, k)
            'MessageBox.Show(CType(CentX + (StdDist * (Math.Cos(k * (Math.PI * a)))), String) + "," + CType(CentY + StdDist * (Math.Sin(k * (Math.PI * a))), String))
        Next k



        Debug.Print(ShapeCent.NumParts)
        Debug.Print(ShapeCent.numPoints)
        Debug.Print(ShapeCent.IsValid)
        Debug.Print(ShapeCent.IsValidReason)
        Debug.Print(ShapeCent.PartIsClockWise(0))
        GeomCent.EditInsertShape(ShapeCent, 0)

   
        'Plot centroid on Map
        hand = MapView.Legend1.Layers.Add(GeomCent, True)
        MapView.Legend1.Map.LayerName(hand) = CType(MapView.Legend1.Map.LayerName(hndl), String) + " - " + "StandardDistance"
    End Sub
 

 

 

Coordinator
Jul 25, 2012 at 8:58 AM

First: Why do you use ToString() in

Totalx += sf.Shape(i).Center.x.ToString()

Totalx is a double and you add it.

Secondly you don't need to add a part if it is a first part.

Third: for a polygon to be valid the first and last points should be the same: closing the polygon.

If your shape isn't valid you can use Shape.FixUp (http://www.mapwindow.org/documentation/mapwingis4.8/class_shape.html#a25b160700f4c07dd86104c6675431ebf) to create a valid shape. Only valid shapes can be added to the shapefile.

Probably

GeomCent.EditInsertShape(ShapeCent, 0)

will return false. You can use sf.ErrorMsg(sf.LastErrorCode) to understand why.

--
Paul