Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to output a GeoJSONFeatureCollection? #44

Open
mklnz opened this issue Jun 3, 2016 · 1 comment
Open

How to output a GeoJSONFeatureCollection? #44

mklnz opened this issue Jun 3, 2016 · 1 comment

Comments

@mklnz
Copy link

mklnz commented Jun 3, 2016

Edit: Now I found out this is due to a conflict between Rail's ActiveSupport to_json and the json gem. Usage outside Rails environment works as intended. How to get around this?

I'm not sure if I'm using this functionality correctly or not, but I'm having trouble outputting a valid GeoJSON collection.

Example:
GeoJSONFeature.new(point1).to_json

Output:

{  
   "type":"Feature",
   "geometry":{  
      "type":"Point",
      "coordinates":[  
         121.4412656998513,
         31.2108137285587
      ]
   },
   "properties":{  
   }
}

But when a GeoJSONFeature is inside an array, it skips over its own GeoJSONFeature#to_json method (double checked with byebug) and is missing the "type": "Feature" attribute.

Example:
[GeoJSONFeature.new(point1)].to_json

Output:

[  
   {  
      "geometry":{  
         "type":"Point",
         "coordinates":[  
            121.4412656998513,
            31.2108137285587
         ]
      },
      "properties":{  
      },
      "id":null
   }
]

Finally putting it together with a FeatureCollection:

    features = [
      GeoJSONFeature.new(start_point),
      GeoJSONFeature.new(end_point)
    ]
    GeoJSONFeatureCollection.new(features)

Output (not valid GeoJSON):

{  
   "type":"FeatureCollection",
   "features":[  
      {  
         "geometry":{  
            "type":"Point",
            "coordinates":[  
               121.4412656998513,
               31.2108137285587
            ]
         },
         "properties":{  
         },
         "id":null
      },
      {  
         "geometry":{  
            "type":"Point",
            "coordinates":[  
               121.4569708797648,
               31.219778491221284
            ]
         },
         "properties":{  
         },
         "id":null
      }
   ]
}
@mklnz mklnz changed the title GeoJSON output FeatureCollection incorrect How to output a GeoJSONFeatureCollection? Jun 3, 2016
@nofxx
Copy link
Owner

nofxx commented Jun 3, 2016

Out of coincidence: working on this json thing.
It's too awkward now. It should be anything.to_geojson and we're good.
Gonna let you know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants