Parse geojson files with C

1k views Asked by At

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 ?

3

There are 3 answers

0
Ho1 On BEST ANSWER

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.

1
Ulysse BN On

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

0
ce.teuf On

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.