Shapefile index file: C++98/STL parsing library

File extension

shx

KS implementation details

License: CC0-1.0

References

This page hosts a formal specification of Shapefile index file using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Runtime library

All parsing code for C++98/STL generated by Kaitai Struct depends on the C++/STL runtime library. You have to install it before you can parse data.

For C++, the easiest way is to clone the runtime library sources and build them along with your project.

Code

Using Kaitai Struct in C++/STL usually consists of 3 steps.

  1. We need to create an STL input stream (std::istream). One can open local file for that, or use existing std::string or char* buffer.
    #include <fstream>
    
    std::ifstream is("path/to/local/file.shx", std::ifstream::binary);
    
    #include <sstream>
    
    std::istringstream is(str);
    
    #include <sstream>
    
    const char buf[] = { ... };
    std::string str(buf, sizeof buf);
    std::istringstream is(str);
    
  2. We need to wrap our input stream into Kaitai stream:
    #include "kaitai/kaitaistream.h"
    
    kaitai::kstream ks(&is);
    
  3. And finally, we can invoke the parsing:
    shapefile_index_t data(&ks);
    

After that, one can get various attributes from the structure by invoking getter methods like:

data.records() // => the size of this section of the file in bytes must equal (header.file_length * 2) - 100

C++98/STL source code to parse Shapefile index file

shapefile_index.h

#ifndef SHAPEFILE_INDEX_H_
#define SHAPEFILE_INDEX_H_

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

#include "kaitai/kaitaistruct.h"
#include <stdint.h>
#include <vector>

#if KAITAI_STRUCT_VERSION < 9000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.9 or later is required"
#endif

class shapefile_index_t : public kaitai::kstruct {

public:
    class file_header_t;
    class record_t;
    class bounding_box_x_y_z_m_t;
    class bounds_min_max_t;

    enum shape_type_t {
        SHAPE_TYPE_NULL_SHAPE = 0,
        SHAPE_TYPE_POINT = 1,
        SHAPE_TYPE_POLY_LINE = 3,
        SHAPE_TYPE_POLYGON = 5,
        SHAPE_TYPE_MULTI_POINT = 8,
        SHAPE_TYPE_POINT_Z = 11,
        SHAPE_TYPE_POLY_LINE_Z = 13,
        SHAPE_TYPE_POLYGON_Z = 15,
        SHAPE_TYPE_MULTI_POINT_Z = 18,
        SHAPE_TYPE_POINT_M = 21,
        SHAPE_TYPE_POLY_LINE_M = 23,
        SHAPE_TYPE_POLYGON_M = 25,
        SHAPE_TYPE_MULTI_POINT_M = 28,
        SHAPE_TYPE_MULTI_PATCH = 31
    };

    shapefile_index_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, shapefile_index_t* p__root = 0);

private:
    void _read();
    void _clean_up();

public:
    ~shapefile_index_t();

    class file_header_t : public kaitai::kstruct {

    public:

        file_header_t(kaitai::kstream* p__io, shapefile_index_t* p__parent = 0, shapefile_index_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~file_header_t();

    private:
        std::string m_file_code;
        std::string m_unused_field_1;
        std::string m_unused_field_2;
        std::string m_unused_field_3;
        std::string m_unused_field_4;
        std::string m_unused_field_5;
        int32_t m_file_length;
        std::string m_version;
        shape_type_t m_shape_type;
        bounding_box_x_y_z_m_t* m_bounding_box;
        shapefile_index_t* m__root;
        shapefile_index_t* m__parent;

    public:

        /**
         * corresponds to s4be value of 9994
         */
        std::string file_code() const { return m_file_code; }
        std::string unused_field_1() const { return m_unused_field_1; }
        std::string unused_field_2() const { return m_unused_field_2; }
        std::string unused_field_3() const { return m_unused_field_3; }
        std::string unused_field_4() const { return m_unused_field_4; }
        std::string unused_field_5() const { return m_unused_field_5; }
        int32_t file_length() const { return m_file_length; }

        /**
         * corresponds to s4le value of 1000
         */
        std::string version() const { return m_version; }
        shape_type_t shape_type() const { return m_shape_type; }
        bounding_box_x_y_z_m_t* bounding_box() const { return m_bounding_box; }
        shapefile_index_t* _root() const { return m__root; }
        shapefile_index_t* _parent() const { return m__parent; }
    };

    class record_t : public kaitai::kstruct {

    public:

        record_t(kaitai::kstream* p__io, shapefile_index_t* p__parent = 0, shapefile_index_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~record_t();

    private:
        int32_t m_offset;
        int32_t m_content_length;
        shapefile_index_t* m__root;
        shapefile_index_t* m__parent;

    public:
        int32_t offset() const { return m_offset; }
        int32_t content_length() const { return m_content_length; }
        shapefile_index_t* _root() const { return m__root; }
        shapefile_index_t* _parent() const { return m__parent; }
    };

    class bounding_box_x_y_z_m_t : public kaitai::kstruct {

    public:

        bounding_box_x_y_z_m_t(kaitai::kstream* p__io, shapefile_index_t::file_header_t* p__parent = 0, shapefile_index_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~bounding_box_x_y_z_m_t();

    private:
        bounds_min_max_t* m_x;
        bounds_min_max_t* m_y;
        bounds_min_max_t* m_z;
        bounds_min_max_t* m_m;
        shapefile_index_t* m__root;
        shapefile_index_t::file_header_t* m__parent;

    public:
        bounds_min_max_t* x() const { return m_x; }
        bounds_min_max_t* y() const { return m_y; }
        bounds_min_max_t* z() const { return m_z; }
        bounds_min_max_t* m() const { return m_m; }
        shapefile_index_t* _root() const { return m__root; }
        shapefile_index_t::file_header_t* _parent() const { return m__parent; }
    };

    class bounds_min_max_t : public kaitai::kstruct {

    public:

        bounds_min_max_t(kaitai::kstream* p__io, shapefile_index_t::bounding_box_x_y_z_m_t* p__parent = 0, shapefile_index_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~bounds_min_max_t();

    private:
        double m_min;
        double m_max;
        shapefile_index_t* m__root;
        shapefile_index_t::bounding_box_x_y_z_m_t* m__parent;

    public:
        double min() const { return m_min; }
        double max() const { return m_max; }
        shapefile_index_t* _root() const { return m__root; }
        shapefile_index_t::bounding_box_x_y_z_m_t* _parent() const { return m__parent; }
    };

private:
    file_header_t* m_header;
    std::vector<record_t*>* m_records;
    shapefile_index_t* m__root;
    kaitai::kstruct* m__parent;

public:
    file_header_t* header() const { return m_header; }

    /**
     * the size of this section of the file in bytes must equal (header.file_length * 2) - 100
     */
    std::vector<record_t*>* records() const { return m_records; }
    shapefile_index_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // SHAPEFILE_INDEX_H_

shapefile_index.cpp

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

#include "shapefile_index.h"
#include "kaitai/exceptions.h"

shapefile_index_t::shapefile_index_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, shapefile_index_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    m_header = 0;
    m_records = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void shapefile_index_t::_read() {
    m_header = new file_header_t(m__io, this, m__root);
    m_records = new std::vector<record_t*>();
    {
        int i = 0;
        while (!m__io->is_eof()) {
            m_records->push_back(new record_t(m__io, this, m__root));
            i++;
        }
    }
}

shapefile_index_t::~shapefile_index_t() {
    _clean_up();
}

void shapefile_index_t::_clean_up() {
    if (m_header) {
        delete m_header; m_header = 0;
    }
    if (m_records) {
        for (std::vector<record_t*>::iterator it = m_records->begin(); it != m_records->end(); ++it) {
            delete *it;
        }
        delete m_records; m_records = 0;
    }
}

shapefile_index_t::file_header_t::file_header_t(kaitai::kstream* p__io, shapefile_index_t* p__parent, shapefile_index_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_bounding_box = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void shapefile_index_t::file_header_t::_read() {
    m_file_code = m__io->read_bytes(4);
    if (!(file_code() == std::string("\x00\x00\x27\x0A", 4))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x00\x00\x27\x0A", 4), file_code(), _io(), std::string("/types/file_header/seq/0"));
    }
    m_unused_field_1 = m__io->read_bytes(4);
    if (!(unused_field_1() == std::string("\x00\x00\x00\x00", 4))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x00\x00\x00\x00", 4), unused_field_1(), _io(), std::string("/types/file_header/seq/1"));
    }
    m_unused_field_2 = m__io->read_bytes(4);
    if (!(unused_field_2() == std::string("\x00\x00\x00\x00", 4))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x00\x00\x00\x00", 4), unused_field_2(), _io(), std::string("/types/file_header/seq/2"));
    }
    m_unused_field_3 = m__io->read_bytes(4);
    if (!(unused_field_3() == std::string("\x00\x00\x00\x00", 4))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x00\x00\x00\x00", 4), unused_field_3(), _io(), std::string("/types/file_header/seq/3"));
    }
    m_unused_field_4 = m__io->read_bytes(4);
    if (!(unused_field_4() == std::string("\x00\x00\x00\x00", 4))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x00\x00\x00\x00", 4), unused_field_4(), _io(), std::string("/types/file_header/seq/4"));
    }
    m_unused_field_5 = m__io->read_bytes(4);
    if (!(unused_field_5() == std::string("\x00\x00\x00\x00", 4))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x00\x00\x00\x00", 4), unused_field_5(), _io(), std::string("/types/file_header/seq/5"));
    }
    m_file_length = m__io->read_s4be();
    m_version = m__io->read_bytes(4);
    if (!(version() == std::string("\xE8\x03\x00\x00", 4))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\xE8\x03\x00\x00", 4), version(), _io(), std::string("/types/file_header/seq/7"));
    }
    m_shape_type = static_cast<shapefile_index_t::shape_type_t>(m__io->read_s4le());
    m_bounding_box = new bounding_box_x_y_z_m_t(m__io, this, m__root);
}

shapefile_index_t::file_header_t::~file_header_t() {
    _clean_up();
}

void shapefile_index_t::file_header_t::_clean_up() {
    if (m_bounding_box) {
        delete m_bounding_box; m_bounding_box = 0;
    }
}

shapefile_index_t::record_t::record_t(kaitai::kstream* p__io, shapefile_index_t* p__parent, shapefile_index_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void shapefile_index_t::record_t::_read() {
    m_offset = m__io->read_s4be();
    m_content_length = m__io->read_s4be();
}

shapefile_index_t::record_t::~record_t() {
    _clean_up();
}

void shapefile_index_t::record_t::_clean_up() {
}

shapefile_index_t::bounding_box_x_y_z_m_t::bounding_box_x_y_z_m_t(kaitai::kstream* p__io, shapefile_index_t::file_header_t* p__parent, shapefile_index_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_x = 0;
    m_y = 0;
    m_z = 0;
    m_m = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void shapefile_index_t::bounding_box_x_y_z_m_t::_read() {
    m_x = new bounds_min_max_t(m__io, this, m__root);
    m_y = new bounds_min_max_t(m__io, this, m__root);
    m_z = new bounds_min_max_t(m__io, this, m__root);
    m_m = new bounds_min_max_t(m__io, this, m__root);
}

shapefile_index_t::bounding_box_x_y_z_m_t::~bounding_box_x_y_z_m_t() {
    _clean_up();
}

void shapefile_index_t::bounding_box_x_y_z_m_t::_clean_up() {
    if (m_x) {
        delete m_x; m_x = 0;
    }
    if (m_y) {
        delete m_y; m_y = 0;
    }
    if (m_z) {
        delete m_z; m_z = 0;
    }
    if (m_m) {
        delete m_m; m_m = 0;
    }
}

shapefile_index_t::bounds_min_max_t::bounds_min_max_t(kaitai::kstream* p__io, shapefile_index_t::bounding_box_x_y_z_m_t* p__parent, shapefile_index_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void shapefile_index_t::bounds_min_max_t::_read() {
    m_min = m__io->read_f8be();
    m_max = m__io->read_f8be();
}

shapefile_index_t::bounds_min_max_t::~bounds_min_max_t() {
    _clean_up();
}

void shapefile_index_t::bounds_min_max_t::_clean_up() {
}