Vb.net 将文本文件的Streamread部分转换为字符串
使用VB.NET。我正在尝试创建一个空间坐标字符串,用作查询web服务时的参数。坐标需要从文本文件中提取,这是GIS的标准输出,可以是任意长度 web服务需要以逗号分隔的“已知文本”格式查看坐标对字符串。例如-2.780299 53.373266,-2.780606 53.372617,-2.782207 53.370392,-2.784552 53.362061 表面上类似于被问的问题 文本文件位于此结构中。重申一下,我需要一个字符串,它只包含161后面和pen前面用逗号分隔的坐标对。161是一个不一致的数字,尽管单词Region是Vb.net 将文本文件的Streamread部分转换为字符串,vb.net,string,parsing,streamreader,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 表面上类似于被问的问题 文本文件位于此结构中。重申一下,我需要一个字符串,它只包含161后面和pen前面用逗号分隔的坐标对。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
我会用正则表达式处理这些行。逐行将每一行加载到解析器中,但除了与正则表达式模式匹配外,不检测任何内容。通过采用这种方法,您可以处理任意大小的文件,并且只从您感兴趣的文件中检索这些值 如果你想让我发布关于正则表达式的解释,请让我知道,我也可以这样做。 如果你想自己找出正则表达式,在谷歌中输入正则表达式备忘单,然后选择任何你想了解更多正则表达式匹配信息的结果 我的代码基于以下假设。正如您发布的文件格式所暗示的那样,每个坐标都位于单独的一行上。我还假设您需要将坐标以四个一组的形式传递给您的系统。您提到的著名格式是两个坐标,后跟一个逗号和另外两个坐标。我编写的代码为您提供了一个我包含的类的列表,该类的每个实例都有四个坐标,它们构成了属性集合,还有一个属性以您想要的格式返回它们。列表中不能添加无效的坐标集。我的代码中可能有一个bug,因为它可能会错过最后一个坐标,我从来没有测试过那么远
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
非常感谢您的回复。我还没有机会尝试你的解决方案,但只是通读一下你的解决方案,我想我可能有点搞混了。文本文件格式在显示的结构中,每个坐标点位于单独的一行上,但我不需要for集合,而是需要一个由描述多边形的所有点组成的字符串。每个坐标/直线用逗号分隔。我可能可以使用你发布的代码,但是请容忍我!正则表达式在识别要提取的行时特别有用。需要对代码进行一些修改以适应我的最终用途,但大多数修改如本解决方案所示。干杯,威森加莫!