Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stack allocate 2d points using tiny_vec. #222

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ serde_json = "~1.0"
geo-types = { version = "0.7", features = ["serde"], optional = true }
thiserror = "1.0.20"
log = "0.4.17"
tinyvec = { version = "1.6.0", features = ["serde", "alloc"] }

[dev-dependencies]
num-traits = "0.2"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ let geojson = geojson_str.parse::<GeoJson>().unwrap();
use geojson::{Feature, GeoJson, Geometry, Value, JsonObject, JsonValue};

let geometry = Geometry::new(
Value::Point(vec![-120.66029,35.2812])
Value::Point(Position::from([-120.66029,35.2812]))
);

let mut properties = JsonObject::new();
Expand Down
3 changes: 1 addition & 2 deletions src/conversion/from_geo_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ where
{
let x: f64 = point.x().to_f64().unwrap();
let y: f64 = point.y().to_f64().unwrap();

vec![x, y]
crate::Position::from([x, y])
}

fn create_line_string_type<T>(line_string: &geo_types::LineString<T>) -> LineStringType
Expand Down
74 changes: 37 additions & 37 deletions src/conversion/to_geo_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,15 +361,15 @@ fn mismatch_geom_err(expected_type: &'static str, found: &geometry::Value) -> Er

#[cfg(test)]
mod tests {
use crate::{Geometry, Value};
use crate::{Geometry, Position, Value};
use serde_json::json;

use std::convert::TryInto;

#[test]
fn geojson_point_conversion_test() {
let coords = vec![100.0, 0.2];
let geojson_point = Value::Point(coords.clone());
let coords = [100.0, 0.2];
let geojson_point = Value::Point(Position(coords.clone().into()));
let geo_point: geo_types::Point<f64> = geojson_point.try_into().unwrap();

assert_almost_eq!(geo_point.x(), coords[0], 1e-6);
Expand All @@ -378,8 +378,8 @@ mod tests {

#[test]
fn geojson_multi_point_conversion_test() {
let coord1 = vec![100.0, 0.2];
let coord2 = vec![101.0, 1.0];
let coord1 = Position([100.0, 0.2].into());
let coord2 = Position([101.0, 1.0].into());
let geojson_multi_point = Value::MultiPoint(vec![coord1.clone(), coord2.clone()]);
let geo_multi_point: geo_types::MultiPoint<f64> = geojson_multi_point.try_into().unwrap();

Expand All @@ -391,8 +391,8 @@ mod tests {

#[test]
fn geojson_line_string_conversion_test() {
let coord1 = vec![100.0, 0.2];
let coord2 = vec![101.0, 1.0];
let coord1 = Position::from([100.0, 0.2]);
let coord2 = Position::from([101.0, 1.0]);
let geojson_line_string = Value::LineString(vec![coord1.clone(), coord2.clone()]);
let geo_line_string: geo_types::LineString<f64> = geojson_line_string.try_into().unwrap();

Expand All @@ -404,9 +404,9 @@ mod tests {

#[test]
fn geojson_multi_line_string_conversion_test() {
let coord1 = vec![100.0, 0.2];
let coord2 = vec![101.0, 1.0];
let coord3 = vec![102.0, 0.8];
let coord1 = Position::from([100.0, 0.2]);
let coord2 = Position::from([101.0, 1.0]);
let coord3 = Position::from([102.0, 0.8]);
let geojson_multi_line_string = Value::MultiLineString(vec![
vec![coord1.clone(), coord2.clone()],
vec![coord2.clone(), coord3.clone()],
Expand All @@ -429,12 +429,12 @@ mod tests {

#[test]
fn geojson_polygon_conversion_test() {
let coord1 = vec![100.0, 0.0];
let coord2 = vec![101.0, 1.0];
let coord3 = vec![101.0, 1.0];
let coord4 = vec![104.0, 0.2];
let coord5 = vec![100.9, 0.2];
let coord6 = vec![100.9, 0.7];
let coord1 = Position::from([100.0, 0.0]);
let coord2 = Position::from([101.0, 1.0]);
let coord3 = Position::from([101.0, 1.0]);
let coord4 = Position::from([104.0, 0.2]);
let coord5 = Position::from([100.9, 0.2]);
let coord6 = Position::from([100.9, 0.7]);

let geojson_multi_line_string_type1 = vec![
vec![
Expand Down Expand Up @@ -484,9 +484,9 @@ mod tests {

#[test]
fn geojson_polygon_without_interiors_conversion_test() {
let coord1 = vec![100.0, 0.0];
let coord2 = vec![101.0, 1.0];
let coord3 = vec![101.0, 1.0];
let coord1 = Position::from([100.0, 0.0]);
let coord2 = Position::from([101.0, 1.0]);
let coord3 = Position::from([101.0, 1.0]);

let geojson_multi_line_string_type1 = vec![vec![
coord1.clone(),
Expand All @@ -512,12 +512,12 @@ mod tests {

#[test]
fn geojson_multi_polygon_conversion_test() {
let coord1 = vec![100.0, 0.0];
let coord2 = vec![101.0, 1.0];
let coord3 = vec![101.0, 1.0];
let coord4 = vec![104.0, 0.2];
let coord5 = vec![100.9, 0.2];
let coord6 = vec![100.9, 0.7];
let coord1 = Position::from([100.0, 0.0]);
let coord2 = Position::from([101.0, 1.0]);
let coord3 = Position::from([101.0, 1.0]);
let coord4 = Position::from([104.0, 0.2]);
let coord5 = Position::from([100.9, 0.2]);
let coord6 = Position::from([100.9, 0.7]);

let geojson_line_string_type1 = vec![
coord1.clone(),
Expand Down Expand Up @@ -562,11 +562,11 @@ mod tests {

#[test]
fn geojson_geometry_collection_conversion_test() {
let coord1 = vec![100.0, 0.0];
let coord2 = vec![100.0, 1.0];
let coord3 = vec![101.0, 1.0];
let coord4 = vec![102.0, 0.0];
let coord5 = vec![101.0, 0.0];
let coord1 = Position::from([100.0, 0.0]);
let coord2 = Position::from([100.0, 1.0]);
let coord3 = Position::from([101.0, 1.0]);
let coord4 = Position::from([102.0, 0.0]);
let coord5 = Position::from([101.0, 0.0]);

let geojson_multi_point = Value::MultiPoint(vec![coord1.clone(), coord2.clone()]);
let geojson_multi_line_string = Value::MultiLineString(vec![
Expand Down Expand Up @@ -602,7 +602,7 @@ mod tests {

#[test]
fn geojson_geometry_conversion() {
let coords = vec![100.0, 0.2];
let coords = Position::from([100.0, 0.2]);
let geojson_geometry = Geometry::from(Value::Point(coords.clone()));
let geo_geometry: geo_types::Geometry<f64> = geojson_geometry
.try_into()
Expand All @@ -615,8 +615,8 @@ mod tests {

#[test]
fn geojson_mismatch_geometry_conversion_test() {
let coord1 = vec![100.0, 0.2];
let coord2 = vec![101.0, 1.0];
let coord1 = Position::from([100.0, 0.2]);
let coord2 = Position::from([101.0, 1.0]);
let geojson_line_string = Value::LineString(vec![coord1.clone(), coord2.clone()]);
use std::convert::TryFrom;
let error = geo_types::Point::<f64>::try_from(geojson_line_string).unwrap_err();
Expand Down Expand Up @@ -678,10 +678,10 @@ mod tests {

#[test]
fn borrowed_value_conversions_test() -> crate::Result<()> {
let coord1 = vec![100.0, 0.2];
let coord2 = vec![101.0, 1.0];
let coord3 = vec![102.0, 0.8];
let coord4 = vec![104.0, 0.2];
let coord1 = Position::from([100.0, 0.2]);
let coord2 = Position::from([101.0, 1.0]);
let coord3 = Position::from([102.0, 0.8]);
let coord4 = Position::from([104.0, 0.2]);

let geojson_point = Value::Point(coord1.clone());
let _: geo_types::Point<f64> = (&geojson_point).try_into()?;
Expand Down
11 changes: 5 additions & 6 deletions src/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@ impl Serialize for Id {

#[cfg(test)]
mod tests {
use crate::JsonObject;
use crate::{feature, Error, Feature, GeoJson, Geometry, Value};
use crate::{feature, Error, Feature, GeoJson, Geometry, JsonObject, Position, Value};
use serde_json::json;

use std::str::FromStr;
Expand Down Expand Up @@ -252,7 +251,7 @@ mod tests {
}

fn value() -> Value {
Value::Point(vec![1.1, 2.1])
Value::Point(Position::from([1.1, 2.1]))
}

fn geometry() -> Geometry {
Expand Down Expand Up @@ -347,7 +346,7 @@ mod tests {
let feature_json_str = "{\"geometry\":{\"coordinates\":[1.1,2.1],\"type\":\"Point\"},\"id\":0,\"properties\":{},\"type\":\"Feature\"}";
let feature = crate::Feature {
geometry: Some(Geometry {
value: Value::Point(vec![1.1, 2.1]),
value: Value::Point(Position::from([1.1, 2.1])),
bbox: None,
foreign_members: None,
}),
Expand All @@ -373,7 +372,7 @@ mod tests {
let feature_json_str = "{\"geometry\":{\"coordinates\":[1.1,2.1],\"type\":\"Point\"},\"id\":\"foo\",\"properties\":{},\"type\":\"Feature\"}";
let feature = crate::Feature {
geometry: Some(Geometry {
value: Value::Point(vec![1.1, 2.1]),
value: Value::Point(Position::from([1.1, 2.1])),
bbox: None,
foreign_members: None,
}),
Expand Down Expand Up @@ -424,7 +423,7 @@ mod tests {
);
let feature = crate::Feature {
geometry: Some(Geometry {
value: Value::Point(vec![1.1, 2.1]),
value: Value::Point(Position::from([1.1, 2.1])),
bbox: None,
foreign_members: None,
}),
Expand Down
16 changes: 9 additions & 7 deletions src/feature_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ use serde_json::json;
/// Collect from an iterator:
///
/// ```rust
/// use geojson::{Feature, FeatureCollection, Value};
/// use geojson::{Feature, FeatureCollection, Position, Value};
///
/// let fc: FeatureCollection = (0..10)
/// .map(|idx| -> Feature {
/// let c = idx as f64;
/// Value::Point(vec![1.0 * c, 2.0 * c, 3.0 * c]).into()
/// Value::Point(Position::from(vec![1.0 * c, 2.0 * c, 3.0 * c])).into()
/// })
/// .collect();
/// assert_eq!(fc.features.len(), 10);
Expand Down Expand Up @@ -253,22 +253,24 @@ impl FromIterator<Feature> for FeatureCollection {

#[cfg(test)]
mod tests {
use crate::{Error, Feature, FeatureCollection, Value};
use crate::{Error, Feature, FeatureCollection, Position, Value};
use serde_json::json;

use std::str::FromStr;

#[test]
fn test_fc_from_iterator() {
let features: Vec<Feature> = vec![
{
let mut feat: Feature = Value::Point(vec![0., 0., 0.]).into();
let mut feat: Feature = Value::Point(Position::from(vec![0., 0., 0.])).into();
feat.bbox = Some(vec![-1., -1., -1., 1., 1., 1.]);
feat
},
{
let mut feat: Feature =
Value::MultiPoint(vec![vec![10., 10., 10.], vec![11., 11., 11.]]).into();
let mut feat: Feature = Value::MultiPoint(vec![
Position::from(vec![10., 10., 10.]),
Position::from(vec![11., 11., 11.]),
])
.into();
feat.bbox = Some(vec![10., 10., 10., 11., 11., 11.]);
feat
},
Expand Down
22 changes: 11 additions & 11 deletions src/feature_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::{Geometry, Value};
use crate::{Geometry, Position, Value};

use std::io::BufReader;

Expand Down Expand Up @@ -187,7 +187,7 @@ mod tests {
assert_eq!(
Geometry {
bbox: None,
value: Value::Point(vec![102.0, 0.5]),
value: Value::Point(Position::from([102.0, 0.5])),
foreign_members: None,
},
fi.next().unwrap().unwrap().geometry.unwrap()
Expand All @@ -196,10 +196,10 @@ mod tests {
Geometry {
bbox: None,
value: Value::LineString(vec![
vec![102.0, 0.0],
vec![103.0, 1.0],
vec![104.0, 0.0],
vec![105.0, 1.0]
Position::from([102.0, 0.0]),
Position::from([103.0, 1.0]),
Position::from([104.0, 0.0]),
Position::from([105.0, 1.0])
]),
foreign_members: None,
},
Expand All @@ -209,11 +209,11 @@ mod tests {
Geometry {
bbox: None,
value: Value::Polygon(vec![vec![
vec![100.0, 0.0],
vec![101.0, 0.0],
vec![101.0, 1.0],
vec![100.0, 1.0],
vec![100.0, 0.0]
Position::from([100.0, 0.0]),
Position::from([101.0, 0.0]),
Position::from([101.0, 1.0]),
Position::from([100.0, 1.0]),
Position::from([100.0, 0.0])
]]),
foreign_members: None,
},
Expand Down
15 changes: 10 additions & 5 deletions src/feature_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,10 @@ impl<W: Write> Drop for FeatureWriter<W> {
#[cfg(test)]
mod tests {
use super::*;
use crate::JsonValue;
use crate::{JsonValue, Position};

use serde_json::json;
use tinyvec::tiny_vec;

// an example struct that we want to serialize
#[derive(Serialize)]
Expand Down Expand Up @@ -284,7 +285,9 @@ mod tests {

Feature {
bbox: None,
geometry: Some(crate::Geometry::from(crate::Value::Point(vec![1.1, 1.2]))),
geometry: Some(crate::Geometry::from(crate::Value::Point(Position::from(
[1.1, 1.2],
)))),
id: None,
properties: Some(props),
foreign_members: None,
Expand All @@ -298,7 +301,9 @@ mod tests {

Feature {
bbox: None,
geometry: Some(crate::Geometry::from(crate::Value::Point(vec![2.1, 2.2]))),
geometry: Some(crate::Geometry::from(crate::Value::Point(Position::from(
[2.1, 2.2],
)))),
id: None,
properties: Some(props),
foreign_members: None,
Expand Down Expand Up @@ -340,12 +345,12 @@ mod tests {
{
let mut writer = FeatureWriter::from_writer(&mut buffer);
let record_1 = MyRecord {
geometry: crate::Geometry::from(crate::Value::Point(vec![1.1, 1.2])),
geometry: crate::Geometry::from(crate::Value::Point(Position::from([1.1, 1.2]))),
name: "Mishka".to_string(),
age: 12,
};
let record_2 = MyRecord {
geometry: crate::Geometry::from(crate::Value::Point(vec![2.1, 2.2])),
geometry: crate::Geometry::from(crate::Value::Point(Position::from([2.1, 2.2]))),
name: "Jane".to_string(),
age: 22,
};
Expand Down
Loading