fson icon indicating copy to clipboard operation
fson copied to clipboard

Read polygon geojson

Open ihsienlee opened this issue 4 years ago • 9 comments

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

ihsienlee avatar Sep 26 '20 12:09 ihsienlee

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

acroucher avatar Sep 28 '20 05:09 acroucher

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

ihsienlee avatar Oct 05 '20 07:10 ihsienlee

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

acroucher avatar Oct 06 '20 02:10 acroucher

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

ihsienlee avatar Oct 06 '20 06:10 ihsienlee

There are a few problems here:

  • you are re-using the 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).
  • the file 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.

acroucher avatar Oct 06 '20 22:10 acroucher

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.

ihsienlee avatar Oct 07 '20 00:10 ihsienlee

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

acroucher avatar Oct 07 '20 04:10 acroucher

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.

acroucher avatar Oct 08 '20 22:10 acroucher

Thank you for your help. I will try it.

ihsienlee avatar Oct 09 '20 02:10 ihsienlee