将文本文件的部分流式传输到字符串

时间:2014-10-01 16:01:04

标签: vb.net string parsing streamreader

使用VB.NET。我正在尝试创建一个空间坐标字符串,以用作查询Web服务的参数。需要从文本文件(GIS的标准输出)中提取坐标,该文件可以是任意长度。

Web服务需要在众所周知的文本中看到坐标对的字符串。格式用逗号分隔。例如。 -2.780299 53.373266,-2.780606 53.372617,-2.782207 53.370392,-2.784552 53.362061

(表面上类似于问here

文本文件在此结构中。重申我需要一个字符串,其中只包含#34; 161"之后的坐标(以逗号分隔的对)。之前" pen"。虽然单词" Region"是161并不是一致的数字。是

            Version   300
            Charset "WindowsLatin1"
            Delimiter ","
            CoordSys Earth Projection 1, 104
            Columns 1
              id Char(10)
            Data

            Region  1
              161
            -2.780299 53.373266
            -2.780312 53.373235
            -2.780535 53.372905
            -2.780606 53.372617
            -2.780844 53.372234
            -2.781227 53.371664
            -2.781239 53.371646
            -2.781409 53.371407
            -2.781779 53.3709
            -2.782101 53.370549
            -2.782204 53.370402
            -2.782207 53.370392
            -2.781142 53.37014
            -2.780263 53.369894
            -2.780314 53.369698
            -2.780378 53.36953
            -2.780453 53.369362
            -2.780532 53.369223
            -2.780611 53.369101
            -2.780826 53.368803
            -2.781468 53.367978
            -2.781768 53.367608
            -2.78184 53.367497
            -2.781863 53.367458
            -2.781806 53.367446
            -2.781751 53.367435
            -2.781712 53.367428
            -2.781733 53.367398
            -2.781753 53.367369
            -2.781847 53.36723
            -2.781949 53.367077
            -2.781866 53.367046
            -2.781492 53.366907
            -2.781182 53.36682
            -2.781269 53.366388
            -2.780878 53.366173
            -2.781063 53.365894
            -2.781693 53.365255
            -2.782277 53.364606
            -2.782594 53.364311
            -2.782652 53.364272
            -2.782676 53.364242
            -2.782687 53.364205
            -2.78268 53.364158
            -2.782643 53.364085
            -2.782643 53.364035
            -2.782943 53.363738
            -2.783346 53.363266
            -2.783736 53.362905
            -2.784254 53.362383
            -2.784552 53.362061
            -2.784617 53.361959
            -2.784113 53.361843
            -2.783923 53.361764
            -2.783968 53.361533
            -2.783978 53.361504
            -2.784358 53.361226
            -2.784823 53.360723
            -2.784833 53.360712
            -2.785201 53.360193
            -2.785831 53.359301
            -2.785832 53.359301
            -2.786182 53.358787
            -2.786484 53.358288
            -2.786318 53.35821
            -2.787152 53.356913
            -2.787255 53.356614
            -2.787199 53.356333
            -2.787559 53.356315
            -2.790294 53.356237
            -2.791854 53.35619
            -2.792974 53.356147
            -2.794152 53.356092
            -2.794698 53.35605
            -2.79521 53.356012
            -2.795333 53.356006
            -2.795732 53.355982
            -2.79586 53.355973
            -2.796257 53.355942
            -2.797842 53.355898
            -2.797931 53.355936
            -2.798299 53.355921
            -2.798399 53.355926
            -2.798727 53.355937
            -2.799055 53.355943
            -2.799382 53.355944
            -2.799711 53.355942
            -2.799919 53.355939
            -2.800248 53.355929
            -2.800575 53.355915
            -2.800901 53.355898
            -2.802135 53.355819
            -2.803302 53.355749
            -2.804436 53.355658
            -2.805646 53.355515
            -2.807964 53.355207
            -2.809126 53.355058
            -2.809593 53.354684
            -2.810372 53.354184
            -2.81147 53.353449
            -2.81147 53.353444
            -2.811472 53.353428
            -2.811474 53.353413
            -2.811477 53.353397
            -2.811481 53.353381
            -2.811487 53.353365
            -2.811492 53.35335
            -2.811506 53.353325
            -2.811538 53.35328
            -2.811578 53.353237
            -2.811617 53.353193
            -2.811622 53.353188
            -2.811663 53.353145
            -2.811704 53.353101
            -2.811749 53.353069
            -2.811852 53.352994
            -2.811875 53.352979
            -2.811983 53.352906
            -2.812094 53.352834
            -2.812209 53.352764
            -2.812324 53.352695
            -2.812443 53.352629
            -2.813163 53.352174
            -2.81362 53.351928
            -2.813782 53.352417
            -2.814005 53.353349
            -2.814147 53.354287
            -2.814206 53.355228
            -2.814183 53.356169
            -2.814077 53.357109
            -2.813889 53.358043
            -2.813619 53.358971
            -2.813268 53.359889
            -2.812837 53.360795
            -2.812327 53.361686
            -2.81174 53.36256
            -2.811077 53.363414
            -2.81034 53.364247
            -2.809531 53.365055
            -2.808652 53.365837
            -2.807706 53.366591
            -2.806694 53.367313
            -2.805621 53.368004
            -2.804489 53.36866
            -2.803301 53.369279
            -2.802059 53.369861
            -2.800769 53.370403
            -2.799433 53.370904
            -2.798055 53.371362
            -2.796638 53.371777
            -2.795187 53.372147
            -2.793705 53.372471
            -2.792197 53.372749
            -2.790666 53.372979
            -2.789118 53.37316
            -2.787556 53.373294
            -2.785984 53.373378
            -2.784407 53.373413
            -2.782829 53.373399
            -2.781254 53.373335
            -2.780299 53.373266
                Pen (2,2,16711680) 
                Brush (5,16711680)
                Center -2.797227 53.362684

一如既往,任何方向的帮助都将受到赞赏。现有代码适用于基于旧SOAP的服务,如下所示。但是,它使用的是我将无法访问的自定义库,并且不知道如何替换。它还利用了新服务不再识别的空间系统(东向和北向)。

我有可能在程序中转换空间系统,所以如果只能在不需要NBN库的情况下实现这一点,那将是朝着正确方向迈出的一大步。

            Function GetPolyFromMIF(ByVal strMIF As String) As NBN.Polygon

                    Dim sr As StreamReader = New StreamReader(strMIF)
                    Dim sLine As String = sr.ReadLine
                    Dim iCoords As Integer
                    Dim iCoord As Integer
                    Dim sCoords() As String
                    Dim dblEasting As Double
                    Dim dblNorthing As Double
                    Dim coords() As NBN.Coordinate
                    Dim bPolyComplete As Boolean = False
                    Dim poly As NBN.Polygon = New NBN.Polygon

                    Do While Not sr.EndOfStream And Not bPolyComplete

                        If sLine.StartsWith("Region") Then

                            iCoords = Convert.ToInt16(sr.ReadLine)
                            ReDim coords(iCoords)
                            For iCoord = 1 To iCoords
                                sCoords = sr.ReadLine.Split(" ")
                                dblEasting = Convert.ToDouble(sCoords(0))
                                dblNorthing = Convert.ToDouble(sCoords(1))
                                coords(iCoord - 1) = New NBN.Coordinate
                                coords(iCoord - 1).x = dblEasting
                                coords(iCoord - 1).y = dblNorthing
                            Next
                            poly.srs = NBN.SpatialReferenceSystem.osgb_BNG
                            Dim boundary As NBN.PolygonBoundary = New NBN.PolygonBoundary
                            boundary.Ring = coords
                            poly.Boundary = boundary
                            bPolyComplete = True
                        Else
                            sLine = sr.ReadLine
                        End If
                    Loop

                    Return poly
                End Function

1 个答案:

答案 0 :(得分:0)

我会使用正则表达式处理这些行。逐行将每行加载到解析器中,但不检测任何与正则表达式匹配的内容。通过采用这种方法,您可以处理任何大小的文件,只从您感兴趣的文件中检索这些值。

如果您希望我发布正则表达式的解释,请告诉我,我也可以这样做。 如果您想自己弄清楚正则表达式,请在Google中输入正则表达式备忘单,然后选择您想要了解有关正则表达式匹配的更多结果。

我的代码基于以下假设。每个坐标都在一个单独的行上,就像您发布的文件格式所暗示的那样。我还假设你需要以四个为单位将坐标传递给你的系统。您提到众所周知的格式是两个坐标,后跟逗号和另外两个坐标。我编写的代码为您提供了一个我包含的类的列表,该类的每个实例将具有构成属性集的四个坐标和一个以您想要的格式返回它们的属性。该列表不能添加无效的坐标集。你的代码中可能有一个错误,它可能会错过最后一个坐标,我从未测试过那么远。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim lst As New List(Of CoordinateSet)
    Using sr As New System.IO.StreamReader("c:\temp\coords.txt")
        Dim line As String = sr.ReadLine
        Dim counter As Integer = 0
        Dim coord As New CoordinateSet
        While line IsNot Nothing
            If coord.CoordSet IsNot Nothing Then
                lst.Add(coord.Clone)
                coord = New CoordinateSet
            End If
            If IsMatch(line.Trim) Then
                If coord.One Is Nothing Then
                    coord.One = line.Trim
                ElseIf coord.Two Is Nothing Then
                    coord.Two = line.Trim
                ElseIf coord.Three Is Nothing Then
                    coord.Three = line.Trim
                ElseIf coord.Four Is Nothing Then
                    coord.Four = line.Trim
                End If
            End If
            line = sr.ReadLine
        End While
    End Using
    MessageBox.Show(String.Format("I found {0} coordinate pairs.", lst.Count))
End Sub

Private Function IsMatch(line As String) As Boolean
    Dim reg As New System.Text.RegularExpressions.Regex("^[-]?[0-9]+[\.]+[0-9]+[ ][-]?[0-9]+[\.]+[0-9]+")
    Return reg.IsMatch(line)
End Function

Public Class CoordinateSet
    Implements ICloneable
    Public Property One As String
    Public Property Two As String
    Public Property Three As String
    Public Property Four As String
    Public ReadOnly Property CoordSet As String
        Get
            If Not String.IsNullOrEmpty(One) AndAlso Not String.IsNullOrEmpty(Two) AndAlso Not String.IsNullOrEmpty(Three) AndAlso Not String.IsNullOrEmpty(Four) Then
                Return String.Format("{0} {1}, {2} {3}", One, Two, Three, Four)
            Else
                Return Nothing
            End If
        End Get
    End Property

    Public Function Clone() As Object Implements ICloneable.Clone
        Return MemberwiseClone()
    End Function
End Class