2

I can't find any simple C source file or library to parse geojson files (implementing rfc7946 standard).

Maybe such parsers exist in projects like GRASS or GDAL? But I still have a little trouble navigating the world of C libraries.

Some tracks ?

ce.teuf
  • 746
  • 6
  • 13
  • 1
    You could always write your own parser – h0r53 Aug 31 '21 at 17:05
  • Three doesn't appear to be anything special about the JSON you're referring to. Why do you need a special parser? – Andrew Henle Aug 31 '21 at 17:07
  • @h0r53 I'm agree with that, I also ask the question to know if I will reinvent the wheel or not. But it would be cool to do – ce.teuf Aug 31 '21 at 17:07
  • @AndrewHenle Some keys are required, case-sentives and other things. I guess JSON parsers don't implement these checks. – ce.teuf Aug 31 '21 at 17:22
  • 1
    @ce.teuf If GeoJSON is a strict subset of JSON, than any JSON parser can parse it. You'd only have to add checks for objects outside of the GeoJSON specification. That's a lot easier than creating a JSON parser from nothing. – Andrew Henle Aug 31 '21 at 17:27
  • @AndrewHenle Ok so I'll do it like this. Thank you a lot ! – ce.teuf Aug 31 '21 at 17:30

3 Answers3

2

GeoJSON is in JSON format, so you can use JSON parsers. There are several out there:

https://github.com/search?l=C&q=json+parser&type=Repositories

You should add checks and other stuff yourself.

If you need a certain representation of data, you can write it yourself. Mapbox wrote a C++ version of GeoJSON parser for converting GeoJSON into geometry.hpp representation based on RapidJSON in less than 1k lines of code.

A standard-compliant (RFC 7946) implementation of GeoJSON in C can be found inside SpatiaLite, but may be it's easier for you to use typical JSON parsers instead. You can take a look in virtualgeojson.c if you want to use this library.

Ho1
  • 1,239
  • 1
  • 11
  • 29
1

Ho1's answer is perfect if you want to do basic geometry manipulations. For advanced stuff (that you do not want to reinvent), you could consider using geos's CAPI.

Note that the CAPI GeoJSON writer is quite new, and the doc not up to date unfortunately. Here's the header file containing those changes: https://github.com/libgeos/geos/blob/main/capi/geos_c.h.in

Ulysse BN
  • 10,116
  • 7
  • 54
  • 82
  • 1
    Thank You ! I will practice by trying to implement the restrictions enacted by the standard. But I keep the package you suggest under my elbow. – ce.teuf Aug 31 '21 at 17:35
0

I finally got started.

I am writing a small library based on json-parser which allows to say whether or not the parsed json is a valid geojson. (according to standard 7946).

This is obviously a first draft, there is still a lot of work.

I am a beginner in C, so if you have any advice or comments I am obviously interested.

ce.teuf
  • 746
  • 6
  • 13