lib/terraformer.rb in terraformer-0.0.1 vs lib/terraformer.rb in terraformer-0.0.2
- old
+ new
@@ -1,13 +1,20 @@
require 'json'
require 'bigdecimal'
require 'bigdecimal/math'
require 'bigdecimal/util'
+require 'ext/array'
+require 'ext/big_decimal'
+require 'ext/big_math'
+require 'ext/enumerable'
+require 'ext/hash'
+require 'forwardable'
module Terraformer
- PRECISION = 8
+ PRECISION = BigDecimal.double_fig
+ BigDecimal.limit PRECISION
PI = BigMath.PI PRECISION
DEFAULT_BUFFER_RESOLUTION = 64
EARTH_RADIUS = 6378137.to_d
DEGREES_PER_RADIAN = 180.0.to_d / PI
@@ -26,11 +33,14 @@
type: "ogcwkt"
}
}
def self.parse geojson
- geojson = JSON.parse geojson if String === geojson
+ if String === geojson
+ geojson = File.read geojson if File.readable? geojson
+ geojson = JSON.parse geojson
+ end
raise ArgumentError.new "invalid arg: #{geojson}" unless Hash === geojson
if klass = Terraformer.const_get(geojson['type'])
klass.new geojson
else
@@ -58,77 +68,36 @@
def bbox type = :bbox
Bounds.bounds self, type
end
def to_json *args
- self.to_hash.to_json *args
+ h = self.to_hash
+ h[:bbox] = bbox if Hash === args.last and args.pop[:include_bbox]
+ h.to_json *args
end
- def [] prop
- self.__send__ prop.to_sym
- end
-
end
-end
+ # yikes!
+ module BBox
-module Enumerable
-
- def each_coordinate opts = {}, &block
- iter_coordinate :each, opts, &block
- end
-
- def map_coordinate opts = {}, &block
- iter_coordinate :map, opts, &block
- end
- alias_method :collect_coordinate, :map_coordinate
-
- def map_coordinate! opts = {}, &block
- iter_coordinate :map!, opts, &block
- end
- alias_method :collect_coordinate!, :map_coordinate!
-
- def iter_coordinate meth, opts = {}, &block
- opts[:recurse] = true if opts[:recurse].nil?
-
- if Array === self and Numeric === self[0]
- yield self
- else
-
- self.__send__ meth do |pair|
- raise IndexError unless Array === pair
- case pair[0]
- when Numeric
- yield pair
- when Array
- pair.iter_coordinate meth, opts, &block if opts[:recurse]
- else
- raise IndexError.new "#{pair[0]} is not a Numeric or Array type"
- end
- end
+ def to_json *args
+ map {|e| e.to_f if e}.to_json *args
end
- end
-end
-
-class BigDecimal
-
- def to_deg
- self * Terraformer::DEGREES_PER_RADIAN
end
- def to_rad
- self * Terraformer::RADIANS_PER_DEGREE
- end
-
end
require 'terraformer/coordinate'
+require 'terraformer/geodesic'
require 'terraformer/bounds'
require 'terraformer/geometry'
require 'terraformer/feature'
require 'terraformer/point'
require 'terraformer/multi_point'
require 'terraformer/line_string'
require 'terraformer/multi_line_string'
require 'terraformer/polygon'
require 'terraformer/multi_polygon'
+require 'terraformer/convex_hull'
+require 'terraformer/circle'