Private Sub AppendSHPPolys(ByVal filename As String, ByVal NoOfItems As Integer, ByVal IsZ As Boolean)
Dim myAltitudes() As Double
Dim myColors() As Color
Dim myNames() As String
Dim myGuids() As String
Dim myTypes() As String
Dim A As String
Dim hdbf As Integer
Dim recCount As Integer
Dim N, N1, N2, NV, K, J, JK, I, M, NP As Integer
Dim FieldTypes() As DBFFieldType
Dim fname As String = "12345678901"
Dim fieldWidth As Integer
Dim numDecimals As Integer
' open the dbase file
hdbf = DBFOpen(filename, "rb")
recCount = DBFGetRecordCount(hdbf)
NoOfFields = DBFGetFieldCount(hdbf)
If recCount <> NoOfItems Then
MsgBox("SBuilderX can not read the database Shapefile!", MsgBoxStyle.Exclamation)
DBFClose(hdbf)
Exit Sub
End If
ReDim FieldNames(NoOfFields - 1)
ReDim FieldTypes(NoOfFields - 1)
Dim TypeField As Integer = -1 ' added because of Luis Feliz - type for traffic lines was lost!
For N = 0 To NoOfFields - 1
FieldTypes(N) = DBFGetFieldInfo(hdbf, N, fname, fieldWidth, numDecimals)
FieldNames(N) = Left(fname, InStr(fname, vbNullChar) - 1)
If FieldNames(N) = "Type" Then TypeField = N
Next N
frmSHPPoly.ShowDialog()
' *********************
If ShapePolyCancel Then
DBFClose(hdbf)
Exit Sub
End If
If ShapePolyNameField > 0 Then
If FieldTypes(ShapePolyNameField - 1) <> DBFFieldType.FTString Then
A = "Field """ & FieldNames(ShapePolyNameField - 1) & """ is not a string and will be ignored!"
ShapePolyNameField = 0
MsgBox(A, MsgBoxStyle.Exclamation)
End If
End If
If ShapePolyGuidField > 0 Then
If FieldTypes(ShapePolyGuidField - 1) <> DBFFieldType.FTString Then
A = "Field """ & FieldNames(ShapePolyGuidField - 1) & """ is not a string and will be ignored!"
ShapePolyGuidField = 0
MsgBox(A, MsgBoxStyle.Exclamation)
End If
End If
If IsZ Then ' after scott
If ShapePolyAltitudeField > 1 Then
If FieldTypes(ShapePolyAltitudeField - 2) <> DBFFieldType.FTDouble Then
A = "Field """ & FieldNames(ShapePolyAltitudeField - 2) & """ is not a double precision number and will be ignored!"
ShapePolyAltitudeField = 0
MsgBox(A, MsgBoxStyle.Exclamation)
End If
End If
Else
If ShapePolyAltitudeField > 0 Then
If FieldTypes(ShapePolyAltitudeField - 1) <> DBFFieldType.FTDouble Then
A = "Field """ & FieldNames(ShapePolyAltitudeField - 1) & """ is not a double precision number and will be ignored!"
ShapePolyAltitudeField = 0
MsgBox(A, MsgBoxStyle.Exclamation)
End If
End If
End If
If ShapePolyColorField > 0 Then
If FieldTypes(ShapePolyColorField - 1) <> DBFFieldType.FTInteger Then
A = "Field """ & FieldNames(ShapePolyColorField - 1) & """ is not a integer number and will be ignored!"
ShapePolyColorField = 0
MsgBox(A, MsgBoxStyle.Exclamation)
End If
End If
ReDim myAltitudes(NoOfItems - 1)
ReDim myColors(NoOfItems - 1)
ReDim myNames(NoOfItems - 1)
ReDim myGuids(NoOfItems - 1)
ReDim myTypes(NoOfItems - 1)
For N = 0 To NoOfItems - 1
If ShapePolyNameField = 0 Then
myNames(N) = ShapePolyName
Else
If (DBFIsAttributeNULL(hdbf, N, ShapePolyNameField - 1) = 0) Then
myNames(N) = DBFReadStringAttribute(hdbf, N, ShapePolyNameField - 1)
Else
myNames(N) = ""
End If
End If
If ShapePolyGuidField = 0 Then
myGuids(N) = ShapePolyGuid
Else
If (DBFIsAttributeNULL(hdbf, N, ShapePolyGuidField - 1) = 0) Then
myGuids(N) = DBFReadStringAttribute(hdbf, N, ShapePolyGuidField - 1)
Else
myGuids(N) = ShapePolyGuid
End If
End If
Try
myTypes(N) = DBFReadStringAttribute(hdbf, N, TypeField)
If myTypes(N) = "" Then
myTypes(N) = GetPolyTypeFromGuid(myGuids(N))
End If
Catch ex As Exception
myTypes(N) = GetPolyTypeFromGuid(myGuids(N))
End Try
If ShapePolyColorField = 0 Then
myColors(N) = ShapePolyColor
Else
If (DBFIsAttributeNULL(hdbf, N, ShapePolyColorField - 1) = 0) Then ''''
myColors(N) = Color.FromArgb(CInt(DBFReadStringAttribute(hdbf, N, ShapePolyColorField - 1)))
Else
myColors(N) = ShapePolyColor
End If
End If
If IsZ Then ' after scott
If ShapePolyAltitudeField = 0 Then
myAltitudes(N) = ShapePolyAltitude
ElseIf ShapePolyAltitudeField > 1 Then
If (DBFIsAttributeNULL(hdbf, N, ShapePolyAltitudeField - 2) = 0) Then
myAltitudes(N) = DBFReadStringAttribute(hdbf, N, ShapePolyAltitudeField - 2)
Else
myAltitudes(N) = ShapePolyAltitude
End If
End If
Else
If ShapePolyAltitudeField = 0 Then
myAltitudes(N) = ShapePolyAltitude
ElseIf ShapePolyAltitudeField > 0 Then
If (DBFIsAttributeNULL(hdbf, N, ShapePolyAltitudeField - 1) = 0) Then
myAltitudes(N) = DBFReadStringAttribute(hdbf, N, ShapePolyAltitudeField - 1)
Else
myAltitudes(N) = ShapePolyAltitude
End If
End If
End If
Next
' close the dbase
DBFClose(hdbf)
Dim shpObj As SHPObject
Dim pShpObj As IntPtr
Dim hShp As Integer
Dim X() As Double
Dim Y() As Double
Dim Z() As Double
Dim P() As Integer
hShp = SHPOpen(filename, "rb")
' test SHPReadObject on the shape
K = NoOfPolys 'counts number of polygons
ReDim Preserve Polys(K + 100)
For N = 0 To NoOfItems - 1
pShpObj = SHPReadObject(hShp, N)
shpObj = Marshal.PtrToStructure(pShpObj, GetType(SHPObject))
NV = shpObj.nVertices
'MsgBox("Vertices = " & NV)
ReDim X(NV - 1)
Marshal.Copy(shpObj.padfX, X, 0, NV)
ReDim Y(NV - 1)
Marshal.Copy(shpObj.padfY, Y, 0, NV)
If IsZ And ShapePolyAltitudeField = 1 Then
ReDim Z(NV - 1)
Marshal.Copy(shpObj.padfZ, Z, 0, NV)
Else
ReDim Z(0)
End If
NP = shpObj.nParts
ReDim P(NP - 1)
If NP > 1 Then
Marshal.Copy(shpObj.panPartStart, P, 0, NP)
End If
For J = 1 To NP
K = K + 1
Polys(K).Name = myNames(N)
Polys(K).Color = myColors(N)
Polys(K).Guid = myGuids(N)
Polys(K).Type = myTypes(N)
If NP = 1 Then
ReDim Polys(K).Childs(0)
Polys(K).NoOfChilds = 0
N1 = 0
N2 = NV - 1
Else
If J = 1 Then ' first
ReDim Polys(K).Childs(NP - 1)
Polys(K).NoOfChilds = NP - 1
For I = 1 To NP - 1
Polys(K).Childs(I) = K + I
Next
N1 = 0
N2 = P(1) - 1
JK = K
ElseIf J = NP Then ' last
ReDim Polys(K).Childs(0)
Polys(K).NoOfChilds = -JK
N1 = P(J - 1)
N2 = NV - 1
Else ' others
ReDim Polys(K).Childs(0)
Polys(K).NoOfChilds = -JK
N1 = P(J - 1)
N2 = P(J) - 1
End If
End If
ReDim Polys(K).GPoints(N2 - N1)
Polys(K).NoOfPoints = N2 - N1
For I = N1 To N2 - 1
M = I - N1 + 1
Polys(K).GPoints(M).lon = X(I)
Polys(K).GPoints(M).lat = Y(I)
If IsZ And ShapePolyAltitudeField = 1 Then
Polys(K).GPoints(M).alt = Z(I)
Else
Polys(K).GPoints(M).alt = myAltitudes(N)
End If
Next
AddLatLonToPoly(K)
If K = NoOfPolys + 100 Then
ReDim Preserve Polys(NoOfPolys + 1000)
End If
If K = NoOfPolys + 1000 Then
ReDim Preserve Polys(NoOfPolys + 10000)
End If
If K = NoOfPolys + 10000 Then
ReDim Preserve Polys(NoOfPolys + 100000)
End If
If K = NoOfPolys + 100000 Then
ReDim Preserve Polys(NoOfPolys + 1000000)
End If
Next
SHPDestroyObject(pShpObj)
Next
'K is total and final number of polys
ReDim Preserve Polys(K)
NoOfPolys = K
SHPClose(hShp)
End Sub