ul-wukong 4.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +60 -0
- data/.gitmodules +6 -0
- data/.rspec +2 -0
- data/.travis.yml +19 -0
- data/.yardopts +6 -0
- data/CHANGELOG.md +7 -0
- data/Gemfile +17 -0
- data/Guardfile +12 -0
- data/LICENSE.md +95 -0
- data/NOTES-travis.md +31 -0
- data/README-old.md +422 -0
- data/README.md +1308 -0
- data/Rakefile +28 -0
- data/TODO.md +99 -0
- data/bin/cutc +30 -0
- data/bin/cuttab +5 -0
- data/bin/greptrue +6 -0
- data/bin/md5sort +20 -0
- data/bin/setcat +11 -0
- data/bin/tabchar +5 -0
- data/bin/uniq-ord +59 -0
- data/bin/uniqc +3 -0
- data/bin/wu +34 -0
- data/bin/wu-clean-encoding +31 -0
- data/bin/wu-date +13 -0
- data/bin/wu-datetime +13 -0
- data/bin/wu-hist +3 -0
- data/bin/wu-lign +186 -0
- data/bin/wu-local +4 -0
- data/bin/wu-plus +9 -0
- data/bin/wu-source +5 -0
- data/bin/wu-sum +31 -0
- data/diagrams/wu_local.dot +39 -0
- data/diagrams/wu_local.dot.png +0 -0
- data/examples/Gemfile +38 -0
- data/examples/README.md +9 -0
- data/examples/basic/string_reverser.rb +23 -0
- data/examples/basic/tiny_count.rb +8 -0
- data/examples/basic/word_count/accumulator.rb +26 -0
- data/examples/basic/word_count/tokenizer.rb +13 -0
- data/examples/basic/word_count/word_count.rb +6 -0
- data/examples/dataflow/scraper_macro_flow.rb +28 -0
- data/examples/deploy_pack/Gemfile +6 -0
- data/examples/deploy_pack/README.md +6 -0
- data/examples/deploy_pack/a/b/c/.gitkeep +0 -0
- data/examples/deploy_pack/app/processors/string_reverser.rb +5 -0
- data/examples/deploy_pack/config/environment.rb +1 -0
- data/examples/dsl/dataflow/fibonacci_series.rb +101 -0
- data/examples/dsl/dataflow/scraper_macro_flow.rb +28 -0
- data/examples/dsl/dataflow/simple.rb +12 -0
- data/examples/dsl/dataflow/telegram.rb +45 -0
- data/examples/dsl/workflow/cherry_pie.dot +97 -0
- data/examples/dsl/workflow/cherry_pie.md +104 -0
- data/examples/dsl/workflow/cherry_pie.png +0 -0
- data/examples/dsl/workflow/cherry_pie.rb +101 -0
- data/examples/empty/.gitkeep +0 -0
- data/examples/examples_helper.rb +9 -0
- data/examples/geo.rb +4 -0
- data/examples/geo/geo_grids.numbers +0 -0
- data/examples/geo/geolocated.rb +331 -0
- data/examples/geo/quadtile.rb +69 -0
- data/examples/geo/spec/geolocated_spec.rb +247 -0
- data/examples/geo/tile_fetcher.rb +77 -0
- data/examples/graph/implied_geolocation/README.md +63 -0
- data/examples/graph/minimum_spanning_tree/airfares_graphviz.rb +73 -0
- data/examples/improver/tweet_summary.rb +73 -0
- data/examples/loadable.rb +2 -0
- data/examples/munging/airline_flights/airline_flights.rake +83 -0
- data/examples/munging/airline_flights/airplane.rb +0 -0
- data/examples/munging/airline_flights/airport_id_unification.rb +129 -0
- data/examples/munging/airline_flights/airport_ok_chars.rb +4 -0
- data/examples/munging/airline_flights/indexable.rb +75 -0
- data/examples/munging/airline_flights/indexable_spec.rb +90 -0
- data/examples/munging/airline_flights/reconcile_airports.rb +142 -0
- data/examples/munging/airline_flights/tasks.rake +83 -0
- data/examples/munging/airline_flights/topcities.rb +167 -0
- data/examples/munging/geo/geo_json.rb +54 -0
- data/examples/munging/geo/geo_models.rb +69 -0
- data/examples/munging/geo/geonames_models.rb +107 -0
- data/examples/munging/geo/iso_codes.rb +172 -0
- data/examples/munging/geo/reconcile_countries.rb +124 -0
- data/examples/munging/geo/tasks.rake +71 -0
- data/examples/munging/wikipedia/articles/extract_articles-parsed.rb +79 -0
- data/examples/munging/wikipedia/articles/extract_articles-templated.rb +136 -0
- data/examples/munging/wikipedia/articles/textualize_articles.rb +54 -0
- data/examples/munging/wikipedia/articles/verify_structure.rb +43 -0
- data/examples/munging/wikipedia/articles/wp2txt-LICENSE.txt +22 -0
- data/examples/munging/wikipedia/articles/wp2txt_article.rb +259 -0
- data/examples/munging/wikipedia/articles/wp2txt_utils.rb +452 -0
- data/examples/munging/wikipedia/dbpedia/dbpedia_common.rb +5 -0
- data/examples/munging/wikipedia/dbpedia/dbpedia_extract_geocoordinates.rb +78 -0
- data/examples/munging/wikipedia/dbpedia/extract_links-cruft.rb +66 -0
- data/examples/munging/wikipedia/dbpedia/extract_links.rb +260 -0
- data/examples/munging/wikipedia/dbpedia/sameas_extractor.rb +20 -0
- data/examples/rake_helper.rb +97 -0
- data/examples/ruby_project/Gemfile +6 -0
- data/examples/ruby_project/README.md +6 -0
- data/examples/ruby_project/a/b/c/.gitkeep +0 -0
- data/examples/server_logs/geo_ip_mapping/munge_geolite.rb +82 -0
- data/examples/server_logs/logline.rb +95 -0
- data/examples/server_logs/models.rb +66 -0
- data/examples/server_logs/page_counts.pig +48 -0
- data/examples/server_logs/server_logs-01-parse-script.rb +13 -0
- data/examples/server_logs/server_logs-02-histograms-full.rb +33 -0
- data/examples/server_logs/server_logs-02-histograms-mapper.rb +14 -0
- data/examples/server_logs/server_logs-03-breadcrumbs-full.rb +71 -0
- data/examples/server_logs/server_logs-04-page_page_edges-full.rb +40 -0
- data/examples/serverlogs/geo_ip_mapping/munge_geolite.rb +82 -0
- data/examples/serverlogs/models/logline.rb +102 -0
- data/examples/serverlogs/parser/apache_parser_widget.rb +46 -0
- data/examples/serverlogs/visit_paths/common.rb +4 -0
- data/examples/serverlogs/visit_paths/page_counts.pig +48 -0
- data/examples/serverlogs/visit_paths/serverlogs-01-parse-script.rb +11 -0
- data/examples/serverlogs/visit_paths/serverlogs-02-histograms-full.rb +31 -0
- data/examples/serverlogs/visit_paths/serverlogs-02-histograms-mapper.rb +12 -0
- data/examples/serverlogs/visit_paths/serverlogs-03-breadcrumbs-full.rb +67 -0
- data/examples/serverlogs/visit_paths/serverlogs-04-page_page_edges-full.rb +38 -0
- data/examples/splitter.rb +94 -0
- data/examples/string_reverser.rb +7 -0
- data/examples/text/pig_latin/pig_latinizer.rb +35 -0
- data/examples/text/pig_latin/pig_latinizer_widget.rb +16 -0
- data/examples/text/regional_flavor/README.md +14 -0
- data/examples/text/regional_flavor/article_wordbags.pig +39 -0
- data/examples/text/regional_flavor/j01-article_wordbags.rb +4 -0
- data/examples/text/regional_flavor/simple_pig_script.pig +27 -0
- data/examples/twitter.rb +5 -0
- data/lib/hanuman.rb +36 -0
- data/lib/hanuman/graph.rb +97 -0
- data/lib/hanuman/graphvizzer.rb +206 -0
- data/lib/hanuman/graphvizzer/gv_models.rb +161 -0
- data/lib/hanuman/graphvizzer/gv_presenter.rb +97 -0
- data/lib/hanuman/link.rb +35 -0
- data/lib/hanuman/registry.rb +46 -0
- data/lib/hanuman/stage.rb +128 -0
- data/lib/hanuman/tree.rb +67 -0
- data/lib/wu/geo.rb +4 -0
- data/lib/wu/geo/geo_grids.numbers +0 -0
- data/lib/wu/geo/geolocated.rb +331 -0
- data/lib/wu/geo/quadtile.rb +69 -0
- data/lib/wu/graph/union_find.rb +62 -0
- data/lib/wu/model/reconcilable.rb +63 -0
- data/lib/wu/munging.rb +71 -0
- data/lib/wu/social/models/twitter.rb +31 -0
- data/lib/wu/wikipedia/models.rb +20 -0
- data/lib/wukong.rb +54 -0
- data/lib/wukong/dataflow.rb +43 -0
- data/lib/wukong/doc_helpers.rb +14 -0
- data/lib/wukong/doc_helpers/dataflow_handler.rb +29 -0
- data/lib/wukong/doc_helpers/field_handler.rb +91 -0
- data/lib/wukong/doc_helpers/processor_handler.rb +29 -0
- data/lib/wukong/driver.rb +214 -0
- data/lib/wukong/driver/event_machine_driver.rb +15 -0
- data/lib/wukong/driver/wiring.rb +68 -0
- data/lib/wukong/local.rb +42 -0
- data/lib/wukong/local/runner.rb +96 -0
- data/lib/wukong/local/stdio_driver.rb +104 -0
- data/lib/wukong/logger.rb +102 -0
- data/lib/wukong/model/faker.rb +136 -0
- data/lib/wukong/model/flatpack_parser/flat.rb +60 -0
- data/lib/wukong/model/flatpack_parser/flatpack.rb +4 -0
- data/lib/wukong/model/flatpack_parser/lang.rb +46 -0
- data/lib/wukong/model/flatpack_parser/parser.rb +55 -0
- data/lib/wukong/model/flatpack_parser/tokens.rb +130 -0
- data/lib/wukong/plugin.rb +48 -0
- data/lib/wukong/processor.rb +110 -0
- data/lib/wukong/rake_helper.rb +6 -0
- data/lib/wukong/runner.rb +169 -0
- data/lib/wukong/runner/boot_sequence.rb +123 -0
- data/lib/wukong/runner/code_loader.rb +52 -0
- data/lib/wukong/runner/command_runner.rb +44 -0
- data/lib/wukong/runner/deploy_pack_loader.rb +75 -0
- data/lib/wukong/runner/help_message.rb +42 -0
- data/lib/wukong/source.rb +33 -0
- data/lib/wukong/source/source_driver.rb +74 -0
- data/lib/wukong/source/source_runner.rb +38 -0
- data/lib/wukong/spec_helpers.rb +74 -0
- data/lib/wukong/spec_helpers/integration_tests.rb +150 -0
- data/lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb +207 -0
- data/lib/wukong/spec_helpers/integration_tests/integration_test_runner.rb +97 -0
- data/lib/wukong/spec_helpers/shared_examples.rb +22 -0
- data/lib/wukong/spec_helpers/unit_tests.rb +135 -0
- data/lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb +132 -0
- data/lib/wukong/spec_helpers/unit_tests/unit_test_matchers.rb +169 -0
- data/lib/wukong/spec_helpers/unit_tests/unit_test_runner.rb +60 -0
- data/lib/wukong/version.rb +3 -0
- data/lib/wukong/widget/echo.rb +55 -0
- data/lib/wukong/widget/extract.rb +122 -0
- data/lib/wukong/widget/filters.rb +452 -0
- data/lib/wukong/widget/logger.rb +56 -0
- data/lib/wukong/widget/operators.rb +82 -0
- data/lib/wukong/widget/reducers.rb +10 -0
- data/lib/wukong/widget/reducers/accumulator.rb +73 -0
- data/lib/wukong/widget/reducers/bin.rb +368 -0
- data/lib/wukong/widget/reducers/count.rb +73 -0
- data/lib/wukong/widget/reducers/group.rb +128 -0
- data/lib/wukong/widget/reducers/group_concat.rb +98 -0
- data/lib/wukong/widget/reducers/improver.rb +71 -0
- data/lib/wukong/widget/reducers/join_xml.rb +37 -0
- data/lib/wukong/widget/reducers/moments.rb +72 -0
- data/lib/wukong/widget/reducers/sort.rb +180 -0
- data/lib/wukong/widget/reducers/uniq.rb +91 -0
- data/lib/wukong/widget/serializers.rb +317 -0
- data/lib/wukong/widget/utils.rb +46 -0
- data/lib/wukong/widgets.rb +7 -0
- data/spec/examples/dataflow/fibonacci_series_spec.rb +18 -0
- data/spec/examples/dataflow/parse_apache_logs_spec.rb +8 -0
- data/spec/examples/dataflow/parsing_spec.rb +14 -0
- data/spec/examples/dataflow/simple_spec.rb +34 -0
- data/spec/examples/dataflow/telegram_spec.rb +43 -0
- data/spec/examples/graph/minimum_spanning_tree_spec.rb +34 -0
- data/spec/examples/munging/airline_flights/identifiers_spec.rb +16 -0
- data/spec/examples/munging/airline_flights_spec.rb +202 -0
- data/spec/examples/text/pig_latin_spec.rb +18 -0
- data/spec/examples/workflow/cherry_pie_spec.rb +36 -0
- data/spec/hanuman/graph_spec.rb +119 -0
- data/spec/hanuman/hanuman_spec.rb +10 -0
- data/spec/hanuman/registry_spec.rb +123 -0
- data/spec/hanuman/stage_spec.rb +81 -0
- data/spec/hanuman/tree_spec.rb +119 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +43 -0
- data/spec/support/example_test_helpers.rb +95 -0
- data/spec/support/hanuman_test_helpers.rb +92 -0
- data/spec/support/integration_helper.rb +38 -0
- data/spec/support/model_test_helpers.rb +115 -0
- data/spec/support/shared_context_for_graphs.rb +57 -0
- data/spec/support/shared_context_for_reducers.rb +37 -0
- data/spec/support/shared_examples_for_builders.rb +94 -0
- data/spec/support/shared_examples_for_shortcuts.rb +57 -0
- data/spec/wu/model/reconcilable_spec.rb +152 -0
- data/spec/wukong/dataflow_spec.rb +87 -0
- data/spec/wukong/driver_spec.rb +154 -0
- data/spec/wukong/local/runner_spec.rb +29 -0
- data/spec/wukong/local/stdio_driver_spec.rb +73 -0
- data/spec/wukong/local_spec.rb +6 -0
- data/spec/wukong/logger_spec.rb +49 -0
- data/spec/wukong/model/faker_spec.rb +132 -0
- data/spec/wukong/processor_spec.rb +21 -0
- data/spec/wukong/runner_spec.rb +132 -0
- data/spec/wukong/source_spec.rb +6 -0
- data/spec/wukong/widget/extract_spec.rb +101 -0
- data/spec/wukong/widget/filters_spec.rb +79 -0
- data/spec/wukong/widget/logger_spec.rb +23 -0
- data/spec/wukong/widget/operators_spec.rb +25 -0
- data/spec/wukong/widget/reducers/bin_spec.rb +92 -0
- data/spec/wukong/widget/reducers/count_spec.rb +11 -0
- data/spec/wukong/widget/reducers/group_spec.rb +21 -0
- data/spec/wukong/widget/reducers/join_xml_spec.rb +25 -0
- data/spec/wukong/widget/reducers/moments_spec.rb +36 -0
- data/spec/wukong/widget/reducers/sort_spec.rb +26 -0
- data/spec/wukong/widget/reducers/uniq_spec.rb +14 -0
- data/spec/wukong/widget/serializers_spec.rb +114 -0
- data/spec/wukong/widget/sink_spec.rb +19 -0
- data/spec/wukong/widget/source_spec.rb +65 -0
- data/spec/wukong/wu-local_spec.rb +109 -0
- data/spec/wukong/wu-source_spec.rb +32 -0
- data/spec/wukong/wu_spec.rb +14 -0
- data/spec/wukong/wukong_spec.rb +10 -0
- data/wukong.gemspec +35 -0
- metadata +465 -0
data/lib/hanuman/tree.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'tsort'
|
2
|
+
module Hanuman
|
3
|
+
|
4
|
+
module TreeInstanceMethods
|
5
|
+
include TSort
|
6
|
+
|
7
|
+
MultipleRoots = Class.new(TSort::Cyclic)
|
8
|
+
|
9
|
+
def tsort_each_node(&blk)
|
10
|
+
stages.keys.each(&blk)
|
11
|
+
end
|
12
|
+
|
13
|
+
def tsort_each_child(label, &blk)
|
14
|
+
links.select { |link| link.into == label }.map(&:from).each(&blk)
|
15
|
+
end
|
16
|
+
|
17
|
+
def directed_sort() self.tsort ; end
|
18
|
+
|
19
|
+
def each_stage &blk
|
20
|
+
directed_sort.map { |label| stages[label]}.compact.each(&blk)
|
21
|
+
end
|
22
|
+
|
23
|
+
def root stage=nil
|
24
|
+
return stages[directed_sort.first] unless stage
|
25
|
+
return stage unless ancestor(stage)
|
26
|
+
self.root(ancestor(stage))
|
27
|
+
end
|
28
|
+
|
29
|
+
def ancestor(stage)
|
30
|
+
ancestors(stage).first
|
31
|
+
end
|
32
|
+
|
33
|
+
def leaves
|
34
|
+
the_leaves = (descendents - ancestors)
|
35
|
+
the_leaves.empty? ? [root] : the_leaves
|
36
|
+
end
|
37
|
+
|
38
|
+
def add_link type, from, into
|
39
|
+
return if has_link?(from, into)
|
40
|
+
raise TSort::Cyclic.new("Cannot link from a stage <#{from.label}> to itself") if into == from
|
41
|
+
raise MultipleRoots.new("Cannot link from <#{from.label}> to <#{into.label}> because <#{into.label}> aleady has an ancestor <#{ancestor(into).label}>") if ancestor(into)
|
42
|
+
raise TSort::Cyclic.new("Cannot link from leaf <#{from.label}> to the root <#{into.label}>") if into == root && leaves.include?(from)
|
43
|
+
super(type, from, into)
|
44
|
+
end
|
45
|
+
|
46
|
+
def prepend stage
|
47
|
+
add_link(:simple, stage, root)
|
48
|
+
end
|
49
|
+
|
50
|
+
def append stage
|
51
|
+
leaves.each do |leaf|
|
52
|
+
stage_for_leaf = stage.clone
|
53
|
+
stage_for_leaf.label = "#{stage_for_leaf.label}_for_#{leaf.label}".to_sym
|
54
|
+
add_link(:simple, leaf, stage_for_leaf)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
class Tree < Graph
|
61
|
+
include TreeInstanceMethods
|
62
|
+
end
|
63
|
+
|
64
|
+
class TreeBuilder < GraphBuilder
|
65
|
+
include TreeInstanceMethods
|
66
|
+
end
|
67
|
+
end
|
data/lib/wu/geo.rb
ADDED
Binary file
|
@@ -0,0 +1,331 @@
|
|
1
|
+
require 'gorillib/numeric/clamp'
|
2
|
+
|
3
|
+
Numeric.class_eval do
|
4
|
+
def to_radians() self.to_f * Math::PI / 180.0 ; end
|
5
|
+
def to_degrees() self.to_f * 180.0 / Math::PI ; end
|
6
|
+
end
|
7
|
+
|
8
|
+
module Wukong
|
9
|
+
#
|
10
|
+
# reference: [Bing Maps Tile System](http://msdn.microsoft.com/en-us/library/bb259689.aspx)
|
11
|
+
#
|
12
|
+
module Geolocated
|
13
|
+
module_function # call methods as eg Wukong::Geolocated.tile_xy_to_quadkey or, if included in class, on self as private methods
|
14
|
+
|
15
|
+
# field :longitude, type: Float, description: "Longitude (X) of a point, in decimal degrees"
|
16
|
+
# field :latitude, type: Float, description: "Latitude (Y) of a point, in decimal degrees"
|
17
|
+
# field :zoom_level, type: Integer, description: "Zoom level of tile to fetch. An integer between 0 (world) and 16 or so"
|
18
|
+
# field :quadkey, type: String, description: "Quadkey of tile, eg 002313012"
|
19
|
+
# field :tile_x, type: Integer, description: "Tile X index, an integer between 0 and 2^zoom_level - 1"
|
20
|
+
# field :tile_y, type: Integer, description: "Tile Y index, an integer between 0 and 2^zoom_level - 1"
|
21
|
+
|
22
|
+
module ByCoordinates
|
23
|
+
extend Gorillib::Concern
|
24
|
+
|
25
|
+
# The quadkey is a string of 2-bit tile selectors for a quadtile
|
26
|
+
#
|
27
|
+
# @example
|
28
|
+
# infochimps_hq = Geo::Place.receive("Infochimps HQ", -97.759003, 30.273884)
|
29
|
+
# infochimps_hq.quadkey(8) # => "02313012"
|
30
|
+
#
|
31
|
+
# Interesting quadkey properties:
|
32
|
+
#
|
33
|
+
# * The quadkey length is its zoom level
|
34
|
+
#
|
35
|
+
# * To zoom out (lower zoom level, larger quadtile), just truncate the
|
36
|
+
# quadkey: austin at ZL=8 has quadkey "02313012"; at ZL=3, "023"
|
37
|
+
#
|
38
|
+
# * Nearby points typically have "nearby" quadkeys: up to the smallest
|
39
|
+
# tile that contains both, their quadkeys will have a common prefix.
|
40
|
+
# If you sort your records by quadkey,
|
41
|
+
# - Nearby points are nearby-ish on disk. (hello, HBase/Cassandra
|
42
|
+
# database owners!) This allows efficient lookup and caching of
|
43
|
+
# "popular" regions or repeated queries in an area.
|
44
|
+
# - the tiles covering a region can be covered by a limited, enumerable
|
45
|
+
# set of range scans. For map-reduce programmers, this leads to very
|
46
|
+
# efficient reducers
|
47
|
+
#
|
48
|
+
# * The quadkey is the bit-interleaved combination of its tile ids:
|
49
|
+
#
|
50
|
+
# tile_x 58 binary 0 0 1 1 1 0 1 0
|
51
|
+
# tile_y 105 binary 0 1 1 0 1 0 0 1
|
52
|
+
# interleaved binary 00 10 11 01 11 00 01 10
|
53
|
+
# quadkey 0 2 3 1 3 0 1 2 # "02313012"
|
54
|
+
#
|
55
|
+
def quadkey(zl) ; Wukong::Geolocated.tile_xy_zl_to_quadkey( tile_x(zl), tile_y(zl), zl) ; end
|
56
|
+
|
57
|
+
# the packed quadkey is the integer formed by interleaving the bits of tile_x with tile_y:
|
58
|
+
#
|
59
|
+
# tile_x 58 binary 0 0 1 1 1 0 1 0
|
60
|
+
# tile_y 105 binary 0 1 1 0 1 0 0 1
|
61
|
+
# interleaved binary 00 10 11 01 11 00 01 10
|
62
|
+
# quadkey 0 2 3 1 3 0 1 2 # "02313012"
|
63
|
+
#
|
64
|
+
# (see `quadkey` for more.)
|
65
|
+
#
|
66
|
+
# At zoom level 15, the packed quadkey is a 30-bit unsigned integer --
|
67
|
+
# meaning you can store it in a pig `int`; for languages with an `unsigned
|
68
|
+
# int` type, you can go to zoom level 16 before you have to use a
|
69
|
+
# less-efficient type. Zoom level 15 has a resolution of about one tile
|
70
|
+
# per kilometer (about 1.25 km/tile near the equator; 0.75 km/tile at
|
71
|
+
# London's latitude). It takes 1 billion tiles to tile the world at that
|
72
|
+
# scale. Ruby's integer type goes up to 60 bits, enough for any practical
|
73
|
+
# zoom level.
|
74
|
+
#
|
75
|
+
def packed_qk ; Wukong::Geolocated.tile_xy_zl_to_packed_qk(tile_x(zl), tile_y(zl), zl) ; end
|
76
|
+
|
77
|
+
# @return [Float] x index of the tile this object lies on at given zoom level
|
78
|
+
def tile_xf(zl) ; Wukong::Geolocated.lng_zl_to_tile_xf(longitude, zl) ; end
|
79
|
+
# @return [Float] y index of the tile this object lies on at given zoom level
|
80
|
+
def tile_yf(zl) ; Wukong::Geolocated.lat_zl_to_tile_yf(latitude, zl) ; end
|
81
|
+
# @return [Integer] x index of the tile this object lies on at given zoom level
|
82
|
+
def tile_x(zl) ; tile_xf(zl).floor ; end
|
83
|
+
# @return [Integer] y index of the tile this object lies on at given zoom level
|
84
|
+
def tile_y(zl) ; tile_yf(zl).floor ; end
|
85
|
+
|
86
|
+
# @return [Float] tile coordinates `(x,y)` for this object at given zoom level
|
87
|
+
def tile_xy(zl) ; [tile_x(xl), tile_y(zl)] ; end
|
88
|
+
|
89
|
+
# @returns [Array<Numeric, Numeric>] a `[longitude, latitude]` pair representing object as a point.
|
90
|
+
def lng_lat ; [longitude, latitude] ; end
|
91
|
+
|
92
|
+
# @returns [left, btm, right, top]
|
93
|
+
def bbox_for_radius(radius) ; Wukong::Geolocated.lng_lat_rad_to_bbox(longitude, latitude, radius) ; end
|
94
|
+
end
|
95
|
+
|
96
|
+
EARTH_RADIUS = 6371000 # meters
|
97
|
+
MIN_LONGITUDE = -180
|
98
|
+
MAX_LONGITUDE = 180
|
99
|
+
MIN_LATITUDE = -85.05112878
|
100
|
+
MAX_LATITUDE = 85.05112878
|
101
|
+
ALLOWED_LONGITUDE = (MIN_LONGITUDE..MAX_LONGITUDE)
|
102
|
+
ALLOWED_LATITUDE = (MIN_LATITUDE..MAX_LATITUDE)
|
103
|
+
TILE_PIXEL_SIZE = 256
|
104
|
+
|
105
|
+
# Width or height in number of tiles
|
106
|
+
def map_tile_size(zl)
|
107
|
+
1 << zl
|
108
|
+
end
|
109
|
+
|
110
|
+
#
|
111
|
+
# Tile coordinates
|
112
|
+
#
|
113
|
+
|
114
|
+
# Convert longitude in degrees to _floating-point_ tile x,y coordinates at given zoom level
|
115
|
+
def lng_zl_to_tile_xf(longitude, zl)
|
116
|
+
raise ArgumentError, "longitude must be within bounds ((#{longitude}) vs #{ALLOWED_LONGITUDE})" unless (ALLOWED_LONGITUDE.include?(longitude))
|
117
|
+
xx = (longitude.to_f + 180.0) / 360.0
|
118
|
+
(map_tile_size(zl) * xx)
|
119
|
+
end
|
120
|
+
|
121
|
+
# Convert latitude in degrees to _floating-point_ tile x,y coordinates at given zoom level
|
122
|
+
def lat_zl_to_tile_yf(latitude, zl)
|
123
|
+
raise ArgumentError, "latitude must be within bounds ((#{latitude}) vs #{ALLOWED_LATITUDE})" unless (ALLOWED_LATITUDE.include?(latitude))
|
124
|
+
sin_lat = Math.sin(latitude.to_radians)
|
125
|
+
yy = Math.log((1 + sin_lat) / (1 - sin_lat)) / (4 * Math::PI)
|
126
|
+
(map_tile_size(zl) * (0.5 - yy))
|
127
|
+
end
|
128
|
+
|
129
|
+
# Convert latitude in degrees to integer tile x,y coordinates at given zoom level
|
130
|
+
def lng_lat_zl_to_tile_xy(longitude, latitude, zl)
|
131
|
+
[lng_zl_to_tile_xf(longitude, zl).floor, lat_zl_to_tile_yf(latitude, zl).floor]
|
132
|
+
end
|
133
|
+
|
134
|
+
# Convert from tile_x, tile_y, zoom level to longitude and latitude in
|
135
|
+
# degrees (slight loss of precision).
|
136
|
+
#
|
137
|
+
# Tile coordinates may be floats or integer; they must lie within map range.
|
138
|
+
def tile_xy_zl_to_lng_lat(tile_x, tile_y, zl)
|
139
|
+
tile_size = map_tile_size(zl)
|
140
|
+
raise ArgumentError, "tile index must be within bounds ((#{tile_x},#{tile_y}) vs #{tile_size})" unless ((0..(tile_size-1)).include?(tile_x)) && ((0..(tile_size-1)).include?(tile_x))
|
141
|
+
xx = (tile_x.to_f / tile_size)
|
142
|
+
yy = 0.5 - (tile_y.to_f / tile_size)
|
143
|
+
lng = 360.0 * xx - 180.0
|
144
|
+
lat = 90 - 360 * Math.atan(Math.exp(-yy * 2 * Math::PI)) / Math::PI
|
145
|
+
[lng, lat]
|
146
|
+
end
|
147
|
+
|
148
|
+
#
|
149
|
+
# Quadkey coordinates
|
150
|
+
#
|
151
|
+
|
152
|
+
# converts from even/odd state of tile x and tile y to quadkey. NOTE: bit order means y, x
|
153
|
+
BIT_TO_QUADKEY = { [false, false] => "0", [false, true] => "1", [true, false] => "2", [true, true] => "3", }
|
154
|
+
# converts from quadkey char to bits. NOTE: bit order means y, x
|
155
|
+
QUADKEY_TO_BIT = { "0" => [0,0], "1" => [0,1], "2" => [1,0], "3" => [1,1]}
|
156
|
+
|
157
|
+
# Convert from tile x,y into a quadkey at a specified zoom level
|
158
|
+
def tile_xy_zl_to_quadkey(tile_x, tile_y, zl)
|
159
|
+
quadkey_chars = []
|
160
|
+
tx = tile_x.to_i
|
161
|
+
ty = tile_y.to_i
|
162
|
+
zl.times do
|
163
|
+
quadkey_chars.push BIT_TO_QUADKEY[[ty.odd?, tx.odd?]] # bit order y,x
|
164
|
+
tx >>= 1 ; ty >>= 1
|
165
|
+
end
|
166
|
+
quadkey_chars.join.reverse
|
167
|
+
end
|
168
|
+
|
169
|
+
# Convert a quadkey into tile x,y coordinates and level
|
170
|
+
def quadkey_to_tile_xy_zl(quadkey)
|
171
|
+
raise ArgumentError, "Quadkey must contain only the characters 0, 1, 2 or 3: #{quadkey}!" unless quadkey =~ /\A[0-3]*\z/
|
172
|
+
zl = quadkey.to_s.length
|
173
|
+
tx = 0 ; ty = 0
|
174
|
+
quadkey.chars.each do |char|
|
175
|
+
ybit, xbit = QUADKEY_TO_BIT[char] # bit order y, x
|
176
|
+
tx = (tx << 1) + xbit
|
177
|
+
ty = (ty << 1) + ybit
|
178
|
+
end
|
179
|
+
[tx, ty, zl]
|
180
|
+
end
|
181
|
+
|
182
|
+
# Convert from tile x,y into a packed quadkey at a specified zoom level
|
183
|
+
def tile_xy_zl_to_packed_qk(tile_x, tile_y, zl)
|
184
|
+
# don't optimize unless you're sure your way is faster; string ops are
|
185
|
+
# faster than you think and loops are slower than you think
|
186
|
+
quadkey_str = tile_xy_zl_to_quadkey(tile_x, tile_y, zl)
|
187
|
+
quadkey_str.to_i(4)
|
188
|
+
end
|
189
|
+
|
190
|
+
# Convert a packed quadkey (integer) into tile x,y coordinates and level
|
191
|
+
def packed_qk_zl_to_tile_xy(packed_qk, zl=16)
|
192
|
+
# don't "optimize" this without testing... string operations are faster than you think in ruby
|
193
|
+
raise ArgumentError, "Quadkey must be an integer in range of the zoom level: #{packed_qk}, #{zl}" unless packed_qk.is_a?(Fixnum) && (packed_qk < 2 ** (zl*2))
|
194
|
+
quadkey_rhs = packed_qk.to_s(4)
|
195
|
+
quadkey = ("0" * (zl - quadkey_rhs.length)) << quadkey_rhs
|
196
|
+
quadkey_to_tile_xy_zl(quadkey)
|
197
|
+
end
|
198
|
+
|
199
|
+
# Convert a lat/lng and zoom level into a quadkey
|
200
|
+
def lng_lat_zl_to_quadkey(longitude, latitude, zl)
|
201
|
+
tile_x, tile_y = lng_lat_zl_to_tile_xy(longitude, latitude, zl)
|
202
|
+
tile_xy_zl_to_quadkey(tile_x, tile_y, zl)
|
203
|
+
end
|
204
|
+
|
205
|
+
#
|
206
|
+
# Bounding box coordinates
|
207
|
+
#
|
208
|
+
|
209
|
+
# Convert a quadkey into a bounding box using adjacent tile
|
210
|
+
def quadkey_to_bbox(quadkey)
|
211
|
+
tile_x, tile_y, zl = quadkey_to_tile_xy_zl(quadkey)
|
212
|
+
# bottom right of me is top left of my southeast neighbor
|
213
|
+
left, top = tile_xy_zl_to_lng_lat(tile_x, tile_y, zl)
|
214
|
+
right, btm = tile_xy_zl_to_lng_lat(tile_x + 1, tile_y + 1, zl)
|
215
|
+
[left, btm, right, top]
|
216
|
+
end
|
217
|
+
|
218
|
+
# Retuns the smallest quadkey containing both of corners of the given bounding box
|
219
|
+
def quadkey_containing_bbox(left, btm, right, top)
|
220
|
+
qk_tl = lng_lat_zl_to_quadkey(left, top, 23)
|
221
|
+
qk_2 = lng_lat_zl_to_quadkey(right, btm, 23)
|
222
|
+
# the containing qk is the longest one that both agree on
|
223
|
+
containing_key = ""
|
224
|
+
qk_tl.chars.zip(qk_2.chars).each do |char_tl, char_2|
|
225
|
+
break if char_tl != char_2
|
226
|
+
containing_key << char_tl
|
227
|
+
end
|
228
|
+
containing_key
|
229
|
+
end
|
230
|
+
|
231
|
+
# Returns a bounding box containing the circle created by the lat/lng and radius
|
232
|
+
def lng_lat_rad_to_bbox(longitude, latitude, radius)
|
233
|
+
left, _ = point_east( longitude, latitude, -radius)
|
234
|
+
_, btm = point_north(longitude, latitude, -radius)
|
235
|
+
right, _ = point_east( longitude, latitude, radius)
|
236
|
+
_, top = point_north(longitude, latitude, radius)
|
237
|
+
[left, btm, right, top]
|
238
|
+
end
|
239
|
+
|
240
|
+
# Returns the centroid of a bounding box
|
241
|
+
#
|
242
|
+
# @param [Array<Float, Float>] left_btm Longitude, Latitude of SW point
|
243
|
+
# @param [Array<Float, Float>] right_top Longitude, Latitude of NE point
|
244
|
+
#
|
245
|
+
# @return [Array<Float, Float>] Longitude, Latitude of centroid
|
246
|
+
def bbox_centroid(left_btm, right_top)
|
247
|
+
haversine_midpoint(*left_btm, *right_top)
|
248
|
+
end
|
249
|
+
|
250
|
+
# Return the haversine distance in meters between two points
|
251
|
+
def haversine_distance(left, btm, right, top)
|
252
|
+
delta_lng = (right - left).abs.to_radians
|
253
|
+
delta_lat = (top - btm ).abs.to_radians
|
254
|
+
btm_rad = btm.to_radians
|
255
|
+
top_rad = top.to_radians
|
256
|
+
|
257
|
+
aa = (Math.sin(delta_lat / 2.0))**2 + Math.cos(top_rad) * Math.cos(btm_rad) * (Math.sin(delta_lng / 2.0))**2
|
258
|
+
cc = 2.0 * Math.atan2(Math.sqrt(aa), Math.sqrt(1.0 - aa))
|
259
|
+
cc * EARTH_RADIUS
|
260
|
+
end
|
261
|
+
|
262
|
+
# Return the haversine midpoint in meters between two points
|
263
|
+
def haversine_midpoint(left, btm, right, top)
|
264
|
+
cos_btm = Math.cos(btm.to_radians)
|
265
|
+
cos_top = Math.cos(top.to_radians)
|
266
|
+
bearing_x = cos_btm * Math.cos((right - left).to_radians)
|
267
|
+
bearing_y = cos_btm * Math.sin((right - left).to_radians)
|
268
|
+
mid_lat = Math.atan2(
|
269
|
+
(Math.sin(top.to_radians) + Math.sin(btm.to_radians)),
|
270
|
+
(Math.sqrt((cos_top + bearing_x)**2 + bearing_y**2)))
|
271
|
+
mid_lng = left.to_radians + Math.atan2(bearing_y, (cos_top + bearing_x))
|
272
|
+
[mid_lng.to_degrees, mid_lat.to_degrees]
|
273
|
+
end
|
274
|
+
|
275
|
+
# From a given point, calculate the point directly north a specified distance
|
276
|
+
def point_north(longitude, latitude, distance)
|
277
|
+
north_lat = (latitude.to_radians + (distance.to_f / EARTH_RADIUS)).to_degrees
|
278
|
+
[longitude, north_lat]
|
279
|
+
end
|
280
|
+
|
281
|
+
# From a given point, calculate the change in degrees directly east a given distance
|
282
|
+
def point_east(longitude, latitude, distance)
|
283
|
+
radius = EARTH_RADIUS * Math.sin(((Math::PI / 2.0) - latitude.to_radians.abs))
|
284
|
+
east_lng = (longitude.to_radians + (distance.to_f / radius)).to_degrees
|
285
|
+
[east_lng, latitude]
|
286
|
+
end
|
287
|
+
|
288
|
+
#
|
289
|
+
# Pixel coordinates
|
290
|
+
#
|
291
|
+
# Use with a standard (256x256 pixel) grid-based tileserver
|
292
|
+
#
|
293
|
+
|
294
|
+
# Width or height of grid bitmap in pixels at given zoom level
|
295
|
+
def map_pixel_size(zl)
|
296
|
+
TILE_PIXEL_SIZE * map_tile_size(zl)
|
297
|
+
end
|
298
|
+
|
299
|
+
# Return pixel resolution in meters per pixel at a specified latitude and zoom level
|
300
|
+
def pixel_resolution(latitude, zl)
|
301
|
+
lat = latitude.clamp(MIN_LATITUDE, MAX_LATITUDE)
|
302
|
+
Math.cos(lat.to_radians) * 2 * Math::PI * EARTH_RADIUS / map_pixel_size(zl).to_f
|
303
|
+
end
|
304
|
+
|
305
|
+
# Map scale at a specified latitude, zoom level, & screen resolution in dpi
|
306
|
+
def map_scale_for_dpi(latitude, zl, screen_dpi)
|
307
|
+
pixel_resolution(latitude, zl) * screen_dpi / 0.0254
|
308
|
+
end
|
309
|
+
|
310
|
+
# Convert from x,y pixel pair into tile x,y coordinates
|
311
|
+
def pixel_xy_to_tile_xy(pixel_x, pixel_y)
|
312
|
+
[pixel_x / TILE_PIXEL_SIZE, pixel_y / TILE_PIXEL_SIZE]
|
313
|
+
end
|
314
|
+
|
315
|
+
# Convert from x,y tile pair into pixel x,y coordinates (top left corner)
|
316
|
+
def tile_xy_to_pixel_xy(tile_x, tile_y)
|
317
|
+
[tile_x * TILE_PIXEL_SIZE, tile_y * TILE_PIXEL_SIZE]
|
318
|
+
end
|
319
|
+
|
320
|
+
def pixel_xy_zl_to_lng_lat(pixel_x, pixel_y, zl)
|
321
|
+
tile_xy_zl_to_lng_lat(pixel_x.to_f / TILE_PIXEL_SIZE, pixel_y.to_f / TILE_PIXEL_SIZE, zl)
|
322
|
+
end
|
323
|
+
|
324
|
+
def lng_lat_zl_to_pixel_xy(lng, lat, zl)
|
325
|
+
pixel_x = lng_zl_to_tile_xf(lng, zl)
|
326
|
+
pixel_y = lat_zl_to_tile_yf(lat, zl)
|
327
|
+
[(pixel_x * TILE_PIXEL_SIZE + 0.5).floor, (pixel_y * TILE_PIXEL_SIZE + 0.5).floor]
|
328
|
+
end
|
329
|
+
|
330
|
+
end
|
331
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module Wukong
|
2
|
+
module Geo
|
3
|
+
class Quadtile
|
4
|
+
include Gorillib::Model
|
5
|
+
#
|
6
|
+
field :tile_x, Integer, position: 0, doc: "Tile X index, an integer between 0 and 2^zoom_level - 1"
|
7
|
+
field :tile_y, Integer, position: 1, doc: "Tile Y index, an integer between 0 and 2^zoom_level - 1"
|
8
|
+
field :zl, Integer, position: 2, doc: "Zoom level of tile to fetch. 0 is the world; 16 is about a kilometer."
|
9
|
+
field :slug, String, default: 'tile', doc: "Name, prefixed on saved tiles"
|
10
|
+
|
11
|
+
def quadkey ; Wukong::Geolocated.tile_xy_zl_to_quadkey( tile_x, tile_y, zl) ; end
|
12
|
+
def packed_qk ; Wukong::Geolocated.tile_xy_zl_to_packed_qk(tile_x, tile_y, zl) ; end
|
13
|
+
|
14
|
+
# Base of URL for map tile server; anything X/Y/Z.png-addressable works,
|
15
|
+
# eg `http://b.tile.openstreetmap.org`. Defaults to 'http://b.tile.stamen.com/toner-lite'`.
|
16
|
+
class_attribute :tileserver_url_base
|
17
|
+
self.tileserver_url_base = 'http://a.tile.stamen.com/toner-lite'
|
18
|
+
|
19
|
+
def self.from_whatever(hsh)
|
20
|
+
zl = hsh[:zl] ? hsh[:zl].to_i : nil
|
21
|
+
case
|
22
|
+
when hsh[:tile_x].present? && hsh[:tile_y].present? && zl.present?
|
23
|
+
tile_x, tile_y = [hsh[:tile_x], hsh[:tile_y]]
|
24
|
+
when hsh[:longitude].present? && hsh[:latitude].present? && zl.present?
|
25
|
+
tile_x, tile_y = Wukong::Geolocated.lng_lat_zl_to_tile_xy(hsh[:longitude], hsh[:latitude], zl)
|
26
|
+
when hsh[:quadkey].present?
|
27
|
+
quadkey = hsh[:quadkey]
|
28
|
+
quadkey = quadkey[0..zl] if zl.to_i > 0
|
29
|
+
tile_x, tile_y, zl = Wukong::Geolocated.quadkey_to_tile_xy_zl(quadkey)
|
30
|
+
else
|
31
|
+
raise ArgumentError, "You must supply keys for either `:longitude`, `:latitude` and `:zl`; `:tile_x`, `:tile_y` and `:zl`; or `:quadkey`: #{hsh.inspect}"
|
32
|
+
end
|
33
|
+
return new(tile_x, tile_y, zl, hsh.to_hash)
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.tileserver_conn
|
37
|
+
@tileserver_conn = Faraday.new(:url => tileserver_url_base)
|
38
|
+
end
|
39
|
+
|
40
|
+
def tile_url
|
41
|
+
[tileserver_url_base, zl, tile_x, tile_y].join('/') << ".png"
|
42
|
+
end
|
43
|
+
|
44
|
+
# A
|
45
|
+
#
|
46
|
+
# @example
|
47
|
+
# qt = Quadtile.from_whatever(longitude: -97.759003, latitude: 30.273884, zl: 15)
|
48
|
+
# qt.slug # tile-15-64587
|
49
|
+
#
|
50
|
+
#
|
51
|
+
# @returns [String]
|
52
|
+
def basename(options={})
|
53
|
+
options = { sep: '-', ext: 'png'}
|
54
|
+
sep = options[:sep]
|
55
|
+
# "%s%s%02d%s%04d%s%04d.%s" % [slug, sep, zl, sep, tile_x, sep, tile_y, options[:ext]]
|
56
|
+
"%s/%02d/%s%s%s.%s" % [slug, zl, slug, sep, quadkey, options[:ext]]
|
57
|
+
end
|
58
|
+
|
59
|
+
# Fetch the contents of a map tile from a tileserver
|
60
|
+
#
|
61
|
+
# You are responsible for requiring the faraday library and its adapter
|
62
|
+
#
|
63
|
+
def fetch
|
64
|
+
self.class.tileserver_conn.get(tile_url)
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|