Skip to content

Commit

Permalink
Position as opaque struct
Browse files Browse the repository at this point in the history
tiny_vec becomes private implementation detail in case we want to change
it further in the future.

It's admittedly more verbose with this commit. We could clean it up with
a pos! macro, or add some helper methods like Value::pt2d([1, 2]).
  • Loading branch information
michaelkirk committed Jul 13, 2023
1 parent f0b72a2 commit 9778880
Show file tree
Hide file tree
Showing 13 changed files with 183 additions and 140 deletions.
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(tiny_vec![-120.66029,35.2812])
Value::Point(Position::from([-120.66029,35.2812]))
);

let mut properties = JsonObject::new();
Expand Down
1 change: 0 additions & 1 deletion example-output-countries.geojson

This file was deleted.

5 changes: 1 addition & 4 deletions src/conversion/from_geo_types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use geo_types::{self, CoordFloat};

use tinyvec::tiny_vec;

use crate::{geometry, Feature, FeatureCollection};

use crate::{LineStringType, PointType, PolygonType};
Expand Down Expand Up @@ -187,8 +185,7 @@ where
{
let x: f64 = point.x().to_f64().unwrap();
let y: f64 = point.y().to_f64().unwrap();

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

fn create_line_string_type<T>(line_string: &geo_types::LineString<T>) -> LineStringType
Expand Down
75 changes: 37 additions & 38 deletions src/conversion/to_geo_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,16 +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 tinyvec::tiny_vec;

use std::convert::TryInto;

#[test]
fn geojson_point_conversion_test() {
let coords = tiny_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 @@ -379,8 +378,8 @@ mod tests {

#[test]
fn geojson_multi_point_conversion_test() {
let coord1 = tiny_vec![100.0, 0.2];
let coord2 = tiny_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 @@ -392,8 +391,8 @@ mod tests {

#[test]
fn geojson_line_string_conversion_test() {
let coord1 = tiny_vec![100.0, 0.2];
let coord2 = tiny_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 @@ -405,9 +404,9 @@ mod tests {

#[test]
fn geojson_multi_line_string_conversion_test() {
let coord1 = tiny_vec![100.0, 0.2];
let coord2 = tiny_vec![101.0, 1.0];
let coord3 = tiny_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 @@ -430,12 +429,12 @@ mod tests {

#[test]
fn geojson_polygon_conversion_test() {
let coord1 = tiny_vec![100.0, 0.0];
let coord2 = tiny_vec![101.0, 1.0];
let coord3 = tiny_vec![101.0, 1.0];
let coord4 = tiny_vec![104.0, 0.2];
let coord5 = tiny_vec![100.9, 0.2];
let coord6 = tiny_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 @@ -485,9 +484,9 @@ mod tests {

#[test]
fn geojson_polygon_without_interiors_conversion_test() {
let coord1 = tiny_vec![100.0, 0.0];
let coord2 = tiny_vec![101.0, 1.0];
let coord3 = tiny_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 @@ -513,12 +512,12 @@ mod tests {

#[test]
fn geojson_multi_polygon_conversion_test() {
let coord1 = tiny_vec![100.0, 0.0];
let coord2 = tiny_vec![101.0, 1.0];
let coord3 = tiny_vec![101.0, 1.0];
let coord4 = tiny_vec![104.0, 0.2];
let coord5 = tiny_vec![100.9, 0.2];
let coord6 = tiny_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 @@ -563,11 +562,11 @@ mod tests {

#[test]
fn geojson_geometry_collection_conversion_test() {
let coord1 = tiny_vec![100.0, 0.0];
let coord2 = tiny_vec![100.0, 1.0];
let coord3 = tiny_vec![101.0, 1.0];
let coord4 = tiny_vec![102.0, 0.0];
let coord5 = tiny_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 @@ -603,7 +602,7 @@ mod tests {

#[test]
fn geojson_geometry_conversion() {
let coords = tiny_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 @@ -616,8 +615,8 @@ mod tests {

#[test]
fn geojson_mismatch_geometry_conversion_test() {
let coord1 = tiny_vec![100.0, 0.2];
let coord2 = tiny_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 @@ -679,10 +678,10 @@ mod tests {

#[test]
fn borrowed_value_conversions_test() -> crate::Result<()> {
let coord1 = tiny_vec![100.0, 0.2];
let coord2 = tiny_vec![101.0, 1.0];
let coord3 = tiny_vec![102.0, 0.8];
let coord4 = tiny_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
12 changes: 5 additions & 7 deletions src/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,10 @@ 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;
use tinyvec::tiny_vec;

fn feature_json_str() -> &'static str {
"{\"geometry\":{\"coordinates\":[1.1,2.1],\"type\":\"Point\"},\"properties\":{},\"type\":\
Expand All @@ -253,7 +251,7 @@ mod tests {
}

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

fn geometry() -> Geometry {
Expand Down Expand Up @@ -348,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(tiny_vec![1.1, 2.1]),
value: Value::Point(Position::from([1.1, 2.1])),
bbox: None,
foreign_members: None,
}),
Expand All @@ -374,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(tiny_vec![1.1, 2.1]),
value: Value::Point(Position::from([1.1, 2.1])),
bbox: None,
foreign_members: None,
}),
Expand Down Expand Up @@ -425,7 +423,7 @@ mod tests {
);
let feature = crate::Feature {
geometry: Some(Geometry {
value: Value::Point(tiny_vec![1.1, 2.1]),
value: Value::Point(Position::from([1.1, 2.1])),
bbox: None,
foreign_members: None,
}),
Expand Down
18 changes: 9 additions & 9 deletions src/feature_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ use serde_json::json;
/// Collect from an iterator:
///
/// ```rust
/// use geojson::{Feature, FeatureCollection, Value};
/// use tinyvec::tiny_vec;
/// use geojson::{Feature, FeatureCollection, Position, Value};
///
/// let fc: FeatureCollection = (0..10)
/// .map(|idx| -> Feature {
/// let c = idx as f64;
/// Value::Point(tiny_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 @@ -254,23 +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;
use tinyvec::tiny_vec;
#[test]
fn test_fc_from_iterator() {
let features: Vec<Feature> = vec![
{
let mut feat: Feature = Value::Point(tiny_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![tiny_vec![10., 10., 10.], tiny_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
23 changes: 11 additions & 12 deletions src/feature_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,9 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::{Geometry, Value};
use crate::{Geometry, Position, Value};

use std::io::BufReader;
use tinyvec::tiny_vec;

fn fc() -> &'static str {
r#"
Expand Down Expand Up @@ -188,7 +187,7 @@ mod tests {
assert_eq!(
Geometry {
bbox: None,
value: Value::Point(tiny_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 @@ -197,10 +196,10 @@ mod tests {
Geometry {
bbox: None,
value: Value::LineString(vec![
tiny_vec![102.0, 0.0],
tiny_vec![103.0, 1.0],
tiny_vec![104.0, 0.0],
tiny_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 @@ -210,11 +209,11 @@ mod tests {
Geometry {
bbox: None,
value: Value::Polygon(vec![vec![
tiny_vec![100.0, 0.0],
tiny_vec![101.0, 0.0],
tiny_vec![101.0, 1.0],
tiny_vec![100.0, 1.0],
tiny_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
18 changes: 9 additions & 9 deletions src/feature_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ 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;
Expand Down Expand Up @@ -285,9 +285,9 @@ mod tests {

Feature {
bbox: None,
geometry: Some(crate::Geometry::from(crate::Value::Point(tiny_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 @@ -301,9 +301,9 @@ mod tests {

Feature {
bbox: None,
geometry: Some(crate::Geometry::from(crate::Value::Point(tiny_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 @@ -345,12 +345,12 @@ mod tests {
{
let mut writer = FeatureWriter::from_writer(&mut buffer);
let record_1 = MyRecord {
geometry: crate::Geometry::from(crate::Value::Point(tiny_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(tiny_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

0 comments on commit 9778880

Please sign in to comment.