-
Dec 1st, 2019, 12:54 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Reading GeoJson polygon array
Hi All,
I have a GoeJson file which contain polygon array (X an Y xoordinates )
I would like to read these data and save them into X() and Y() arrays.
In fact, i already read this file and save them into string array but i couldn't split this string to my new arrays (X(),Y())
Here is my code
Code:
Dim JsonFile as string
JSonFile={"features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[27.68405,36.72502],[27.68405,36.72501],[27.68399,36.72492],[27.6839,36.72491],[27.68383,36.72499]]]}}]}
Dim JSon = Newtonsoft.Json.Linq.JObject.Parse(JsonFile)
Dim X as string
For Each element In Json.SelectToken("features")(0)("geometry")("coordinates") '(0)
X = element.ToString
Next
-
Dec 2nd, 2019, 07:08 AM
#2
Re: Reading GeoJson polygon array
Hi Levent,
I'm not familiar with GoeJson files but I believe they consist of a series of coordinates in the form of two integers separated by a comma.
That being so, and assuming that your string variable 'JsonFile' contains the coordinates in that form, you might like to try something like:
Code:
'Read all coordinates to String Array.
Dim coords() As String = JsonFile.Split(",")
'Separate into X and Y.
Dim X(), Y() As String
For i = 0 to coords.length-1 Step 2
X(i) = coords(i)
Y(i + 1) = coords(i + 1)
Next
Poppa.
Last edited by Poppa Mintin; Dec 2nd, 2019 at 07:10 AM.
Reason: Typo.
Along with the sunshine there has to be a little rain sometime.
-
Dec 2nd, 2019, 11:27 PM
#3
Re: Reading GeoJson polygon array
Originally Posted by levent
Hi All,
I have a GoeJson file which contain polygon array (X an Y xoordinates )
I would like to read these data and save them into X() and Y() arrays.
In fact, i already read this file and save them into string array but i couldn't split this string to my new arrays (X(),Y())
Here is my code
Code:
Dim JsonFile as string
JSonFile={"features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[27.68405,36.72502],[27.68405,36.72501],[27.68399,36.72492],[27.6839,36.72491],[27.68383,36.72499]]]}}]}
Dim JSon = Newtonsoft.Json.Linq.JObject.Parse(JsonFile)
Dim X as string
For Each element In Json.SelectToken("features")(0)("geometry")("coordinates") '(0)
X = element.ToString
Next
So what does X contain at that point?
You mentioned getting the values into a string array, but it doesn't look like the code you posted does that. It just takes the elements one by one and converts them to a string.
I guess since your code example creates the "Jason File", there is only one element, and X probably contains:
"[27.68405,36.72502],[27.68405,36.72501],[27.68399,36.72492],[27.6839,36.72491],[27.68383,36.72499]"
or perhaps an extra set of brackets around the set.
I'm not real familiar with Regular Expressions, and there would probably be a good way to parse this using one, but I'll post a less robust, perhaps more brute force example.
If there are extra brackets around the set, then the code below won't work as is (it will drop the first X).
There are plenty of ways this could be done, this is just the first example off the top of my head.
Code:
Public Class Form1
Dim XList As New List(Of Double)
Dim YList As New List(Of Double)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim t As String = "[27.68405,36.72502],[27.68405,36.72501],[27.68399,36.72492],[27.6839,36.72491],[27.68383,36.72499]"
Dim p() As String
Dim tVal As Double
p = t.Split(","c)
For Each s As String In p
If s(0) = "[" Then
If Char.IsDigit(s(1)) Then 'It looks like we have an X
If Double.TryParse(s.Substring(1), tVal) Then
XList.Add(tVal)
End If
End If
ElseIf Char.IsDigit(s(0)) Then 'it looks like we have a Y
Dim bracket As Integer = s.IndexOf("]"c)
If Double.TryParse(s.Substring(0, bracket), tVal) Then
YList.Add(tVal)
End If
End If
Next
End Sub
End Class
I just wrote the values in Lists, which can be indexed like arrays if you like.
If you really want the values in arrays, then you can use the List.ToArray method to copy the list to an array.
Last edited by passel; Dec 2nd, 2019 at 11:31 PM.
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Dec 4th, 2019, 03:13 AM
#4
Thread Starter
Addicted Member
Re: Reading GeoJson polygon array
Dear Papa Minton and Passel;
I would like to thank your for your answers.
I will test both method and let you know about result.
Best regards
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|