-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert everything to mercator internally, after all
the new algorithm still acting weird though
- Loading branch information
1 parent
3a92dd6
commit f1ffdab
Showing
7 changed files
with
131 additions
and
82 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,48 @@ | ||
use geo::{BoundingRect, Coord, HaversineLength, LineString, Rect}; | ||
|
||
/// Projects WGS84 points onto a Euclidean plane, using a Mercator projection. The top-left is (0, | ||
/// 0) and grows to the right and down (screen-drawing order, not Cartesian), with units of meters. | ||
/// The accuracy of this weakens for larger areas. | ||
pub struct Mercator { | ||
wgs84_bounds: Rect, | ||
width: f64, | ||
height: f64, | ||
} | ||
|
||
impl Mercator { | ||
// TODO The API is kind of annoying, or wasteful. Do builder style. | ||
/// Create a boundary covering some geometry | ||
pub fn from<T: BoundingRect<f64>>(geometry: T) -> Option<Self> { | ||
let wgs84_bounds = geometry.bounding_rect().into()?; | ||
let width = LineString::from(vec![ | ||
(wgs84_bounds.min().x, wgs84_bounds.min().y), | ||
(wgs84_bounds.max().x, wgs84_bounds.min().y), | ||
]) | ||
.haversine_length(); | ||
let height = LineString::from(vec![ | ||
(wgs84_bounds.min().x, wgs84_bounds.min().y), | ||
(wgs84_bounds.min().x, wgs84_bounds.max().y), | ||
]) | ||
.haversine_length(); | ||
Some(Self { | ||
wgs84_bounds, | ||
width, | ||
height, | ||
}) | ||
} | ||
|
||
pub fn to_mercator(&self, pt: Coord) -> Coord { | ||
let x = self.width * (pt.x - self.wgs84_bounds.min().x) / self.wgs84_bounds.width(); | ||
// Invert y, so that the northernmost latitude is 0 | ||
let y = self.height | ||
- self.height * (pt.y - self.wgs84_bounds.min().y) / self.wgs84_bounds.height(); | ||
Coord { x, y } | ||
} | ||
|
||
pub fn to_wgs84(&self, pt: Coord) -> Coord { | ||
let x = self.wgs84_bounds.min().x + (pt.x / self.width * self.wgs84_bounds.width()); | ||
let y = self.wgs84_bounds.min().y | ||
+ (self.wgs84_bounds.height() * (self.height - pt.y) / self.height); | ||
Coord { x, y } | ||
} | ||
} |
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