libpysal

repository·main·Indexed 43 days ago

https://github.com/pysal/libpysal

Core components of PySAL, providing essential building blocks for spatial analysis in Python, including computational geometry, spatial weights, graph encoding, and I/O operations.

Tokens
11.6K
Snippets
31
Records
64
Agent score
55%

What's inside libpysal

  1. Overview of libpysal modules

    main

    libpysal is the core Python Spatial Analysis Library. It provides five foundational modules used by many upstream packages in the PySAL ecosystem:

    • libpysal.cg: Computational geometry.
    • libpysal.examples: Built-in example datasets for testing and demonstration.
    • libpysal.graph: Provides a Graph class used to encode spatial weights matrices.
    • libpysal.io: Handles input and output operations.
    • libpysal.weights: Contains spatial weights (noted as legacy).
  2. Datasets used for spatial weights testing in libpysal

    main

    The wmat example directory contains various datasets used to test spatial weights loading and processing capabilities in libpysal. These datasets cover multiple file formats including GeoBUGS, ArcGIS SWM, GAL, MATLAB MAT, Lotus Wk1, and Matrix Market MTX.

    ### Supported spatial weights formats in this example:
    * GeoBUGS text format (`geobugs_scot`, `spdep_listw2WB_columbus`)
    * ArcGIS SWM format (`ohio.swm`)
    * GAL format (`rook31.gal`)
    * MATLAB MAT format (`spat-sym-us.mat`)
    * Lotus Wk1 format (`spat-sym-us.wk1`)
    * DAT format (`wmat.dat`)
    * Matrix Market MTX format (`wmat.mtx`)
    * Stata formats (`stata_full.txt`, `stata_sparse.txt`)
  3. Use legacy Spatial Weights (W)

    main

    The libpysal.weights module contains the legacy implementation of spatial weights matrices, centered around the libpysal.weights.W class. This module is categorized into several functional groups:

    Distance Weights

    Used for weights based on distance metrics:

    • DistanceBand, Kernel, KNN, Gabriel, Delaunay, Relative_Neighborhood

    Contiguity Weights

    Used for weights based on shared boundaries:

    • Queen, Rook, Voronoi

    spint Weights

    Specialized weight types:

    • WSP, netW, mat2L, ODW, vecW

    Set Theoretic Weights

    Operations to combine or manipulate weights sets:

    • w_union, w_intersection, w_difference, w_symmetric_difference, w_subset, w_clip

    Spatial Lag

    Functions to calculate spatial lags:

    • lag_spatial, lag_categorical
  4. Understand the structure of a Point Shapefile

    main

    A Point Shapefile in this context consists of several standard components required for spatial data representation and attribute management:

    • Point.shp: The main point shapefile containing the geometry (n=9).
    • Point.dbf: The attribute data associated with the points (k=3).
    • Point.prj: The ESRI projection file defining the coordinate system.
    • Point.shx: The spatial index for the shapefile.

    This specific set of files is provided in the examples directory for testing purposes.

  5. Use Spatial Graphs for modern spatial weights encoding

    main

    The libpysal.graph module provides a modern implementation for encoding spatial weights matrices as spatial graphs. This is the recommended approach for new implementations. Key classes and functions include:

    • libpysal.graph.Graph: The primary class for representing spatial graphs.
    • libpysal.graph.GraphSummary: Provides summary statistics for a graph.
    • libpysal.graph.read_parquet, read_gal, read_gwt: Functions to load graph data from various formats.

    For users migrating from the legacy libpysal.weights.W class, refer to the migration guide in the official documentation.

  6. Understand the Line Shapefile structure used in examples

    main

    The Line example in libpysal utilizes a standard ESRI Shapefile format consisting of four specific files. This structure is used for testing spatial operations involving line geometries:

    • Line.shp: The main line shapefile containing the geometry (n=4 features).
    • Line.dbf: The dBASE table containing attribute data (k=3 fields).
    • Line.prj: The projection file defining the spatial coordinate system.
    • Line.shx: The index file used for spatial indexing.
  7. Perform Computational Geometry (cg) operations

    main

    The libpysal.cg module provides a suite of computational geometry tools. Key functional areas include:

    Alpha Shapes

    • alpha_shape, alpha_shape_auto: Generate alpha shapes from point sets.

    Sphere and Earth Geometry

    Tools for spherical calculations and coordinate transformations:

    • arcdist, arcdist2linear, linear2arcdist: Distance conversions.
    • toLngLat, toXYZ, lonlat: Coordinate transformations.
    • RADIUS_EARTH_KM, RADIUS_EARTH_MILES: Earth radius constants.
    • geointerpolate, geogrid: Spatial interpolation and grid generation.

    Geometric Shapes

    Primitive shape definitions:

    • Point, LineSegment, Line, Ray, Chain, Polygon, Rectangle, asShape

    Geometric Predicates and Intersections

    • get_bounding_box, convex_hull, is_clockwise, is_collinear
    • get_segments_intersect, get_segment_point_intersect, get_polygon_point_intersect, get_rectangle_point_intersect, get_ray_segment_intersect, get_rectangle_rectangle_intersection
    • get_polygon_point_dist, get_points_dist, get_segment_point_dist, get_point_at_angle_and_dist
    • Grid, PointLocator, PolygonLocator
    • KDTree: For efficient spatial searching.
  8. Install the released version of libpysal via pip

    main

    You can install libpysal from PyPI using pip.

    To upgrade an existing installation to the latest released version, use the -U flag. Alternatively, if you have downloaded and decompressed the source distribution (.tar.gz), navigate to the folder in your terminal and install it locally using pip install ..

    pip install -U libpysal
    
    # Or from a decompressed source directory:
    pip install .