Skip to content

Commit

Permalink
feat(tile): add geojson
Browse files Browse the repository at this point in the history
  • Loading branch information
slavik-pastushenko committed Feb 6, 2024
1 parent 5ebe02b commit 9bc9e2d
Show file tree
Hide file tree
Showing 10 changed files with 315 additions and 292 deletions.
6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@ documentation = "https://docs.rs/supercluster"
repository = "https://github.com/chargetrip/supercluster-rust"

[dependencies]
serde = { version = "1.0.193", features = ["derive"] }

[dev-dependencies]
serde_json = "1.0.108"
geojson = "0.24.1"
serde_json = "1.0.113"
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Chargetrip
Copyright (c) 2024 Chargetrip

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ This crate is deeply inspired by Mapbox's supercluster [JS package](https://www.

## Features

- `load(points)`: Loads an array of [GeoJSON Feature](https://tools.ietf.org/html/rfc7946#section-3.2) objects. Each feature's `geometry` must be a [GeoJSON Point](https://tools.ietf.org/html/rfc7946#section-3.1.2).
- `load(points)`: Loads a [FeatureCollection](https://datatracker.ietf.org/doc/html/rfc7946#section-3.3) Object. Each feature should be a [Feature Object](https://datatracker.ietf.org/doc/html/rfc7946#section-3.2).

- `get_clusters(bbox, zoom)`: For the given `bbox` array (`[west_lng, south_lat, east_lng, north_lat]`) and `zoom`, returns an array of clusters and points as [GeoJSON Feature](https://tools.ietf.org/html/rfc7946#section-3.2) objects.
- `get_clusters(bbox, zoom)`: For the given `bbox` array (`[west_lng, south_lat, east_lng, north_lat]`) and `zoom`, returns an array of clusters and points as [Feature Object](https://datatracker.ietf.org/doc/html/rfc7946#section-3.2) objects.

- `get_tile(z, x, y)`: For a given zoom and x/y coordinates, returns a [geojson-vt](https://github.com/mapbox/geojson-vt)-compatible JSON tile object with cluster/point features.
- `get_tile(z, x, y)`: For a given zoom and x/y coordinates, returns a [FeatureCollection](https://datatracker.ietf.org/doc/html/rfc7946#section-3.3) Object.

- `get_children(cluster_id)`: Returns the children of a cluster (on the next zoom level) given its id (`cluster_id` value from feature properties).

Expand Down Expand Up @@ -58,8 +58,7 @@ cargo add supercluster
```

```rust
extern crate supercluster;

use geojson::FeatureCollection;
use supercluster::{ Supercluster, Options };

fn main() {
Expand All @@ -75,11 +74,18 @@ fn main() {
// Create a new instance with the specified configuration settings
let mut cluster = Supercluster::new(options);

// Load the input GeoJSON points into the Supercluster instance
let points = Vec::new(); // your points
let index = cluster.load(points);
// Load a FeatureCollection Object into the Supercluster instance

// [GeoJSON Format Specification § 5](https://tools.ietf.org/html/rfc7946#section-5)
let feature_collection = FeatureCollection {
bbox: None,
// [GeoJSON Format Specification § 3.2](https://datatracker.ietf.org/doc/html/rfc7946#section-3.2)
features: vec![],
foreign_members: None,
};
let index = cluster.load(feature_collection);

// Retrieve a vector of features within a tile at the given zoom level and tile coordinates
// Retrieve a FeatureCollection Object within a tile at the given zoom level and tile coordinates
let tile = index.get_tile(0, 0.0, 0.0).expect("cannot get a tile");

...
Expand All @@ -106,7 +112,7 @@ Run [clippy](https://github.com/rust-lang/rust-clippy):
cargo clippy --all-targets --all-features -- -D warnings
```

Run [lint](https://github.com/rust-lang/rustfmt):
Run [rustfmt](https://github.com/rust-lang/rustfmt):

```bash
cargo fmt
Expand Down
14 changes: 7 additions & 7 deletions src/kdbush.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/// Array of coordinates with longitude as first value and latitude as second one
/// Array of coordinates with longitude as first value and latitude as second one.
type Point = [f64; 2];

/// A very fast static spatial index for 2D points based on a flat KD-tree
/// A very fast static spatial index for 2D points based on a flat KD-tree.
#[derive(Clone, Debug)]
pub struct KDBush {
/// Node size for the KD-tree. Determines the number of points in a leaf node
/// Node size for the KD-tree. Determines the number of points in a leaf node.
pub node_size: usize,

/// A list of point IDs used to reference points in the KD-tree
/// A list of point IDs used to reference points in the KD-tree.
pub ids: Vec<usize>,

/// A flat array containing the X and Y coordinates of all points in interleaved order
/// A flat array containing the X and Y coordinates of all points in interleaved order.
pub coords: Vec<f64>,

/// A list of 2D points represented as an array of [longitude, latitude] coordinates
/// A list of 2D points represented as an array of [longitude, latitude] coordinates.
pub points: Vec<Point>,

/// A list of additional data associated with the points (e.g., properties)
/// A list of additional data associated with the points (e.g., properties).
pub data: Vec<f64>,
}

Expand Down
Loading

0 comments on commit 9bc9e2d

Please sign in to comment.