[cpp] LoadGeodata()
Viewer
- bool LoadGeodata()
- {
- using namespace json11;
- using namespace std;
- string err;
- /* Load the GeoJSON file as a string initially. We'll parse it soon.. */
- size_t filesize;
- char* text{};
- FILE *handle = FioFOpenFile(_file_to_saveload.name, "rb", GEODATA_DIR, &filesize);
- if (handle == nullptr) return false;
- text = ReallocT(text, filesize);
- size_t read = fread(text, 1, filesize, handle);
- fclose(handle);
- if (read != filesize) return false;
- /* Parse the string as a Json object and check its validity. */
- Json loaded = Json::parse(text, err).object_items();
- if (!loaded["features"].is_array()) return false; // TODO: Throw a descriptive error to the player if the json is invalid.
- /* We only want the contents of "features", so let's drill down. */
- std::vector<Json> geodata = loaded["features"].array_items();
- /* First we need to find NORTH_EXTENT and SOUTH_EXTENT, to determine map scaling. */
- RealCoordinates north_extent;
- RealCoordinates south_extent;
- for (auto item : geodata) {
- if (item["name"].string_value() == "NORTH_EXTENT") {
- std::string print = item["coordinates"].string_value();
- std::cout << print << "\n";
- }
- }
- return true;
- }
Editor
You can edit this paste and save as new: