josephalevin / fson

Fortran 95 JSON Parser
60 stars 25 forks source link

Read polygon geojson #30

Open ihsienlee opened 4 years ago

ihsienlee commented 4 years ago

Dear all, I want to read geojson file and get the coordinations. Can you help me to read coordinations array? Thanks IH Lee

acroucher commented 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
ihsienlee commented 4 years ago

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" } } } ] }

acroucher commented 4 years ago

Could you post the code you used which gave this error?

ihsienlee commented 4 years ago

This zip is my code and json file. geojson_test.zip

acroucher commented 4 years ago

There are a few problems here:

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.

ihsienlee commented 4 years ago

I was so pleased to hear from you. Thank you very much.

  1. You are right. I can't re-used 'no_data' for loop.
  2. I test the file 'GeojsonTEST.json', but this is valid JSON. testJSON The type of first feature is 'MultiPolygon'.
  3. I know that the 'fson' parse arrays up to rank 2 directly into Fortran arrays. But GeoJSON format is more popularer in GIS domain. I suggest that adding a test code for geojson.
acroucher commented 4 years ago

Whoops, yes the file GeojsonTEST.json is valid- not sure what happened there, I may have accidentally edited it before testing.

acroucher commented 4 years ago

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.

ihsienlee commented 4 years ago

Thank you for your help. I will try it.