-
-
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.
Changing order to longitude, latitude to reflect geojson.
- Loading branch information
Showing
10 changed files
with
250 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com> | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in | ||
# all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
# THE SOFTWARE. | ||
|
||
require_relative 'location' | ||
require_relative 'aligned_box' | ||
|
||
module Geospatial | ||
class Map | ||
# Will use 60 bits to store hash: | ||
ORDER = 30 | ||
|
||
class Point | ||
def initialize(location) | ||
@location = location | ||
end | ||
|
||
attr :location | ||
|
||
def hash | ||
@hash ||= Hilbert.hash(@location.latitude, @location.longitude, ORDER).freeze | ||
end | ||
end | ||
|
||
EARTH_BOUNDS = AlignedBox.new(Vector[-180, -90], Vector[180, 90]).freeze | ||
|
||
def initialize(bounds = EARTH_BOUNDS) | ||
@bounds = bounds | ||
|
||
@points = [] | ||
end | ||
|
||
def << point | ||
@points << point | ||
end | ||
|
||
def sort! | ||
@points.sort_by(&:hash) | ||
end | ||
|
||
def query(bounding_box) | ||
ranges = [] | ||
|
||
Hilbert.traverse(ORDER, origin: @bounds.origin, size: @bounds.size) do |child_origin, child_size, prefix, order| | ||
child = AlignedBox.new(child_origin, child_size) | ||
|
||
if bounding_box.overlaps(child) | ||
end | ||
end | ||
end | ||
end | ||
end |
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,40 @@ | ||
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com> | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in | ||
# all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
# THE SOFTWARE. | ||
|
||
require_relative 'location' | ||
|
||
|
||
module Geospatial | ||
class Map | ||
EARTH_BOUNDS = AlignedBox.new(Vector[-180, -90], Vector[180, 90]).freeze | ||
|
||
def self.for_earth | ||
self.new(EARTH_BOUNDS) | ||
end | ||
|
||
def initialize(bounds) | ||
@bounds = bounds | ||
end | ||
|
||
def << object | ||
|
||
end | ||
end | ||
end |
File renamed without changes.
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,91 @@ | ||
# Copyright, 2015, by Samuel G. D. Williams. <http://www.codeotaku.com> | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in | ||
# all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
# THE SOFTWARE. | ||
|
||
require 'geospatial/map' | ||
|
||
module Geospatial::MapSpec | ||
describe Geospatial::Map do | ||
it "base case should be identity" do | ||
# The base case for our coordinate system: | ||
expect(Geospatial::Hilbert.rotate(0, 0)).to be == 0 | ||
expect(Geospatial::Hilbert.rotate(0, 1)).to be == 1 | ||
expect(Geospatial::Hilbert.rotate(0, 2)).to be == 2 | ||
expect(Geospatial::Hilbert.rotate(0, 3)).to be == 3 | ||
end | ||
|
||
it "rotation is self-inverting" do | ||
4.times do |rotation| | ||
4.times do |quadrant| | ||
# rotate(rotation, rotate(rotation, quadrant)) == quadrant | ||
rotated = Geospatial::Hilbert.rotate(rotation, quadrant) | ||
expect(Geospatial::Hilbert.rotate(rotation, rotated)).to be == quadrant | ||
end | ||
end | ||
end | ||
|
||
it "computes the correct hash of order=0" do | ||
expect(Geospatial::Hilbert.hash(0, 0, 0)).to be == 0 | ||
expect(Geospatial::Hilbert.hash(1, 0, 0)).to be == 1 | ||
expect(Geospatial::Hilbert.hash(1, 1, 0)).to be == 2 | ||
expect(Geospatial::Hilbert.hash(0, 1, 0)).to be == 3 | ||
end | ||
|
||
it "computes the correct hash of order=1" do | ||
expect(Geospatial::Hilbert.hash(0, 0, 1)).to be == 0 | ||
expect(Geospatial::Hilbert.hash(1, 0, 1)).to be == 1 | ||
expect(Geospatial::Hilbert.hash(1, 1, 1)).to be == 2 | ||
expect(Geospatial::Hilbert.hash(0, 1, 1)).to be == 3 | ||
|
||
expect(Geospatial::Hilbert.hash(0, 2, 1)).to be == 4 | ||
expect(Geospatial::Hilbert.hash(0, 3, 1)).to be == 5 | ||
expect(Geospatial::Hilbert.hash(1, 3, 1)).to be == 6 | ||
expect(Geospatial::Hilbert.hash(1, 2, 1)).to be == 7 | ||
|
||
expect(Geospatial::Hilbert.hash(2, 2, 1)).to be == 8 | ||
expect(Geospatial::Hilbert.hash(2, 3, 1)).to be == 9 | ||
expect(Geospatial::Hilbert.hash(3, 3, 1)).to be == 10 | ||
expect(Geospatial::Hilbert.hash(3, 2, 1)).to be == 11 | ||
|
||
expect(Geospatial::Hilbert.hash(3, 1, 1)).to be == 12 | ||
expect(Geospatial::Hilbert.hash(2, 1, 1)).to be == 13 | ||
expect(Geospatial::Hilbert.hash(2, 0, 1)).to be == 14 | ||
expect(Geospatial::Hilbert.hash(3, 0, 1)).to be == 15 | ||
end | ||
|
||
it "computes the correct unhash" do | ||
expect(Geospatial::Hilbert.unhash(12)).to be == [3, 1] | ||
end | ||
|
||
it "traverses and generates all prefixes" do | ||
items = Geospatial::Hilbert.traverse(1).to_a | ||
|
||
# 4 items of order 1, 16 items of order 0. | ||
expect(items.size).to be == (4 + 16) | ||
|
||
expect(items[0]).to be == [[0, 0], [0.5, 0.5], 0b0000, 1] | ||
expect(items[5]).to be == [[0.0, 0.5], [0.5, 0.5], 0b0100, 1] | ||
expect(items[10]).to be == [[0.5, 0.5], [0.5, 0.5], 0b1000, 1] | ||
expect(items[15]).to be == [[0.5, 0], [0.5, 0.5], 0b1100, 1] | ||
|
||
prefixes = items.collect{|item| item[2]} | ||
expect(prefixes).to be_sorted | ||
end | ||
end | ||
end |
File renamed without changes.