Skip to content

Commit

Permalink
feat(tile): resolve clippy & update crates
Browse files Browse the repository at this point in the history
  • Loading branch information
slavik-pastushenko committed Jul 22, 2024
1 parent ecca724 commit 8142715
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ repository = "https://github.com/chargetrip/supercluster-rust"

[dependencies]
geojson = "0.24.1"
serde_json = "1.0.113"
serde_json = "1.0.120"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

A very fast Rust crate for geospatial point clustering.

This crate is deeply inspired by Mapbox's supercluster [JS package](https://www.npmjs.com/package/supercluster) and [blog post](https://www.mapbox.com/blog/supercluster/).
This crate is inspired by Mapbox's supercluster [JS package](https://www.npmjs.com/package/supercluster) and [blog post](https://www.mapbox.com/blog/supercluster/).

## Reference implementation

Expand Down
3 changes: 3 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[toolchain]
channel = "1.79.0"
components = ["clippy", "rustfmt"]
18 changes: 6 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod kdbush;
use geojson::{feature::Id, Feature, FeatureCollection, Geometry, JsonObject, Value::Point};
use kdbush::KDBush;
use serde_json::json;
use std::f64::{consts::PI, INFINITY};
use std::f64::consts::PI;

/// An offset index used to access the zoom level value associated with a cluster in the data arrays.
const OFFSET_ZOOM: usize = 2;
Expand Down Expand Up @@ -123,7 +123,7 @@ impl Supercluster {
data.push(lat_y(coordinates[1]));

// The last zoom the point was processed at
data.push(INFINITY);
data.push(f64::INFINITY);

// Index of the source feature in the original input array
data.push(i as f64);
Expand Down Expand Up @@ -162,13 +162,13 @@ impl Supercluster {
/// A vector of GeoJSON features representing the clusters within the specified bounding box and zoom level.
pub fn get_clusters(&self, bbox: [f64; 4], zoom: u8) -> Vec<Feature> {
let mut min_lng = ((((bbox[0] + 180.0) % 360.0) + 360.0) % 360.0) - 180.0;
let min_lat = f64::max(-90.0, f64::min(90.0, bbox[1]));
let min_lat = bbox[1].clamp(-90.0, 90.0);
let mut max_lng = if bbox[2] == 180.0 {
180.0
} else {
((((bbox[2] + 180.0) % 360.0) + 360.0) % 360.0) - 180.0
};
let max_lat = f64::max(-90.0, f64::min(90.0, bbox[3]));
let max_lat = bbox[3].clamp(-90.0, 90.0);

if bbox[2] - bbox[0] >= 360.0 {
min_lng = -180.0;
Expand Down Expand Up @@ -610,7 +610,7 @@ impl Supercluster {

next_data.push(wx / num_points);
next_data.push(wy / num_points);
next_data.push(INFINITY);
next_data.push(f64::INFINITY);
next_data.push(id as f64);
next_data.push(-1.0);
next_data.push(num_points);
Expand Down Expand Up @@ -755,13 +755,7 @@ fn lat_y(lat: f64) -> f64 {
let sin = lat.to_radians().sin();
let y = 0.5 - (0.25 * ((1.0 + sin) / (1.0 - sin)).ln()) / PI;

if y < 0.0 {
0.0
} else if y > 1.0 {
1.0
} else {
y
}
y.clamp(0.0, 1.0)
}

/// Convert spherical mercator to longitude.
Expand Down

0 comments on commit 8142715

Please sign in to comment.