class NSWTopo::GeoJSON::Collection
Attributes
features[R]
projection[R]
Public Class Methods
load(json, projection = nil)
click to toggle source
# File lib/nswtopo/gis/geojson/collection.rb, line 9 def self.load(json, projection = nil) collection = JSON.parse(json) proj4 = collection.dig "crs", "properties", "name" projection ||= proj4 ? Projection.new(proj4) : Projection.wgs84 collection["features"].map do |feature| geometry, properties = feature.values_at "geometry", "properties" type, coordinates = geometry.values_at "type", "coordinates" raise Error, "unsupported geometry type: #{type}" unless TYPES === type GeoJSON.const_get(type).new coordinates, properties end.yield_self do |features| new projection, features end rescue JSON::ParserError raise Error, "invalid GeoJSON data" end
new(projection = Projection.wgs84, features = [])
click to toggle source
# File lib/nswtopo/gis/geojson/collection.rb, line 4 def initialize(projection = Projection.wgs84, features = []) @projection, @features = projection, features end
Public Instance Methods
<<(feature)
click to toggle source
# File lib/nswtopo/gis/geojson/collection.rb, line 25 def <<(feature) tap { @features << feature } end
Also aliased as: push
bounds()
click to toggle source
TODO: what about empty collections?
# File lib/nswtopo/gis/geojson/collection.rb, line 89 def bounds map(&:bounds).transpose.map(&:flatten).map(&:minmax) end
clip!(hull)
click to toggle source
# File lib/nswtopo/gis/geojson/collection.rb, line 81 def clip!(hull) @features.map! do |feature| feature.clip hull end.compact! self end
each(&block)
click to toggle source
# File lib/nswtopo/gis/geojson/collection.rb, line 31 def each(&block) block_given? ? tap { @features.each(&block) } : @features.each end
explode()
click to toggle source
# File lib/nswtopo/gis/geojson/collection.rb, line 63 def explode Collection.new @projection, flat_map(&:explode) end
merge(other)
click to toggle source
# File lib/nswtopo/gis/geojson/collection.rb, line 71 def merge(other) raise Error, "can't merge different projections" unless @projection == other.projection Collection.new @projection, @features + other.features end
merge!(other)
click to toggle source
# File lib/nswtopo/gis/geojson/collection.rb, line 76 def merge!(other) raise Error, "can't merge different projections" unless @projection == other.projection tap { @features.concat other.features } end
multi()
click to toggle source
# File lib/nswtopo/gis/geojson/collection.rb, line 67 def multi Collection.new @projection, map(&:multi) end
reproject_to(projection)
click to toggle source
# File lib/nswtopo/gis/geojson/collection.rb, line 35 def reproject_to(projection) return self if self.projection == projection json = OS.ogr2ogr "-t_srs", projection, "-f", "GeoJSON", "-lco", "RFC7946=NO", "/vsistdout/", "GeoJSON:/vsistdin/" do |stdin| stdin.puts to_json end Collection.load json, projection end
reproject_to_wgs84()
click to toggle source
# File lib/nswtopo/gis/geojson/collection.rb, line 43 def reproject_to_wgs84 reproject_to Projection.wgs84 end
to_h()
click to toggle source
# File lib/nswtopo/gis/geojson/collection.rb, line 47 def to_h { "type" => "FeatureCollection", "crs" => { "type" => "name", "properties" => { "name" => @projection } }, "features" => map(&:to_h) } end
to_json(**extras)
click to toggle source
# File lib/nswtopo/gis/geojson/collection.rb, line 59 def to_json(**extras) to_h.merge(extras).to_json end