Open ihsienlee opened 4 years ago
As I understand it, GeoJSON is just a particular JSON schema, so you should be able to read GeoJSON files with fson.
If you were reading a file like the one in the example here: https://en.wikipedia.org/wiki/GeoJSON
you could read the coordinates something like this:
json_data => fson_parse("example.json")
call fson_get(json_data, "features", features)
do i = 1, fson_value_count(features)
item => fson_value_get(features, i)
call fson_get(item, "geometry.coordinates", coords)
! do something with coords array
end do
Thank you for your response I try your code. But I got the information, 'Resolved value is not an array. geometry.coordinates[ 1 ]'
This is my test geojson data. { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "Point", "coordinates": [102.0, 0.5] }, "properties": { "prop0": "value0" } }, { "type": "Feature", "geometry": { "type": "LineString", "coordinates": [ [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0] ] }, "properties": { "prop0": "value0", "prop1": 0.0 } }, { "type": "Feature", "geometry": { "type": "Polygon", "coordinates": [ [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] ] }, "properties": { "prop0": "value0", "prop1": { "this": "that" } } } ] }
Could you post the code you used which gave this error?
This zip is my code and json file. geojson_test.zip
There are a few problems here:
no_data
variable for two things in your code- it's used for the length of the features
array, and then re-used as the length of the coords
array inside the loop. You should use two variables for these two different things (especially as no_data
is the upper bound on the for
loop and should not be changed inside the loop).GeojsonTEST.json
is not valid JSON- you can test that by running it through a JSON validator e.g. https://jsonlint.com/. The first feature has got something wrong with it- the coordinates
value has got a rank-4 array, followed by a comma and then some other arrays.Note that if you want to parse JSON arrays, fson
will parse arrays up to rank 2 directly into Fortran arrays (as stated in the readme) but if you have arrays of higher rank than 2, you will have to iterate over them to parse them.
Another problem I can see is that some of the features have geometry.coordinates
arrays of rank 1 and some of rank 2. You will need to declare and use Fortran arrays of the appropriate rank in each case- you can't read a rank-1 array into a rank-2 variable.
I was so pleased to hear from you. Thank you very much.
Whoops, yes the file GeojsonTEST.json is valid- not sure what happened there, I may have accidentally edited it before testing.
For parsing arrays of rank greater than two, I am not sure but it's possible that the json-fortran library may be able to do this.
Thank you for your help. I will try it.
Dear all, I want to read geojson file and get the coordinations. Can you help me to read coordinations array? Thanks IH Lee