2014-10-01 145 views
0

使用VB.NET。我想創建一個空間座標的字符串作爲參數查詢Web服務。座標需要從文本文件(GIS的標準輸出)中提取,該文件可以是任意長度的。Streamread將文本文件的一部分寫入字符串

Web服務需要以逗號分隔的「公知文本」格式查看座標對的字符串。例如。 -2.780299 53.373266,-2.780606 53.372617,-2.782207 53.370392,-2.784552 53.362061

(表面上類似於一個問題問here

的文本文件,在這個結構。重申我需要一個只包含座標(用逗號分隔的對)在「161」之後和「筆」之前的字符串。儘管「地區」一詞是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 

回答

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 
+0

非常感謝您的回覆。我沒有機會嘗試你的解決方案,但只是閱讀你的解決方案,我想我可能會有些困惑。文本文件格式在所示的結構中(每個座標點在一個單獨的行上),但我不需要for的集合,而是需要所有點(描述多邊形)的單個字符串。每個座標/行用逗號分隔。我可能可以使用您發佈的代碼,但請耐心等待! – benj 2014-10-02 17:18:36

+0

正則表達式在識別要提取的行中特別有用。我們需要對代碼進行一些修改才能符合我的最終用途,但大多數情況與此解決方案中的相同。歡呼Wizengamot! – benj 2014-10-10 09:11:44