lwgeom 0.2.1

Rust bindings for the PostGIS LWGEOM geometry library.
Documentation
mod error;
mod foreign_type;
mod gbox;
mod lwcollection;
mod lwgeom;
mod lwgeom_parser_result;
mod lwpoly;
#[cfg(feature = "mvt")]
mod mvt;

pub use error::{LWGeomError, Result};
pub use gbox::GBox;
pub use lwgeom::{LWGeom, LWGeomRef};

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_split() {
        let result = LWGeom::from_ewkb(
            LWGeom::from_text(
                "MULTILINESTRING((10 10,190 190), (15 15,30 30,100 90))",
                None,
            )
            .unwrap()
            .split(&LWGeom::from_text("POINT(30 30)", None).unwrap())
            .as_ewkb()
            .unwrap()
            .as_slice(),
        )
        .unwrap()
        .as_text(None)
        .unwrap();
        assert_eq!(
            result,
            "GEOMETRYCOLLECTION(LINESTRING(10 10,30 30),LINESTRING(30 30,190 190),LINESTRING(15 15,30 30),LINESTRING(30 30,100 90))"
        );
    }

    #[test]
    fn test_box2d() {
        let result = LWGeom::from_text("LINESTRING(1 2, 3 4, 5 6)", None)
            .unwrap()
            .calculate_bbox()
            .to_string();
        assert_eq!(result, "GBOX((1,2),(5,6))");
    }

    #[test]
    fn test_from_ewkb() {
        let result = LWGeom::from_ewkb(
            &LWGeom::from_text("LINESTRING(1 2, 3 4, 5 6)", None)
                .unwrap()
                .as_ewkb()
                .unwrap(),
        )
        .unwrap()
        .as_ewkt(None)
        .unwrap();
        assert_eq!(result, "LINESTRING(1 2,3 4,5 6)");
    }

    #[test]
    fn test_tile_envelope() {
        let result = LWGeom::from_ewkb(
            LWGeom::tile_envelope(2, 1, 1, None, None)
                .unwrap()
                .as_ewkb()
                .unwrap()
                .as_slice(),
        )
        .unwrap()
        .as_text(None)
        .unwrap();
        assert_eq!(
            result,
            "POLYGON((-10018754.1713945 0,-10018754.1713945 10018754.1713945,0 10018754.1713945,0 0,-10018754.1713945 0))"
        );
    }

    #[cfg(feature = "mvt")]
    #[test]
    fn test_into_mvt_geom() {
        let result = LWGeom::from_ewkb(
            LWGeom::from_ewkt("POLYGON((0 0,10 0,10 5,0 -5,0 0))")
                .unwrap()
                .into_mvt_geom(
                    &GBox::make_box((0.0, 0.0), (4096.0, 4096.0)),
                    4096,
                    0,
                    false,
                )
                .unwrap()
                .as_ewkb()
                .unwrap()
                .as_slice(),
        )
        .unwrap()
        .as_text(None)
        .unwrap();
        assert_eq!(
            result,
            "MULTIPOLYGON(((5 4096,10 4091,10 4096,5 4096)),((5 4096,0 4101,0 4096,5 4096)))"
        );
    }

    #[test]
    fn test_n_points() {
        let result = LWGeom::from_text("POLYGON ((114.22071 34.919066, 114.220522 34.906801, 114.211085 34.860877, 114.199824 34.832729, 114.185354 34.825628, 114.162517 34.828883, 114.133703 34.825628, 114.122504 34.81833, 114.134143 34.802154, 114.161762 34.781238, 114.172331 34.768311, 114.158364 34.759379, 114.138484 34.73475, 114.141189 34.7191, 114.158113 34.704336, 114.157547 34.686507, 114.160441 34.665611, 114.150375 34.656669, 114.157547 34.625979, 114.15157 34.618268, 114.15459 34.60576, 114.145153 34.604277, 114.14553 34.61456, 114.127726 34.61718, 114.125461 34.607886, 114.136848 34.605711, 114.133451 34.591025, 114.127852 34.584943, 114.129802 34.575348, 114.118478 34.570155, 114.107217 34.557887, 114.100296 34.535178, 114.093942 34.50182, 114.094383 34.492265, 114.083373 34.480927, 114.07035 34.481472, 114.062486 34.490532, 114.035119 34.503998, 114.0226 34.488552, 114.009577 34.480135, 114.008256 34.470082, 114.01851 34.46721, 114.021719 34.458989, 114.019894 34.445368, 114.006557 34.439225, 113.990326 34.435707, 113.968495 34.439324, 113.948615 34.435063, 113.948929 34.452699, 113.938108 34.454729, 113.929993 34.443485, 113.915586 34.444922, 113.912126 34.452352, 113.903507 34.454185, 113.891616 34.437342, 113.878153 34.411478, 113.878531 34.394577, 113.885199 34.370485, 113.879978 34.356056, 113.888093 34.348369, 113.885451 34.327785, 113.880481 34.32972, 113.85022 34.32719, 113.848584 34.322725, 113.832353 34.32342, 113.803791 34.33721, 113.791208 34.331803, 113.793159 34.328083, 113.777242 34.314589, 113.756732 34.313795, 113.736852 34.317863, 113.727227 34.305757, 113.713386 34.301391, 113.71326 34.29092, 113.7003 34.289381, 113.70332 34.276725, 113.679728 34.282532, 113.669851 34.275385, 113.658589 34.275037, 113.643994 34.288488, 113.634494 34.290026, 113.624805 34.303375, 113.608951 34.3132, 113.600836 34.313299, 113.579697 34.319897, 113.566737 34.318359, 113.556608 34.311513, 113.529619 34.317913, 113.516848 34.328579, 113.518169 34.335771, 113.510871 34.342715, 113.498729 34.338598, 113.482875 34.342864, 113.471425 34.330811, 113.459409 34.334432, 113.429085 34.350303, 113.414175 34.349361, 113.406751 34.355213, 113.414867 34.374947, 113.401655 34.377525, 113.389828 34.375988, 113.366991 34.379805, 113.370388 34.389918, 113.353968 34.396411, 113.348809 34.404738, 113.32528 34.404986, 113.315969 34.391157, 113.320436 34.383375, 113.314019 34.373856, 113.314648 34.352287, 113.276586 34.351444, 113.276334 34.342814, 113.267904 34.337805, 113.235567 34.339045, 113.224557 34.33473, 113.223362 34.325057, 113.210276 34.312009, 113.196436 34.312257, 113.18379 34.295634, 113.184356 34.290225, 113.175297 34.281639, 113.15202 34.266351, 113.142583 34.272804, 113.109365 34.263471, 113.097726 34.26223, 113.082439 34.26645, 113.075518 34.276527, 113.059476 34.280944, 113.037142 34.280299, 113.027642 34.289282, 113.020847 34.290424, 113.013235 34.300398, 112.992977 34.296478, 112.960829 34.297867, 112.954097 34.302135, 112.936293 34.295535, 112.91245 34.293749, 112.884516 34.30144, 112.87357 34.298016, 112.856458 34.302185, 112.84117 34.297818, 112.842114 34.311612, 112.854633 34.316325, 112.83318 34.333638, 112.824184 34.33344, 112.81223 34.340037, 112.788953 34.331158, 112.787002 34.343261, 112.776622 34.345046, 112.760076 34.346584, 112.756805 34.357394, 112.732898 34.350898, 112.728117 34.361163, 112.730318 34.377426, 112.721825 34.380152, 112.72409 34.388877, 112.733653 34.39309, 112.735918 34.404242, 112.729626 34.416978, 112.736673 34.422578, 112.741894 34.433973, 112.744285 34.4683, 112.740384 34.491077, 112.767374 34.49479, 112.774609 34.500137, 112.798012 34.487166, 112.806002 34.479243, 112.838024 34.477164, 112.856017 34.478402, 112.862371 34.473648, 112.896155 34.475827, 112.905215 34.468201, 112.922956 34.47657, 112.920125 34.482016, 112.927297 34.499691, 112.925598 34.509789, 112.929813 34.526023, 112.947995 34.531467, 112.976683 34.53117, 112.984736 34.539038, 112.967749 34.541264, 112.952273 34.547647, 112.929562 34.547548, 112.910751 34.545173, 112.890682 34.547102, 112.87898 34.560015, 112.877722 34.567731, 112.864133 34.580146, 112.862183 34.590284, 112.846329 34.58885, 112.839408 34.596514, 112.845574 34.609913, 112.840226 34.623161, 112.82475 34.625188, 112.825001 34.628698, 112.841799 34.628648, 112.847147 34.640362, 112.831041 34.656669, 112.835885 34.667785, 112.829217 34.683346, 112.830223 34.694163, 112.8496 34.699694, 112.867782 34.708089, 112.879735 34.705077, 112.893827 34.694953, 112.901692 34.693521, 112.91635 34.710953, 112.931764 34.711694, 112.939502 34.72384, 112.909933 34.737465, 112.902258 34.753161, 112.908486 34.757405, 112.909367 34.771123, 112.89993 34.780596, 112.887914 34.782718, 112.875017 34.779659, 112.846329 34.780054, 112.837144 34.782915, 112.817263 34.778278, 112.810595 34.784938, 112.80984 34.7951, 112.814118 34.811475, 112.827266 34.819218, 112.838653 34.812116, 112.853501 34.810736, 112.866461 34.829524, 112.873192 34.832827, 112.879106 34.849688, 112.884516 34.853089, 112.902321 34.852301, 112.914148 34.84747, 112.938181 34.831595, 112.943025 34.831102, 112.976117 34.847765, 112.989077 34.856343, 112.992851 34.863539, 113.004364 34.864081, 113.025063 34.854913, 113.039595 34.841948, 113.06023 34.83756, 113.107666 34.844709, 113.119431 34.853977, 113.14793 34.856096, 113.139815 34.884631, 113.179512 34.893697, 113.197631 34.900299, 113.227703 34.908427, 113.236951 34.925174, 113.239342 34.939504, 113.243934 34.946201, 113.260543 34.953586, 113.28986 34.952601, 113.315277 34.942212, 113.333018 34.944871, 113.343462 34.950533, 113.362587 34.970323, 113.37561 34.98002, 113.407821 34.989518, 113.427198 34.983563, 113.429463 34.964761, 113.449658 34.960232, 113.477339 34.957278, 113.495835 34.959937, 113.515086 34.965992, 113.543271 34.96801, 113.552582 34.965155, 113.57479 34.950583, 113.592091 34.933743, 113.631348 34.929311, 113.650348 34.928769, 113.665069 34.918525, 113.670731 34.910299, 113.684824 34.906358, 113.716846 34.913402, 113.725528 34.917392, 113.749057 34.919017, 113.766106 34.91488, 113.777116 34.905028, 113.78649 34.890445, 113.800456 34.87995, 113.826062 34.877683, 113.870163 34.885666, 113.905331 34.901333, 113.953585 34.898082, 113.973654 34.910447, 113.985293 34.928276, 114.018385 34.958608, 114.028451 34.959346, 114.051225 34.944527, 114.080542 34.944527, 114.101869 34.954768, 114.120491 34.956638, 114.160566 34.933496, 114.172834 34.931871, 114.200704 34.939504, 114.208505 34.926602, 114.22071 34.919066))", None)
            .unwrap()
            .n_points();
        assert_eq!(result, 297);
    }
}