Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
216 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
--color | ||
--backtrace | ||
--color --tty --format nested |
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 |
---|---|---|
@@ -1,31 +1,64 @@ | ||
require 'set' | ||
require 'forwardable' | ||
require 'nokogiri' | ||
require 'border_patrol/version' | ||
require 'border_patrol/point' | ||
require 'border_patrol/polygon' | ||
require 'border_patrol/region' | ||
|
||
module BorderPatrol | ||
class InsufficientPointsToActuallyFormAPolygonError < ArgumentError; end | ||
class Point < Struct.new(:x, :y); end | ||
|
||
def self.parse_kml(string) | ||
doc = Nokogiri::XML(string) | ||
|
||
polygons = doc.search('Polygon').map do |polygon_kml| | ||
parse_kml_polygon_data(polygon_kml.to_s) | ||
placemark_name = placemark_name_for_polygon(polygon_kml) | ||
parse_kml_polygon_data(polygon_kml.to_s,placemark_name) | ||
end | ||
BorderPatrol::Region.new(polygons) | ||
end | ||
|
||
def self.bounding_box(points) | ||
max_x, min_x, max_y, min_y = -Float::MAX, Float::MAX, -Float::MAX, Float::MAX | ||
points.each do |point| | ||
max_y = point.y if point.y > max_y | ||
min_y = point.y if point.y < min_y | ||
max_x = point.x if point.x > max_x | ||
min_x = point.x if point.x < min_x | ||
end | ||
[Point.new(min_x, max_y), Point.new(max_x, min_y)] | ||
end | ||
|
||
def self.central_point(box) | ||
point1, point2 = box | ||
|
||
x = (point1.x + point2.x) / 2 | ||
y = (point1.y + point2.y) / 2 | ||
|
||
Point.new(x, y) | ||
end | ||
|
||
private | ||
|
||
def self.parse_kml_polygon_data(string) | ||
|
||
def self.parse_kml_polygon_data(string, name = nil) | ||
doc = Nokogiri::XML(string) | ||
coordinates = doc.xpath('//coordinates').text.strip.split(/\s+/) | ||
points = coordinates.map do |coord| | ||
x, y, _ = coord.strip.split(',') | ||
BorderPatrol::Point.new(x.to_f, y.to_f) | ||
end | ||
BorderPatrol::Polygon.new(points) | ||
BorderPatrol::Polygon.new(points).with_placemark_name(name) | ||
end | ||
|
||
def self.placemark_name_for_polygon(p) | ||
# A polygon can be contained by a MultiGeometry or Placemark | ||
parent = p.parent | ||
parent = parent.parent if parent.name == "MultiGeometry" | ||
|
||
return nil unless parent.name == "Placemark" | ||
|
||
parent.search("name").text | ||
end | ||
end | ||
|
||
require 'set' | ||
require 'nokogiri' | ||
require 'border_patrol/version' | ||
require 'border_patrol/polygon' | ||
require 'border_patrol/region' |
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,33 @@ | ||
module BorderPatrol | ||
Point = Struct.new(:x, :y) unless defined?(::BorderPatrol::Point) | ||
|
||
class Point | ||
alias :latitude :y | ||
alias :latitude= :y= | ||
alias :lat :y | ||
alias :lat= :y= | ||
|
||
alias :longitude :x | ||
alias :longitude= :x= | ||
alias :lng :x | ||
alias :lng= :x= | ||
alias :lon :x | ||
alias :lon= :x= | ||
|
||
# Lots of Map APIs want the coordinates in lat-lng order | ||
def latlng | ||
[lat, lon] | ||
end | ||
alias :coords :latlng | ||
|
||
def inspect | ||
self.class.inspect_string % self.latlng | ||
end | ||
|
||
protected | ||
# IE: #<BorderPatrol::Point(lat, lng) = (-25.363882, 131.044922)> | ||
def self.inspect_string | ||
@inspect_string ||= "#<#{self.name}(lat, lng) = (%p, %p)>" | ||
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
require 'rubygems' | ||
require 'bundler' | ||
Bundler.setup | ||
require 'bundler/setup' | ||
|
||
Bundler.require :development, :test, :default | ||
|
||
require 'border_patrol' |