Skip to content

Commit d1e3dec

Browse files
committed
Position as opaque struct
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]).
1 parent 948188a commit d1e3dec

13 files changed

+179
-126
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ let geojson = geojson_str.parse::<GeoJson>().unwrap();
3737
use geojson::{Feature, GeoJson, Geometry, Value, JsonObject, JsonValue};
3838

3939
let geometry = Geometry::new(
40-
Value::Point(tiny_vec![-120.66029,35.2812])
40+
Value::Point(Position::from([-120.66029,35.2812]))
4141
);
4242

4343
let mut properties = JsonObject::new();

example-output-countries.geojson

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/conversion/from_geo_types.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use geo_types::{self, CoordFloat};
22

3-
use tinyvec::tiny_vec;
4-
53
use crate::{geometry, Feature, FeatureCollection};
64

75
use crate::{LineStringType, PointType, PolygonType};
@@ -187,8 +185,7 @@ where
187185
{
188186
let x: f64 = point.x().to_f64().unwrap();
189187
let y: f64 = point.y().to_f64().unwrap();
190-
191-
tiny_vec![x, y]
188+
crate::Position::from([x, y])
192189
}
193190

194191
fn create_line_string_type<T>(line_string: &geo_types::LineString<T>) -> LineStringType

src/conversion/to_geo_types.rs

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -361,16 +361,15 @@ fn mismatch_geom_err(expected_type: &'static str, found: &geometry::Value) -> Er
361361

362362
#[cfg(test)]
363363
mod tests {
364-
use crate::{Geometry, Value};
364+
use crate::{Geometry, Position, Value};
365365
use serde_json::json;
366-
use tinyvec::tiny_vec;
367366

368367
use std::convert::TryInto;
369368

370369
#[test]
371370
fn geojson_point_conversion_test() {
372-
let coords = tiny_vec![100.0, 0.2];
373-
let geojson_point = Value::Point(coords.clone());
371+
let coords = [100.0, 0.2];
372+
let geojson_point = Value::Point(Position(coords.clone().into()));
374373
let geo_point: geo_types::Point<f64> = geojson_point.try_into().unwrap();
375374

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

380379
#[test]
381380
fn geojson_multi_point_conversion_test() {
382-
let coord1 = tiny_vec![100.0, 0.2];
383-
let coord2 = tiny_vec![101.0, 1.0];
381+
let coord1 = Position([100.0, 0.2].into());
382+
let coord2 = Position([101.0, 1.0].into());
384383
let geojson_multi_point = Value::MultiPoint(vec![coord1.clone(), coord2.clone()]);
385384
let geo_multi_point: geo_types::MultiPoint<f64> = geojson_multi_point.try_into().unwrap();
386385

@@ -392,8 +391,8 @@ mod tests {
392391

393392
#[test]
394393
fn geojson_line_string_conversion_test() {
395-
let coord1 = tiny_vec![100.0, 0.2];
396-
let coord2 = tiny_vec![101.0, 1.0];
394+
let coord1 = Position::from([100.0, 0.2]);
395+
let coord2 = Position::from([101.0, 1.0]);
397396
let geojson_line_string = Value::LineString(vec![coord1.clone(), coord2.clone()]);
398397
let geo_line_string: geo_types::LineString<f64> = geojson_line_string.try_into().unwrap();
399398

@@ -405,9 +404,9 @@ mod tests {
405404

406405
#[test]
407406
fn geojson_multi_line_string_conversion_test() {
408-
let coord1 = tiny_vec![100.0, 0.2];
409-
let coord2 = tiny_vec![101.0, 1.0];
410-
let coord3 = tiny_vec![102.0, 0.8];
407+
let coord1 = Position::from([100.0, 0.2]);
408+
let coord2 = Position::from([101.0, 1.0]);
409+
let coord3 = Position::from([102.0, 0.8]);
411410
let geojson_multi_line_string = Value::MultiLineString(vec![
412411
vec![coord1.clone(), coord2.clone()],
413412
vec![coord2.clone(), coord3.clone()],
@@ -430,12 +429,12 @@ mod tests {
430429

431430
#[test]
432431
fn geojson_polygon_conversion_test() {
433-
let coord1 = tiny_vec![100.0, 0.0];
434-
let coord2 = tiny_vec![101.0, 1.0];
435-
let coord3 = tiny_vec![101.0, 1.0];
436-
let coord4 = tiny_vec![104.0, 0.2];
437-
let coord5 = tiny_vec![100.9, 0.2];
438-
let coord6 = tiny_vec![100.9, 0.7];
432+
let coord1 = Position::from([100.0, 0.0]);
433+
let coord2 = Position::from([101.0, 1.0]);
434+
let coord3 = Position::from([101.0, 1.0]);
435+
let coord4 = Position::from([104.0, 0.2]);
436+
let coord5 = Position::from([100.9, 0.2]);
437+
let coord6 = Position::from([100.9, 0.7]);
439438

440439
let geojson_multi_line_string_type1 = vec![
441440
vec![
@@ -485,9 +484,9 @@ mod tests {
485484

486485
#[test]
487486
fn geojson_polygon_without_interiors_conversion_test() {
488-
let coord1 = tiny_vec![100.0, 0.0];
489-
let coord2 = tiny_vec![101.0, 1.0];
490-
let coord3 = tiny_vec![101.0, 1.0];
487+
let coord1 = Position::from([100.0, 0.0]);
488+
let coord2 = Position::from([101.0, 1.0]);
489+
let coord3 = Position::from([101.0, 1.0]);
491490

492491
let geojson_multi_line_string_type1 = vec![vec![
493492
coord1.clone(),
@@ -513,12 +512,12 @@ mod tests {
513512

514513
#[test]
515514
fn geojson_multi_polygon_conversion_test() {
516-
let coord1 = tiny_vec![100.0, 0.0];
517-
let coord2 = tiny_vec![101.0, 1.0];
518-
let coord3 = tiny_vec![101.0, 1.0];
519-
let coord4 = tiny_vec![104.0, 0.2];
520-
let coord5 = tiny_vec![100.9, 0.2];
521-
let coord6 = tiny_vec![100.9, 0.7];
515+
let coord1 = Position::from([100.0, 0.0]);
516+
let coord2 = Position::from([101.0, 1.0]);
517+
let coord3 = Position::from([101.0, 1.0]);
518+
let coord4 = Position::from([104.0, 0.2]);
519+
let coord5 = Position::from([100.9, 0.2]);
520+
let coord6 = Position::from([100.9, 0.7]);
522521

523522
let geojson_line_string_type1 = vec![
524523
coord1.clone(),
@@ -563,11 +562,11 @@ mod tests {
563562

564563
#[test]
565564
fn geojson_geometry_collection_conversion_test() {
566-
let coord1 = tiny_vec![100.0, 0.0];
567-
let coord2 = tiny_vec![100.0, 1.0];
568-
let coord3 = tiny_vec![101.0, 1.0];
569-
let coord4 = tiny_vec![102.0, 0.0];
570-
let coord5 = tiny_vec![101.0, 0.0];
565+
let coord1 = Position::from([100.0, 0.0]);
566+
let coord2 = Position::from([100.0, 1.0]);
567+
let coord3 = Position::from([101.0, 1.0]);
568+
let coord4 = Position::from([102.0, 0.0]);
569+
let coord5 = Position::from([101.0, 0.0]);
571570

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

604603
#[test]
605604
fn geojson_geometry_conversion() {
606-
let coords = tiny_vec![100.0, 0.2];
605+
let coords = Position::from([100.0, 0.2]);
607606
let geojson_geometry = Geometry::from(Value::Point(coords.clone()));
608607
let geo_geometry: geo_types::Geometry<f64> = geojson_geometry
609608
.try_into()
@@ -616,8 +615,8 @@ mod tests {
616615

617616
#[test]
618617
fn geojson_mismatch_geometry_conversion_test() {
619-
let coord1 = tiny_vec![100.0, 0.2];
620-
let coord2 = tiny_vec![101.0, 1.0];
618+
let coord1 = Position::from([100.0, 0.2]);
619+
let coord2 = Position::from([101.0, 1.0]);
621620
let geojson_line_string = Value::LineString(vec![coord1.clone(), coord2.clone()]);
622621
use std::convert::TryFrom;
623622
let error = geo_types::Point::<f64>::try_from(geojson_line_string).unwrap_err();
@@ -679,10 +678,10 @@ mod tests {
679678

680679
#[test]
681680
fn borrowed_value_conversions_test() -> crate::Result<()> {
682-
let coord1 = tiny_vec![100.0, 0.2];
683-
let coord2 = tiny_vec![101.0, 1.0];
684-
let coord3 = tiny_vec![102.0, 0.8];
685-
let coord4 = tiny_vec![104.0, 0.2];
681+
let coord1 = Position::from([100.0, 0.2]);
682+
let coord2 = Position::from([101.0, 1.0]);
683+
let coord3 = Position::from([102.0, 0.8]);
684+
let coord4 = Position::from([104.0, 0.2]);
686685

687686
let geojson_point = Value::Point(coord1.clone());
688687
let _: geo_types::Point<f64> = (&geojson_point).try_into()?;

src/feature.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,10 @@ impl Serialize for Id {
222222

223223
#[cfg(test)]
224224
mod tests {
225-
use crate::JsonObject;
226-
use crate::{feature, Error, Feature, GeoJson, Geometry, Value};
225+
use crate::{feature, Error, Feature, GeoJson, Geometry, JsonObject, Position, Value};
227226
use serde_json::json;
228227

229228
use std::str::FromStr;
230-
use tinyvec::tiny_vec;
231229

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

255253
fn value() -> Value {
256-
Value::Point(tiny_vec![1.1, 2.1])
254+
Value::Point(Position::from([1.1, 2.1]))
257255
}
258256

259257
fn geometry() -> Geometry {
@@ -348,7 +346,7 @@ mod tests {
348346
let feature_json_str = "{\"geometry\":{\"coordinates\":[1.1,2.1],\"type\":\"Point\"},\"id\":0,\"properties\":{},\"type\":\"Feature\"}";
349347
let feature = crate::Feature {
350348
geometry: Some(Geometry {
351-
value: Value::Point(tiny_vec![1.1, 2.1]),
349+
value: Value::Point(Position::from([1.1, 2.1])),
352350
bbox: None,
353351
foreign_members: None,
354352
}),
@@ -374,7 +372,7 @@ mod tests {
374372
let feature_json_str = "{\"geometry\":{\"coordinates\":[1.1,2.1],\"type\":\"Point\"},\"id\":\"foo\",\"properties\":{},\"type\":\"Feature\"}";
375373
let feature = crate::Feature {
376374
geometry: Some(Geometry {
377-
value: Value::Point(tiny_vec![1.1, 2.1]),
375+
value: Value::Point(Position::from([1.1, 2.1])),
378376
bbox: None,
379377
foreign_members: None,
380378
}),
@@ -425,7 +423,7 @@ mod tests {
425423
);
426424
let feature = crate::Feature {
427425
geometry: Some(Geometry {
428-
value: Value::Point(tiny_vec![1.1, 2.1]),
426+
value: Value::Point(Position::from([1.1, 2.1])),
429427
bbox: None,
430428
foreign_members: None,
431429
}),

src/feature_collection.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,12 @@ use serde_json::json;
5151
/// Collect from an iterator:
5252
///
5353
/// ```rust
54-
/// use geojson::{Feature, FeatureCollection, Value};
55-
/// use tinyvec::tiny_vec;
54+
/// use geojson::{Feature, FeatureCollection, Position, Value};
5655
///
5756
/// let fc: FeatureCollection = (0..10)
5857
/// .map(|idx| -> Feature {
5958
/// let c = idx as f64;
60-
/// Value::Point(tiny_vec![1.0 * c, 2.0 * c, 3.0 * c]).into()
59+
/// Value::Point(Position::from(vec![1.0 * c, 2.0 * c, 3.0 * c])).into()
6160
/// })
6261
/// .collect();
6362
/// assert_eq!(fc.features.len(), 10);
@@ -254,22 +253,24 @@ impl FromIterator<Feature> for FeatureCollection {
254253

255254
#[cfg(test)]
256255
mod tests {
257-
use crate::{Error, Feature, FeatureCollection, Value};
256+
use crate::{Error, Feature, FeatureCollection, Position, Value};
258257
use serde_json::json;
259258

260259
use std::str::FromStr;
261-
use tinyvec::tiny_vec;
262260
#[test]
263261
fn test_fc_from_iterator() {
264262
let features: Vec<Feature> = vec![
265263
{
266-
let mut feat: Feature = Value::Point(tiny_vec![0., 0., 0.]).into();
264+
let mut feat: Feature = Value::Point(Position::from(vec![0., 0., 0.])).into();
267265
feat.bbox = Some(vec![-1., -1., -1., 1., 1., 1.]);
268266
feat
269267
},
270268
{
271-
let mut feat: Feature =
272-
Value::MultiPoint(vec![tiny_vec![10., 10., 10.], tiny_vec![11., 11., 11.]]).into();
269+
let mut feat: Feature = Value::MultiPoint(vec![
270+
Position::from(vec![10., 10., 10.]),
271+
Position::from(vec![11., 11., 11.]),
272+
])
273+
.into();
273274
feat.bbox = Some(vec![10., 10., 10., 11., 11., 11.]);
274275
feat
275276
},

src/feature_iterator.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,9 @@ where
129129
#[cfg(test)]
130130
mod tests {
131131
use super::*;
132-
use crate::{Geometry, Value};
132+
use crate::{Geometry, Position, Value};
133133

134134
use std::io::BufReader;
135-
use tinyvec::tiny_vec;
136135

137136
fn fc() -> &'static str {
138137
r#"
@@ -188,7 +187,7 @@ mod tests {
188187
assert_eq!(
189188
Geometry {
190189
bbox: None,
191-
value: Value::Point(tiny_vec![102.0, 0.5]),
190+
value: Value::Point(Position::from([102.0, 0.5])),
192191
foreign_members: None,
193192
},
194193
fi.next().unwrap().unwrap().geometry.unwrap()
@@ -197,10 +196,10 @@ mod tests {
197196
Geometry {
198197
bbox: None,
199198
value: Value::LineString(vec![
200-
tiny_vec![102.0, 0.0],
201-
tiny_vec![103.0, 1.0],
202-
tiny_vec![104.0, 0.0],
203-
tiny_vec![105.0, 1.0]
199+
Position::from([102.0, 0.0]),
200+
Position::from([103.0, 1.0]),
201+
Position::from([104.0, 0.0]),
202+
Position::from([105.0, 1.0])
204203
]),
205204
foreign_members: None,
206205
},
@@ -210,11 +209,11 @@ mod tests {
210209
Geometry {
211210
bbox: None,
212211
value: Value::Polygon(vec![vec![
213-
tiny_vec![100.0, 0.0],
214-
tiny_vec![101.0, 0.0],
215-
tiny_vec![101.0, 1.0],
216-
tiny_vec![100.0, 1.0],
217-
tiny_vec![100.0, 0.0]
212+
Position::from([100.0, 0.0]),
213+
Position::from([101.0, 0.0]),
214+
Position::from([101.0, 1.0]),
215+
Position::from([100.0, 1.0]),
216+
Position::from([100.0, 0.0])
218217
]]),
219218
foreign_members: None,
220219
},

src/feature_writer.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl<W: Write> Drop for FeatureWriter<W> {
226226
#[cfg(test)]
227227
mod tests {
228228
use super::*;
229-
use crate::JsonValue;
229+
use crate::{JsonValue, Position};
230230

231231
use serde_json::json;
232232

@@ -284,7 +284,9 @@ mod tests {
284284

285285
Feature {
286286
bbox: None,
287-
geometry: Some(crate::Geometry::from(crate::Value::Point(vec![1.1, 1.2]))),
287+
geometry: Some(crate::Geometry::from(crate::Value::Point(Position::from(
288+
[1.1, 1.2],
289+
)))),
288290
id: None,
289291
properties: Some(props),
290292
foreign_members: None,
@@ -298,7 +300,9 @@ mod tests {
298300

299301
Feature {
300302
bbox: None,
301-
geometry: Some(crate::Geometry::from(crate::Value::Point(vec![2.1, 2.2]))),
303+
geometry: Some(crate::Geometry::from(crate::Value::Point(Position::from(
304+
[2.1, 2.2],
305+
)))),
302306
id: None,
303307
properties: Some(props),
304308
foreign_members: None,
@@ -340,12 +344,12 @@ mod tests {
340344
{
341345
let mut writer = FeatureWriter::from_writer(&mut buffer);
342346
let record_1 = MyRecord {
343-
geometry: crate::Geometry::from(crate::Value::Point(vec![1.1, 1.2])),
347+
geometry: crate::Geometry::from(crate::Value::Point(Position::from([1.1, 1.2]))),
344348
name: "Mishka".to_string(),
345349
age: 12,
346350
};
347351
let record_2 = MyRecord {
348-
geometry: crate::Geometry::from(crate::Value::Point(vec![2.1, 2.2])),
352+
geometry: crate::Geometry::from(crate::Value::Point(Position::from([2.1, 2.2]))),
349353
name: "Jane".to_string(),
350354
age: 22,
351355
};

0 commit comments

Comments
 (0)