shapelib

repository·master·Indexed 41 days ago

https://github.com/osgeo/shapelib

A library for reading and writing ESRI Shapefiles. It includes a CMake-based build system supporting Unix, Linux, MinGW, and MSYS, as well as support for Visual C++. The library provides functionality for quadtree searching via SHPTreeFindLikelyShapes() and a utility program, shptreedump, for generating ASCII renderings of shapefile internal quadtree structures.

Tokens
1.6K
Snippets
7
Records
8
Agent score
51%

What's inside shapelib

  1. How quadtree searching works in Shapelib

    master

    Searching the quadtree is a multi-stage process designed to balance speed and accuracy. The SHPTreeFindLikelyShapes() function in shptree.c implements the first stage.

    1. Phase 1: Quadtree Traversal: The algorithm traverses the quadtree and collects shapeids from every node whose bounding rectangle overlaps the search rectangle. This stage is fast but can produce "false positives" because a shape's actual bounding box is often much smaller than its containing quadtree node.
    2. Phase 2: Shape Verification: The actual shapes are fetched, and their specific bounding boxes are checked against the search area to filter out the false positives from Phase 1.
    3. Phase 3 (Optional/Manual): A third phase can be implemented to verify if actual line segments within a shape cross the area of interest, though this is typically handled by drawing engines via clipping.
  2. Build Shapelib with VC++

    master

    To build the Shapelib distribution using Microsoft Visual C++:

    1. Navigate to the shapelib subdirectory within the unpacked distribution.
    2. Open a DOS/Command Prompt window.
    3. Run the following command:
    nmake -f makefile.vc

    Alternatively, you can create a manual Visual C++ project by including the following source files and building them as a Win32 console application:

    • shpopen.c
    • shptree.c
    • shpdumptree.c
  3. Configure CMake for MinGW or MSYS

    master

    When building on MinGW or MSYS platforms, the procedure is identical to Unix/Linux, but you should specify the appropriate generator using the -G flag. Instead of "Unix Makefiles", use "MSYS Makefiles" to ensure compatibility with the platform.

    cmake \
    -S /path/to/shapelib-1.6.0 \
    -B build_dir \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=/path/to/install \
    -DEG_DATA=/path/to/eg_data/
  4. Build shapelib on Unix

    master

    To build shapelib on a Unix-like system, follow these steps in order:

    1. Generate build scripts by running ./configure.
      • You can run ./configure --help to see available fine-tuning options.
    2. Compile the project by running make.
    3. (Optional) Run self-tests using make check.
    4. Install the project using make install.
    ./configure
    make
    make check
    make install
  5. Build and install shapelib using CMake

    master

    To build and install shapelib on Unix, Linux, MinGW, or MSYS, use the CMake build system. It is recommended to use an out-of-source build by creating a separate build directory.

    Important Notes:

    • The gcc -fvisibility=hidden option is not yet supported by shapelib.
    • To run the "stream1" tests, you must provide the location of the eg_data tree using the -DEG_DATA flag. If this is not provided, the stream1 tests will be skipped.

    Steps:

    1. Download and unpack the shapelib source.
    2. Copy the CMakeLists.txt file into the top-level of the unpacked source tree.
    3. (Optional) Download shape_eg_data.zip from http://dl.maptools.org/dl/shapelib/shape_eg_data.zip to enable full testing.
    4. Configure, build, install, and test using the commands below.
    # 1. Configure
    # Replace paths with your actual local paths
    env CC=gcc CFLAGS="-O3 -Wuninitialized" \
    cmake \
    -S /path/to/shapelib-1.6.0 \
    -B build_dir \
    -G "Unix Makefiles" \
    -DCMAKE_INSTALL_PREFIX=/home/software/shapelib/install \
    -DEG_DATA=/path/to/eg_data/
    
    # 2. Build and install
    cmake --build build_dir -- VERBOSE=1 -j4 install
    
    # 3. Test
    ctest --test-dir build_dir
  6. Build shapelib on Windows

    master

    To build shapelib on Windows using the Visual C++ compiler:

    1. Ensure you have run VCVARS32.BAT to set up your environment.
    2. Open a command window and run nmake /f makefile.vc to build the code and executables.

    Alternatively, you can manually create your own VC++ project using the source files.

    nmake /f makefile.vc
  7. Reference: CMake configuration options for shapelib

    master

    The following CMake flags are used during the configuration step to control the build environment and test coverage.

    -S <dir>             Path to the shapelib source tree
    -B <dir>             Path to the build directory (out-of-source build)
    -G <generator>       The build system generator (e.g., "Unix Makefiles" or "MSYS Makefiles")
    -DCMAKE_INSTALL_PREFIX=<path>  The destination directory for the installation
    -DEG_DATA=<path>     Path to the eg_data tree (required for "stream1" tests)
  8. Dump the Shapefile Quadtree with shptreedump

    master

    The shptreedump program (demonstrated in shpdumptree.c) allows you to generate an ASCII rendering of the internal quadtree structure of a shapefile. This is useful for visualizing the tree hierarchy or as a template for writing custom programs that traverse the tree to populate databases.

    Command Line Options

    • -maxdepth <depth>: Controls the maximum depth of the tree traversal. If not specified, the program computes a reasonable depth based on the number of structures in the shapefile.
    • -v: Expands the report to include full shape information (including bounding boxes and vertices) instead of just the shapeid.
    • -search <minX> <minY> <maxX> <maxY> <shapefile>: Performs a quadtree search within the specified bounding box.
    # Dump tree with a specific depth
    shptreedump -maxdepth 6 eg_data/polygon.shp
    
    # Dump tree with full shape details
    shptreedump -v eg_data/polygon.shp
    
    # Search the quadtree in a specific region
    shptreedump -search 471127 4751545 476622 4759281 eg_data/polygon.shp