forked from Maximkaaa/galileo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support running map without a window (Maximkaaa#34)
* Add an example to draw map to a file * Support GeoJson loading * Add examples description
- Loading branch information
Showing
41 changed files
with
1,174 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use thiserror::Error; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum GalileoTypesError { | ||
#[error("invalid input geometry: {0}")] | ||
Conversion(String), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use crate::cartesian::impls::contour::Contour; | ||
use crate::cartesian::impls::multipolygon::MultiPolygon; | ||
use crate::cartesian::impls::polygon::Polygon; | ||
use crate::geo::traits::projection::Projection; | ||
use crate::geojson::point::GeoJsonPoint; | ||
use crate::geometry::{Geom, Geometry}; | ||
use crate::impls::multi_contour::MultiContour; | ||
use crate::impls::multi_point::MultiPoint; | ||
use geojson::{LineStringType, PolygonType, Position, Value}; | ||
|
||
// mod line; | ||
mod point; | ||
|
||
impl Geometry for geojson::Geometry { | ||
type Point = GeoJsonPoint; | ||
|
||
fn project<Proj>(&self, projection: &Proj) -> Option<Geom<Proj::OutPoint>> | ||
where | ||
Proj: Projection<InPoint = Self::Point> + ?Sized, | ||
{ | ||
match &self.value { | ||
Value::Point(p) => GeoJsonPoint::try_from(p.clone()).ok()?.project(projection), | ||
Value::MultiPoint(points) => convert_multi_point(points)?.project(projection), | ||
Value::LineString(points) => convert_contour(points)?.project(projection), | ||
Value::MultiLineString(lines) => convert_multi_contour(lines)?.project(projection), | ||
Value::Polygon(polygon) => convert_polygon(polygon)?.project(projection), | ||
Value::MultiPolygon(mp) => convert_multi_polygon(mp)?.project(projection), | ||
Value::GeometryCollection(_) => todo!(), | ||
} | ||
} | ||
} | ||
|
||
fn convert_contour(line_string: &LineStringType) -> Option<Contour<GeoJsonPoint>> { | ||
let is_closed = line_string.len() > 0 && line_string[0] == line_string[line_string.len() - 1]; | ||
Some(Contour::new( | ||
line_string | ||
.iter() | ||
.map(|p| GeoJsonPoint::try_from(p.clone()).ok()) | ||
.collect::<Option<Vec<_>>>()?, | ||
is_closed, | ||
)) | ||
} | ||
|
||
fn convert_multi_point(points: &Vec<Position>) -> Option<MultiPoint<GeoJsonPoint>> { | ||
Some(MultiPoint::from( | ||
points | ||
.iter() | ||
.map(|p| GeoJsonPoint::try_from(p.clone()).ok()) | ||
.collect::<Option<Vec<_>>>()?, | ||
)) | ||
} | ||
|
||
fn convert_multi_contour(lines: &Vec<LineStringType>) -> Option<MultiContour<GeoJsonPoint>> { | ||
Some(MultiContour::from( | ||
lines | ||
.iter() | ||
.map(|l| convert_contour(l)) | ||
.collect::<Option<Vec<_>>>()?, | ||
)) | ||
} | ||
|
||
fn convert_polygon(polygon: &PolygonType) -> Option<Polygon<GeoJsonPoint>> { | ||
Some(Polygon::new( | ||
convert_contour(&polygon[0])?.into_closed()?, | ||
polygon[1..] | ||
.iter() | ||
.map(|p| convert_contour(p).and_then(|c| c.into_closed())) | ||
.collect::<Option<Vec<_>>>()?, | ||
)) | ||
} | ||
|
||
fn convert_multi_polygon(mp: &Vec<PolygonType>) -> Option<MultiPolygon<GeoJsonPoint>> { | ||
Some(MultiPolygon::from( | ||
mp.iter() | ||
.map(|p| convert_polygon(p)) | ||
.collect::<Option<Vec<_>>>()?, | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use crate::error::GalileoTypesError; | ||
use crate::geo::traits::point::{GeoPoint, NewGeoPoint}; | ||
use crate::geometry_type::{GeoSpace2d, GeometryType, PointGeometryType}; | ||
use geojson::Position; | ||
|
||
pub struct GeoJsonPoint(Position); | ||
|
||
impl TryFrom<Position> for GeoJsonPoint { | ||
type Error = GalileoTypesError; | ||
|
||
fn try_from(value: Position) -> Result<Self, Self::Error> { | ||
if value.len() < 2 { | ||
Err(GalileoTypesError::Conversion( | ||
"point must contain at least 2 dimensions".to_string(), | ||
)) | ||
} else { | ||
Ok(GeoJsonPoint(value)) | ||
} | ||
} | ||
} | ||
|
||
impl GeometryType for GeoJsonPoint { | ||
type Type = PointGeometryType; | ||
type Space = GeoSpace2d; | ||
} | ||
|
||
impl GeoPoint for GeoJsonPoint { | ||
type Num = f64; | ||
|
||
fn lat(&self) -> Self::Num { | ||
self.0[1] | ||
} | ||
|
||
fn lon(&self) -> Self::Num { | ||
self.0[0] | ||
} | ||
} | ||
|
||
impl NewGeoPoint for GeoJsonPoint { | ||
fn latlon(lat: f64, lon: f64) -> Self { | ||
Self(vec![lon, lat]) | ||
} | ||
} |
Oops, something went wrong.